Skip to content

Commit

Permalink
simplify fmtCallData usage
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Dec 18, 2023
1 parent d7f7e00 commit b5e12ea
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
8 changes: 5 additions & 3 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Account struct {
ChainId *felt.Felt
AccountAddress *felt.Felt
publicKey string
CairoVersion int
ks Keystore
}

Expand All @@ -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())
Expand Down Expand Up @@ -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:
Expand Down
30 changes: 15 additions & 15 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions examples/deployAccount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand All @@ -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)
}
Expand Down
20 changes: 9 additions & 11 deletions examples/simpleInvoke/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down

0 comments on commit b5e12ea

Please sign in to comment.