Skip to content

Commit 72666c3

Browse files
Add more transaction examples (#19)
* Added more transaction examples * Expanded Tranasction Execute example * Added 4 more transaction execution examples * Batch example was made mode concise * Added examples to documentation * Added tests for balance and perbil display
1 parent bbf6bc0 commit 72666c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+700
-300
lines changed

README.md

-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# avail-go-sdk
22

3-
TODO
4-
53
# Note
64
This repo uses `mdbook` to generate documentation and `just` to run all the shell commands.
75
You can install both tools by running and executing `./install_dependencies.sh`. It should work as long as you have cargo installed.
@@ -12,7 +10,6 @@ This project uses [GitHub Flow](https://www.alexhyett.com/git-flow-github-flow/)
1210
# Documentation
1311
[Link](https://availproject.github.io/avail-go-sdk/) to documentation (web preview of examples)
1412

15-
1613
# Logging
1714
To enable logging add this to the `main` function.
1815

documentation/src/SUMMARY.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@
1717
- [Data Submission Filtered by Tx Index](block_data_submission_by_index.md)
1818
- [Data Submission Filtered by Signer](block_data_submission_by_signer.md)
1919
- [Block Events](block_events.md)
20+
- [Transaction](./transaction.md)
21+
- [Execute](./transaction_execute.md)
22+
- [Execute And Watch](./transaction_execute_and_watch.md)
23+
- [Execute And Watch Inclusion](./transaction_execute_and_watch_inclusion.md)
24+
- [Execute And Watch Finalization](./transaction_execute_and_watch_finalization.md)
25+
- [Options](./transaction_options.md)
26+
- [Payment](./transaction_payment.md)
27+
- [Custom](./transaction_custom.md)
2028
- [Data Submission](./data_submission.md)
2129
- [Storage](./storage.md)
2230
- [Batch](./batch.md)
2331
- [RPC](./rpc.md)
24-
- [Transaction Options](./transaction_options.md)
25-
- [Transaction Payment](./transaction_payment.md)
26-
- [Custom Transaction](./custom_transaction.md)
27-
-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
11
# Custom Transaction
2-
3-
```go
4-
{{#include ./../../examples/custom_transaction.go}}
5-
```

documentation/src/installation.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ func main() {
6161
acc := SDK.Account.Ferdie()
6262

6363
tx := sdk.Tx.DataAvailability.SubmitData([]byte("MyData"))
64-
println("Submitting new Transaction... Can take up to 20 seconds")
64+
fmt.Println("Submitting new Transaction... Can take up to 20 seconds")
6565
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(1))
6666
if err != nil {
6767
panic(err)
6868
}
6969

7070
// Transaction Details
71-
println(fmt.Sprintf(`Block Hash: %v, Block Index: %v, Tx Hash: %v, Tx Index: %v`, res.BlockHash.ToHexWith0x(), res.BlockNumber, res.TxHash.ToHexWith0x(), res.TxIndex))
71+
fmt.Println(fmt.Sprintf(`Block Hash: %v, Block Index: %v, Tx Hash: %v, Tx Index: %v`, res.BlockHash.ToHexWith0x(), res.BlockNumber, res.TxHash.ToHexWith0x(), res.TxIndex))
7272
}
7373
```
7474

documentation/src/transaction.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Transaction
2+
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Transaction Custom
2+
3+
```go
4+
{{#include ./../../examples/transaction_custom.go}}
5+
```
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Transaction Execute
2+
3+
```go
4+
{{#include ./../../examples/transaction_execute.go}}
5+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Transaction Execute And Watch
2+
3+
```go
4+
{{#include ./../../examples/transaction_execute_and_watch.go}}
5+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Transaction Execute And Watch Finalization
2+
3+
```go
4+
{{#include ./../../examples/transaction_execute_and_watch_finalization.go}}
5+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Transaction Execute And Watch Inclusion
2+
3+
```go
4+
{{#include ./../../examples/transaction_execute_and_watch_inclusion.go}}
5+
```

examples/account_balance.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package examples
22

33
import (
4+
"fmt"
5+
46
"github.com/availproject/avail-go-sdk/metadata"
57
syPallet "github.com/availproject/avail-go-sdk/metadata/pallets/system"
68
"github.com/availproject/avail-go-sdk/primitives"
@@ -22,17 +24,17 @@ func RunAccountBalance() {
2224
val, err := storage.Fetch(&storageAt, accountId)
2325
PanicOnError(err)
2426

25-
println("Free Balance: ", val.Value.AccountData.Free.ToHuman())
26-
println("Reserved Balance: ", val.Value.AccountData.Reserved.ToHuman())
27-
println("Frozen Balance: ", val.Value.AccountData.Frozen.ToHuman())
27+
fmt.Println("Free Balance: ", val.Value.AccountData.Free.ToHuman())
28+
fmt.Println("Reserved Balance: ", val.Value.AccountData.Reserved.ToHuman())
29+
fmt.Println("Frozen Balance: ", val.Value.AccountData.Frozen.ToHuman())
2830

2931
// Via Abstraction
3032
balance, err := SDK.Account.Balance(sdk.Client, accountId)
3133
PanicOnError(err)
3234

33-
println("Free Balance: ", balance.Free.ToHuman())
34-
println("Reserved Balance: ", balance.Reserved.ToHuman())
35-
println("Frozen Balance: ", balance.Frozen.ToHuman())
35+
fmt.Println("Free Balance: ", balance.Free.ToHuman())
36+
fmt.Println("Reserved Balance: ", balance.Reserved.ToHuman())
37+
fmt.Println("Frozen Balance: ", balance.Frozen.ToHuman())
3638

37-
println("RunAccountBalance finished correctly.")
39+
fmt.Println("RunAccountBalance finished correctly.")
3840
}

examples/account_nonce.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package examples
22

33
import (
4+
"fmt"
5+
46
"github.com/availproject/avail-go-sdk/metadata"
57
SDK "github.com/availproject/avail-go-sdk/sdk"
68
)
@@ -13,7 +15,7 @@ func RunAccountNonce() {
1315
nonce, err := sdk.Client.Rpc.System.AccountNextIndex("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
1416
PanicOnError(err)
1517

16-
println("RPC Nonce: ", nonce)
18+
fmt.Println("RPC Nonce: ", nonce)
1719

1820
// Via Abstraction
1921
accountId, err := metadata.NewAccountIdFromAddress("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
@@ -22,7 +24,7 @@ func RunAccountNonce() {
2224
nonce2, err := SDK.Account.Nonce(sdk.Client, accountId)
2325
PanicOnError(err)
2426

25-
println("Abstraction Nonce: ", nonce2)
27+
fmt.Println("Abstraction Nonce: ", nonce2)
2628

27-
println("RunAccountNonce finished correctly.")
29+
fmt.Println("RunAccountNonce finished correctly.")
2830
}

examples/batch.go

+42-53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package examples
22

33
import (
4+
"fmt"
5+
46
"github.com/availproject/avail-go-sdk/metadata"
57
baPallet "github.com/availproject/avail-go-sdk/metadata/pallets/balances"
68
syPallet "github.com/availproject/avail-go-sdk/metadata/pallets/system"
@@ -30,9 +32,8 @@ func RunBatch() {
3032
// The other was it to create a transaction using the sdk api and then use the `call` field member
3133
{
3234
destCharlie, err := metadata.NewAccountIdFromAddress("5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y")
33-
if err != nil {
34-
panic(err)
35-
}
35+
PanicOnError(err)
36+
3637
tx := sdk.Tx.Balances.TransferKeepAlive(destCharlie.ToMultiAddress(), SDK.OneAvail())
3738
callsToExecute = append(callsToExecute, tx.Payload.Call)
3839
}
@@ -47,27 +48,25 @@ func RunBatch() {
4748
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(0))
4849
PanicOnError(err)
4950

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")
5554

5655
events := res.Events.Unwrap()
5756

5857
if SDK.EventFindFirst(events, utPallet.EventBatchCompleted{}).IsSome() {
59-
println("Batch was successfully completed")
58+
fmt.Println("Batch was successfully completed")
6059
} else {
6160
panic("Batch call failed")
6261
}
6362

6463
if len(SDK.EventFindAll(events, utPallet.EventItemCompleted{})) == 2 {
65-
println("All batch items completed")
64+
fmt.Println("All batch items completed")
6665
} else {
6766
panic("No all items were completed")
6867
}
6968

70-
println("Batch call done")
69+
fmt.Println("Batch call done")
7170
}
7271

7372
// Batch All call
@@ -76,27 +75,25 @@ func RunBatch() {
7675
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(0))
7776
PanicOnError(err)
7877

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")
8481

8582
events := res.Events.Unwrap()
8683

8784
if SDK.EventFindFirst(events, utPallet.EventBatchCompleted{}).IsSome() {
88-
println("Batch was successfully completed")
85+
fmt.Println("Batch was successfully completed")
8986
} else {
9087
panic("Batch All call failed")
9188
}
9289

9390
if len(SDK.EventFindAll(events, utPallet.EventItemCompleted{})) == 2 {
94-
println("All batch items completed")
91+
fmt.Println("All batch items completed")
9592
} else {
9693
panic("No all items were completed")
9794
}
9895

99-
println("Batch All call done")
96+
fmt.Println("Batch All call done")
10097
}
10198

10299
// Force Batch call
@@ -105,27 +102,25 @@ func RunBatch() {
105102
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(0))
106103
PanicOnError(err)
107104

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")
113108

114109
events := res.Events.Unwrap()
115110

116111
if SDK.EventFindFirst(events, utPallet.EventBatchCompleted{}).IsSome() {
117-
println("Batch was successfully completed")
112+
fmt.Println("Batch was successfully completed")
118113
} else {
119114
panic("Batch All call failed")
120115
}
121116

122117
if len(SDK.EventFindAll(events, utPallet.EventItemCompleted{})) == 2 {
123-
println("All batch items completed")
118+
fmt.Println("All batch items completed")
124119
} else {
125120
panic("No all items were completed")
126121
}
127122

128-
println("Force Batch call done")
123+
fmt.Println("Force Batch call done")
129124
}
130125

131126
//
@@ -156,29 +151,27 @@ func RunBatch() {
156151
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(0))
157152
PanicOnError(err)
158153

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")
164157

165158
events := res.Events.Unwrap()
166159

167160
if event := SDK.EventFindFirst(events, utPallet.EventBatchInterrupted{}); event.IsSome() {
168161
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)
171164
} else {
172165
panic("Failed to find EventBatchInterrupted event.")
173166
}
174167

175168
if len(SDK.EventFindAll(events, utPallet.EventItemCompleted{})) == 2 {
176-
println("Some batch items completed")
169+
fmt.Println("Some batch items completed")
177170
} else {
178171
panic("Cannot be more than 2")
179172
}
180173

181-
println("Batch call done")
174+
fmt.Println("Batch call done")
182175
}
183176

184177
// Batch All call
@@ -187,21 +180,19 @@ func RunBatch() {
187180
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(0))
188181
PanicOnError(err)
189182

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")
195186

196187
events := res.Events.Unwrap()
197188

198189
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())
200191
} else {
201192
panic("Failed to find EventExtrinsicFailed event.")
202193
}
203194

204-
println("Batch All call done")
195+
fmt.Println("Batch All call done")
205196
}
206197

207198
// Force Batch call
@@ -210,34 +201,32 @@ func RunBatch() {
210201
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(0))
211202
PanicOnError(err)
212203

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")
218207

219208
events := res.Events.Unwrap()
220209

221210
if SDK.EventFindFirst(events, utPallet.EventBatchCompletedWithErrors{}).IsSome() {
222-
println("Batch completed with errors")
211+
fmt.Println("Batch completed with errors")
223212
} else {
224213
panic("Failed to find EventBatchCompletedWithErrors")
225214
}
226215

227216
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")
229218
} else {
230219
panic("3 items must be completed")
231220
}
232221

233222
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())
235224
} else {
236225
panic("Failed to find EventItemFailed")
237226
}
238227

239-
println("Force Batch call done")
228+
fmt.Println("Force Batch call done")
240229
}
241230

242-
println("RunBatch finished correctly.")
231+
fmt.Println("RunBatch finished correctly.")
243232
}

examples/block.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package examples
22

3-
import ()
3+
import "fmt"
44

55
func RunBlock() {
66
RunBlockTransactionAll()
@@ -15,5 +15,5 @@ func RunBlock() {
1515
RunBlockDataSubmissionBySigner()
1616
RunBlockEvents()
1717

18-
println("RunBlock finished correctly.")
18+
fmt.Println("RunBlock finished correctly.")
1919
}

0 commit comments

Comments
 (0)