-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathmain.go
89 lines (76 loc) · 2.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"context"
"fmt"
"math"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/rpc"
"github.com/NethermindEth/starknet.go/utils"
setup "github.com/NethermindEth/starknet.go/examples/internal"
)
var (
someContract string = "0x049D36570D4e46f48e99674bd3fcc84644DdD6b96F7C741B1562B82f9e004dC7" // Sepolia ETH contract address
contractMethod string = "decimals"
contractMethodWithCalldata string = "balance_of"
)
// main entry point of the program.
//
// It initializes the environment and establishes a connection with the client.
// It then makes two contract calls and prints the responses.
//
// Parameters:
//
// none
//
// Returns:
//
// none
func main() {
fmt.Println("Starting simpleCall example")
// Load variables from '.env' file
rpcProviderUrl := setup.GetRpcProviderUrl()
accountAddress := setup.GetAccountAddress()
// Initialize connection to RPC provider
client, err := rpc.NewProvider(rpcProviderUrl)
if err != nil {
panic(fmt.Sprintf("Error dialing the RPC provider: %s", err))
}
fmt.Println("Established connection with the client")
contractAddress, err := utils.HexToFelt(someContract)
if err != nil {
fmt.Println("Failed to transform the token contract address, did you give the hex address?")
panic(err)
}
// Here we are converting the account address to felt
accountAddressInFelt, err := utils.HexToFelt(accountAddress)
if err != nil {
fmt.Println("Failed to transform the account address, did you give the hex address?")
panic(err)
}
// Get token's decimals. Make read contract call without calldata
getDecimalsTx := rpc.FunctionCall{
ContractAddress: contractAddress,
EntryPointSelector: utils.GetSelectorFromNameFelt(contractMethod),
}
decimalsResp, rpcErr := client.Call(context.Background(), getDecimalsTx, rpc.BlockID{Tag: "latest"})
if rpcErr != nil {
panic(rpcErr)
}
decimals, _ := utils.FeltToBigInt(decimalsResp[0]).Float64()
fmt.Printf("Decimals: %v \n", decimals)
// Get balance from specified account address. Make read contract call with calldata
tx := rpc.FunctionCall{
ContractAddress: contractAddress,
EntryPointSelector: utils.GetSelectorFromNameFelt(contractMethodWithCalldata),
Calldata: []*felt.Felt{accountAddressInFelt},
}
balanceResp, rpcErr := client.Call(context.Background(), tx, rpc.BlockID{Tag: "latest"})
if rpcErr != nil {
panic(rpcErr)
}
balance, _ := utils.FeltToBigInt(balanceResp[0]).Float64()
fmt.Printf("Balance: %d \n", int(balance))
// Getting result
balance = balance / (math.Pow(10, decimals))
fmt.Printf("Token balance of %s is %f ETH \n", accountAddress, balance)
}