Skip to content

Commit 40d63bf

Browse files
Added documentation for enum and rpcs. Added more rpcs, added logger (#16)
* Added documentation for enum and rpcs. Added more rpcs * Added logging * Added timestamp to logger
1 parent 0f1d734 commit 40d63bf

24 files changed

+625
-55
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,23 @@ This project uses [GitHub Flow](https://www.alexhyett.com/git-flow-github-flow/)
1111

1212
# Documentation
1313
[Link](https://availproject.github.io/avail-go-sdk/) to documentation (web preview of examples)
14+
15+
16+
# Logging
17+
To enable logging add this to the `main` function.
18+
19+
```go
20+
// Set log level based on the environment variable
21+
level, err := logrus.ParseLevel(os.Getenv("LOG_LEVEL"))
22+
if err != nil {
23+
level = logrus.InfoLevel // Default to INFO if parsing fails
24+
}
25+
logrus.SetLevel(level)
26+
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
27+
```
28+
29+
And run go command with `LOG_LEVEL` set to debug
30+
31+
```bash
32+
LOG_LEVEL=debug go run .
33+
```

documentation/src/SUMMARY.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
- [Start](./start.md)
44
- [Installation](./installation.md)
5+
- [Enum](./enum.md)
56
- [Account Nonce](./account_nonce.md)
67
- [Block](./block.md)
78
- [Data Submission](./data_submission.md)
89
- [Events](./events.md)
910
- [Storage](./storage.md)
1011
- [Batch](./batch.md)
12+
- [RPC](./rpc.md)
1113

documentation/src/enum.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Enum
2+
3+
There are no such thing as tagged union (enums from Rust) in Go. From Scale perspective, a enum is represented by a `variant index` that defines what enum variant is described and by additional data that each variant could have.
4+
5+
## Simple Enums
6+
Example:
7+
```go
8+
{{#include ./../../examples/enum.go:simpleenum}}
9+
```
10+
11+
Simple enums don't have anything attach to it besides the variant index. The scale encoding and decoding is done automatically so there is no need to write our own encode/decode methods. In order to know how many variants there are and what each variant means, we create the ToString() method that shows thats.
12+
13+
In order to set the enum to a variant, we just need to set `VariantIndex` to the desired and correct value.
14+
15+
Example of correct setup:
16+
```go
17+
enum := SimpleEnum{}
18+
enum.VariantIndex = 1
19+
```
20+
21+
Example of incorrect setup:
22+
```go
23+
// VariantIndex out of range
24+
enum := SimpleEnum{}
25+
enum.VariantIndex = 200
26+
```
27+
28+
## Complex Enums
29+
Example:
30+
```go
31+
{{#include ./../../examples/enum.go:complexenum}}
32+
```
33+
34+
When at least one variant has additional data attach to it, we are forced to created on our encode and decode methods.
35+
36+
First of all the additional variant data needs to be stored as an option, and the field member should have the same name as the variant itself. In this case `Day`, `Month`, `Year` now carry additional data and that's why there are three fields with the same name in our enum struct.
37+
38+
The EncodeTo method manually scale encodes the data. The `VariantIndex` is a u8 so it's going to be encoded like that. The rest depends on what option has been set. If `VariantIndex` is set to 1, and `Day` is set to 25, both will be encoded correctly. Take care: If you set up the wrong option or set up more than one option then the transaction will fail. It's up to you to be diligent and not mess up.
39+
40+
Example of correct setup:
41+
```go
42+
enum := ComplexEnum{}
43+
enum.VariantIndex = 2
44+
enum.Month.Set(12)
45+
```
46+
47+
Example of incorrect setup:
48+
```go
49+
// VariantIndex out of range
50+
enum := ComplexEnum{}
51+
enum.VariantIndex = 125
52+
53+
// VariantIndex and data not matching
54+
enum.VariantIndex = 0
55+
enum.Year.Set(1990)
56+
57+
// Too many data fields are set
58+
enum.VariantIndex = 1
59+
enum.Day.Set(24)
60+
enum.Year.Set(1990)
61+
```
62+
63+
There isn't much room for errors in the Decode method unless the devs messed it up.

documentation/src/installation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module mymodule
5959

6060
go 1.23.4
6161

62-
require github.com/availproject/avail-go-sdk v0.2.0-rc3
62+
require github.com/availproject/avail-go-sdk v0.2.0-rc4
6363
```
6464

6565
3. Fetch dependencies:

documentation/src/rpc.md

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

examples/README

-7
This file was deleted.

examples/account_nonce.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
SDK "github.com/availproject/avail-go-sdk/sdk"
55
)
66

7-
func run_account_nonce() {
7+
func Run_account_nonce() {
88
sdk := SDK.NewSDK(SDK.LocalEndpoint)
99

1010
nonce, err := sdk.Client.Rpc.System.AccountNextIndex("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")

examples/batch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/itering/scale.go/utiles/uint128"
1212
)
1313

14-
func run_batch() {
14+
func Run_batch() {
1515
sdk := SDK.NewSDK(SDK.LocalEndpoint)
1616

1717
// Use SDK.Account.NewKeyPair("Your key") to use a different account than Alice

examples/block.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
SDK "github.com/availproject/avail-go-sdk/sdk"
99
)
1010

11-
func run_block() {
11+
func Run_block() {
1212
sdk := SDK.NewSDK(SDK.LocalEndpoint)
1313

1414
acc, err := SDK.Account.Alice()

examples/data_submission.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
SDK "github.com/availproject/avail-go-sdk/sdk"
99
)
1010

11-
func run_data_submission() {
11+
func Run_data_submission() {
1212
sdk := SDK.NewSDK(SDK.LocalEndpoint)
1313

1414
// Use SDK.Account.NewKeyPair("Your key") to use a different account than Alice

examples/enum.go

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package examples
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
prim "github.com/availproject/avail-go-sdk/primitives"
8+
)
9+
10+
// ANCHOR: simpleenum
11+
type SimpleEnum struct {
12+
VariantIndex uint8
13+
}
14+
15+
func (this SimpleEnum) ToString() string {
16+
switch this.VariantIndex {
17+
case 0:
18+
return "Nothing"
19+
case 1:
20+
return "Day"
21+
case 2:
22+
return "Month"
23+
case 3:
24+
return "Year"
25+
default:
26+
panic("Unknown SimpleEnum Variant Index")
27+
}
28+
}
29+
30+
// ANCHOR_END: simpleenum
31+
32+
// ANCHOR: complexenum
33+
type ComplexEnum struct {
34+
VariantIndex uint8
35+
Day prim.Option[uint16]
36+
Month prim.Option[uint8]
37+
Year prim.Option[uint32]
38+
}
39+
40+
func (this ComplexEnum) ToString() string {
41+
switch this.VariantIndex {
42+
case 0:
43+
return "Nothing"
44+
case 1:
45+
return fmt.Sprintf("Set: %v", this.Day.Unwrap())
46+
case 2:
47+
return fmt.Sprintf("Set: %v", this.Month.Unwrap())
48+
case 3:
49+
return fmt.Sprintf("Set: %v", this.Year.Unwrap())
50+
default:
51+
panic("Unknown ComplexEnum Variant Index")
52+
}
53+
}
54+
55+
func (this *ComplexEnum) EncodeTo(dest *string) {
56+
prim.Encoder.EncodeTo(this.VariantIndex, dest)
57+
58+
if this.Day.IsSome() {
59+
prim.Encoder.EncodeTo(this.Day.Unwrap(), dest)
60+
}
61+
62+
if this.Month.IsSome() {
63+
prim.Encoder.EncodeTo(this.Month.Unwrap(), dest)
64+
}
65+
66+
if this.Year.IsSome() {
67+
prim.Encoder.EncodeTo(this.Year.Unwrap(), dest)
68+
}
69+
}
70+
71+
func (this *ComplexEnum) Decode(decoder *prim.Decoder) error {
72+
*this = ComplexEnum{}
73+
74+
if err := decoder.Decode(&this.VariantIndex); err != nil {
75+
return err
76+
}
77+
78+
switch this.VariantIndex {
79+
case 0:
80+
case 1:
81+
var t uint16
82+
if err := decoder.Decode(&t); err != nil {
83+
return err
84+
}
85+
this.Day.Set(t)
86+
case 2:
87+
var t uint8
88+
if err := decoder.Decode(&t); err != nil {
89+
return err
90+
}
91+
this.Month.Set(t)
92+
case 3:
93+
var t uint32
94+
if err := decoder.Decode(&t); err != nil {
95+
return err
96+
}
97+
this.Year.Set(t)
98+
default:
99+
return errors.New("Unknown ComplexEnum Variant Index while Decoding")
100+
}
101+
102+
return nil
103+
}
104+
105+
// ANCHOR_END: complexenum
106+
107+
func tst() {
108+
// VariantIndex out of range
109+
enum := ComplexEnum{}
110+
enum.VariantIndex = 125
111+
112+
// VariantIndex and data not matching
113+
enum.VariantIndex = 0
114+
enum.Year.Set(1990)
115+
116+
// Too many data fields are set
117+
enum.VariantIndex = 1
118+
enum.Day.Set(24)
119+
enum.Year.Set(1990)
120+
}

examples/events.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
SDK "github.com/availproject/avail-go-sdk/sdk"
77
)
88

9-
func run_events() {
9+
func Run_events() {
1010
sdk := SDK.NewSDK(SDK.LocalEndpoint)
1111

1212
acc, err := SDK.Account.Alice()

examples/mod.go

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

33
func Run() {
4-
run_block()
5-
run_events()
6-
run_account_nonce()
7-
run_data_submission()
8-
run_storage()
4+
Run_account_nonce()
5+
Run_batch()
6+
Run_block()
7+
Run_data_submission()
8+
Run_events()
9+
Run_storage()
10+
Run_rpc()
911
}

0 commit comments

Comments
 (0)