-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
180 lines (155 loc) · 5.24 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"embed"
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/bjartek/overflow"
"github.com/pkg/errors"
"github.com/psiemens/sconfig"
"github.com/samber/lo"
"github.com/spf13/cobra"
"go.mitsakis.org/workerpool"
)
//This file is boilerplate to set up a CLI application in go using cobra/sconfig and embed to embed files into the binary
// we embedd the flow.json and the transactions we need
//
//go:embed transactions/award_manually_many.cdc
//go:embed transactions/adminAddKeys.cdc
//go:embed flow.json
var path embed.FS
// For config we use psiemens brilliant sconfig repo, IMHO a lot easier to use then plain viper/pflags
type Config struct {
File string `default:"recipients.csv" flag:"file,f" info:"Path to file of recipients, one address per line"`
BatchSize int `default:"100" flag:"batchSize,b" info:"How many floats to award in a single batch"`
Workers int `default:"100" flag:"workers,w" info:"Workers to paralell mint as"`
Host string `flag:"host" info:"Host to mint for if not main"`
Private_Key string `flag:"private_key,k" info:"REQUIRED: privateKey to sign as, recommend setting as env var FLOATILLA_PRIVATE_KEY"`
Address string `flag:"address,a" info:"REQUIRED: Address to mint as, recomend setting as env var FLOATILLA_ADDRESS"`
}
var conf Config
// we set up our one and only command
var cmd = &cobra.Command{
Use: "floatilla <eventId>",
Short: "Send a floatilla of floats with the given `eventId` to the recipient in `file`",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("You need to send in floatEventID as a valid number")
}
eventID, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return fmt.Errorf("You need to send in floatEventID as a valid number")
}
if conf.Private_Key == "" || conf.Address == "" {
return fmt.Errorf("You need to set FLOATILLA_ADDRESS|FLOATILLA_PRIVATE_KEY or call this binary with -k -a flags")
}
addresses, err := readAddresses(conf.File)
if err != nil {
return err
}
//These needs to be env because of flow json
if os.Getenv("FLOATILLA_PRIVATE_KEY") == "" {
os.Setenv("FLOATILLA_PRIVATE_KEY", conf.Private_Key)
}
if os.Getenv("FLOATILLA_ADDRESS") == "" {
os.Setenv("FLOATILLA_ADDRESS", conf.Address)
}
o := overflow.Overflow(
overflow.WithNetwork("mainnet"),
overflow.WithBasePath(""),
overflow.WithLogNone(),
overflow.WithReturnErrors(),
overflow.WithEmbedFS(path),
)
account, err := o.GetAccount("admin")
//I have no idea why this has 0x prefix but we cannot have it here
publicKey := strings.TrimPrefix(account.Keys[0].PublicKey.String(), "0x")
if conf.Host == "" {
host := o.Address("admin")
conf.Host = host
}
//should we configure this with a key somehow?
if len(account.Keys) == 1 {
fmt.Println("You do not have minter keys so we add them for you")
result := o.Tx("adminAddKeys",
overflow.WithSigner("admin"),
overflow.WithArg("number", 100),
overflow.WithArg("key", publicKey),
)
if result.Err != nil {
return result.Err
}
fmt.Printf("Minter keys added in transactions %s\n", result.Id)
}
batches := lo.Chunk(addresses, conf.BatchSize)
p, err := workerpool.NewPoolSimple(conf.Workers, func(job workerpool.Job[[]string], workerID int) error {
addresses := job.Payload
fmt.Printf("worker=%03d awarding %03d floats\n", workerID, len(addresses))
result := o.Tx("award_manually_many",
overflow.WithSigner(fmt.Sprintf("floatilla%d", workerID+1)),
overflow.WithArg("forHost", conf.Host),
overflow.WithArg("eventId", eventID),
overflow.WithArg("recipients", addresses),
)
if result.Err != nil {
fmt.Printf("worker=%03d failed with error %v\n", workerID, result.Err)
} else {
ids := result.GetIdsFromEvent("Transferred", "id")
fmt.Printf("worker=%03d awared %03d floats computation:%03d \n", workerID, len(ids), result.FeeGas)
}
return nil
})
if err != nil {
return err
}
for _, batch := range batches {
p.Submit(batch)
}
p.StopAndWait()
return nil
},
}
func init() {
//We do not care about timestamps in logs so we just disable that
log.SetFlags(0)
//set up sconfig to read using the FLOATILLA prefix
err := sconfig.New(&conf).
FromEnvironment("FLOATILLA").
BindFlags(cmd.PersistentFlags()).
Parse()
help := cmd.Flags().Changed("help")
if err != nil && !help {
fmt.Println("Required fields are not set")
cmd.Help()
log.Fatal(err)
}
}
// The main method simply executes the command and exits either successfully or not, note that we print errors to stderr not stdout
func main() {
if err := cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func readAddresses(file string) ([]string, error) {
f, err := os.Open(conf.File)
if err != nil {
return nil, errors.Wrapf(err, "could not open file with name %s", conf.File)
}
// remember to close the file at the end of the program
defer f.Close()
// read csv values using csv.Reader
csvReader := csv.NewReader(f)
data, err := csvReader.ReadAll()
if err != nil {
return nil, err
}
addresses := []string{}
for _, line := range data {
addresses = append(addresses, strings.TrimSpace(strings.TrimSuffix(line[0], ".find")))
}
return addresses, nil
}