Skip to content

Commit

Permalink
updating examples
Browse files Browse the repository at this point in the history
  • Loading branch information
cicr99 committed Oct 31, 2023
1 parent 474b966 commit 94ccd46
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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

Expand Down
2 changes: 2 additions & 0 deletions examples/simpleInvoke/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# use this variable to change the RPC base URL
# INTEGRATION_BASE=http_insert_end_point
2 changes: 1 addition & 1 deletion examples/simpleInvoke/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
40 changes: 20 additions & 20 deletions examples/simpleInvoke/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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)

}

0 comments on commit 94ccd46

Please sign in to comment.