diff --git a/account/account.go b/account/account.go index 17272cf5..56c7c623 100644 --- a/account/account.go +++ b/account/account.go @@ -48,6 +48,7 @@ type Account struct { ChainId *felt.Felt AccountAddress *felt.Felt publicKey string + CairoVersion int ks Keystore } @@ -61,12 +62,13 @@ type Account struct { // It returns: // - *Account: a pointer to newly created Account // - error: an error if any -func NewAccount(provider rpc.RpcProvider, accountAddress *felt.Felt, publicKey string, keystore Keystore) (*Account, error) { +func NewAccount(provider rpc.RpcProvider, accountAddress *felt.Felt, publicKey string, keystore Keystore, cairoVersion int) (*Account, error) { account := &Account{ provider: provider, AccountAddress: accountAddress, publicKey: publicKey, ks: keystore, + CairoVersion: cairoVersion, } chainID, err := provider.ChainID(context.Background()) @@ -892,8 +894,8 @@ func (account *Account) GetTransactionStatus(ctx context.Context, Txnhash *felt. // Returns: // - a slice of *felt.Felt representing the formatted calldata. // - an error if Cairo version is not supported. -func (account *Account) FmtCalldata(fnCalls []rpc.FunctionCall, cairoVersion int) ([]*felt.Felt, error) { - switch cairoVersion { +func (account *Account) FmtCalldata(fnCalls []rpc.FunctionCall) ([]*felt.Felt, error) { + switch account.CairoVersion { case 0: return FmtCallDataCairo0(fnCalls), nil case 2: diff --git a/account/account_test.go b/account/account_test.go index 691e74df..69ade7e8 100644 --- a/account/account_test.go +++ b/account/account_test.go @@ -166,7 +166,7 @@ func TestTransactionHashInvoke(t *testing.T) { } mockRpcProvider.EXPECT().ChainID(context.Background()).Return(test.ChainID, nil) - account, err := account.NewAccount(mockRpcProvider, test.AccountAddress, test.PubKey, ks) + account, err := account.NewAccount(mockRpcProvider, test.AccountAddress, test.PubKey, ks, 0) require.NoError(t, err, "error returned from account.NewAccount()") invokeTxn := rpc.InvokeTxnV1{ Calldata: test.FnCall.Calldata, @@ -280,10 +280,10 @@ func TestFmtCallData(t *testing.T) { for _, test := range testSet { mockRpcProvider.EXPECT().ChainID(context.Background()).Return(test.ChainID, nil) - acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "pubkey", account.NewMemKeystore()) + acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "pubkey", account.NewMemKeystore(), test.CairoVersion) require.NoError(t, err) - fmtCallData, err := acnt.FmtCalldata([]rpc.FunctionCall{test.FnCall}, test.CairoVersion) + fmtCallData, err := acnt.FmtCalldata([]rpc.FunctionCall{test.FnCall}) require.NoError(t, err) require.Equal(t, fmtCallData, test.ExpectedCallData) } @@ -330,7 +330,7 @@ func TestChainIdMOCK(t *testing.T) { for _, test := range testSet { mockRpcProvider.EXPECT().ChainID(context.Background()).Return(test.ChainID, nil) - account, err := account.NewAccount(mockRpcProvider, &felt.Zero, "pubkey", account.NewMemKeystore()) + account, err := account.NewAccount(mockRpcProvider, &felt.Zero, "pubkey", account.NewMemKeystore(), 0) require.NoError(t, err) require.Equal(t, account.ChainId.String(), test.ExpectedID) } @@ -372,7 +372,7 @@ func TestChainId(t *testing.T) { require.NoError(t, err, "Error in rpc.NewClient") provider := rpc.NewProvider(client) - account, err := account.NewAccount(provider, &felt.Zero, "pubkey", account.NewMemKeystore()) + account, err := account.NewAccount(provider, &felt.Zero, "pubkey", account.NewMemKeystore(), 0) require.NoError(t, err) require.Equal(t, account.ChainId.String(), test.ExpectedID) } @@ -434,7 +434,7 @@ func TestSignMOCK(t *testing.T) { ks.Put(test.Address.String(), privKeyBI) mockRpcProvider.EXPECT().ChainID(context.Background()).Return(test.ChainId, nil) - account, err := account.NewAccount(mockRpcProvider, test.Address, test.Address.String(), ks) + account, err := account.NewAccount(mockRpcProvider, test.Address, test.Address.String(), ks, 0) require.NoError(t, err, "error returned from account.NewAccount()") msg := utils.TestHexToFelt(t, "0x73cf79c4bfa0c7a41f473c07e1be5ac25faa7c2fdf9edcbd12c1438f40f13d8") @@ -604,10 +604,10 @@ func TestAddInvoke(t *testing.T) { ks.Put(test.PubKey.String(), fakePrivKeyBI) } - acnt, err := account.NewAccount(provider, test.AccountAddress, test.PubKey.String(), ks) + acnt, err := account.NewAccount(provider, test.AccountAddress, test.PubKey.String(), ks, 0) require.NoError(t, err) - test.InvokeTx.Calldata, err = acnt.FmtCalldata([]rpc.FunctionCall{test.FnCall}, test.CairoContractVersion) + test.InvokeTx.Calldata, err = acnt.FmtCalldata([]rpc.FunctionCall{test.FnCall}) require.NoError(t, err) err = acnt.SignInvokeTransaction(context.Background(), &test.InvokeTx) @@ -659,7 +659,7 @@ func TestAddDeployAccountDevnet(t *testing.T) { require.True(t, ok) ks.Put(fakeUser.PublicKey, fakePrivKeyBI) - acnt, err := account.NewAccount(provider, fakeUserAddr, fakeUser.PublicKey, ks) + acnt, err := account.NewAccount(provider, fakeUserAddr, fakeUser.PublicKey, ks, 0) require.NoError(t, err) classHash := utils.TestHexToFelt(t, "0x7b3e05f48f0c69e4a65ce5e076a66271a527aff2c34ce1083ec6e1526997a69") // preDeployed classhash @@ -712,7 +712,7 @@ func TestTransactionHashDeclare(t *testing.T) { mockRpcProvider := mocks.NewMockRpcProvider(mockCtrl) mockRpcProvider.EXPECT().ChainID(context.Background()).Return("SN_GOERLI", nil) - acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "", account.NewMemKeystore()) + acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "", account.NewMemKeystore(), 0) require.NoError(t, err) type testSetType struct { @@ -782,7 +782,7 @@ func TestTransactionHashInvokeV3(t *testing.T) { mockRpcProvider := mocks.NewMockRpcProvider(mockCtrl) mockRpcProvider.EXPECT().ChainID(context.Background()).Return("SN_GOERLI", nil) - acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "", account.NewMemKeystore()) + acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "", account.NewMemKeystore(), 0) require.NoError(t, err) type testSetType struct { @@ -853,7 +853,7 @@ func TestTransactionHashdeployAccount(t *testing.T) { mockRpcProvider := mocks.NewMockRpcProvider(mockCtrl) mockRpcProvider.EXPECT().ChainID(context.Background()).Return("SN_GOERLI", nil) - acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "", account.NewMemKeystore()) + acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "", account.NewMemKeystore(), 0) require.NoError(t, err) type testSetType struct { @@ -944,7 +944,7 @@ func TestWaitForTransactionReceiptMOCK(t *testing.T) { mockRpcProvider := mocks.NewMockRpcProvider(mockCtrl) mockRpcProvider.EXPECT().ChainID(context.Background()).Return("SN_GOERLI", nil) - acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "", account.NewMemKeystore()) + acnt, err := account.NewAccount(mockRpcProvider, &felt.Zero, "", account.NewMemKeystore(), 0) require.NoError(t, err, "error returned from account.NewAccount()") type testSetType struct { @@ -1029,7 +1029,7 @@ func TestWaitForTransactionReceipt(t *testing.T) { require.NoError(t, err, "Error in rpc.NewClient") provider := rpc.NewProvider(client) - acnt, err := account.NewAccount(provider, &felt.Zero, "pubkey", account.NewMemKeystore()) + acnt, err := account.NewAccount(provider, &felt.Zero, "pubkey", account.NewMemKeystore(), 0) require.NoError(t, err, "error returned from account.NewAccount()") type testSetType struct { @@ -1095,7 +1095,7 @@ func TestAddDeclareTxn(t *testing.T) { require.NoError(t, err, "Error in rpc.NewClient") provider := rpc.NewProvider(client) - acnt, err := account.NewAccount(provider, AccountAddress, PubKey.String(), ks) + acnt, err := account.NewAccount(provider, AccountAddress, PubKey.String(), ks, 0) require.NoError(t, err) // Class Hash diff --git a/examples/deployAccount/main.go b/examples/deployAccount/main.go index 499e30c8..127ffbab 100644 --- a/examples/deployAccount/main.go +++ b/examples/deployAccount/main.go @@ -14,9 +14,10 @@ import ( ) var ( - network string = "testnet" - predeployedClassHash = "0x2794ce20e5f2ff0d40e632cb53845b9f4e526ebd8471983f7dbd355b721d5a" - accountAddress = "0xdeadbeef" + network string = "testnet" + predeployedClassHash = "0x2794ce20e5f2ff0d40e632cb53845b9f4e526ebd8471983f7dbd355b721d5a" + accountAddress = "0xdeadbeef" + accountContractVersion = 0 //Replace with the cairo version of your account contract ) // main initializes the client, sets up the account, deploys a contract, and sends a transaction to the network. @@ -27,9 +28,12 @@ var ( // and finally sends the transaction to the network. // // Parameters: -// none +// +// none +// // Returns: -// none +// +// none func main() { // Initialise the client. godotenv.Load(fmt.Sprintf(".env.%s", network)) @@ -49,7 +53,7 @@ func main() { } // Set up account - acnt, err := account.NewAccount(clientv02, accountAddressFelt, pub.String(), ks) + acnt, err := account.NewAccount(clientv02, accountAddressFelt, pub.String(), ks, accountContractVersion) if err != nil { panic(err) } diff --git a/examples/simpleInvoke/main.go b/examples/simpleInvoke/main.go index 3e446e27..d74b0b72 100644 --- a/examples/simpleInvoke/main.go +++ b/examples/simpleInvoke/main.go @@ -15,12 +15,13 @@ import ( // NOTE : Please add in your keys only for testing purposes, in case of a leak you would potentially lose your funds. var ( - name string = "testnet" //env."name" - account_addr string = "0x06f36e8a0fc06518125bbb1c63553e8a7d8597d437f9d56d891b8c7d3c977716" //Replace it with your account address - privateKey string = "0x0687bf84896ee63f52d69e6de1b41492abeadc0dc3cb7bd351d0a52116915937" //Replace it with your account private key - public_key string = "0x58b0824ee8480133cad03533c8930eda6888b3c5170db2f6e4f51b519141963" //Replace it with your account public key - someContract string = "0x4c1337d55351eac9a0b74f3b8f0d3928e2bb781e5084686a892e66d49d510d" //Replace it with the contract that you want to invoke - contractMethod string = "increase_value" //Replace it with the function name that you want to invoke + name string = "testnet" //env."name" + account_addr string = "0x06f36e8a0fc06518125bbb1c63553e8a7d8597d437f9d56d891b8c7d3c977716" //Replace it with your account address + account_cairo_version = 0 //Replace with the cairo version of your account + privateKey string = "0x0687bf84896ee63f52d69e6de1b41492abeadc0dc3cb7bd351d0a52116915937" //Replace it with your account private key + public_key string = "0x58b0824ee8480133cad03533c8930eda6888b3c5170db2f6e4f51b519141963" //Replace it with your account public key + someContract string = "0x4c1337d55351eac9a0b74f3b8f0d3928e2bb781e5084686a892e66d49d510d" //Replace it with the contract that you want to invoke + contractMethod string = "increase_value" //Replace it with the function name that you want to invoke ) func main() { @@ -61,7 +62,7 @@ func main() { } // Initializing the account - accnt, err := account.NewAccount(clientv02, account_address, public_key, ks) + accnt, err := account.NewAccount(clientv02, account_address, public_key, ks, account_cairo_version) if err != nil { panic(err.Error()) } @@ -93,11 +94,8 @@ func main() { EntryPointSelector: utils.GetSelectorFromNameFelt(contractMethod), //this is the function that we want to call } - // Mentioning the contract version - CairoContractVersion := 2 - // Building the Calldata with the help of FmtCalldata where we pass in the FnCall struct along with the Cairo version - InvokeTx.Calldata, err = accnt.FmtCalldata([]rpc.FunctionCall{FnCall}, CairoContractVersion) + InvokeTx.Calldata, err = accnt.FmtCalldata([]rpc.FunctionCall{FnCall}) if err != nil { panic(err.Error()) }