Skip to content

Commit 0703859

Browse files
General improvements (#17)
* Added qol improvements * spell check * Replaced Uint128 with Balance * Added Sudo calls * Added tests for string and compact structs as struct fields * Added Acount Balance example. Updated documentation for GO installation * Updated Block Example
1 parent 40d63bf commit 0703859

31 files changed

+626
-173
lines changed

documentation/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [Installation](./installation.md)
55
- [Enum](./enum.md)
66
- [Account Nonce](./account_nonce.md)
7+
- [Account Balance](./account_balance.md)
78
- [Block](./block.md)
89
- [Data Submission](./data_submission.md)
910
- [Events](./events.md)

documentation/src/account_balance.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Account Nonce
2+
3+
```go
4+
{{#include ./../../examples/account_balance.go}}
5+
```

documentation/src/installation.md

+27-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
You can find Go installation instructions [here](https://go.dev/doc/install). The required minimum version of Go is 1.23.0
44

5-
If you already have installed Go, jump to -First Time Running- section.
5+
If you already have installed Go (version no less than 1.23.0), jump to `Add Avail-GO SDK as dependency` section.
66

77
## Installing GO in an Empty Ubuntu Container
88
Here are the instructions on how to install GO using the latest Ubuntu image.
@@ -20,9 +20,27 @@ export PATH=$PATH:/usr/local/go/bin
2020
go version
2121
# "go version go1.23.5 linux/amd64"
2222
```
23+
## Add Avail-GO SDK as dependency
2324

25+
#### To Existing Project
2426

25-
## First Time Running
27+
```bash
28+
# Fetches Avail-GO SDK v0.2.0-rc5. This might not be the newest version so make sure to check out the latest github avail-go-sdk release.
29+
# Link to Github: https://github.com/availproject/avail-go-sdk/releases
30+
go get github.com/availproject/[email protected]
31+
```
32+
33+
#### To A New Project
34+
35+
```bash
36+
# Creates a new project with name myproject
37+
go mod init myproject
38+
# Fetches Avail-GO SDK v0.2.0-rc5. This might not be the newest version so make sure to check out the latest github avail-go-sdk release.
39+
# Link to Github: https://github.com/availproject/avail-go-sdk/releases
40+
go get github.com/availproject/[email protected]
41+
```
42+
43+
#### First Time Running
2644

2745
1. Paste the following code to `main.go`:
2846
```go
@@ -34,15 +52,16 @@ import (
3452
)
3553

3654
func main() {
37-
sdk := SDK.NewSDK(SDK.TuringEndpoint)
38-
39-
// Use SDK.Account.NewKeyPair("Your key") to use a different account than Alice
40-
acc, err := SDK.Account.Alice()
55+
sdk, err := SDK.NewSDK(SDK.TuringEndpoint)
4156
if err != nil {
4257
panic(err)
4358
}
4459

60+
// Use SDK.Account.NewKeyPair("Your key") to use a different account than Ferdie
61+
acc := SDK.Account.Ferdie()
62+
4563
tx := sdk.Tx.DataAvailability.SubmitData([]byte("MyData"))
64+
println("Submitting new Transaction... Can take up to 20 seconds")
4665
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(1))
4766
if err != nil {
4867
panic(err)
@@ -53,21 +72,12 @@ func main() {
5372
}
5473
```
5574

56-
2. Paste the following code to `go.mod`
57-
```go
58-
module mymodule
59-
60-
go 1.23.4
61-
62-
require github.com/availproject/avail-go-sdk v0.2.0-rc4
63-
```
64-
65-
3. Fetch dependencies:
75+
2. Fetch dependencies:
6676
```bash
6777
go mod tidy
6878
```
6979

70-
4. Run Example:
80+
3. Run Example:
7181
```bash
7282
go run .
7383
```

documentation/src/start.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# SDK Version
22

3-
This Documentation is based upon avail-go version v0.2.0-rc4
3+
This Documentation is based upon avail-go version v0.2.0-rc5

examples/account_balance.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package examples
2+
3+
import (
4+
"github.com/availproject/avail-go-sdk/metadata"
5+
SDK "github.com/availproject/avail-go-sdk/sdk"
6+
)
7+
8+
func Run_account_balance() {
9+
sdk, err := SDK.NewSDK(SDK.TuringEndpoint)
10+
if err != nil {
11+
panic(err)
12+
}
13+
14+
// Via Abstraction
15+
accountId, err := metadata.NewAccountIdFromAddress("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
16+
if err != nil {
17+
panic(err)
18+
}
19+
balance, err := SDK.Account.Balance(sdk.Client, accountId)
20+
if err != nil {
21+
panic(err)
22+
}
23+
println("Free Balance: ", balance.Free.ToHuman())
24+
println("Reserved Balance: ", balance.Reserved.ToHuman())
25+
println("Frozen Balance: ", balance.Frozen.ToHuman())
26+
}

examples/account_nonce.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
package examples
22

33
import (
4+
"github.com/availproject/avail-go-sdk/metadata"
45
SDK "github.com/availproject/avail-go-sdk/sdk"
56
)
67

78
func Run_account_nonce() {
8-
sdk := SDK.NewSDK(SDK.LocalEndpoint)
9+
sdk, err := SDK.NewSDK(SDK.TuringEndpoint)
10+
if err != nil {
11+
panic(err)
12+
}
913

14+
// Via RPC
1015
nonce, err := sdk.Client.Rpc.System.AccountNextIndex("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
1116
if err != nil {
1217
panic(err)
1318
}
14-
println("Nonce: ", nonce)
19+
println("RPC Nonce: ", nonce)
20+
21+
// Via Abstraction
22+
accountId, err := metadata.NewAccountIdFromAddress("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
23+
if err != nil {
24+
panic(err)
25+
}
26+
nonce2, err := SDK.Account.Nonce(sdk.Client, accountId)
27+
if err != nil {
28+
panic(err)
29+
}
30+
println("Abstraction Nonce: ", nonce2)
1531
}

examples/batch.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@ import (
77
utPallet "github.com/availproject/avail-go-sdk/metadata/pallets/utility"
88
prim "github.com/availproject/avail-go-sdk/primitives"
99
SDK "github.com/availproject/avail-go-sdk/sdk"
10-
11-
"github.com/itering/scale.go/utiles/uint128"
1210
)
1311

1412
func Run_batch() {
15-
sdk := SDK.NewSDK(SDK.LocalEndpoint)
16-
17-
// Use SDK.Account.NewKeyPair("Your key") to use a different account than Alice
18-
acc, err := SDK.Account.Alice()
13+
sdk, err := SDK.NewSDK(SDK.LocalEndpoint)
1914
if err != nil {
2015
panic(err)
2116
}
2217

18+
// Use SDK.Account.NewKeyPair("Your key") to use a different account than Alice
19+
acc := SDK.Account.Alice()
20+
2321
callsToExecute := []prim.Call{}
2422

2523
// 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
@@ -150,7 +148,7 @@ func Run_batch() {
150148
if err != nil {
151149
panic(err)
152150
}
153-
tx := sdk.Tx.Balances.TransferKeepAlive(destEve.ToMultiAddress(), SDK.OneAvail().Mul(uint128.From64(uint64(1_000_000_000))))
151+
tx := sdk.Tx.Balances.TransferKeepAlive(destEve.ToMultiAddress(), SDK.OneAvail().Mul64(uint64(1_000_000_000)))
154152
callsToExecute = append(callsToExecute, tx.Payload.Call)
155153
}
156154

examples/block.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99
)
1010

1111
func Run_block() {
12-
sdk := SDK.NewSDK(SDK.LocalEndpoint)
13-
14-
acc, err := SDK.Account.Alice()
12+
sdk, err := SDK.NewSDK(SDK.LocalEndpoint)
1513
if err != nil {
1614
panic(err)
1715
}
1816

17+
acc := SDK.Account.Alice()
18+
1919
tx := sdk.Tx.DataAvailability.SubmitData([]byte("MyData"))
2020
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(1))
2121
if err != nil {
@@ -24,10 +24,16 @@ func Run_block() {
2424

2525
// Fetching
2626
// Fetching Best Block
27-
_, _ = SDK.NewBestBlock(sdk.Client)
27+
_, err = SDK.NewBestBlock(sdk.Client)
28+
if err != nil {
29+
panic(err)
30+
}
2831

2932
// Fetching Last Finalized Block
30-
_, _ = SDK.NewFinalizedBlock(sdk.Client)
33+
_, err = SDK.NewFinalizedBlock(sdk.Client)
34+
if err != nil {
35+
panic(err)
36+
}
3137

3238
// Fetching block with Block Hash
3339
block, err := SDK.NewBlock(sdk.Client, res.BlockHash)

examples/data_submission.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
)
1010

1111
func Run_data_submission() {
12-
sdk := SDK.NewSDK(SDK.LocalEndpoint)
13-
14-
// Use SDK.Account.NewKeyPair("Your key") to use a different account than Alice
15-
acc, err := SDK.Account.Alice()
12+
sdk, err := SDK.NewSDK(SDK.LocalEndpoint)
1613
if err != nil {
1714
panic(err)
1815
}
1916

17+
// Use SDK.Account.NewKeyPair("Your key") to use a different account than Alice
18+
acc := SDK.Account.Alice()
19+
2020
key := fmt.Sprintf("MyKey%v", rand.Uint32())
2121
// Transactions can be found under sdk.Tx.*
2222
tx := sdk.Tx.DataAvailability.CreateApplicationKey([]byte(key))
@@ -28,6 +28,11 @@ func Run_data_submission() {
2828
// Failed to submit transaction
2929
panic(err)
3030
}
31+
if isSuc, err := res.IsSuccessful(); err != nil {
32+
panic(err)
33+
} else if !isSuc {
34+
println("The transaction was unsuccessful")
35+
}
3136

3237
events := res.Events.Unwrap()
3338
event := SDK.EventFindFirst(events, daPallet.EventApplicationKeyCreated{}).Unwrap()
@@ -41,6 +46,12 @@ func Run_data_submission() {
4146
panic(err)
4247
}
4348

49+
if isSuc, err := res.IsSuccessful(); err != nil {
50+
panic(err)
51+
} else if !isSuc {
52+
println("The transaction was unsuccessful")
53+
}
54+
4455
// Transaction Details
4556
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))
4657

examples/events.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@ import (
77
)
88

99
func Run_events() {
10-
sdk := SDK.NewSDK(SDK.LocalEndpoint)
11-
12-
acc, err := SDK.Account.Alice()
10+
sdk, err := SDK.NewSDK(SDK.LocalEndpoint)
1311
if err != nil {
1412
panic(err)
1513
}
1614

15+
acc := SDK.Account.Alice()
16+
1717
tx := sdk.Tx.DataAvailability.SubmitData([]byte("MyData"))
1818
res, err := tx.ExecuteAndWatchInclusion(acc, SDK.NewTransactionOptions().WithAppId(1))
1919
if err != nil {
2020
panic(err)
2121
}
2222

23-
eventsM := res.Events
24-
if eventsM.IsNone() {
25-
panic("Failed to decode events")
23+
if isSuc, err := res.IsSuccessful(); err != nil {
24+
panic(err)
25+
} else if !isSuc {
26+
println("The transaction was unsuccessful")
2627
}
27-
events := eventsM.Unwrap()
28+
29+
events := res.Events.Unwrap()
2830
for _, event := range events {
2931
println(fmt.Sprintf(`Pallet Name: %v, Pallet Index: %v, Event Name: %v, Event Index: %v`, event.PalletName, event.PalletIndex, event.EventName, event.EventIndex))
3032
}

examples/mod.go

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

33
func Run() {
44
Run_account_nonce()
5+
Run_account_balance()
56
Run_batch()
67
Run_block()
78
Run_data_submission()

examples/rpc.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
)
77

88
func Run_rpc() {
9-
sdk := SDK.NewSDK(SDK.TuringEndpoint)
9+
sdk, err := SDK.NewSDK(SDK.TuringEndpoint)
10+
if err != nil {
11+
panic(err)
12+
}
1013

1114
{
1215
// chain_GetBlock

examples/storage.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010
)
1111

1212
func Run_storage() {
13-
sdk := SDK.NewSDK(SDK.TuringEndpoint)
13+
sdk, err := SDK.NewSDK(SDK.TuringEndpoint)
14+
if err != nil {
15+
panic(err)
16+
}
1417

1518
blockHash, err := prim.NewH256FromHexString("0x9e813bb85fca217f8f3967bd4b550b05f7d559412571ca1dd621aa37343b300b")
1619
if err != nil {

main.go

+1-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
package main
22

33
import (
4-
"os"
5-
64
"github.com/availproject/avail-go-sdk/examples"
7-
"github.com/sirupsen/logrus"
85
)
96

107
func main() {
11-
// Set log level based on the environment variable
12-
level, err := logrus.ParseLevel(os.Getenv("LOG_LEVEL"))
13-
if err != nil {
14-
level = logrus.InfoLevel // Default to INFO if parsing fails
15-
}
16-
logrus.SetLevel(level)
17-
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
18-
19-
examples.Run_data_submission()
8+
examples.Run()
209
}

metadata/pallets/balances/calls.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (this *CallTransferAlowDeath) DecodeExtrinsic(tx *prim.DecodedExtrinsic) bo
6464
type CallForceTransfer struct {
6565
Source prim.MultiAddress
6666
Dest prim.MultiAddress
67-
Value uint128.Uint128 `scale:"compact"`
67+
Value metadata.Balance `scale:"compact"`
6868
}
6969

7070
func (this CallForceTransfer) PalletIndex() uint8 {
@@ -109,7 +109,7 @@ func (this *CallForceTransfer) DecodeExtrinsic(tx *prim.DecodedExtrinsic) bool {
109109
// kill the origin account.
110110
type CallTransferKeepAlive struct {
111111
Dest prim.MultiAddress
112-
Value uint128.Uint128 `scale:"compact"`
112+
Value metadata.Balance `scale:"compact"`
113113
}
114114

115115
func (this CallTransferKeepAlive) PalletIndex() uint8 {

metadata/pallets/staking/storage.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,7 @@ func (this *StorageErasRewardPoints) MapKeyHasher() uint8 {
248248

249249
func (this *StorageErasRewardPoints) Fetch(blockStorage interfaces.BlockStorageT, key StorageErasRewardPointsKey) (StorageErasRewardPointsEntry, error) {
250250
val, err := GenericMapFetch[StorageErasRewardPoints](blockStorage, key, this)
251-
if err != nil {
252-
return StorageErasRewardPointsEntry{}, nil
253-
}
254-
return val.Unwrap(), nil
251+
return val.Unwrap(), err
255252

256253
}
257254

0 commit comments

Comments
 (0)