diff --git a/README.md b/README.md index b44273ae..ac6935cc 100644 --- a/README.md +++ b/README.md @@ -57,10 +57,7 @@ operations on the wallets. The package has excellent documentation for a smooth - library documentation available at [pkg.go.dev](https://pkg.go.dev/github.com/NethermindEth/starknet.go). - [simple call example](./examples/simpleCall) to make a contract call to a mainnet contract - [deploy account example](./examples/deployAccount) to deploy a new account contract on testnet. - +- [invoke transaction example](./examples/simpleInvoke) to add a new invoke transaction on testnet. ### Run Examples @@ -83,6 +80,16 @@ go run main.go > Check [here](examples/deployAccount/README.md) for more details +***starknet invokeTransaction*** + +```sh +cd examples/simpleInvoke +go mod tidy +go run main.go +``` + +> Check [here](examples/simpleInvoke/README.md) for more details + ### RPC diff --git a/examples/simpleInvoke/.env.template b/examples/simpleInvoke/.env.template new file mode 100644 index 00000000..a055aebc --- /dev/null +++ b/examples/simpleInvoke/.env.template @@ -0,0 +1,2 @@ +# use this variable to change the RPC base URL +# INTEGRATION_BASE=http_insert_end_point diff --git a/examples/simpleInvoke/README.md b/examples/simpleInvoke/README.md index b715fb2e..f41092e6 100644 --- a/examples/simpleInvoke/README.md +++ b/examples/simpleInvoke/README.md @@ -1,6 +1,6 @@ Note: To run this example, you need a testnet endpoint. -Steps to run this example on mainnet: +Steps to run this example on testnet: 1. rename ".env.template" to ".env.testnet" 2. uncomment, and set INTEGRATION_BASE to the testnet url //You can get it from here www.alchemy.com/starknet diff --git a/examples/simpleInvoke/main.go b/examples/simpleInvoke/main.go index b6f577b3..3e446e27 100644 --- a/examples/simpleInvoke/main.go +++ b/examples/simpleInvoke/main.go @@ -13,7 +13,7 @@ import ( "github.com/joho/godotenv" ) -// NOTE : Please add in your keys only for testing purposes, incase of a leak you would potentially lose your funds. +// 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 @@ -24,27 +24,27 @@ var ( ) func main() { - //Loading the env + // Loading the env godotenv.Load(fmt.Sprintf(".env.%s", name)) - base := os.Getenv("INTEGRATION_BASE") //please modify the .env.testnet and replace the INTEGRATION_BASE with an starknet goerli RPC. + base := os.Getenv("INTEGRATION_BASE") //please modify the .env.testnet and replace the INTEGRATION_BASE with a starknet goerli RPC. fmt.Println("Starting simpleInvoke example") - //Initialising the connection + // Initialising the connection c, err := ethrpc.DialContext(context.Background(), base) if err != nil { fmt.Println("Failed to connect to the client, did you specify the url in the .env.testnet?") panic(err) } - //Initialising the provider + // Initialising the provider clientv02 := rpc.NewProvider(c) - //Here we are converting the account address to felt + // Here we are converting the account address to felt account_address, err := utils.HexToFelt(account_addr) if err != nil { panic(err.Error()) } - //Initializing the account memkeyStore + // Initializing the account memkeyStore ks := account.NewMemKeystore() fakePrivKeyBI, ok := new(big.Int).SetString(privateKey, 0) if !ok { @@ -54,25 +54,25 @@ func main() { fmt.Println("Established connection with the client") - //Here we are setting the maxFee + // Here we are setting the maxFee maxfee, err := utils.HexToFelt("0x9184e72a000") if err != nil { panic(err.Error()) } - //Initializing the account + // Initializing the account accnt, err := account.NewAccount(clientv02, account_address, public_key, ks) if err != nil { panic(err.Error()) } - //Getting the nonce from the account + // Getting the nonce from the account nonce, err := accnt.Nonce(context.Background(), rpc.BlockID{Tag: "latest"}, accnt.AccountAddress) if err != nil { panic(err.Error()) } - //Building the InvokeTx struct + // Building the InvokeTx struct InvokeTx := rpc.InvokeTxnV1{ MaxFee: maxfee, Version: rpc.TransactionV1, @@ -81,39 +81,39 @@ func main() { SenderAddress: accnt.AccountAddress, } - //Converting the contractaddress from hex to felt + // Converting the contractAddress from hex to felt contractAddress, err := utils.HexToFelt(someContract) if err != nil { panic(err.Error()) } - //Building the functioncall struct, where : + // Building the functionCall struct, where : FnCall := rpc.FunctionCall{ - ContractAddress: contractAddress, //contractAddress is the contract that we wanna call - EntryPointSelector: utils.GetSelectorFromNameFelt(contractMethod), //this is the function that we wanna call + ContractAddress: contractAddress, //contractAddress is the contract that we want to call + EntryPointSelector: utils.GetSelectorFromNameFelt(contractMethod), //this is the function that we want to call } - //Mentioning the contract version + // 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 + // 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) if err != nil { panic(err.Error()) } - //Signing of the the transaction that is done by the account + // Signing of the transaction that is done by the account err = accnt.SignInvokeTransaction(context.Background(), &InvokeTx) if err != nil { panic(err.Error()) } - //After the signing we finally call the AddInvokeTransaction in order to invoke the contract function + // After the signing we finally call the AddInvokeTransaction in order to invoke the contract function resp, err := accnt.AddInvokeTransaction(context.Background(), InvokeTx) if err != nil { panic(err.Error()) } - //This returns us with the transaction hash - fmt.Println("Response : ", resp.TransactionHash) + // This returns us with the transaction hash + fmt.Println("Transaction hash response : ", resp.TransactionHash) }