Skip to content

Commit 5f34808

Browse files
Added Storage examples (#11)
* Added storage examples * Expanded storage * Added new storages * Expanded storage with additional custom types * Added the rest of staking storages * Added session storage * Added Nomination Pools * Added Identity storage * Added examples * Cleaning up
1 parent cb2dd13 commit 5f34808

Some content is hidden

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

42 files changed

+3802
-62
lines changed

.vscode/launch.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch Package",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "auto",
12+
"program": "./."
13+
}
14+
]
15+
}

documentation/book.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ authors = ["Avail"]
33
language = "en"
44
multilingual = false
55
src = "src"
6-
title = "Avail Rust SDK Examples"
6+
title = "Avail Go SDK Examples"
77

88
[output.markdown]
99
[output.html]

documentation/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
- [Block](./block.md)
66
- [Data Submission](./data_submission.md)
77
- [Events](./events.md)
8+
- [Storage](./storage.md)
89

documentation/src/storage.md

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

examples/mod.go

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

33
func Run() {
4-
run_block()
5-
run_account_nonce()
6-
run_data_submission()
4+
// run_block()
5+
// run_events()
6+
// run_account_nonce()
7+
// run_data_submission()
8+
run_storage()
79
}

examples/storage.go

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package examples
2+
3+
import (
4+
"go-sdk/metadata"
5+
idenPallet "go-sdk/metadata/pallets/identity"
6+
staPallet "go-sdk/metadata/pallets/staking"
7+
sysPallet "go-sdk/metadata/pallets/system"
8+
prim "go-sdk/primitives"
9+
SDK "go-sdk/sdk"
10+
)
11+
12+
func run_storage() {
13+
sdk := SDK.NewSDK(SDK.TuringEndpoint)
14+
15+
blockHash, err := prim.NewH256FromHexString("0x9e813bb85fca217f8f3967bd4b550b05f7d559412571ca1dd621aa37343b300b")
16+
if err != nil {
17+
panic(err)
18+
}
19+
blockStorage, err := sdk.Client.StorageAt(prim.NewSome(blockHash))
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
// Simple Storage
25+
{
26+
storage := staPallet.StorageMinValidatorBond{}
27+
val, err := storage.Fetch(&blockStorage)
28+
if err != nil {
29+
panic(err)
30+
}
31+
println("Min Validator Bond: ", val.ToHuman())
32+
}
33+
34+
// Simple Storage that returns Option
35+
{
36+
storage := staPallet.StorageCurrentEra{}
37+
val, err := storage.Fetch(&blockStorage)
38+
if err != nil {
39+
panic(err)
40+
}
41+
if val.IsSome() {
42+
println("Current Era: ", val.Unwrap())
43+
}
44+
}
45+
46+
// Fetch Map Storage
47+
{
48+
storage := sysPallet.StorageAccount{}
49+
acc, err := metadata.NewAccountIdFromAddress("5C869t2dWzmmYkE8NT1oocuEEdwqNnAm2XhvnuHcavNUcTTT")
50+
if err != nil {
51+
panic(err)
52+
}
53+
val, err := storage.Fetch(&blockStorage, acc)
54+
if err != nil {
55+
panic(err)
56+
}
57+
if val.IsSome() {
58+
val := val.Unwrap()
59+
println("Account Key: ", val.Key.ToHuman())
60+
println("Account Nonce: ", val.Value.Nonce)
61+
println("Account Free Balance: ", val.Value.AccountData.Free.ToHuman())
62+
}
63+
}
64+
65+
// Fetch All Map Storage
66+
{
67+
storage := idenPallet.StorageIdentityOf{}
68+
val, err := storage.FetchAll(&blockStorage)
69+
if err != nil {
70+
panic(err)
71+
}
72+
for i := 0; i < len(val); i++ {
73+
println("Identity Key: ", val[i].Key.ToHuman())
74+
println("Identity Deposit: ", val[i].Value.T0.Deposit.ToHuman())
75+
println("Identity Display: ", val[i].Value.T0.Info.Display.ToHuman())
76+
if i >= 2 {
77+
break
78+
}
79+
}
80+
}
81+
82+
// Fetch Double Map Storage
83+
{
84+
storage := staPallet.StorageErasValidatorPrefs{}
85+
era := uint32(299)
86+
acc, err := metadata.NewAccountIdFromAddress("5EFTSpRN2nMZDLjkniBYdmMxquMNm5CLVsrX2V3HHue6QFFF")
87+
if err != nil {
88+
panic(err)
89+
}
90+
val, err := storage.Fetch(&blockStorage, era, acc)
91+
if err != nil {
92+
panic(err)
93+
}
94+
println("Era: ", val.Key1)
95+
println("Address: ", val.Key2.ToHuman())
96+
println("Commission: ", val.Value.Commission.ToHuman())
97+
println("Blocked: ", val.Value.Blocked)
98+
}
99+
100+
// Fetch All Double Map Storage
101+
{
102+
storage := staPallet.StorageErasValidatorPrefs{}
103+
era := uint32(299)
104+
val, err := storage.FetchAll(&blockStorage, era)
105+
if err != nil {
106+
panic(err)
107+
}
108+
109+
for i := 0; i < len(val); i++ {
110+
println("Era: ", val[i].Key1)
111+
println("Address: ", val[i].Key2.ToHuman())
112+
println("Commission: ", val[i].Value.Commission.ToHuman())
113+
println("Blocked: ", val[i].Value.Blocked)
114+
if i >= 2 {
115+
break
116+
}
117+
}
118+
119+
}
120+
}

