-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
200 lines (173 loc) · 5.13 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"context"
"errors"
"fmt"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types/ethtypes"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"google.golang.org/api/sheets/v4"
"os"
"strconv"
"strings"
verifregtypes9 "github.com/filecoin-project/go-state-types/builtin/v9/verifreg"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
logging "github.com/ipfs/go-log/v2"
"google.golang.org/api/option"
)
var log = logging.Logger("main")
func addDataCap(ctx context.Context, api v0api.FullNode, from address.Address, inputAddress string, allowance types.BigInt) (*types.SignedMessage, error) {
var filecoinAddress address.Address
var decodeError error
var err error
if strings.HasPrefix(inputAddress, "0x") {
ethAddress, err := ethtypes.ParseEthAddress(inputAddress)
if err != nil {
return nil, err
}
filecoinAddress, decodeError = ethAddress.ToFilecoinAddress()
} else {
filecoinAddress, decodeError = address.NewFromString(inputAddress)
}
if decodeError != nil {
return nil, decodeError
}
if filecoinAddress == address.Undef {
return nil, errors.New("invalid address")
}
var params []byte
params, err = actors.SerializeParams(
&verifregtypes9.AddVerifiedClientParams{
Address: filecoinAddress,
Allowance: allowance,
})
if err != nil {
return nil, err
}
var smsg *types.SignedMessage
smsg, err = api.MpoolPushMessage(
ctx, &types.Message{
Params: params,
From: from,
To: verifreg.Address,
Method: verifreg.Methods.AddVerifiedClient,
}, nil)
if err != nil {
return nil, err
}
return smsg, err
}
func main() {
logging.SetLogLevel("*", "INFO")
log.Info("Starting fountain")
local := []*cli.Command{
runCmd,
}
app := &cli.App{
Name: "auto-notary",
Usage: "Devnet token distribution utility",
Version: build.UserVersion(),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
EnvVars: []string{"LOTUS_PATH"},
Value: "~/.lotus", // TODO: Consider XDG_DATA_HOME
},
},
Commands: local,
}
if err := app.Run(os.Args); err != nil {
log.Warn(err)
return
}
}
var runCmd = &cli.Command{
Name: "run",
Usage: "Start auto notary",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "from",
},
&cli.StringFlag{
Name: "spreadsheetID",
},
&cli.StringFlag{
Name: "credFile",
},
},
Action: func(cctx *cli.Context) error {
from, err := address.NewFromString(cctx.String("from"))
if err != nil {
return xerrors.Errorf("parsing source address (provide correct --from flag!): %w", err)
}
// Load the credentials file obtained from the Google Cloud Console
// Replace "path/to/credentials.json" with the actual path to your credentials file
credentialsFile := cctx.String("credFile")
ctx := lcli.ReqContext(cctx)
client, err := sheets.NewService(ctx, option.WithCredentialsFile(credentialsFile))
if err != nil {
log.Fatalf("Failed to create Google Sheets client: %v", err)
}
// Specify the spreadsheet ID and range to read from
spreadsheetID := cctx.String("spreadsheetID")
readRange := "Form Responses 1!A2:D"
// Read values from the specified range in the spreadsheet
response, err := client.Spreadsheets.Values.Get(spreadsheetID, readRange).Do()
if err != nil {
log.Fatalf("Failed to read from Google Sheets: %v", err)
}
// Process the response
if len(response.Values) > 0 {
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
for rowIndex, row := range response.Values {
for _, cell := range row {
log.Infof("%s", cell)
}
if len(row) > 3 {
log.Info("skip")
continue
}
fmt.Println()
to := fmt.Sprintf("%s", row[1])
allow := fmt.Sprintf("%s", row[2])
ui64, _ := strconv.ParseUint(allow, 10, 64)
allowance := types.NewInt(ui64 * 1024 * 1024)
log.Infof("from:%v to:%v with %v", from, to, allowance)
var updatedValue string
smsg, err := addDataCap(ctx, api, from, to, allowance)
if err != nil {
updatedValue = err.Error()
} else {
// Modify the desired field on the current line
updatedValue = smsg.Cid().String()
}
log.Infof("addDataCap return %v", updatedValue)
//response.Values[rowIndex][3] = "updatedValue" // Modifying the third field (index 2) on each line
// Write the modified value back to the spreadsheet for the current line
writeRange := fmt.Sprintf("Form Responses 1!d%d", rowIndex+2) // Specify the range to write back for the current line
updateRange := &sheets.ValueRange{
Range: writeRange,
Values: [][]interface{}{{updatedValue}},
}
_, err = client.Spreadsheets.Values.Update(spreadsheetID, writeRange, updateRange).ValueInputOption("RAW").Do()
if err != nil {
log.Fatalf("Failed to update value in Google Sheets: %v", err)
}
}
} else {
fmt.Println("No data found.")
}
return nil
},
}