forked from red/wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth-batch.red
288 lines (263 loc) · 7.05 KB
/
eth-batch.red
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
281
282
283
284
285
286
287
288
Red [
Title: "Batch payment utils"
Author: "Xie Qingtian"
File: %eth-batch.red
Tabs: 4
License: {
Distributed under the Boost Software License, Version 1.0.
See https://github.com/red/red/blob/master/BSL-License.txt
}
]
eth-batch: context [
list-font: make font! [name: get 'font-fixed size: 11]
sanitized?: yes
payment-stop?: no
batch-results: make block! 4
payment-list: none
batch-result-btn: none
batch-send-btn: none
payment-name: none
payment-addr: none
payment-amount: none
add-payment-btn: none
signed-data: none
total-balance: none
sanitize-payments: func [data [block! none!] /local entry c][
if block? data [
foreach entry data [
if find "√×" last entry [
clear skip tail entry -3
]
]
new-line/all next data yes
]
sanitized?: yes
data
]
check-payment: func [
data [block!]
return: [word!]
/local sum entry addr amount
][
unless wallet/valid-amount? batch-gas-price/text [
batch-gas-price/text: copy "Invaild gas price"
return 'invalid-price
]
sum: to-i256 0
foreach entry data [
entry: trim/head skip entry 20
addr: copy/part entry 42
unless wallet/valid-address? addr [
payment-addr/text: copy "Invalid address"
return 'invalid-address
]
amount: trim copy skip entry 42
either wallet/valid-amount? amount [
sum: add256 sum eth/eth-to-wei amount
if lesser-or-equal256? total-balance sum [
wallet/alert "Insufficient Balance"
return 'Insufficient-balance
]
][
payment-amount/text: copy "Invaild amount"
return 'invalid-amount
]
]
return 'ok
]
do-add-payment: func [face event /local entry res][
unless sanitized? [sanitize-payments payment-list/data]
entry: rejoin [
pad copy/part trim/head payment-name/text 18 20
trim payment-addr/text " "
trim payment-amount/text
]
data: copy payment-list/data
either add-payment-btn/text = "Add" [
append data entry
][
poke data payment-list/selected entry
]
if 'ok = check-payment data [
payment-list/data: data
unview
]
]
do-import-payments: function [face event][
if f: request-file [
payment-list/data: sanitize-payments load f
]
]
do-export-payments: func [face event][
if f: request-file/save [
save f sanitize-payments payment-list/data
]
]
do-check-result: function [face event][
foreach result batch-results [
either string? result [
browse rejoin [wallet/explorer result]
][ ;-- error
wallet/alert result
]
]
]
do-batch-payment: func [
face [object!]
event [event!]
/local from-addr nonce entry addr to-addr amount result idx
][
either batch-send-btn/text = "Stop" [
floating-text/text: copy "Cancel the transaction on your key"
floating-dlg/visible?: yes
center-face/with floating-dlg batch-send-dialog
view/no-wait floating-dlg
floating-text/rate: 0:0:2
payment-stop?: yes
exit
][
unless sanitized? [sanitize-payments payment-list/data]
if 'ok <> check-payment payment-list/data [exit]
]
clear batch-results
payment-stop?: no
batch-result-btn/visible?: no
from-addr: batch-addr-from/text
nonce: eth/get-nonce wallet/network from-addr
if nonce = -1 [
view/flags nonce-error-dlg 'modal
exit
]
;-- Edge case: ledger key may locked in this moment
unless string? keys/get-address 0 [
view/flags unlock-dev-dlg 'modal
exit
]
batch-send-btn/text: "Stop"
idx: 1
floating-text/text: copy "Check the transaction on your key"
floating-dlg/visible?: yes
center-face/with floating-dlg batch-send-dialog
view/no-wait floating-dlg
floating-text/rate: 0:0:2
foreach entry payment-list/data [
payment-list/selected: idx
wallet/process-events
addr: next find entry " 0x"
to-addr: copy/part addr 42
amount: trim copy skip addr 42
signed-data: wallet/sign-transaction
from-addr
to-addr
batch-gas-price/text
batch-gas-limit/text
amount
nonce
if payment-stop? [break]
append pad entry 80 either all [
signed-data
binary? signed-data
][
result: eth/send-raw-tx wallet/network signed-data
append batch-results result
either string? result [nonce: nonce + 1 "√"]["×"]
][
if signed-data = 'token-error [
view/flags wallet/contract-data-dlg 'modal
break
]
"×"
]
sanitized?: no
idx: idx + 1
]
unless empty? batch-results [batch-result-btn/visible?: yes]
batch-send-btn/text: "Send"
]
batch-send-dialog: layout [
title "Batch Payment"
style lbl: text 350 middle font [name: font-fixed size: 11]
style btn: button 68
text 55 "Account:" batch-addr-from: lbl
text 60 "Gas Limit:" batch-gas-limit: field 60
text 60 "Gas Price:" batch-gas-price: field 48 "21" return
payment-list: text-list font list-font data [] 685x400 below
btn "Add" [
add-payment-dialog/text: "Add payment"
add-payment-btn/text: "Add"
view/flags add-payment-dialog 'modal
]
btn "Edit" [
unless empty? payment-list/data [
unless sanitized? [sanitize-payments payment-list/data]
add-payment-dialog/text: "Edit payment"
entry: pick payment-list/data payment-list/selected
payment-name/text: copy/part entry find entry #" "
payment-addr/text: copy/part addr: next find entry " 0x" 42
payment-amount/text: trim copy skip addr 42
add-payment-btn/text: "OK"
view/flags add-payment-dialog 'modal
]
]
btn "Remove" [
dlg: copy [
below center
text font-size 11 "Are you sure to remove it?"
text "none"
panel [
pad 0x0
button "Yes" [
remove at payment-list/data payment-list/selected
unview
]
button "Cancel" [unview]
]
]
either payment-list/selected > 0 [
dlg/8: pick payment-list/data payment-list/selected
view/flags dlg 'modal
][
dlg/6: "Please select an item to remove"
clear skip dlg 6
append dlg [button "OK" [unview]]
view/flags dlg 'modal
]
]
btn "Import" :do-import-payments
btn "Export" :do-export-payments
pad 0x20
batch-send-btn: btn "Send" :do-batch-payment
batch-result-btn: btn "Results" :do-check-result
do [batch-result-btn/visible?: no]
]
add-payment-dialog: layout [
style field: field 360 font [name: font-fixed size: 10]
group-box [
text "Name:" payment-name: field return
text "Address:" payment-addr: field return
text "Amount:" payment-amount: field
] return
pad 160x0 add-payment-btn: button "Add" :do-add-payment
pad 20x0 button "Cancel" [unview]
]
floating-dlg: layout/flags [
floating-text: text font-size 14 "Cancel the transaction on your key"
rate 0:0:2 on-time [face/rate: none floating-dlg/visible?: no]
] 'no-title
actors-init: does [
batch-send-dialog/actors: make object! [
on-close: func [face event][
sanitize-payments payment-list/data
batch-result-btn/visible?: no
]
]
]
open: func [addr [string!] balance [vector!] /local price-wei][
batch-addr-from/text: copy addr
total-balance: balance
clear payment-list/data
batch-gas-limit/text: either wallet/token-contract ["79510"]["21000"]
if batch-send-dialog/state [unview batch-send-dialog]
view batch-send-dialog
]
]