go.mod

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,22 @@ go 1.23.4
55
require (
66
github.com/centrifuge/go-substrate-rpc-client/v4 v4.2.1
77
github.com/itering/scale.go v1.9.14
8+
github.com/shopspring/decimal v1.2.0
89
github.com/vedhavyas/go-subkey/v2 v2.0.0
910
golang.org/x/crypto v0.31.0
1011
)
1112

1213
require (
1314
github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect
1415
github.com/cosmos/go-bip39 v1.0.0 // indirect
15-
github.com/deckarep/golang-set v1.8.0 // indirect
1616
github.com/decred/base58 v1.0.4 // indirect
1717
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
1818
github.com/ethereum/go-ethereum v1.10.20 // indirect
1919
github.com/go-stack/stack v1.8.1 // indirect
20-
github.com/gorilla/websocket v1.5.0 // indirect
2120
github.com/gtank/merlin v0.1.1 // indirect
2221
github.com/gtank/ristretto255 v0.1.2 // indirect
2322
github.com/huandu/xstrings v1.3.1 // indirect
2423
github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b // indirect
2524
github.com/pierrec/xxHash v0.1.5 // indirect
26-
github.com/rs/cors v1.8.2 // indirect
27-
github.com/shopspring/decimal v1.2.0 // indirect
2825
golang.org/x/sys v0.28.0 // indirect
29-
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
3026
)

go.sum

-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
github.com/ChainSafe/go-schnorrkel v1.0.0 h1:3aDA67lAykLaG1y3AOjs88dMxC88PgUuHRrLeDnvGIM=
22
github.com/ChainSafe/go-schnorrkel v1.0.0/go.mod h1:dpzHYVxLZcp8pjlV+O+UR8K0Hp/z7vcchBSbMBEhCw4=
3-
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 h1:fLjPD/aNc3UIOA6tDi6QXUemppXK3P9BI7mr2hd6gx8=
4-
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
53
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k=
64
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
75
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ=
@@ -14,8 +12,6 @@ github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4x
1412
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1513
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1614
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
17-
github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4=
18-
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
1915
github.com/decred/base58 v1.0.4 h1:QJC6B0E0rXOPA8U/kw2rP+qiRJsUaE2Er+pYb3siUeA=
2016
github.com/decred/base58 v1.0.4/go.mod h1:jJswKPEdvpFpvf7dsDvFZyLT22xZ9lWqEByX38oGd9E=
2117
github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0=
@@ -24,14 +20,10 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1
2420
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
2521
github.com/ethereum/go-ethereum v1.10.20 h1:75IW830ClSS40yrQC1ZCMZCt5I+zU16oqId2SiQwdQ4=
2622
github.com/ethereum/go-ethereum v1.10.20/go.mod h1:LWUN82TCHGpxB3En5HVmLLzPD7YSrEUFmFfN1nKkVN0=
27-
github.com/go-ole/go-ole v1.2.1 h1:2lOsA72HgjxAuMlKpFiCbHTvu44PIVkZ5hqm3RSdI/E=
28-
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
2923
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
3024
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
3125
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa h1:Q75Upo5UN4JbPFURXZ8nLKYUvF85dyFRop/vQ0Rv+64=
3226
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
33-
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
34-
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
3527
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
3628
github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is=
3729
github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
@@ -48,21 +40,13 @@ github.com/pierrec/xxHash v0.1.5 h1:n/jBpwTHiER4xYvK3/CdPVnLDPchj8eTJFFLUb4QHBo=
4840
github.com/pierrec/xxHash v0.1.5/go.mod h1:w2waW5Zoa/Wc4Yqe0wgrIYAGKqRMf7czn2HNKXmuL+I=
4941
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5042
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
51-
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
52-
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
53-
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
54-
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
5543
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
5644
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
5745
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5846
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
5947
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
6048
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
6149
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
62-
github.com/tklauser/go-sysconf v0.3.5 h1:uu3Xl4nkLzQfXNsWn15rPc/HQCJKObbt1dKJeWp3vU4=
63-
github.com/tklauser/go-sysconf v0.3.5/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
64-
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
65-
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
6650
github.com/vedhavyas/go-subkey/v2 v2.0.0 h1:LemDIsrVtRSOkp0FA8HxP6ynfKjeOj3BY2U9UNfeDMA=
6751
github.com/vedhavyas/go-subkey/v2 v2.0.0/go.mod h1:95aZ+XDCWAUUynjlmi7BtPExjXgXxByE0WfBwbmIRH4=
6852
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -77,8 +61,6 @@ golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
7761
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7862
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
7963
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
80-
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
81-
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
8264
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
8365
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8466
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

