@@ -3,11 +3,10 @@ package examples
3
3
import (
4
4
"fmt"
5
5
6
- "github.com/availproject/avail-go-sdk/metadata"
7
6
"github.com/availproject/avail-go-sdk/metadata/pallets"
8
7
baPallet "github.com/availproject/avail-go-sdk/metadata/pallets/balances"
9
- syPallet "github.com/availproject/avail-go-sdk/metadata/pallets/system"
10
8
utPallet "github.com/availproject/avail-go-sdk/metadata/pallets/utility"
9
+ "github.com/availproject/avail-go-sdk/primitives"
11
10
prim "github.com/availproject/avail-go-sdk/primitives"
12
11
SDK "github.com/availproject/avail-go-sdk/sdk"
13
12
)
@@ -23,7 +22,7 @@ func RunBatch() {
23
22
24
23
// One way to create a suitable call for the batch transaction is to manually create the desired call and then convert it to a generic call
25
24
{
26
- destBob , err := metadata .NewAccountIdFromAddress ("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty" )
25
+ destBob , err := primitives .NewAccountIdFromAddress ("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty" )
27
26
PanicOnError (err )
28
27
29
28
call := baPallet.CallTransferKeepAlive {Dest : destBob .ToMultiAddress (), Value : SDK .OneAvail ()}
@@ -32,7 +31,7 @@ func RunBatch() {
32
31
33
32
// The other was it to create a transaction using the sdk api and then use the `call` field member
34
33
{
35
- destCharlie , err := metadata .NewAccountIdFromAddress ("5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y" )
34
+ destCharlie , err := primitives .NewAccountIdFromAddress ("5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y" )
36
35
PanicOnError (err )
37
36
38
37
tx := sdk .Tx .Balances .TransferKeepAlive (destCharlie .ToMultiAddress (), SDK .OneAvail ())
@@ -48,21 +47,15 @@ func RunBatch() {
48
47
tx := sdk .Tx .Utility .Batch (callsToExecute )
49
48
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
50
49
PanicOnError (err )
51
- AssertTrue (res .IsSuccessful ().Unwrap (), "Transaction is supposed to succeed" )
50
+ AssertTrue (res .IsSuccessful ().UnsafeUnwrap (), "Transaction is supposed to succeed" )
52
51
53
- events := res .Events .Unwrap ()
52
+ events := res .Events .UnsafeUnwrap ()
54
53
55
- if SDK .EventFindFirst (events , utPallet.EventBatchCompleted {}).IsSome () {
56
- fmt .Println ("Batch was successfully completed" )
57
- } else {
58
- panic ("Batch call failed" )
59
- }
54
+ event := SDK .EventFindFirst (events , utPallet.EventBatchCompleted {})
55
+ AssertTrue (event .IsSome (), "BatchCompleted event must be present." )
60
56
61
- if len (SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 2 {
62
- fmt .Println ("All batch items completed" )
63
- } else {
64
- panic ("No all items were completed" )
65
- }
57
+ event_count := len (SDK .EventFind (events , utPallet.EventItemCompleted {}))
58
+ AssertEq (event_count , 2 , "ItemCompleted events must be produced twice" )
66
59
67
60
fmt .Println ("Batch call done" )
68
61
}
@@ -72,21 +65,15 @@ func RunBatch() {
72
65
tx := sdk .Tx .Utility .BatchAll (callsToExecute )
73
66
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
74
67
PanicOnError (err )
75
- AssertTrue (res .IsSuccessful ().Unwrap (), "Transaction is supposed to succeed" )
68
+ AssertTrue (res .IsSuccessful ().UnsafeUnwrap (), "Transaction is supposed to succeed" )
76
69
77
- events := res .Events .Unwrap ()
70
+ events := res .Events .UnsafeUnwrap ()
78
71
79
- if SDK .EventFindFirst (events , utPallet.EventBatchCompleted {}).IsSome () {
80
- fmt .Println ("Batch was successfully completed" )
81
- } else {
82
- panic ("Batch All call failed" )
83
- }
72
+ event := SDK .EventFindFirst (events , utPallet.EventBatchCompleted {})
73
+ AssertTrue (event .IsSome (), "BatchCompleted event must be present." )
84
74
85
- if len (SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 2 {
86
- fmt .Println ("All batch items completed" )
87
- } else {
88
- panic ("No all items were completed" )
89
- }
75
+ event_count := len (SDK .EventFind (events , utPallet.EventItemCompleted {}))
76
+ AssertEq (event_count , 2 , "ItemCompleted events must be produced twice" )
90
77
91
78
fmt .Println ("Batch All call done" )
92
79
}
@@ -96,21 +83,15 @@ func RunBatch() {
96
83
tx := sdk .Tx .Utility .ForceBatch (callsToExecute )
97
84
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
98
85
PanicOnError (err )
99
- AssertTrue (res .IsSuccessful ().Unwrap (), "Transaction is supposed to succeed" )
86
+ AssertTrue (res .IsSuccessful ().UnsafeUnwrap (), "Transaction is supposed to succeed" )
100
87
101
- events := res .Events .Unwrap ()
88
+ events := res .Events .UnsafeUnwrap ()
102
89
103
- if SDK .EventFindFirst (events , utPallet.EventBatchCompleted {}).IsSome () {
104
- fmt .Println ("Batch was successfully completed" )
105
- } else {
106
- panic ("Batch All call failed" )
107
- }
90
+ event := SDK .EventFindFirst (events , utPallet.EventBatchCompleted {})
91
+ AssertTrue (event .IsSome (), "BatchCompleted event must be present." )
108
92
109
- if len (SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 2 {
110
- fmt .Println ("All batch items completed" )
111
- } else {
112
- panic ("No all items were completed" )
113
- }
93
+ event_count := len (SDK .EventFind (events , utPallet.EventItemCompleted {}))
94
+ AssertEq (event_count , 2 , "ItemCompleted events must be produced twice" )
114
95
115
96
fmt .Println ("Force Batch call done" )
116
97
}
@@ -121,7 +102,7 @@ func RunBatch() {
121
102
122
103
// The 3. is poisoned with a too high transfer amount
123
104
{
124
- destEve , err := metadata .NewAccountIdFromAddress ("5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw" )
105
+ destEve , err := primitives .NewAccountIdFromAddress ("5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw" )
125
106
PanicOnError (err )
126
107
127
108
tx := sdk .Tx .Balances .TransferKeepAlive (destEve .ToMultiAddress (), SDK .OneAvail ().Mul64 (uint64 (1_000_000_000 )))
@@ -130,7 +111,7 @@ func RunBatch() {
130
111
131
112
// The 4. call is a normal one
132
113
{
133
- destDave , err := metadata .NewAccountIdFromAddress ("5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy" )
114
+ destDave , err := primitives .NewAccountIdFromAddress ("5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy" )
134
115
PanicOnError (err )
135
116
136
117
tx := sdk .Tx .Balances .TransferKeepAlive (destDave .ToMultiAddress (), SDK .OneAvail ())
@@ -142,23 +123,18 @@ func RunBatch() {
142
123
tx := sdk .Tx .Utility .Batch (callsToExecute )
143
124
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
144
125
PanicOnError (err )
145
- AssertTrue (res .IsSuccessful ().Unwrap (), "Transaction is supposed to succeed" )
126
+ AssertTrue (res .IsSuccessful ().UnsafeUnwrap (), "Transaction is supposed to succeed" )
146
127
147
- events := res .Events .Unwrap ()
128
+ events := res .Events .UnsafeUnwrap ()
148
129
149
- if event := SDK .EventFindFirst (events , utPallet.EventBatchInterrupted {}); event .IsSome () {
150
- ev := event .Unwrap ()
151
- fmt .Println ("Batch was interrupted. Reason: " , ev .Error .ToHuman ())
152
- fmt .Println ("Tx Index that caused failure: " , ev .Index )
153
- } else {
154
- panic ("Failed to find EventBatchInterrupted event." )
155
- }
130
+ event := SDK .EventFindFirst (events , utPallet.EventBatchInterrupted {})
131
+ AssertTrue (event .IsSome (), "BatchInterrupted event must be present." )
156
132
157
- if len ( SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 2 {
158
- fmt . Println ( "Some batch items completed " )
159
- } else {
160
- panic ( "Cannot be more than 2" )
161
- }
133
+ event2 := SDK .EventFindFirst (events , utPallet.EventBatchCompleted {})
134
+ AssertTrue ( event2 . IsNone (), "BatchCompleted event must NOT be present. " )
135
+
136
+ event_count := len ( SDK . EventFind ( events , utPallet. EventItemCompleted {}) )
137
+ AssertEq ( event_count , 2 , "ItemCompleted events must be produced twice" )
162
138
163
139
fmt .Println ("Batch call done" )
164
140
}
@@ -168,18 +144,7 @@ func RunBatch() {
168
144
tx := sdk .Tx .Utility .BatchAll (callsToExecute )
169
145
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
170
146
PanicOnError (err )
171
-
172
- isOk := res .IsSuccessful ()
173
- AssertTrue (isOk .IsSome (), "It should be possible to decode transaction events." )
174
- AssertEq (isOk .Unwrap (), false , "Transaction is supposed to fail" )
175
-
176
- events := res .Events .Unwrap ()
177
-
178
- if event := SDK .EventFindFirst (events , syPallet.EventExtrinsicFailed {}); event .IsSome () {
179
- fmt .Println ("Batch was interrupted. Reason: " , event .Unwrap ().DispatchError .ToHuman ())
180
- } else {
181
- panic ("Failed to find EventExtrinsicFailed event." )
182
- }
147
+ AssertEq (res .IsSuccessful (), prim .NewSome (false ), "Transaction is supposed to fail" )
183
148
184
149
fmt .Println ("Batch All call done" )
185
150
}
@@ -189,27 +154,18 @@ func RunBatch() {
189
154
tx := sdk .Tx .Utility .ForceBatch (callsToExecute )
190
155
res , err := tx .ExecuteAndWatchInclusion (acc , SDK .NewTransactionOptions ().WithAppId (0 ))
191
156
PanicOnError (err )
192
- AssertTrue (res .IsSuccessful ().Unwrap (), "Transaction is supposed to succeed" )
193
-
194
- events := res .Events .Unwrap ()
195
-
196
- if SDK .EventFindFirst (events , utPallet.EventBatchCompletedWithErrors {}).IsSome () {
197
- fmt .Println ("Batch completed with errors" )
198
- } else {
199
- panic ("Failed to find EventBatchCompletedWithErrors" )
200
- }
201
-
202
- if len (SDK .EventFindAll (events , utPallet.EventItemCompleted {})) == 3 {
203
- fmt .Println ("3 of out 4 items completed" )
204
- } else {
205
- panic ("3 items must be completed" )
206
- }
207
-
208
- if event := SDK .EventFindFirst (events , utPallet.EventItemFailed {}); event .IsSome () {
209
- fmt .Println ("Item failed. Reason: " , event .Unwrap ().Error .ToHuman ())
210
- } else {
211
- panic ("Failed to find EventItemFailed" )
212
- }
157
+ AssertTrue (res .IsSuccessful ().UnsafeUnwrap (), "Transaction is supposed to succeed" )
158
+
159
+ events := res .Events .UnsafeUnwrap ()
160
+
161
+ event := SDK .EventFindFirst (events , utPallet.EventBatchCompletedWithErrors {})
162
+ AssertTrue (event .IsSome (), "BatchCompletedWithErrors event must be present." )
163
+
164
+ event_count := len (SDK .EventFind (events , utPallet.EventItemCompleted {}))
165
+ AssertEq (event_count , 3 , "ItemCompleted events must be produced thrice" )
166
+
167
+ event_count2 := len (SDK .EventFind (events , utPallet.EventItemFailed {}))
168
+ AssertEq (event_count2 , 1 , "ItemFailed events must be produced once" )
213
169
214
170
fmt .Println ("Force Batch call done" )
215
171
}
0 commit comments