-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract_read_erc20.go
62 lines (49 loc) · 1.61 KB
/
contract_read_erc20.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
package main
import (
"fmt"
"log"
"math"
"math/big"
"os"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
token "github.com/marioidival/geth-study/contracts_erc20"
)
func main() {
client, err := ethclient.Dial(os.Getenv("MAINNET_INFURA_URL"))
if err != nil {
log.Fatalf("ethclient connection error: %s", err.Error())
}
// Golem (GNT) address
tokenAddress := common.HexToAddress("0xa74476443119A942dE498590Fe1f2454d7D4aC0d")
instance, err := token.NewToken(tokenAddress, client)
if err != nil {
log.Fatalf("try get new token error: %s", err.Error())
}
address := common.HexToAddress("0x0536806df512d6cdde913cf95c9886f65b1d3462")
bal, err := instance.BalanceOf(&bind.CallOpts{}, address)
if err != nil {
log.Fatalf("try get balance from smart contract error: %s", err.Error())
}
name, err := instance.Name(&bind.CallOpts{})
if err != nil {
log.Fatalf("try get name from smart contract error: %s", err.Error())
}
symbol, err := instance.Symbol(&bind.CallOpts{})
if err != nil {
log.Fatalf("try get symbol from smart contract error: %s", err.Error())
}
decimals, err := instance.Decimals(&bind.CallOpts{})
if err != nil {
log.Fatalf("try get decimals from smart contract error: %s", err.Error())
}
fmt.Println("Name: ", name)
fmt.Println("Symbol: ", symbol)
fmt.Println("decimals: ", decimals)
fmt.Println("WEI (unformatted): ", bal)
fbal := new(big.Float)
fbal.SetString(bal.String())
value := new(big.Float).Quo(fbal, big.NewFloat(math.Pow10(int(decimals))))
fmt.Printf("Balance: %f\n", value)
}