interfaces/mod.go

+5
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ type EventT interface {
1414
EventIndex() uint8
1515
EventName() string
1616
}
17+
18+
type BlockStorageT interface {
19+
Fetch(storageEntryKey string) (string, error)
20+
FetchKeys(key string) ([]string, error)
21+
}

main.go

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

3-
import "go-sdk/examples"
3+
import (
4+
"go-sdk/examples"
5+
)
46

57
func main() {
68
examples.Run()

metadata/pallets/balances/storage.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package balances
2+
3+
import (
4+
"go-sdk/interfaces"
5+
. "go-sdk/metadata"
6+
prim "go-sdk/primitives"
7+
)
8+
9+
type StorageTotalIssuance struct {
10+
Value Balance
11+
}
12+
13+
func (this *StorageTotalIssuance) PalletName() string {
14+
return PalletName
15+
}
16+
17+
func (this *StorageTotalIssuance) StorageName() string {
18+
return "TotalIssuance"
19+
}
20+
21+
func (this *StorageTotalIssuance) Fetch(blockStorage interfaces.BlockStorageT) (prim.Option[StorageTotalIssuance], error) {
22+
return GenericFetch[StorageTotalIssuance](blockStorage, this)
23+
}
24+
25+
//
26+
//
27+
//
28+
29+
type StorageInactiveIssuance struct {
30+
Value Balance
31+
}
32+
33+
func (this *StorageInactiveIssuance) PalletName() string {
34+
return PalletName
35+
}
36+
37+
func (this *StorageInactiveIssuance) StorageName() string {
38+
return "InactiveIssuance"
39+
}
40+
41+
func (this *StorageInactiveIssuance) Fetch(blockStorage interfaces.BlockStorageT) (prim.Option[StorageInactiveIssuance], error) {
42+
return GenericFetch[StorageInactiveIssuance](blockStorage, this)
43+
}
44+
45+
//
46+
//
47+
//
48+
49+
type StorageLocksKey = AccountId
50+
type StorageLocksEntry = StorageEntry[StorageLocksKey, StorageLocks]
51+
52+
type StorageLocks struct {
53+
Value []BalanceLock
54+
}
55+
56+
func (this *StorageLocks) PalletName() string {
57+
return PalletName
58+
}
59+
60+
func (this *StorageLocks) StorageName() string {
61+
return "Locks"
62+
}
63+
64+
func (this *StorageLocks) MapKeyHasher() uint8 {
65+
return Blake2_128ConcatHasher
66+
}
67+
68+
func (this *StorageLocks) Fetch(blockStorage interfaces.BlockStorageT, key StorageLocksKey) (prim.Option[StorageLocksEntry], error) {
69+
return GenericMapFetch[StorageLocks](blockStorage, key, this)
70+
}
71+
72+
func (this *StorageLocks) FetchAll(blockStorage interfaces.BlockStorageT) ([]StorageLocksEntry, error) {
73+
return GenericMapKeysFetch[StorageLocks, StorageLocksKey](blockStorage, this)
74+
}
75+
76+
type BalanceLock struct {
77+
Id [8]uint8
78+
Amount Balance
79+
Reasons Reasons
80+
}
81+
82+
type Reasons struct {
83+
VariantIndex uint8
84+
}
85+
86+
func (this Reasons) ToString() string {
87+
switch this.VariantIndex {
88+
case 0:
89+
return "Fee"
90+
case 1:
91+
return "Misc"
92+
case 2:
93+
return "All"
94+
default:
95+
panic("Unknown Reasons Variant Index")
96+
}
97+
}

0 commit comments

Comments
 (0)