-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ethereum.go
100 lines (92 loc) · 2.15 KB
/
ethereum.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
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"io/ioutil"
"math/big"
"strconv"
)
var (
eth *ethclient.Client
ropsten *ethclient.Client
rinkeby *ethclient.Client
)
func LoadEthBlockchains() error {
var err error
eth, err = ethclient.Dial(ETHapi)
if err != nil {
return err
}
ropsten, err = ethclient.Dial(ROPSTENapi)
if err != nil {
return err
}
rinkeby, err = ethclient.Dial(RINKEBYapi)
if err != nil {
return err
}
return err
}
func EthBalance(api *ethclient.Client, address string) string {
balance, _ := api.BalanceAt(context.TODO(), common.HexToAddress(address), nil)
amount := BigIntDecimal(balance, 18)
amountFloat, _ := strconv.ParseFloat(amount, 10)
return fmt.Sprintf("%0.3f", amountFloat)
}
func BigIntDecimal(balance *big.Int, decimals int64) string {
if balance.Sign() == 0 {
return "0"
}
bal := big.NewFloat(0)
bal.SetInt(balance)
pow := BigPow(10, decimals)
p := big.NewFloat(0)
p.SetInt(pow)
bal.Quo(bal, p)
deci := strconv.Itoa(int(decimals))
dec := "%." + deci + "f"
newNum := Clean(fmt.Sprintf(dec, bal))
return newNum
}
func Clean(newNum string) string {
stringBytes := bytes.TrimRight([]byte(newNum), "0")
newNum = string(stringBytes)
if stringBytes[len(stringBytes)-1] == 46 {
newNum += "0"
}
if stringBytes[0] == 46 {
newNum = "0" + newNum
}
return newNum
}
func BigPow(a, b int64) *big.Int {
r := big.NewInt(a)
return r.Exp(r, big.NewInt(b), nil)
}
func TokenBalance(token, address string) (string, string, error) {
var url string
url = fmt.Sprintf("https://api.tokenbalance.com/token/%v/%v", token, address)
resp, err := httpGet(url, "GET", []byte(""))
if err != nil {
return "0", "ERR", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "0", "ERR", err
}
var tokenBalance TokenBalanceResponse
err = json.Unmarshal(body, &tokenBalance)
if err != nil {
return "0", "ERR", err
}
amount, err := strconv.ParseFloat(tokenBalance.Balance, 10)
if err != nil {
return "0", "ERR", err
}
return fmt.Sprintf("%0.3f", amount), tokenBalance.Symbol, err
}