Skip to content

Commit

Permalink
Add an example that fetches token balance of account (#509)
Browse files Browse the repository at this point in the history
first commit to add example that fetches token balance of an account
  • Loading branch information
ChiHaoLu authored Jan 24, 2024
1 parent 915109a commit dccc412
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/getTokenBalance/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# use this variable to change the RPC base URL
#INTEGRATION_BASE=http_insert_end_point
#ACCOUNT_ADDR=account_contract_address
9 changes: 9 additions & 0 deletions examples/getTokenBalance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Note: To run this example, you need a mainnet endpoint.

Steps to run this example on mainnet:
1. rename ".env.template" to ".env.mainnet"
2. uncomment, and set INTEGRATION_BASE to the mainnet url
3. uncomment, and set ACCOUNT_ADDR to the account contract address
4. make sure you are in the "getTokenBalance" directory
5. execute `go mod tidy`
6. execute `go run main.go`
35 changes: 35 additions & 0 deletions examples/getTokenBalance/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module account

go 1.21

require (
github.com/NethermindEth/juno v0.3.1
github.com/NethermindEth/starknet.go v0.2.1-0.20220620163912-1db2ca279608
github.com/ethereum/go-ethereum v1.10.26
github.com/joho/godotenv v1.4.0
)

replace github.com/NethermindEth/starknet.go => ../../

require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/bits-and-blooms/bitset v1.7.0 // indirect
github.com/consensys/gnark-crypto v0.11.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/test-go/testify v1.1.4 // indirect
github.com/tklauser/go-sysconf v0.3.5 // indirect
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.2.0 // indirect
golang.org/x/sys v0.3.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 6 additions & 0 deletions examples/getTokenBalance/go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.21

use (
.
../..
)
91 changes: 91 additions & 0 deletions examples/getTokenBalance/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"context"
"fmt"
"os"

"math/big"

"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/joho/godotenv"
)

var (
name string = "mainnet"
ethMainnetContract string = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"
// usdcMainnetContract string="0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8"
// daiMainnetContract string="0x00da114221cb83fa859dbdb4c44beeaa0bb37c7537ad5ae66fe5e0efd20e6eb3"
getBalanceMethod string = "balanceOf"
getDecimalsMethod string = "decimals"
)

// main entry point of the program.
//
// It initializes the environment and establishes a connection with the client.
// It then calls a token contract to get the account balance and decimals.
// In the end, it transfomers the response to readable number.
//
// Parameters:
//
// none
//
// Returns:
//
// none
func main() {
fmt.Println("Starting getTokenBalance example")
godotenv.Load(fmt.Sprintf(".env.%s", name))
base := os.Getenv("INTEGRATION_BASE")
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.mainnet?")
panic(err)
}
clientv02 := rpc.NewProvider(c)
fmt.Println("Established connection with the client")

tokenAddressInFelt, err := utils.HexToFelt(ethMainnetContract)
if err != nil {
fmt.Println("Failed to transform the token contract address, did you give the hex address?")
panic(err)
}

accountAddress := os.Getenv("ACCOUNT_ADDR")
accountAddressInFelt, err := utils.HexToFelt(accountAddress)
if err != nil {
fmt.Println("Failed to transform the account address, did you give the hex address?")
panic(err)
}

// Make read contract call
tx := rpc.FunctionCall{
ContractAddress: tokenAddressInFelt,
EntryPointSelector: utils.GetSelectorFromNameFelt(getBalanceMethod),
Calldata: []*felt.Felt{accountAddressInFelt},
}

fmt.Println("Making balanceOf() request")
callResp, err := clientv02.Call(context.Background(), tx, rpc.BlockID{Tag: "latest"})
if err != nil {
panic(err.Error())
}

// Get token's decimals
getDecimalsTx := rpc.FunctionCall{
ContractAddress: tokenAddressInFelt,
EntryPointSelector: utils.GetSelectorFromNameFelt(getDecimalsMethod),
}
getDecimalsResp, err := clientv02.Call(context.Background(), getDecimalsTx, rpc.BlockID{Tag: "latest"})
if err != nil {
panic(err)
}

floatValue := new(big.Float).SetInt(utils.FeltToBigInt(callResp[0]))
floatValue.Quo(floatValue, new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), utils.FeltToBigInt(getDecimalsResp[0]), nil)))

fmt.Printf("Token balance of %s is %f", accountAddress, floatValue)
}

0 comments on commit dccc412

Please sign in to comment.