-
Notifications
You must be signed in to change notification settings - Fork 0
/
balances_scanner.go
51 lines (41 loc) · 1011 Bytes
/
balances_scanner.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
package ethutils
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
)
type (
TokenBalances map[common.Address]*big.Int
tokensBalanceResp struct {
Success bool
Data []byte
}
)
const (
BalanceScannerContractAddress = "0xF62107c53a5b18646E823a21ed531ED934B1CE9E"
)
func (p *Provider) TokensBalance(ctx context.Context, owner common.Address, tokenAddresses []common.Address) (TokenBalances, error) {
var (
resp []tokensBalanceResp
tokensBalanceFunc = w3.MustNewFunc("tokensBalance(address,address[])", "(bool success, bytes data)[]")
)
err := p.Client.CallCtx(
ctx,
eth.CallFunc(
w3.A(BalanceScannerContractAddress),
tokensBalanceFunc,
owner,
tokenAddresses,
).Returns(&resp),
)
if err != nil {
return nil, err
}
tokenBalances := make(TokenBalances)
for i, v := range resp {
tokenBalances[tokenAddresses[i]] = new(big.Int).SetBytes(v.Data)
}
return tokenBalances, nil
}