diff --git a/account/account_test.go b/account/account_test.go index 732d358e..0e0a36df 100644 --- a/account/account_test.go +++ b/account/account_test.go @@ -998,9 +998,9 @@ func TestWaitForTransactionReceiptMOCK(t *testing.T) { Hash: new(felt.Felt).SetUint64(2), ShouldCallTransactionReceipt: true, ExpectedReceipt: &rpc.TransactionReceiptWithBlockInfo{ - UnknownTransactionReceipt: rpc.UnknownTransactionReceipt{}, - BlockHash: new(felt.Felt).SetUint64(2), - BlockNumber: 2, + TransactionReceipt: rpc.TransactionReceipt{}, + BlockHash: new(felt.Felt).SetUint64(2), + BlockNumber: 2, }, ExpectedErr: nil, @@ -1026,7 +1026,8 @@ func TestWaitForTransactionReceiptMOCK(t *testing.T) { if test.ExpectedErr != nil { require.Equal(t, test.ExpectedErr, err) } else { - require.Equal(t, test.ExpectedReceipt.GetExecutionStatus(), (resp.TransactionReceipt).GetExecutionStatus()) + // check + require.Equal(t, test.ExpectedReceipt.ExecutionStatus, (resp.TransactionReceipt).ExecutionStatus) } } @@ -1074,7 +1075,7 @@ func TestWaitForTransactionReceipt(t *testing.T) { { Timeout: 3, // Should poll 3 times Hash: new(felt.Felt).SetUint64(100), - ExpectedReceipt: nil, + ExpectedReceipt: rpc.TransactionReceipt{}, ExpectedErr: rpc.Err(rpc.InternalError, "Post \"http://0.0.0.0:5050/\": context deadline exceeded"), }, }, @@ -1088,7 +1089,7 @@ func TestWaitForTransactionReceipt(t *testing.T) { if test.ExpectedErr != nil { require.Equal(t, test.ExpectedErr.Error(), err.Error()) } else { - require.Equal(t, test.ExpectedReceipt.GetExecutionStatus(), (*resp).GetExecutionStatus()) + require.Equal(t, test.ExpectedReceipt.ExecutionStatus, (*resp).ExecutionStatus) } } diff --git a/rpc/mock_test.go b/rpc/mock_test.go index b9b2d903..fa70cdd4 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -394,13 +394,13 @@ func mock_starknet_getTransactionReceipt(result interface{}, method string, args return err } - transaction := InvokeTransactionReceipt(CommonTransactionReceipt{ + transaction := TransactionReceipt{ TransactionHash: arg0Felt, FinalityStatus: TxnFinalityStatusAcceptedOnL1, Events: []Event{{ FromAddress: fromAddressFelt, }}, - }) + } outputContent, err := json.Marshal(transaction) if err != nil { return err diff --git a/rpc/transaction_test.go b/rpc/transaction_test.go index 1ac9df2b..1d0b0960 100644 --- a/rpc/transaction_test.go +++ b/rpc/transaction_test.go @@ -187,110 +187,108 @@ func TestTransactionReceipt(t *testing.T) { testConfig := beforeEach(t) type testSetType struct { - TxnHash *felt.Felt - ExpectedResp TransactionReceiptWithBlockInfo + TxnHash *felt.Felt + ExpectedResp TransactionReceiptWithBlockInfo } - var receiptTxn52767_16 = InvokeTransactionReceipt( - CommonTransactionReceipt{ - TransactionHash: utils.TestHexToFelt(t, "0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b"), - ActualFee: FeePayment{ - Amount: utils.TestHexToFelt(t, "0x16409a78a10b00"), - Unit: UnitStrk, - }, - Type: "INVOKE", - ExecutionStatus: TxnExecutionStatusSUCCEEDED, - FinalityStatus: TxnFinalityStatusAcceptedOnL1, - MessagesSent: []MsgToL1{}, - Events: []Event{ - { - Data: []*felt.Felt{ - utils.TestHexToFelt(t, "0x3028044a4c4df95c0b0a907307c6feffa76b9c38e83088ade29b186a250eb13"), - utils.TestHexToFelt(t, "0x3"), - utils.TestHexToFelt(t, "0x17a393a5e943cec833c8a8f4cbbf7c58361fb2fdd9caa0c36d901eedec4938e"), - utils.TestHexToFelt(t, "0x776731d30bd922ac0390edfc664ed31b232aa7c7ce389c333e34c6b32957532"), - utils.TestHexToFelt(t, "0x40851db0ebaebb9f8a18eda25005c050793f2a69e9e7d1f44bc133752898918"), - }, - FromAddress: utils.TestHexToFelt(t, "0x243d436e1f7cea085aaa42834975488029b1ebf67cea1d2e86f7de58e7d34a3"), - Keys: []*felt.Felt{ - utils.TestHexToFelt(t, "0x15bd0500dc9d7e69ab9577f73a8d753e8761bed10f25ba0f124254dc4edb8b4"), - }, + var receiptTxn52767_16 = TransactionReceipt{ + TransactionHash: utils.TestHexToFelt(t, "0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b"), + ActualFee: FeePayment{ + Amount: utils.TestHexToFelt(t, "0x16409a78a10b00"), + Unit: UnitStrk, + }, + Type: "INVOKE", + ExecutionStatus: TxnExecutionStatusSUCCEEDED, + FinalityStatus: TxnFinalityStatusAcceptedOnL1, + MessagesSent: []MsgToL1{}, + Events: []Event{ + { + Data: []*felt.Felt{ + utils.TestHexToFelt(t, "0x3028044a4c4df95c0b0a907307c6feffa76b9c38e83088ade29b186a250eb13"), + utils.TestHexToFelt(t, "0x3"), + utils.TestHexToFelt(t, "0x17a393a5e943cec833c8a8f4cbbf7c58361fb2fdd9caa0c36d901eedec4938e"), + utils.TestHexToFelt(t, "0x776731d30bd922ac0390edfc664ed31b232aa7c7ce389c333e34c6b32957532"), + utils.TestHexToFelt(t, "0x40851db0ebaebb9f8a18eda25005c050793f2a69e9e7d1f44bc133752898918"), }, - { - Data: []*felt.Felt{ - utils.TestHexToFelt(t, "0x6016d919abf2ddefe03dacc2ff5c8f42eb80cf65add1e90dd73c5c5e06ef3e2"), - utils.TestHexToFelt(t, "0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8"), - utils.TestHexToFelt(t, "0x16409a78a10b00"), - utils.TestHexToFelt(t, "0x0"), - }, - FromAddress: utils.TestHexToFelt(t, "0x4718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"), - Keys: []*felt.Felt{ - utils.TestHexToFelt(t, "0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9"), - }, + FromAddress: utils.TestHexToFelt(t, "0x243d436e1f7cea085aaa42834975488029b1ebf67cea1d2e86f7de58e7d34a3"), + Keys: []*felt.Felt{ + utils.TestHexToFelt(t, "0x15bd0500dc9d7e69ab9577f73a8d753e8761bed10f25ba0f124254dc4edb8b4"), }, }, - ExecutionResources: ExecutionResources{ - ComputationResources: ComputationResources{ - Steps: 5774, - PedersenApps: 24, - ECOPApps: 3, - RangeCheckApps: 152, + { + Data: []*felt.Felt{ + utils.TestHexToFelt(t, "0x6016d919abf2ddefe03dacc2ff5c8f42eb80cf65add1e90dd73c5c5e06ef3e2"), + utils.TestHexToFelt(t, "0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8"), + utils.TestHexToFelt(t, "0x16409a78a10b00"), + utils.TestHexToFelt(t, "0x0"), }, - DataAvailability: DataAvailability{ - L1Gas: 0, - L1DataGas: 0, + FromAddress: utils.TestHexToFelt(t, "0x4718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d"), + Keys: []*felt.Felt{ + utils.TestHexToFelt(t, "0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9"), }, }, - }) + }, + ExecutionResources: ExecutionResources{ + ComputationResources: ComputationResources{ + Steps: 5774, + PedersenApps: 24, + ECOPApps: 3, + RangeCheckApps: 152, + }, + DataAvailability: DataAvailability{ + L1Gas: 0, + L1DataGas: 0, + }, + }, + } // https://voyager.online/tx/0x74011377f326265f5a54e27a27968355e7033ad1de11b77b225374875aff519 - var receiptL1Handler = L1HandlerTransactionReceipt{ + var receiptL1Handler = TransactionReceipt{ MessageHash: NumAsHex("0xf93cd5994e1da0cea88f2310735746d049be9fa55f4a38d5545bc325bcaa49af"), - CommonTransactionReceipt: CommonTransactionReceipt{ - TransactionHash: utils.TestHexToFelt(t, "0x74011377f326265f5a54e27a27968355e7033ad1de11b77b225374875aff519"), - ActualFee: FeePayment{ - Amount: utils.TestHexToFelt(t, "0x0"), - Unit: UnitWei, - }, - Type: "L1_HANDLER", - ExecutionStatus: TxnExecutionStatusSUCCEEDED, - FinalityStatus: TxnFinalityStatusAcceptedOnL2, - MessagesSent: []MsgToL1{}, - Events: []Event{ - { - Data: utils.TestHexArrToFelt(t, []string{ - "0x0", - "0x3580a65260563b5511ddf2eafb83d6b309dce7fc25271df8c040a437f09a399", - "0xdfd54233d96e4b", - "0x0", - }), - FromAddress: utils.TestHexToFelt(t, "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"), - Keys: []*felt.Felt{ - utils.TestHexToFelt(t, "0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9"), - }, - }, - { - Data: utils.TestHexArrToFelt(t, []string{ - "0xdfd54233d96e4b", - "0x0", - }), - FromAddress: utils.TestHexToFelt(t, "0x73314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82"), - Keys: utils.TestHexArrToFelt(t, []string{ - "0x374396cb322ab5ffd35ddb8627514609289d22c07d039ead5327782f61bb833", - "0x455448", - "0x3580a65260563b5511ddf2eafb83d6b309dce7fc25271df8c040a437f09a399", - }), + + TransactionHash: utils.TestHexToFelt(t, "0x74011377f326265f5a54e27a27968355e7033ad1de11b77b225374875aff519"), + ActualFee: FeePayment{ + Amount: utils.TestHexToFelt(t, "0x0"), + Unit: UnitWei, + }, + Type: "L1_HANDLER", + ExecutionStatus: TxnExecutionStatusSUCCEEDED, + FinalityStatus: TxnFinalityStatusAcceptedOnL2, + MessagesSent: []MsgToL1{}, + Events: []Event{ + { + Data: utils.TestHexArrToFelt(t, []string{ + "0x0", + "0x3580a65260563b5511ddf2eafb83d6b309dce7fc25271df8c040a437f09a399", + "0xdfd54233d96e4b", + "0x0", + }), + FromAddress: utils.TestHexToFelt(t, "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"), + Keys: []*felt.Felt{ + utils.TestHexToFelt(t, "0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9"), }, }, - ExecutionResources: ExecutionResources{ - ComputationResources: ComputationResources{ - Steps: 4230, - PedersenApps: 19, - RangeCheckApps: 94, - }, - DataAvailability: DataAvailability{ - L1Gas: 0, - L1DataGas: 128, - }, + { + Data: utils.TestHexArrToFelt(t, []string{ + "0xdfd54233d96e4b", + "0x0", + }), + FromAddress: utils.TestHexToFelt(t, "0x73314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82"), + Keys: utils.TestHexArrToFelt(t, []string{ + "0x374396cb322ab5ffd35ddb8627514609289d22c07d039ead5327782f61bb833", + "0x455448", + "0x3580a65260563b5511ddf2eafb83d6b309dce7fc25271df8c040a437f09a399", + }), + }, + }, + ExecutionResources: ExecutionResources{ + ComputationResources: ComputationResources{ + Steps: 4230, + PedersenApps: 19, + RangeCheckApps: 94, + }, + DataAvailability: DataAvailability{ + L1Gas: 0, + L1DataGas: 128, }, }, } @@ -300,17 +298,17 @@ func TestTransactionReceipt(t *testing.T) { { TxnHash: utils.TestHexToFelt(t, "0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b"), ExpectedResp: TransactionReceiptWithBlockInfo{ - UnknownTransactionReceipt: UnknownTransactionReceipt{receiptTxn52767_16}, - BlockNumber: 52767, - BlockHash: utils.TestHexToFelt(t, "0x4ae5d52c75e4dea5694f456069f830cfbc7bec70427eee170c3385f751b8564"), - }, + TransactionReceipt: receiptTxn52767_16, + BlockNumber: 52767, + BlockHash: utils.TestHexToFelt(t, "0x4ae5d52c75e4dea5694f456069f830cfbc7bec70427eee170c3385f751b8564"), + }, }, { TxnHash: utils.TestHexToFelt(t, "0x74011377f326265f5a54e27a27968355e7033ad1de11b77b225374875aff519"), ExpectedResp: TransactionReceiptWithBlockInfo{ - UnknownTransactionReceipt: UnknownTransactionReceipt{receiptL1Handler}, - BlockNumber: 648615, - BlockHash: utils.TestHexToFelt(t, "0x3c9cd4649a5dfed63db676f6f7fd181975f8e85f10197a6665fd393d1c9c1a0"), + TransactionReceipt: receiptL1Handler, + BlockNumber: 648615, + BlockHash: utils.TestHexToFelt(t, "0x3c9cd4649a5dfed63db676f6f7fd181975f8e85f10197a6665fd393d1c9c1a0"), }, }, }, @@ -318,9 +316,9 @@ func TestTransactionReceipt(t *testing.T) { { TxnHash: utils.TestHexToFelt(t, "0xf2f3d50192637e8d5e817363460c39d3a668fe12f117ecedb9749466d8352b"), ExpectedResp: TransactionReceiptWithBlockInfo{ - UnknownTransactionReceipt: UnknownTransactionReceipt{receiptTxn52767_16}, - BlockNumber: 52767, - BlockHash: utils.TestHexToFelt(t, "0x4ae5d52c75e4dea5694f456069f830cfbc7bec70427eee170c3385f751b8564"), + TransactionReceipt: receiptTxn52767_16, + BlockNumber: 52767, + BlockHash: utils.TestHexToFelt(t, "0x4ae5d52c75e4dea5694f456069f830cfbc7bec70427eee170c3385f751b8564"), }, }, }, diff --git a/rpc/types_block.go b/rpc/types_block.go index 15670387..41769e86 100644 --- a/rpc/types_block.go +++ b/rpc/types_block.go @@ -137,8 +137,8 @@ type BlockBodyWithReceipts struct { } type TransactionWithReceipt struct { - Transaction UnknownTransaction `json:"transaction"` - Receipt UnknownTransactionReceipt `json:"receipt"` + Transaction UnknownTransaction `json:"transaction"` + Receipt TransactionReceipt `json:"receipt"` } // The dynamic block being constructed by the sequencer. Note that this object will be deprecated upon decentralization. diff --git a/rpc/types_transaction_receipt.go b/rpc/types_transaction_receipt.go index 1ed46222..d9d04d4a 100644 --- a/rpc/types_transaction_receipt.go +++ b/rpc/types_transaction_receipt.go @@ -6,9 +6,34 @@ import ( "strconv" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/starknet.go/utils" ) +type MsgToL1 struct { + // FromAddress The address of the L2 contract sending the message + FromAddress *felt.Felt `json:"from_address"` + // ToAddress The target L1 address the message is sent to + ToAddress *felt.Felt `json:"to_address"` + //Payload The payload of the message + Payload []*felt.Felt `json:"payload"` +} + +type MsgFromL1 struct { + // FromAddress The address of the L1 contract sending the message + FromAddress string `json:"from_address"` + // ToAddress The target L2 address the message is sent to + ToAddress *felt.Felt `json:"to_address"` + // EntryPointSelector The selector of the l1_handler in invoke in the target contract + Selector *felt.Felt `json:"entry_point_selector"` + //Payload The payload of the message + Payload []*felt.Felt `json:"payload"` +} + +type OrderedMsg struct { + // The order of the message within the transaction + Order int `json:"order"` + MsgToL1 MsgToL1 +} + type FeePayment struct { Amount *felt.Felt `json:"amount"` Unit FeePaymentUnit `json:"unit"` @@ -21,32 +46,21 @@ const ( UnitStrk FeePaymentUnit = "FRI" ) -// CommonTransactionReceipt Common properties for a transaction receipt -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 FeePayment `json:"actual_fee"` - ExecutionStatus TxnExecutionStatus `json:"execution_status"` - FinalityStatus TxnFinalityStatus `json:"finality_status"` - Type TransactionType `json:"type,omitempty"` - MessagesSent []MsgToL1 `json:"messages_sent"` - RevertReason string `json:"revert_reason,omitempty"` - // Events The events emitted as part of this transaction +// TransactionReceipt represents the common structure of a transaction receipt. +type TransactionReceipt struct { + TransactionHash *felt.Felt `json:"transaction_hash"` + ActualFee FeePayment `json:"actual_fee"` + ExecutionStatus TxnExecutionStatus `json:"execution_status"` + FinalityStatus TxnFinalityStatus `json:"finality_status"` + Type TransactionType `json:"type,omitempty"` + MessagesSent []MsgToL1 `json:"messages_sent"` + RevertReason string `json:"revert_reason,omitempty"` Events []Event `json:"events"` ExecutionResources ExecutionResources `json:"execution_resources"` + ContractAddress *felt.Felt `json:"contract_address,omitempty"` + MessageHash NumAsHex `json:"message_hash,omitempty"` } -// Hash returns the transaction hash associated with the CommonTransactionReceipt. -func (tr CommonTransactionReceipt) Hash() *felt.Felt { - return tr.TransactionHash -} - -func (tr CommonTransactionReceipt) GetExecutionStatus() TxnExecutionStatus { - return tr.ExecutionStatus -} - -// TODO: check how we can move that type up in starknet.go/types type TransactionType string const ( @@ -108,82 +122,6 @@ func (tt TransactionType) MarshalJSON() ([]byte, error) { return []byte(strconv.Quote(string(tt))), nil } -// InvokeTransactionReceipt Invoke Transaction Receipt -type InvokeTransactionReceipt CommonTransactionReceipt - -// Hash returns the hash of the invoke transaction receipt. -func (tr InvokeTransactionReceipt) Hash() *felt.Felt { - return tr.TransactionHash -} - -// GetExecutionStatus returns the execution status of the InvokeTransactionReceipt. -func (tr InvokeTransactionReceipt) GetExecutionStatus() TxnExecutionStatus { - return tr.ExecutionStatus -} - -// DeclareTransactionReceipt Declare Transaction Receipt -type DeclareTransactionReceipt CommonTransactionReceipt - -// Hash returns the transaction hash. -func (tr DeclareTransactionReceipt) Hash() *felt.Felt { - return tr.TransactionHash -} - -// GetExecutionStatus returns the execution status of the DeclareTransactionReceipt function. -func (tr DeclareTransactionReceipt) GetExecutionStatus() TxnExecutionStatus { - return tr.ExecutionStatus -} - -// DeployTransactionReceipt Deploy Transaction Receipt -type DeployTransactionReceipt struct { - CommonTransactionReceipt - // The address of the deployed contract - ContractAddress *felt.Felt `json:"contract_address"` -} - -// Hash returns the transaction hash of the DeployTransactionReceipt. -func (tr DeployTransactionReceipt) Hash() *felt.Felt { - return tr.TransactionHash -} - -// GetExecutionStatus returns the execution status of the DeployTransactionReceipt. -func (tr DeployTransactionReceipt) GetExecutionStatus() TxnExecutionStatus { - return tr.ExecutionStatus -} - -// DeployAccountTransactionReceipt Deploy Account Transaction Receipt -type DeployAccountTransactionReceipt struct { - CommonTransactionReceipt - // ContractAddress The address of the deployed contract - ContractAddress *felt.Felt `json:"contract_address"` -} - -// Hash returns the transaction hash for the given DeployAccountTransactionReceipt. -func (tr DeployAccountTransactionReceipt) Hash() *felt.Felt { - return tr.TransactionHash -} - -// GetExecutionStatus returns the execution status of the DeployAccountTransactionReceipt. -func (tr DeployAccountTransactionReceipt) GetExecutionStatus() TxnExecutionStatus { - return tr.ExecutionStatus -} - -// L1HandlerTransactionReceipt L1 Handler Transaction Receipt -type L1HandlerTransactionReceipt struct { - MessageHash NumAsHex `json:"message_hash"` - CommonTransactionReceipt -} - -// Hash returns the transaction hash. -func (tr L1HandlerTransactionReceipt) Hash() *felt.Felt { - return tr.TransactionHash -} - -// GetExecutionStatus returns the execution status of the L1HandlerTransactionReceipt. -func (tr L1HandlerTransactionReceipt) GetExecutionStatus() TxnExecutionStatus { - return tr.ExecutionStatus -} - type ComputationResources struct { // The number of Cairo steps used Steps int `json:"steps"` @@ -207,7 +145,6 @@ type ComputationResources struct { SegmentArenaBuiltin int `json:"segment_arena_builtin,omitempty"` } -// Validate checks if the fields are non-zero (to match the starknet-specs) func (er *ComputationResources) Validate() bool { if er.Steps == 0 || er.MemoryHoles == 0 || er.RangeCheckApps == 0 || er.PedersenApps == 0 || er.PoseidonApps == 0 || er.ECOPApps == 0 || er.ECDSAApps == 0 || er.BitwiseApps == 0 || @@ -217,7 +154,6 @@ func (er *ComputationResources) Validate() bool { return true } -// The resources consumed by the transaction, includes both computation and data. type ExecutionResources struct { ComputationResources DataAvailability `json:"data_availability"` @@ -230,119 +166,6 @@ type DataAvailability struct { L1DataGas uint `json:"l1_data_gas"` } -type TransactionReceipt interface { - Hash() *felt.Felt - GetExecutionStatus() TxnExecutionStatus -} - -type OrderedMsg struct { - // The order of the message within the transaction - Order int `json:"order"` - MsgToL1 MsgToL1 -} - -type MsgToL1 struct { - // FromAddress The address of the L2 contract sending the message - FromAddress *felt.Felt `json:"from_address"` - // ToAddress The target L1 address the message is sent to - ToAddress *felt.Felt `json:"to_address"` - //Payload The payload of the message - Payload []*felt.Felt `json:"payload"` -} - -type MsgFromL1 struct { - // FromAddress The address of the L1 contract sending the message - FromAddress string `json:"from_address"` - // ToAddress The target L2 address the message is sent to - ToAddress *felt.Felt `json:"to_address"` - // EntryPointSelector The selector of the l1_handler in invoke in the target contract - Selector *felt.Felt `json:"entry_point_selector"` - //Payload The payload of the message - Payload []*felt.Felt `json:"payload"` -} - -type UnknownTransactionReceipt struct{ TransactionReceipt } - -// UnmarshalJSON unmarshals the given JSON data into an UnknownTransactionReceipt. -// -// Parameters: -// - data: It takes a byte slice as a parameter, which represents the JSON data to be unmarshalled -// Returns: -// - error: an error if the unmarshaling fails -func (tr *UnknownTransactionReceipt) UnmarshalJSON(data []byte) error { - var dec map[string]interface{} - if err := json.Unmarshal(data, &dec); err != nil { - return err - } - - // BlockWithReceipts wrap receipts in the TransactionReceipt field. - dec, err := utils.UnwrapJSON(dec, "TransactionReceipt") - if err != nil { - return err - } - bytes, err := json.Marshal(dec) - if err != nil { - return err - } - t, err := unmarshalTransactionReceipt(bytes) - if err != nil { - return err - } - *tr = UnknownTransactionReceipt{t} - return nil -} - -// unmarshalTransactionReceipt unmarshals a transaction receipt from a generic interface. -// -// Parameters: -// - t: The interface{} to be unmarshalled -// Returns: -// - TransactionReceipt: a TransactionReceipt -// - error: an error if the unmarshaling fails -func unmarshalTransactionReceipt(data []byte) (TransactionReceipt, error) { - - var dec map[string]interface{} - if err := json.Unmarshal(data, &dec); err != nil { - return nil, err - } - - typ, ok := dec["type"] - if !ok { - return nil, fmt.Errorf("unknown transaction type: %v", typ) - } - - jsonData, err := json.Marshal(dec) - if err != nil { - return nil, err - } - - switch TransactionType(typ.(string)) { - case TransactionType_Invoke: - var txn InvokeTransactionReceipt - err := json.Unmarshal(jsonData, &txn) - return txn, err - case TransactionType_L1Handler: - var txn L1HandlerTransactionReceipt - err := json.Unmarshal(jsonData, &txn) - return txn, err - case TransactionType_Declare: - var txn DeclareTransactionReceipt - err := json.Unmarshal(jsonData, &txn) - return txn, err - case TransactionType_Deploy: - var txn DeployTransactionReceipt - err := json.Unmarshal(jsonData, &txn) - return txn, err - case TransactionType_DeployAccount: - var txn DeployAccountTransactionReceipt - err := json.Unmarshal(jsonData, &txn) - return txn, err - } - - return nil, fmt.Errorf("unknown transaction type: %v", typ) -} - -// The finality status of the transaction, including the case the txn is still in the mempool or failed validation during the block construction phase type TxnStatus string const ( @@ -358,48 +181,44 @@ type TxnStatusResp struct { } type TransactionReceiptWithBlockInfo struct { - UnknownTransactionReceipt + TransactionReceipt BlockHash *felt.Felt `json:"block_hash,omitempty"` BlockNumber uint `json:"block_number,omitempty"` } -func (t *TransactionReceiptWithBlockInfo) UnmarshalJSON(data []byte) error { - var uTxnRec UnknownTransactionReceipt - err := uTxnRec.UnmarshalJSON(data) - if err != nil { - return err +func (t *TransactionReceiptWithBlockInfo) MarshalJSON() ([]byte, error) { + aux := &struct { + TransactionReceipt + BlockHash string `json:"block_hash,omitempty"` + BlockNumber uint `json:"block_number,omitempty"` + }{ + TransactionReceipt: t.TransactionReceipt, + BlockHash: t.BlockHash.String(), + BlockNumber: t.BlockNumber, } - t.UnknownTransactionReceipt = uTxnRec - aux := &struct { + return json.Marshal(aux) +} + +func (tr *TransactionReceiptWithBlockInfo) UnmarshalJSON(data []byte) error { + var aux struct { + TransactionReceipt BlockHash string `json:"block_hash,omitempty"` BlockNumber uint `json:"block_number,omitempty"` - }{} + } if err := json.Unmarshal(data, &aux); err != nil { return err } + + tr.TransactionReceipt = aux.TransactionReceipt + blockHash, err := new(felt.Felt).SetString(aux.BlockHash) if err != nil { return err } - - t.BlockHash = blockHash - t.BlockNumber = aux.BlockNumber + tr.BlockHash = blockHash + tr.BlockNumber = aux.BlockNumber return nil } - -func (t *TransactionReceiptWithBlockInfo) MarshalJSON() ([]byte, error) { - aux := &struct { - TransactionReceipt - BlockHash string `json:"block_hash,omitempty"` - BlockNumber uint `json:"block_number,omitempty"` - }{ - TransactionReceipt: t.UnknownTransactionReceipt.TransactionReceipt, - BlockHash: t.BlockHash.String(), - BlockNumber: t.BlockNumber, - } - - return json.Marshal(aux) -}