diff --git a/contracts/contracts.go b/contracts/contracts.go index 8b83f4b0..de3c0b28 100644 --- a/contracts/contracts.go +++ b/contracts/contracts.go @@ -42,7 +42,7 @@ func (p *GatewayProvider) declareAndWaitWithWallet(ctx context.Context, compiled return nil, err } if !receipt.Status.IsTransactionFinal() || - rpc.TransactionState(receipt.Status.String()) == rpc.TransactionRejected { + receipt.Status == types.TransactionState(rpc.TxnExecutionStatusREVERTED) { return nil, fmt.Errorf("wrong status: %s", receipt.Status) } return &DeclareOutput{ diff --git a/rpc/mock_test.go b/rpc/mock_test.go index ee964693..c1364305 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -251,7 +251,7 @@ func mock_starknet_getTransactionReceipt(result interface{}, method string, args } transaction := InvokeTransactionReceipt(CommonTransactionReceipt{ TransactionHash: arg0Felt, - Status: TransactionAcceptedOnL1, + FinalityStatus: TxnFinalityStatusAcceptedOnL1, Events: []Event{{ FromAddress: fromAddressFelt, }}, diff --git a/rpc/transaction.go b/rpc/transaction.go index c8062399..98bb3f7d 100644 --- a/rpc/transaction.go +++ b/rpc/transaction.go @@ -107,7 +107,7 @@ func (provider *Provider) TransactionReceipt(ctx context.Context, transactionHas } // WaitForTransaction waits for the transaction to succeed or fail -func (provider *Provider) WaitForTransaction(ctx context.Context, transactionHash *felt.Felt, pollInterval time.Duration) (TransactionState, error) { +func (provider *Provider) WaitForTransaction(ctx context.Context, transactionHash *felt.Felt, pollInterval time.Duration) (TxnExecutionStatus, error) { t := time.NewTicker(pollInterval) for { select { @@ -124,20 +124,20 @@ func (provider *Provider) WaitForTransaction(ctx context.Context, transactionHas } switch r := receipt.(type) { case DeclareTransactionReceipt: - if r.Status.IsTransactionFinal() { - return r.Status, nil + if r.ExecutionStatus == TxnExecutionStatusSUCCEEDED { + return r.ExecutionStatus, nil } case DeployTransactionReceipt: - if r.Status.IsTransactionFinal() { - return r.Status, nil + if r.ExecutionStatus == TxnExecutionStatusSUCCEEDED { + return r.ExecutionStatus, nil } case InvokeTransactionReceipt: - if r.Status.IsTransactionFinal() { - return r.Status, nil + if r.ExecutionStatus == TxnExecutionStatusSUCCEEDED { + return r.ExecutionStatus, nil } case L1HandlerTransactionReceipt: - if r.Status.IsTransactionFinal() { - return r.Status, nil + if r.ExecutionStatus == TxnExecutionStatusSUCCEEDED { + return r.ExecutionStatus, nil } default: return "", fmt.Errorf("unknown receipt %T", receipt) diff --git a/rpc/transaction_test.go b/rpc/transaction_test.go index abaccf5e..65dc8cdb 100644 --- a/rpc/transaction_test.go +++ b/rpc/transaction_test.go @@ -2,7 +2,6 @@ package rpc import ( "context" - "fmt" "regexp" "testing" @@ -172,7 +171,8 @@ func TestTransactionReceipt_MatchesCapturedTransaction(t *testing.T) { TransactionHash: utils.TestHexToFelt(t, "0x40c82f79dd2bc1953fc9b347a3e7ab40fe218ed5740bf4e120f74e8a3c9ac99"), ActualFee: utils.TestHexToFelt(t, "0x1709a2f3a2"), Type: "INVOKE", - Status: TransactionAcceptedOnL1, + ExecutionStatus: TxnExecutionStatusSUCCEEDED, + FinalityStatus: TxnFinalityStatusAcceptedOnL1, BlockHash: utils.TestHexToFelt(t, "0x6c2fe3db009a2e008c2d65fca14204f3405cb74742fcf685f02473acaf70c72"), BlockNumber: 310370, MessagesSent: []MsgToL1{}, @@ -234,15 +234,15 @@ func TestTransactionReceipt_MatchesStatus(t *testing.T) { testConfig := beforeEach(t) type testSetType struct { - TxnHash *felt.Felt - StatusMatch string + TxnHash *felt.Felt + ExecutionStatus string } testSet := map[string][]testSetType{ "mock": {}, "testnet": { { - TxnHash: utils.TestHexToFelt(t, "0x650667fb0f17e63e1c9d1040e750d160f3dbfebcab990e7d4382f33468b1b59"), - StatusMatch: "(ACCEPTED_ON_L1|ACCEPTED_ON_L2|PENDING)", + TxnHash: utils.TestHexToFelt(t, "0x650667fb0f17e63e1c9d1040e750d160f3dbfebcab990e7d4382f33468b1b59"), + ExecutionStatus: "(SUCCEEDED|REVERTED)", }, }, "mainnet": {}, @@ -262,10 +262,9 @@ func TestTransactionReceipt_MatchesStatus(t *testing.T) { if !ok { t.Fatalf("transaction receipt should be InvokeTransactionReceipt, instead %T", txReceiptInterface) } - if ok, err := regexp.MatchString(test.StatusMatch, string(txnReceipt.Status)); err != nil || !ok { - t.Fatal("error checking transaction status", ok, err, txnReceipt.Status) + if ok, err := regexp.MatchString(test.ExecutionStatus, string(txnReceipt.ExecutionStatus)); err != nil || !ok { + t.Fatal("error checking transaction status", ok, err, txnReceipt.ExecutionStatus) } - fmt.Println("transaction status", txnReceipt.Status) } } @@ -282,7 +281,8 @@ func TestDeployOrDeclareReceipt(t *testing.T) { CommonTransactionReceipt: CommonTransactionReceipt{ TransactionHash: utils.TestHexToFelt(t, "0x035bd2978d2061b3463498f83c09322ed6a82e4b2a188506525e272a7adcdf6a"), ActualFee: &felt.Zero, - Status: "ACCEPTED_ON_L1", + FinalityStatus: TxnFinalityStatusAcceptedOnL1, + ExecutionStatus: TxnExecutionStatusSUCCEEDED, BlockHash: utils.TestHexToFelt(t, "0x0424fba26a7760b63895abe0c366c2d254cb47090c6f9e91ba2b3fa0824d4fc9"), BlockNumber: 310843, Type: "DEPLOY", @@ -296,7 +296,8 @@ func TestDeployOrDeclareReceipt(t *testing.T) { CommonTransactionReceipt{ TransactionHash: utils.TestHexToFelt(t, "0x46a9f52a96b2d226407929e04cb02507e531f7c78b9196fc8c910351d8c33f3"), ActualFee: utils.TestHexToFelt(t, "0x0"), - Status: TransactionAcceptedOnL1, + FinalityStatus: TxnFinalityStatusAcceptedOnL1, + ExecutionStatus: TxnExecutionStatusSUCCEEDED, BlockHash: utils.TestHexToFelt(t, "0x184268bfbce24766fa53b65c9c8b30b295e145e8281d543a015b46308e27fdf"), BlockNumber: 300114, Type: "DECLARE", diff --git a/rpc/types.go b/rpc/types.go index 133d7b8a..5fbe10ac 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -176,54 +176,64 @@ type FeeEstimate struct { OverallFee NumAsHex `json:"overall_fee"` } -type TransactionState string +type TxnExecutionStatus string const ( - TransactionAcceptedOnL1 TransactionState = "ACCEPTED_ON_L1" - TransactionAcceptedOnL2 TransactionState = "ACCEPTED_ON_L2" - TransactionNotReceived TransactionState = "NOT_RECEIVED" - TransactionPending TransactionState = "PENDING" - TransactionReceived TransactionState = "RECEIVED" - TransactionRejected TransactionState = "REJECTED" + TxnExecutionStatusSUCCEEDED TxnExecutionStatus = "SUCCEEDED" + TxnExecutionStatusREVERTED TxnExecutionStatus = "REVERTED" ) -func (ts *TransactionState) UnmarshalJSON(data []byte) error { +func (ts *TxnExecutionStatus) UnmarshalJSON(data []byte) error { unquoted, err := strconv.Unquote(string(data)) if err != nil { return err } switch unquoted { - case "ACCEPTED_ON_L2": - *ts = TransactionAcceptedOnL2 - case "ACCEPTED_ON_L1": - *ts = TransactionAcceptedOnL1 - case "NOT_RECEIVED": - *ts = TransactionNotReceived - case "PENDING": - *ts = TransactionPending - case "RECEIVED": - *ts = TransactionReceived - case "REJECTED": - *ts = TransactionRejected + case "SUCCEEDED": + *ts = TxnExecutionStatusSUCCEEDED + case "REVERTED": + *ts = TxnExecutionStatusREVERTED default: return fmt.Errorf("unsupported status: %s", data) } return nil } -func (ts TransactionState) MarshalJSON() ([]byte, error) { +func (ts TxnExecutionStatus) MarshalJSON() ([]byte, error) { return []byte(strconv.Quote(string(ts))), nil } -func (s TransactionState) String() string { +func (s TxnExecutionStatus) String() string { return string(s) } -func (s TransactionState) IsTransactionFinal() bool { - if s == TransactionAcceptedOnL2 || - s == TransactionAcceptedOnL1 || - s == TransactionRejected { - return true +type TxnFinalityStatus string + +const ( + TxnFinalityStatusAcceptedOnL1 TxnFinalityStatus = "ACCEPTED_ON_L1" + TxnFinalityStatusAcceptedOnL2 TxnFinalityStatus = "ACCEPTED_ON_L2" +) + +func (ts *TxnFinalityStatus) UnmarshalJSON(data []byte) error { + unquoted, err := strconv.Unquote(string(data)) + if err != nil { + return err + } + switch unquoted { + case "ACCEPTED_ON_L1": + *ts = TxnFinalityStatusAcceptedOnL1 + case "ACCEPTED_ON_L2": + *ts = TxnFinalityStatusAcceptedOnL2 + default: + return fmt.Errorf("unsupported status: %s", data) } - return false + return nil +} + +func (ts TxnFinalityStatus) MarshalJSON() ([]byte, error) { + return []byte(strconv.Quote(string(ts))), nil +} + +func (s TxnFinalityStatus) String() string { + return string(s) } diff --git a/rpc/types_transaction_receipt.go b/rpc/types_transaction_receipt.go index 08fa85a4..d00db656 100644 --- a/rpc/types_transaction_receipt.go +++ b/rpc/types_transaction_receipt.go @@ -14,12 +14,14 @@ type CommonTransactionReceipt struct { // TransactionHash The hash identifying the transaction TransactionHash *felt.Felt `json:"transaction_hash"` // ActualFee The fee that was charged by the sequencer - ActualFee *felt.Felt `json:"actual_fee"` - Status TransactionState `json:"status"` - BlockHash *felt.Felt `json:"block_hash"` - BlockNumber uint64 `json:"block_number"` - Type TransactionType `json:"type"` - MessagesSent []MsgToL1 `json:"messages_sent"` + ActualFee *felt.Felt `json:"actual_fee"` + ExecutionStatus TxnExecutionStatus `json:"execution_status"` + FinalityStatus TxnFinalityStatus `json:"finality_status"` + BlockHash *felt.Felt `json:"block_hash"` + BlockNumber uint64 `json:"block_number"` + Type TransactionType `json:"type"` + MessagesSent []MsgToL1 `json:"messages_sent"` + RevertReason string `json:"revert_reason"` // Events The events emitted as part of this transaction Events []Event `json:"events"` } @@ -114,9 +116,12 @@ type PendingCommonTransactionReceiptProperties struct { // TransactionHash The hash identifying the transaction TransactionHash *felt.Felt `json:"transaction_hash"` // ActualFee The fee that was charged by the sequencer - ActualFee *felt.Felt `json:"actual_fee"` - Type TransactionType `json:"type,omitempty"` - MessagesSent []MsgToL1 `json:"messages_sent"` + ActualFee *felt.Felt `json:"actual_fee"` + Type TransactionType `json:"type,omitempty"` + MessagesSent []MsgToL1 `json:"messages_sent"` + ExecutionStatus TxnExecutionStatus `json:"execution_status"` + FinalityStatus TxnFinalityStatus `json:"finality_status"` + RevertReason string `json:"revert_reason"` // Events The events emitted as part of this transaction Events []Event `json:"events"` }