forked from fachebot/merkle-distributor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_balance_map.go
50 lines (40 loc) · 1.1 KB
/
parse_balance_map.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
package distributor
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
)
type Claim struct {
Index int `json:"index"`
Amount string `json:"amount"`
Proof []common.Hash `json:"proof"`
}
type MerkleDistributorInfo struct {
MerkleRoot common.Hash `json:"merkleRoot"`
TokenTotal string `json:"tokenTotal"`
Claims []Claim `json:"claims"`
}
func ParseBalanceMap(balances []Balance) (MerkleDistributorInfo, error) {
info := MerkleDistributorInfo{
Claims: make([]Claim, 0),
}
tree, err := NewBalanceTree(balances)
if err != nil {
return info, err
}
tokenTotal := big.NewInt(0)
for idx, balance := range balances {
proof, err := tree.GetProof(idx, balance.Account, balance.Amount)
if err != nil {
return info, err
}
tokenTotal = big.NewInt(0).Add(tokenTotal, balance.Amount)
info.Claims = append(info.Claims, Claim{
Index: idx,
Amount: "0x" + balance.Amount.Text(16),
Proof: proof,
})
}
info.MerkleRoot = tree.GetRoot()
info.TokenTotal = "0x" + tokenTotal.Text(16)
return info, nil
}