diff --git a/account/account.go b/account/account.go index e5d26a18..2dc00bc2 100644 --- a/account/account.go +++ b/account/account.go @@ -604,17 +604,16 @@ func (account *Account) Events(ctx context.Context, input rpc.EventsInput) (*rpc return account.provider.Events(ctx, input) } -// Nonce returns the nonce for the specified account and contract address. +// Nonce retrieves the nonce for a given block ID and contract address. // // Parameters: -// - ctx: The context.Context object for the function -// - blockID: the rpc.BlockID object for the function -// - contractAddress: the felt.Felt (address of the contract) whose nonce we're seeking -// +// - ctx: is the context.Context for the function call +// - blockID: is the ID of the block +// - contractAddress: is the address of the contract // Returns: -// - *string: a string pointer -// - error: an error -func (account *Account) Nonce(ctx context.Context, blockID rpc.BlockID, contractAddress *felt.Felt) (*string, error) { +// - *felt.Felt: the contract's nonce at the requested state +// - error: an error if any +func (account *Account) Nonce(ctx context.Context, blockID rpc.BlockID, contractAddress *felt.Felt) (*felt.Felt, error) { return account.provider.Nonce(ctx, blockID, contractAddress) } diff --git a/account/account_test.go b/account/account_test.go index dd88ed8f..0c80c60e 100644 --- a/account/account_test.go +++ b/account/account_test.go @@ -932,7 +932,7 @@ func TestAddDeclareTxn(t *testing.T) { require.NoError(t, err) tx := rpc.DeclareTxnV2{ - Nonce: utils.TestHexToFelt(t, *nonce), + Nonce: nonce, MaxFee: utils.TestHexToFelt(t, "0x50c8f3053db"), Type: rpc.TransactionType_Declare, Version: rpc.TransactionV2, diff --git a/examples/simpleInvoke/main.go b/examples/simpleInvoke/main.go index 4703acde..b6f577b3 100644 --- a/examples/simpleInvoke/main.go +++ b/examples/simpleInvoke/main.go @@ -66,9 +66,8 @@ func main() { panic(err.Error()) } - //Getting the nonce from the account, and then converting it into felt - nonce_string, _ := accnt.Nonce(context.Background(), rpc.BlockID{Tag: "latest"}, accnt.AccountAddress) - nonce, err := utils.HexToFelt(*nonce_string) + //Getting the nonce from the account + nonce, err := accnt.Nonce(context.Background(), rpc.BlockID{Tag: "latest"}, accnt.AccountAddress) if err != nil { panic(err.Error()) } diff --git a/mocks/mock_account.go b/mocks/mock_account.go index 5aa52b0a..04a9ddd5 100644 --- a/mocks/mock_account.go +++ b/mocks/mock_account.go @@ -1,5 +1,5 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: account.go +// Source: account/account.go // Package mocks is a generated GoMock package. package mocks @@ -25,25 +25,14 @@ type MockAccountInterfaceMockRecorder struct { mock *MockAccountInterface } -// NewMockAccountInterface returns a new instance of MockAccountInterface. -// -// Parameters: -// - ctrl: The gomock.Controller used for creating the mock. -// Returns: -// - *MockAccountInterface: a pointer to the newly created MockAccountInterface. +// NewMockAccountInterface creates a new mock instance. func NewMockAccountInterface(ctrl *gomock.Controller) *MockAccountInterface { mock := &MockAccountInterface{ctrl: ctrl} mock.recorder = &MockAccountInterfaceMockRecorder{mock} return mock } -// EXPECT is a function that returns a pointer to a MockAccountInterfaceMockRecorder -// (allows the caller to indicate expected use). -// -// Parameters: -// none -// Returns: -// - *MockAccountInterfaceMockRecorder: a pointer to a MockAccountInterfaceMockRecorder +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockAccountInterface) EXPECT() *MockAccountInterfaceMockRecorder { return m.recorder } diff --git a/mocks/mock_rpc_provider.go b/mocks/mock_rpc_provider.go index 579e01a6..63675fbf 100644 --- a/mocks/mock_rpc_provider.go +++ b/mocks/mock_rpc_provider.go @@ -232,7 +232,11 @@ func (mr *MockRpcProviderMockRecorder) ClassHashAt(ctx, blockID, contractAddress } // EstimateFee mocks base method. +<<<<<<< HEAD func (m *MockRpcProvider) EstimateFee(ctx context.Context, requests []rpc.BroadcastTxn, blockID rpc.BlockID) ([]rpc.FeeEstimate, error) { +======= +func (m *MockRpcProvider) EstimateFee(ctx context.Context, requests []rpc.EstimateFeeInput, blockID rpc.BlockID) ([]rpc.FeeEstimate, error) { +>>>>>>> main m.ctrl.T.Helper() ret := m.ctrl.Call(m, "EstimateFee", ctx, requests, blockID) ret0, _ := ret[0].([]rpc.FeeEstimate) @@ -292,10 +296,14 @@ func (mr *MockRpcProviderMockRecorder) GetTransactionStatus(ctx, transactionHash } // Nonce mocks base method. +<<<<<<< HEAD func (m *MockRpcProvider) Nonce(ctx context.Context, blockID rpc.BlockID, contractAddress *felt.Felt) (*string, error) { +======= +func (m *MockRpcProvider) Nonce(ctx context.Context, blockID rpc.BlockID, contractAddress *felt.Felt) (*felt.Felt, error) { +>>>>>>> main m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Nonce", ctx, blockID, contractAddress) - ret0, _ := ret[0].(*string) + ret0, _ := ret[0].(*felt.Felt) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/rpc/contract.go b/rpc/contract.go index d069beb9..cabcda64 100644 --- a/rpc/contract.go +++ b/rpc/contract.go @@ -123,15 +123,15 @@ func (provider *Provider) StorageAt(ctx context.Context, contractAddress *felt.F // - blockID: is the ID of the block // - contractAddress: is the address of the contract // Returns: -// - *string: the nonce +// - *felt.Felt: the contract's nonce at the requested state // - error: an error if any -func (provider *Provider) Nonce(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*string, error) { - nonce := "" +func (provider *Provider) Nonce(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*felt.Felt, error) { + var nonce *felt.Felt if err := do(ctx, provider.c, "starknet_getNonce", &nonce, blockID, contractAddress); err != nil { return nil, tryUnwrapToRPCErr(err, ErrContractNotFound, ErrBlockNotFound) } - return &nonce, nil + return nonce, nil } // EstimateFee estimates the fee for executing a set of requests on the StarkNet blockchain. diff --git a/rpc/contract_test.go b/rpc/contract_test.go index 48612151..51609006 100644 --- a/rpc/contract_test.go +++ b/rpc/contract_test.go @@ -323,16 +323,19 @@ func TestNonce(t *testing.T) { type testSetType struct { ContractAddress *felt.Felt + ExpectedNonce *felt.Felt } testSet := map[string][]testSetType{ "mock": { { ContractAddress: utils.TestHexToFelt(t, "0x0207acc15dc241e7d167e67e30e769719a727d3e0fa47f9e187707289885dfde"), + ExpectedNonce: utils.TestHexToFelt(t, "0x0"), }, }, "testnet": { { ContractAddress: utils.TestHexToFelt(t, "0x0207acc15dc241e7d167e67e30e769719a727d3e0fa47f9e187707289885dfde"), + ExpectedNonce: utils.TestHexToFelt(t, "0x0"), }, }, "mainnet": {}, @@ -341,21 +344,23 @@ func TestNonce(t *testing.T) { for _, test := range testSet { spy := NewSpy(testConfig.provider.c) testConfig.provider.c = spy - value, err := testConfig.provider.Nonce(context.Background(), WithBlockTag("latest"), test.ContractAddress) + nonce, err := testConfig.provider.Nonce(context.Background(), WithBlockTag("latest"), test.ContractAddress) if err != nil { t.Fatal(err) } - diff, err := spy.Compare(value, false) + diff, err := spy.Compare(nonce, false) if err != nil { t.Fatal("expecting to match", err) } if diff != "FullMatch" { - spy.Compare(value, true) + spy.Compare(nonce, true) t.Fatal("structure expecting to be FullMatch, instead", diff) } - if *value != "0x0" { - t.Fatalf("expecting value %s, got %s", "0x0", *value) + + if nonce == nil { + t.Fatalf("should return a nonce, instead %v", nonce) } + require.Equal(t, test.ExpectedNonce, nonce) } } diff --git a/rpc/mock_test.go b/rpc/mock_test.go index f88da68b..5a4f3b3a 100644 --- a/rpc/mock_test.go +++ b/rpc/mock_test.go @@ -810,9 +810,12 @@ func mock_starknet_getNonce(result interface{}, method string, args ...interface fmt.Printf("args[0] should be *felt.Felt, got %T\n", args[1]) return errWrongArgs } - output := "0x0" + output, err := utils.HexToFelt("0x0") + if err != nil { + return err + } outputContent, _ := json.Marshal(output) - json.Unmarshal(outputContent, &r) + json.Unmarshal(outputContent, r) return nil } diff --git a/rpc/provider.go b/rpc/provider.go index b7737062..fac8b7ca 100644 --- a/rpc/provider.go +++ b/rpc/provider.go @@ -45,7 +45,7 @@ type RpcProvider interface { EstimateMessageFee(ctx context.Context, msg MsgFromL1, blockID BlockID) (*FeeEstimate, error) Events(ctx context.Context, input EventsInput) (*EventChunk, error) GetTransactionStatus(ctx context.Context, transactionHash *felt.Felt) (*TxnStatusResp, error) - Nonce(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*string, error) + Nonce(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*felt.Felt, error) SimulateTransactions(ctx context.Context, blockID BlockID, txns []Transaction, simulationFlags []SimulationFlag) ([]SimulatedTransaction, error) StateUpdate(ctx context.Context, blockID BlockID) (*StateUpdateOutput, error) StorageAt(ctx context.Context, contractAddress *felt.Felt, key string, blockID BlockID) (string, error)