From cc4b0e837d9613f991f51befbc7469f4e338848c Mon Sep 17 00:00:00 2001 From: Josh Klopfenstein Date: Tue, 14 Nov 2023 23:53:37 -0600 Subject: [PATCH] best guesses at feeder types --- adapters/sn2core/sn2core.go | 71 ++++++++++++++++++++++++---------- rpc/transaction.go | 1 - starknet/transaction.go | 77 ++++++++++++++++++++++++++++++------- 3 files changed, 113 insertions(+), 36 deletions(-) diff --git a/adapters/sn2core/sn2core.go b/adapters/sn2core/sn2core.go index d08863e290..1f4ba79f3a 100644 --- a/adapters/sn2core/sn2core.go +++ b/adapters/sn2core/sn2core.go @@ -156,17 +156,34 @@ func AdaptTransaction(transaction *starknet.Transaction) (core.Transaction, erro func AdaptDeclareTransaction(t *starknet.Transaction) *core.DeclareTransaction { return &core.DeclareTransaction{ - TransactionHash: t.Hash, - SenderAddress: t.SenderAddress, - MaxFee: t.MaxFee, - TransactionSignature: *t.Signature, - Nonce: t.Nonce, - Version: (*core.TransactionVersion)(t.Version), - ClassHash: t.ClassHash, - CompiledClassHash: t.CompiledClassHash, + TransactionHash: t.Hash, + SenderAddress: t.SenderAddress, + MaxFee: t.MaxFee, + TransactionSignature: *t.Signature, + Nonce: t.Nonce, + Version: (*core.TransactionVersion)(t.Version), + ClassHash: t.ClassHash, + CompiledClassHash: t.CompiledClassHash, + ResourceBounds: adaptResourceBounds(t.ResourceBounds), + Tip: t.Tip, + PaymasterData: t.PaymasterData, + AccountDeploymentData: t.AccountDeploymentData, + NonceDAMode: core.DataAvailabilityMode(t.NonceDAMode), + FeeDAMode: core.DataAvailabilityMode(t.FeeDAMode), } } +func adaptResourceBounds(rb map[starknet.Resource]*starknet.ResourceBounds) map[core.Resource]*core.ResourceBounds { + coreBounds := make(map[core.Resource]*core.ResourceBounds, len(rb)) + for resource, bounds := range rb { + coreBounds[core.Resource(resource)] = &core.ResourceBounds{ + MaxAmount: bounds.MaxAmount, + MaxPricePerUnit: bounds.MaxPricePerUnit, + } + } + return coreBounds +} + func AdaptDeployTransaction(t *starknet.Transaction) *core.DeployTransaction { if t.ContractAddress == nil { t.ContractAddress = core.ContractAddress(&felt.Zero, t.ClassHash, t.ContractAddressSalt, *t.ConstructorCallData) @@ -183,15 +200,21 @@ func AdaptDeployTransaction(t *starknet.Transaction) *core.DeployTransaction { func AdaptInvokeTransaction(t *starknet.Transaction) *core.InvokeTransaction { return &core.InvokeTransaction{ - TransactionHash: t.Hash, - ContractAddress: t.ContractAddress, - EntryPointSelector: t.EntryPointSelector, - Nonce: t.Nonce, - CallData: *t.CallData, - TransactionSignature: *t.Signature, - MaxFee: t.MaxFee, - Version: (*core.TransactionVersion)(t.Version), - SenderAddress: t.SenderAddress, + TransactionHash: t.Hash, + ContractAddress: t.ContractAddress, + EntryPointSelector: t.EntryPointSelector, + Nonce: t.Nonce, + CallData: *t.CallData, + TransactionSignature: *t.Signature, + MaxFee: t.MaxFee, + Version: (*core.TransactionVersion)(t.Version), + SenderAddress: t.SenderAddress, + ResourceBounds: adaptResourceBounds(t.ResourceBounds), + Tip: t.Tip, + PaymasterData: t.PaymasterData, + AccountDeploymentData: t.AccountDeploymentData, + NonceDAMode: core.DataAvailabilityMode(t.NonceDAMode), + FeeDAMode: core.DataAvailabilityMode(t.FeeDAMode), } } @@ -208,10 +231,16 @@ func AdaptL1HandlerTransaction(t *starknet.Transaction) *core.L1HandlerTransacti func AdaptDeployAccountTransaction(t *starknet.Transaction) *core.DeployAccountTransaction { return &core.DeployAccountTransaction{ - DeployTransaction: *AdaptDeployTransaction(t), - MaxFee: t.MaxFee, - TransactionSignature: *t.Signature, - Nonce: t.Nonce, + DeployTransaction: *AdaptDeployTransaction(t), + MaxFee: t.MaxFee, + TransactionSignature: *t.Signature, + Nonce: t.Nonce, + ResourceBounds: adaptResourceBounds(t.ResourceBounds), + Tip: t.Tip, + PaymasterData: t.PaymasterData, + AccountDeploymentData: t.AccountDeploymentData, + NonceDAMode: core.DataAvailabilityMode(t.NonceDAMode), + FeeDAMode: core.DataAvailabilityMode(t.FeeDAMode), } } diff --git a/rpc/transaction.go b/rpc/transaction.go index 5bf00ace84..caedea100b 100644 --- a/rpc/transaction.go +++ b/rpc/transaction.go @@ -294,7 +294,6 @@ func adaptBroadcastedTransaction(broadcastedTxn *BroadcastedTransaction, network utils.Network, ) (core.Transaction, core.Class, *felt.Felt, error) { var feederTxn starknet.Transaction - // TODO does this still work in v3 world? if err := copier.Copy(&feederTxn, broadcastedTxn.Transaction); err != nil { return nil, nil, nil, err } diff --git a/starknet/transaction.go b/starknet/transaction.go index 93e49f1c30..a05fbce6ed 100644 --- a/starknet/transaction.go +++ b/starknet/transaction.go @@ -104,22 +104,71 @@ func (t *TransactionType) UnmarshalJSON(data []byte) error { return nil } +type Resource uint32 + +const ( + ResourceL1Gas Resource = iota + 1 + ResourceL2Gas +) + +func (r *Resource) UnmarshalJSON(data []byte) error { + switch string(data) { + case `"L1_GAS"`: + *r = ResourceL1Gas + case `"L2_GAS"`: + *r = ResourceL2Gas + default: + return fmt.Errorf("unknown resource: %s", string(data)) + } + return nil +} + +type DataAvailabilityMode uint32 + +const ( + DAModeL1 DataAvailabilityMode = iota + 1 + DAModeL2 +) + +func (da *DataAvailabilityMode) UnmarshalJSON(data []byte) error { + switch string(data) { + case `"L1"`: + *da = DAModeL1 + case `"L2"`: + *da = DAModeL2 + default: + return fmt.Errorf("unknown DataAvailabilityMode: %s", string(data)) + } + return nil +} + +type ResourceBounds struct { + MaxAmount uint64 `json:"max_amount"` + MaxPricePerUnit *felt.Felt `json:"max_price_per_unit"` +} + // Transaction object returned by the feeder in JSON format for multiple endpoints type Transaction struct { - Hash *felt.Felt `json:"transaction_hash,omitempty" copier:"must,nopanic"` - Version *felt.Felt `json:"version,omitempty"` - ContractAddress *felt.Felt `json:"contract_address,omitempty"` - ContractAddressSalt *felt.Felt `json:"contract_address_salt,omitempty"` - ClassHash *felt.Felt `json:"class_hash,omitempty"` - ConstructorCallData *[]*felt.Felt `json:"constructor_calldata,omitempty"` - Type TransactionType `json:"type,omitempty"` - SenderAddress *felt.Felt `json:"sender_address,omitempty"` - MaxFee *felt.Felt `json:"max_fee,omitempty"` - Signature *[]*felt.Felt `json:"signature,omitempty"` - CallData *[]*felt.Felt `json:"calldata,omitempty"` - EntryPointSelector *felt.Felt `json:"entry_point_selector,omitempty"` - Nonce *felt.Felt `json:"nonce,omitempty"` - CompiledClassHash *felt.Felt `json:"compiled_class_hash,omitempty"` + Hash *felt.Felt `json:"transaction_hash,omitempty" copier:"must,nopanic"` + Version *felt.Felt `json:"version,omitempty"` + ContractAddress *felt.Felt `json:"contract_address,omitempty"` + ContractAddressSalt *felt.Felt `json:"contract_address_salt,omitempty"` + ClassHash *felt.Felt `json:"class_hash,omitempty"` + ConstructorCallData *[]*felt.Felt `json:"constructor_calldata,omitempty"` + Type TransactionType `json:"type,omitempty"` + SenderAddress *felt.Felt `json:"sender_address,omitempty"` + MaxFee *felt.Felt `json:"max_fee,omitempty"` + Signature *[]*felt.Felt `json:"signature,omitempty"` + CallData *[]*felt.Felt `json:"calldata,omitempty"` + EntryPointSelector *felt.Felt `json:"entry_point_selector,omitempty"` + Nonce *felt.Felt `json:"nonce,omitempty"` + CompiledClassHash *felt.Felt `json:"compiled_class_hash,omitempty"` + ResourceBounds map[Resource]*ResourceBounds `json:"resource_bounds,omitempty"` + Tip uint64 `json:"tip,omitempty"` + NonceDAMode DataAvailabilityMode `json:"nonce_data_availability_mode,omitempty"` + FeeDAMode DataAvailabilityMode `json:"fee_data_availability_mode,omitempty"` + AccountDeploymentData []*felt.Felt `json:"account_deployment_data,omitempty"` + PaymasterData []*felt.Felt `json:"paymaster_data,omitempty"` } type TransactionStatus struct {