-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnfd-helpers.go
280 lines (258 loc) · 7.82 KB
/
nfd-helpers.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"context"
"errors"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"
"github.com/algorand/go-algorand-sdk/v2/transaction"
"github.com/algorand/go-algorand-sdk/v2/types"
"github.com/antihax/optional"
"github.com/ssgreg/repeat"
"github.com/TxnLab/batch-asset-send/lib/algo"
"github.com/TxnLab/batch-asset-send/lib/misc"
nfdapi "github.com/TxnLab/batch-asset-send/lib/nfdapi/swagger"
)
// IsContractVersionAtLeast returns true if the specified version string (ie: 1.13a) is at least the specified
// major.minor version. ie: "2.0" and major.minor of 1.3 would be true - because 2.0 > 1.3
func IsContractVersionAtLeast(version string, major, minor int) bool {
majMinReg := regexp.MustCompile(`^(?P<major>\d+)\.(?P<minor>\d+)`)
matches := majMinReg.FindStringSubmatch(version)
if matches == nil || len(matches) != 3 {
return false
}
var contractMajor, contractMinor int
if val := matches[majMinReg.SubexpIndex("major")]; val != "" {
contractMajor, _ = strconv.Atoi(val)
}
if val := matches[majMinReg.SubexpIndex("minor")]; val != "" {
contractMinor, _ = strconv.Atoi(val)
}
if contractMajor > major || (contractMajor >= major && contractMinor >= minor) {
return true
}
return false
}
// IsVaultAutoOptInLockedForSender returns true if the specified 'sender' address
// would be allowed to send to the NFDs vault. This is basically an off-chain
// validation of what the contract itself will do, to save some trouble and skip sending to a vault that won't
// allow it.
// "0" or missing unlocked, "1" explicitly locked, >1 a timestamp specifying unlocked 'until' specified timestamp
func IsVaultAutoOptInLockedForSender(n *nfdapi.NfdRecord, sender string) bool {
lockedVal, ok := n.Properties.Internal["vaultOptInLocked"]
if !ok {
// not found - defaults to UNLOCKED
return false
}
// explicitly unlocked or unlocked until time X (larger number)
intVal, _ := strconv.ParseUint(lockedVal, 10, 64)
if lockedVal == "0" {
return false
} else if intVal > 1 {
if time.Unix(int64(intVal), 0).After(time.Now().UTC()) {
return false
}
}
// Locked explicitly - or implicitly (because expired) - so only owner will return false (unlocked)
return sender != n.Owner
}
func retryNfdApiCalls(meth func() error) error {
return repeat.Repeat(
repeat.Fn(func() error {
err := meth()
if err != nil {
if rate, match := isRateLimited(err); match {
logger.Warn("rate limited", "waiting", rate.SecsRemaining)
time.Sleep(time.Duration(rate.SecsRemaining+1) * time.Second)
return repeat.HintTemporary(err)
}
var swaggerError nfdapi.GenericSwaggerError
if errors.As(err, &swaggerError) {
if moderr, match := swaggerError.Model().(nfdapi.ModelError); match {
return fmt.Errorf("message:%s, err:%w", moderr.Message, err)
}
}
}
return err
}),
repeat.StopOnSuccess(),
)
}
func getAllNfds(config *BatchSendConfig) ([]*nfdapi.NfdRecord, error) {
var (
offset, limit int64 = 0, 200
fetchedNfds nfdapi.NfdV2SearchRecords
err error
nfds []*nfdapi.NfdRecord
)
for ; ; offset += limit {
view := "brief"
if len(config.Destination.VerifiedRequirements) > 0 {
view = "full"
}
err = retryNfdApiCalls(func() error {
searchOpts := &nfdapi.NfdApiNfdSearchV2Opts{
State: optional.NewInterface("owned"),
View: optional.NewString(view),
Limit: optional.NewInt64(limit),
Offset: optional.NewInt64(offset),
}
if config.Destination.OnlyRoots {
searchOpts.Traits = optional.NewInterface("pristine")
}
fetchedNfds, _, err = api.NfdApi.NfdSearchV2(ctx, searchOpts)
return err
})
if err != nil {
return nil, fmt.Errorf("error while fetching nfds: %w", err)
}
if fetchedNfds.Nfds == nil || len(*fetchedNfds.Nfds) == 0 {
break
}
for _, nfd := range *fetchedNfds.Nfds {
if nfd.DepositAccount == "" {
continue
}
newRecord := nfd
nfds = append(nfds, &newRecord)
}
}
return nfds, nil
}
func getSegmentsOfRoot(config *BatchSendConfig) ([]*nfdapi.NfdRecord, error) {
// Fetch root NFD - all we really want is its app id
nfd, _, err := api.NfdApi.NfdGetNFD(ctx, config.Destination.SegmentsOfRoot, nil)
if err != nil {
log.Fatalln(err)
}
misc.Infof(logger, fmt.Sprintf("nfd app id for %s is:%v", nfd.Name, nfd.AppID))
nfds, err := getAllSegments(ctx, config, nfd.AppID)
if err != nil {
log.Fatalln(err)
}
logger.Debug(fmt.Sprintf("fetched segments of root:%s, count:%d", config.Destination.SegmentsOfRoot, len(nfds)))
return nfds, nil
}
func getAllSegments(ctx context.Context, config *BatchSendConfig, parentAppID int64) ([]*nfdapi.NfdRecord, error) {
var (
offset, limit int64 = 0, 200
records nfdapi.NfdV2SearchRecords
err error
nfds []*nfdapi.NfdRecord
)
for ; ; offset += limit {
view := "brief"
if len(config.Destination.VerifiedRequirements) > 0 {
view = "full"
}
err = retryNfdApiCalls(func() error {
records, _, err = api.NfdApi.NfdSearchV2(ctx, &nfdapi.NfdApiNfdSearchV2Opts{
ParentAppID: optional.NewInt64(parentAppID),
State: optional.NewInterface("owned"),
View: optional.NewString(view),
Limit: optional.NewInt64(limit),
Offset: optional.NewInt64(offset),
})
return err
})
if err != nil {
return nil, fmt.Errorf("error while fetching segments: %w", err)
}
if records.Nfds == nil || len(*records.Nfds) == 0 {
break
}
for _, record := range *records.Nfds {
if record.DepositAccount == "" {
continue
}
newRecord := record
nfds = append(nfds, &newRecord)
}
}
return nfds, nil
}
func getAssetSendTxns(
sender string,
sendFromVaultName string,
recipient string,
recipientIsVault bool,
assetID uint64,
amount uint64,
note string,
params types.SuggestedParams,
) (string, []byte, error) {
var (
encodedTxns string
err error
)
if sendFromVaultName == "" && recipientIsVault == false {
// Not sending from vault, nor sending to a vault - so just plain asset transfer
txn, err := transaction.MakeAssetTransferTxn(sender, recipient, amount, []byte(note), params, "", assetID)
if err != nil {
return "", nil, fmt.Errorf("MakeAssetTransferTxn fail: %w", err)
}
txnid, signedBytes, err := signer.SignWithAccount(ctx, txn, sender)
return txnid, signedBytes, err
}
err = retryNfdApiCalls(func() error {
if sendFromVaultName != "" {
receiverType := "account"
if recipientIsVault {
receiverType = "nfdVault"
}
encodedTxns, _, err = api.NfdApi.NfdSendFromVault(
ctx,
nfdapi.SendFromVaultRequestBody{
Amount: int64(amount),
Assets: []int64{int64(assetID)},
Receiver: recipient,
ReceiverType: receiverType,
Sender: sender, // owner address
Note: note,
},
sendFromVaultName,
)
} else {
if recipientIsVault {
encodedTxns, _, err = api.NfdApi.NfdSendToVault(
ctx,
nfdapi.SendToVaultRequestBody{
Amount: int64(amount),
Assets: []int64{int64(assetID)},
Sender: sender, // owner address
Note: note,
},
recipient,
)
} else {
panic("never should have arrived here - pre-checks should have handled")
}
}
return err
})
if err != nil {
return "", nil, fmt.Errorf("error in NfdSendToVault call: %w", err)
}
return algo.DecodeAndSignNFDTransactions(encodedTxns, signer)
}
func isRateLimited(err error) (*nfdapi.RateLimited, bool) {
if swaggerError, match := isSwaggerError(err); match {
if limit, match := swaggerError.Model().(nfdapi.RateLimited); match {
return &limit, true
}
if strings.Contains(string(swaggerError.Body()), "429 Too Many Requests") {
return &nfdapi.RateLimited{0, 0}, true
}
}
return nil, false
}
func isSwaggerError(err error) (*nfdapi.GenericSwaggerError, bool) {
var swaggerError nfdapi.GenericSwaggerError
if errors.As(err, &swaggerError) {
return &swaggerError, true
}
return nil, false
}