Skip to content

Commit

Permalink
Update examples (#456)
Browse files Browse the repository at this point in the history
* updating examples

* add new example in Getting Started section
  • Loading branch information
cicr99 committed Nov 1, 2023
1 parent 474b966 commit 517614a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<!-- Currently not working
- [curve example](./examples/curve) initializing the StarkCurve for signing and verification
- [contract example](./examples/contract) for smart contract deployment and function call
- [account example](./examples/contract) for Account initialization and invocation call -->
- [invoke transaction example](./examples/simpleInvoke) to add a new invoke transaction on testnet.

### Run Examples

Expand All @@ -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

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
42 changes: 21 additions & 21 deletions examples/simpleInvoke/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 517614a

Please sign in to comment.