-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
85 lines (74 loc) · 2.3 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math/big"
"os"
"github.com/ethereum/go-ethereum/common"
)
var zeroAddr = common.HexToAddress("0x0000000000000000000000000000000000000000")
var safe1Address = common.HexToAddress("0x1Aa61c196E76805fcBe394eA00e4fFCEd24FC469")
var (
jsonFile = flag.String("json-file", "", "JSON file of addresses to balances in wei")
outputFile = flag.String("output-file", "addr-to-claim.json", "JSON file of addresses to claiming info")
)
func main() {
flag.Parse()
if *jsonFile == "" {
log.Fatal("Expected --json-file to contain a file path")
}
fullPath, err := expandPath(*jsonFile)
if err != nil {
log.Fatalf("Could not expand path: %v", err)
}
log.Printf("Generating claim info for %s\n", fullPath)
jsonBytes, err := ioutil.ReadFile(fullPath)
if err != nil {
log.Fatalf("Could not read file: %v", err)
}
var stringJson map[string]string
if err := json.Unmarshal(jsonBytes, &stringJson); err != nil {
log.Fatalf("Could not unmarshal json: %v", err)
}
balArray, err := ArrayFromAddrBalMap(stringJson)
if err != nil {
log.Fatalf("Could not get array from string map json")
}
_, addrToClaim, err := CreateDistributionTree(balArray)
if err != nil {
log.Fatalf("Could not create distribution tree: %v", err)
}
log.Printf("Root: %s\n", addrToClaim["root"].Proof[0])
if _, err := createFile(*outputFile, addrToClaim); err != nil {
log.Fatalf("Could not create file: %v", err)
}
log.Printf("Created claim info file at %s\n", *outputFile)
}
func unmarshalJSON(jsonMap map[string]string) (map[common.Address]*big.Int, error) {
balMap := make(map[common.Address]*big.Int, len(jsonMap) - 1)
for k, v := range jsonMap {
bigInt, ok := big.NewInt(0).SetString(v, 10)
if !ok {
return nil, fmt.Errorf("could not cast %s to big int", v)
}
balMap[common.HexToAddress(k)] = bigInt
}
return balMap, nil
}
func createFile(filename string, contents interface{}) (*os.File, error) {
file, err := os.Create(filename)
if err != nil {
return nil, fmt.Errorf("could not create file: %v", err)
}
totalBytes, err := json.Marshal(contents)
if err != nil {
return nil, fmt.Errorf("could not marshal data: %v", err)
}
if _, err := file.Write(totalBytes); err != nil {
return nil, fmt.Errorf("could not write data: %v", err)
}
return file, nil
}