-
Notifications
You must be signed in to change notification settings - Fork 2
/
transaction_sender.go
290 lines (244 loc) · 7.83 KB
/
transaction_sender.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
281
282
283
284
285
286
287
288
289
290
package archethic
import (
"encoding/hex"
"encoding/json"
"fmt"
"sync"
"time"
)
const senderContext = "SENDER"
type TransactionSender struct {
client *APIClient
onSent []func()
onConfirmation []func(nbConfirmations, maxConfirmations uint)
onFullConfirmation []func(maxConfirmations uint)
onRequiredConfirmation []func(nbConfirmations uint)
onError []func(senderContext string, error ErrorDetails)
onTimeout []func(nbConfirmationReceived uint)
nbConfirmationReceived uint
timeout *Timeout
transactionErrorSubscription *AbsintheSubscription
transactionConfirmedSubscription *AbsintheSubscription
}
func NewTransactionSender(client *APIClient) *TransactionSender {
return &TransactionSender{
client,
[]func(){},
[]func(nbConfirmations, maxConfirmations uint){},
[]func(maxConfirmations uint){},
[]func(nbConfirmations uint){},
[]func(senderContext string, error ErrorDetails){},
[]func(nbConfirmationReceived uint){},
0,
nil,
nil,
nil,
}
}
func (ts *TransactionSender) AddOnSent(handler func()) {
ts.onSent = append(ts.onSent, handler)
}
func (ts *TransactionSender) AddOnConfirmation(handler func(nbConfirmations, maxConfirmations uint)) {
ts.onConfirmation = append(ts.onConfirmation, handler)
}
func (ts *TransactionSender) AddOnFullConfirmation(handler func(maxConfirmations uint)) {
ts.onFullConfirmation = append(ts.onFullConfirmation, handler)
}
func (ts *TransactionSender) AddOnRequiredConfirmation(handler func(nbConfirmations uint)) {
ts.onRequiredConfirmation = append(ts.onRequiredConfirmation, handler)
}
func (ts *TransactionSender) AddOnError(handler func(senderContext string, error ErrorDetails)) {
ts.onError = append(ts.onError, handler)
}
func (ts *TransactionSender) AddOnTimeout(handler func(nbConfirmationReceived uint)) {
ts.onTimeout = append(ts.onTimeout, handler)
}
func (ts *TransactionSender) Unsubscribe(event string) error {
if event != "" {
switch event {
case "sent":
ts.onSent = []func(){}
case "confirmation":
ts.onConfirmation = []func(nbConfirmations, maxConfirmations uint){}
case "requiredConfirmation":
ts.onRequiredConfirmation = []func(nbConfirmations uint){}
case "fullConfirmation":
ts.onFullConfirmation = []func(maxConfirmations uint){}
case "error":
ts.onError = []func(senderContext string, error ErrorDetails){}
case "timeout":
ts.onTimeout = []func(nbConfirmationReceived uint){}
default:
return fmt.Errorf("event %s is not supported", event)
}
} else {
ts.transactionErrorSubscription.CancelSubscription()
ts.transactionConfirmedSubscription.CancelSubscription()
ts.onSent = []func(){}
ts.onConfirmation = []func(nbConfirmations, maxConfirmations uint){}
ts.onRequiredConfirmation = []func(nbConfirmations uint){}
ts.onFullConfirmation = []func(maxConfirmations uint){}
ts.onError = []func(senderContext string, error ErrorDetails){}
ts.onTimeout = []func(nbConfirmationReceived uint){}
}
return nil
}
func (ts *TransactionSender) SendTransaction(tx *TransactionBuilder, confirmationThreshold, timeout uint) error {
done := make(chan bool)
var wg sync.WaitGroup
wg.Add(2)
transactionAddress := hex.EncodeToString(tx.Address)
go ts.SubscribeTransactionConfirmed(transactionAddress, func() {
wg.Done()
}, func(transactionConfirmed TransactionConfirmedGQL) {
if ts.handleConfirmation(confirmationThreshold, transactionConfirmed.NbConfirmations, transactionConfirmed.MaxConfirmations) {
done <- true
}
})
go ts.SubscribeTransactionError(transactionAddress, func() {
wg.Done()
}, func(transactionError TransactionErrorGQL) {
ts.handleError(string(transactionError.Context), transactionError.Error)
done <- true
})
wg.Wait()
_, err := ts.client.SendTransaction(tx)
if err != nil {
ts.transactionErrorSubscription.CancelSubscription()
ts.transactionConfirmedSubscription.CancelSubscription()
for _, f := range ts.onError {
f(senderContext, ErrorDetails{Message: err.Error(), Code: -1})
}
return nil
} else {
go ts.handleSend(timeout, func(isFinished bool) {
if isFinished {
done <- true
}
})
}
<-done
return nil
}
func (ts *TransactionSender) SubscribeTransactionError(transactionAddress string, readyHandler func(), handler func(TransactionErrorGQL)) {
query := `subscription ($address: Address!){
transactionError(address: $address) {
context
error {
code,
data,
message
}
}
}`
variables := make(map[string]string)
variables["address"] = transactionAddress
ts.transactionErrorSubscription = new(AbsintheSubscription)
ts.transactionErrorSubscription.GraphqlSubscription(ts.client.wsUrl, query, variables, readyHandler, func(data map[string]interface{}) error {
var response struct {
TransactionError TransactionErrorGQL
}
jsonStr, err := json.Marshal(data)
if err != nil {
return err
}
if err := json.Unmarshal(jsonStr, &response); err != nil {
return err
}
handler(response.TransactionError)
return nil
})
}
func (ts *TransactionSender) SubscribeTransactionConfirmed(transactionAddress string, readyHandler func(), handler func(TransactionConfirmedGQL)) {
query := `subscription ($address: Address!){
transactionConfirmed(address: $address) {
nbConfirmations
maxConfirmations
}
}`
variables := make(map[string]string)
variables["address"] = transactionAddress
ts.transactionConfirmedSubscription = new(AbsintheSubscription)
ts.transactionConfirmedSubscription.GraphqlSubscription(ts.client.wsUrl, query, variables, readyHandler, func(data map[string]interface{}) error {
var response struct {
TransactionConfirmed TransactionConfirmedGQL
}
jsonStr, err := json.Marshal(data)
if err != nil {
return err
}
if err := json.Unmarshal(jsonStr, &response); err != nil {
return err
}
handler(response.TransactionConfirmed)
return nil
})
}
func (ts *TransactionSender) handleConfirmation(confirmationThreshold, nbConfirmations, maxConfirmations uint) bool {
ts.nbConfirmationReceived = nbConfirmations
if nbConfirmations == 1 {
ts.transactionErrorSubscription.CancelSubscription()
}
for _, f := range ts.onConfirmation {
f(nbConfirmations, maxConfirmations)
}
if maxConfirmations*(confirmationThreshold/100) <= nbConfirmations && len(ts.onRequiredConfirmation) > 0 {
for _, f := range ts.onRequiredConfirmation {
f(nbConfirmations)
}
ts.onRequiredConfirmation = []func(nbConfirmations uint){}
ts.timeout.Clear()
return true
}
if nbConfirmations == maxConfirmations {
ts.timeout.Clear()
ts.transactionConfirmedSubscription.CancelSubscription()
for _, f := range ts.onFullConfirmation {
f(maxConfirmations)
}
return true
}
return false
}
func (ts *TransactionSender) handleError(context string, error ErrorDetails) {
ts.timeout.Clear()
ts.transactionErrorSubscription.CancelSubscription()
ts.transactionConfirmedSubscription.CancelSubscription()
for _, f := range ts.onError {
f(context, error)
}
}
func (ts *TransactionSender) handleSend(timeout uint, isFinishedHandler func(bool)) error {
for _, f := range ts.onSent {
f()
}
ts.timeout = SetTimeout(func() {
ts.transactionErrorSubscription.CancelSubscription()
ts.transactionConfirmedSubscription.CancelSubscription()
for _, f := range ts.onTimeout {
f(ts.nbConfirmationReceived)
}
isFinishedHandler(true)
}, time.Duration(timeout*1000000000))
return nil
}
type Timeout struct {
callback func()
duration time.Duration
clearChan chan bool
}
func SetTimeout(callback func(), duration time.Duration) *Timeout {
t := &Timeout{callback: callback, duration: duration, clearChan: make(chan bool)}
go func() {
select {
case <-time.After(duration):
callback()
case <-t.clearChan:
return
}
}()
return t
}
func (t *Timeout) Clear() {
t.clearChan <- true
}