1
1
package examples
2
2
3
3
import (
4
+ "fmt"
5
+
4
6
"github.com/availproject/avail-go-sdk/metadata"
5
7
baPallet "github.com/availproject/avail-go-sdk/metadata/pallets/balances"
6
8
syPallet "github.com/availproject/avail-go-sdk/metadata/pallets/system"
@@ -30,9 +32,8 @@ func RunBatch() {
30
32
// The other was it to create a transaction using the sdk api and then use the `call` field member
31
33
{
32
34
destCharlie , err := metadata .NewAccountIdFromAddress ("5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y" )
33
- if err != nil {
34
- panic (err )
35
- }
35
+ PanicOnError (err )
36
+
36
37
tx := sdk .Tx .Balances .TransferKeepAlive (destCharlie .ToMultiAddress (), SDK .OneAvail ())
37
38
callsToExecute = append (callsToExecute , tx .Payload .Call )
38
39
}
@@ -47,27 +48,25 @@ func RunBatch() {
47
48
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
48
49
PanicOnError (err )
49
50
50
- if isSuc , err := res .IsSuccessful (); err != nil {
51
- panic (err )
52
- } else if ! isSuc {
53
- panic ("The transaction has failed" )
54
- }
51
+ isOk , err := res .IsSuccessful ()
52
+ PanicOnError (err )
53
+ AssertEq (isOk , true , "Transaction is supposed to succeed" )
55
54
56
55
events := res .Events .Unwrap ()
57
56
58
57
if SDK .EventFindFirst (events , utPallet.EventBatchCompleted {}).IsSome () {
59
- println ("Batch was successfully completed" )
58
+ fmt . Println ("Batch was successfully completed" )
60
59
} else {
61
60
panic ("Batch call failed" )
62
61
}
63
62
64
63
if len (SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 2 {
65
- println ("All batch items completed" )
64
+ fmt . Println ("All batch items completed" )
66
65
} else {
67
66
panic ("No all items were completed" )
68
67
}
69
68
70
- println ("Batch call done" )
69
+ fmt . Println ("Batch call done" )
71
70
}
72
71
73
72
// Batch All call
@@ -76,27 +75,25 @@ func RunBatch() {
76
75
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
77
76
PanicOnError (err )
78
77
79
- if isSuc , err := res .IsSuccessful (); err != nil {
80
- panic (err )
81
- } else if ! isSuc {
82
- panic ("The transaction has failed" )
83
- }
78
+ isOk , err := res .IsSuccessful ()
79
+ PanicOnError (err )
80
+ AssertEq (isOk , true , "Transaction is supposed to succeed" )
84
81
85
82
events := res .Events .Unwrap ()
86
83
87
84
if SDK .EventFindFirst (events , utPallet.EventBatchCompleted {}).IsSome () {
88
- println ("Batch was successfully completed" )
85
+ fmt . Println ("Batch was successfully completed" )
89
86
} else {
90
87
panic ("Batch All call failed" )
91
88
}
92
89
93
90
if len (SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 2 {
94
- println ("All batch items completed" )
91
+ fmt . Println ("All batch items completed" )
95
92
} else {
96
93
panic ("No all items were completed" )
97
94
}
98
95
99
- println ("Batch All call done" )
96
+ fmt . Println ("Batch All call done" )
100
97
}
101
98
102
99
// Force Batch call
@@ -105,27 +102,25 @@ func RunBatch() {
105
102
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
106
103
PanicOnError (err )
107
104
108
- if isSuc , err := res .IsSuccessful (); err != nil {
109
- panic (err )
110
- } else if ! isSuc {
111
- panic ("The transaction has failed" )
112
- }
105
+ isOk , err := res .IsSuccessful ()
106
+ PanicOnError (err )
107
+ AssertEq (isOk , true , "Transaction is supposed to succeed" )
113
108
114
109
events := res .Events .Unwrap ()
115
110
116
111
if SDK .EventFindFirst (events , utPallet.EventBatchCompleted {}).IsSome () {
117
- println ("Batch was successfully completed" )
112
+ fmt . Println ("Batch was successfully completed" )
118
113
} else {
119
114
panic ("Batch All call failed" )
120
115
}
121
116
122
117
if len (SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 2 {
123
- println ("All batch items completed" )
118
+ fmt . Println ("All batch items completed" )
124
119
} else {
125
120
panic ("No all items were completed" )
126
121
}
127
122
128
- println ("Force Batch call done" )
123
+ fmt . Println ("Force Batch call done" )
129
124
}
130
125
131
126
//
@@ -156,29 +151,27 @@ func RunBatch() {
156
151
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
157
152
PanicOnError (err )
158
153
159
- if isSuc , err := res .IsSuccessful (); err != nil {
160
- panic (err )
161
- } else if ! isSuc {
162
- panic ("The transaction has failed" )
163
- }
154
+ isOk , err := res .IsSuccessful ()
155
+ PanicOnError (err )
156
+ AssertEq (isOk , true , "Transaction is supposed to succeed" )
164
157
165
158
events := res .Events .Unwrap ()
166
159
167
160
if event := SDK .EventFindFirst (events , utPallet.EventBatchInterrupted {}); event .IsSome () {
168
161
ev := event .Unwrap ()
169
- println ("Batch was interrupted. Reason: " , ev .Error .ToHuman ())
170
- println ("Tx Index that caused failure: " , ev .Index )
162
+ fmt . Println ("Batch was interrupted. Reason: " , ev .Error .ToHuman ())
163
+ fmt . Println ("Tx Index that caused failure: " , ev .Index )
171
164
} else {
172
165
panic ("Failed to find EventBatchInterrupted event." )
173
166
}
174
167
175
168
if len (SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 2 {
176
- println ("Some batch items completed" )
169
+ fmt . Println ("Some batch items completed" )
177
170
} else {
178
171
panic ("Cannot be more than 2" )
179
172
}
180
173
181
- println ("Batch call done" )
174
+ fmt . Println ("Batch call done" )
182
175
}
183
176
184
177
// Batch All call
@@ -187,21 +180,19 @@ func RunBatch() {
187
180
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
188
181
PanicOnError (err )
189
182
190
- if isSuc , err := res .IsSuccessful (); err != nil {
191
- panic (err )
192
- } else if isSuc {
193
- panic ("The transaction is supposed to fail" )
194
- }
183
+ isOk , err := res .IsSuccessful ()
184
+ PanicOnError (err )
185
+ AssertEq (isOk , false , "Transaction is supposed to fail" )
195
186
196
187
events := res .Events .Unwrap ()
197
188
198
189
if event := SDK .EventFindFirst (events , syPallet.EventExtrinsicFailed {}); event .IsSome () {
199
- println ("Batch was interrupted. Reason: " , event .Unwrap ().DispatchError .ToHuman ())
190
+ fmt . Println ("Batch was interrupted. Reason: " , event .Unwrap ().DispatchError .ToHuman ())
200
191
} else {
201
192
panic ("Failed to find EventExtrinsicFailed event." )
202
193
}
203
194
204
- println ("Batch All call done" )
195
+ fmt . Println ("Batch All call done" )
205
196
}
206
197
207
198
// Force Batch call
@@ -210,34 +201,32 @@ func RunBatch() {
210
201
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
211
202
PanicOnError (err )
212
203
213
- if isSuc , err := res .IsSuccessful (); err != nil {
214
- panic (err )
215
- } else if ! isSuc {
216
- panic ("We either failed to decode events or the transaction has failed" )
217
- }
204
+ isOk , err := res .IsSuccessful ()
205
+ PanicOnError (err )
206
+ AssertEq (isOk , true , "Transaction is supposed to succeed" )
218
207
219
208
events := res .Events .Unwrap ()
220
209
221
210
if SDK .EventFindFirst (events , utPallet.EventBatchCompletedWithErrors {}).IsSome () {
222
- println ("Batch completed with errors" )
211
+ fmt . Println ("Batch completed with errors" )
223
212
} else {
224
213
panic ("Failed to find EventBatchCompletedWithErrors" )
225
214
}
226
215
227
216
if len (SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 3 {
228
- println ("3 of out 4 items completed" )
217
+ fmt . Println ("3 of out 4 items completed" )
229
218
} else {
230
219
panic ("3 items must be completed" )
231
220
}
232
221
233
222
if event := SDK .EventFindFirst (events , utPallet.EventItemFailed {}); event .IsSome () {
234
- println ("Item failed. Reason: " , event .Unwrap ().Error .ToHuman ())
223
+ fmt . Println ("Item failed. Reason: " , event .Unwrap ().Error .ToHuman ())
235
224
} else {
236
225
panic ("Failed to find EventItemFailed" )
237
226
}
238
227
239
- println ("Force Batch call done" )
228
+ fmt . Println ("Force Batch call done" )
240
229
}
241
230
242
- println ("RunBatch finished correctly." )
231
+ fmt . Println ("RunBatch finished correctly." )
243
232
}
0 commit comments