Skip to content

Commit

Permalink
Added documentation for enum and rpcs. Added more rpcs, added logger (#…
Browse files Browse the repository at this point in the history
…16)

* Added documentation for enum and rpcs. Added more rpcs

* Added logging

* Added timestamp to logger
  • Loading branch information
markopoloparadox authored Jan 24, 2025
1 parent 0f1d734 commit 40d63bf
Show file tree
Hide file tree
Showing 24 changed files with 625 additions and 55 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,23 @@ This project uses [GitHub Flow](https://www.alexhyett.com/git-flow-github-flow/)

# Documentation
[Link](https://availproject.github.io/avail-go-sdk/) to documentation (web preview of examples)


# Logging
To enable logging add this to the `main` function.

```go
// Set log level based on the environment variable
level, err := logrus.ParseLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
level = logrus.InfoLevel // Default to INFO if parsing fails
}
logrus.SetLevel(level)
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
```

And run go command with `LOG_LEVEL` set to debug

```bash
LOG_LEVEL=debug go run .
```
2 changes: 2 additions & 0 deletions documentation/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

- [Start](./start.md)
- [Installation](./installation.md)
- [Enum](./enum.md)
- [Account Nonce](./account_nonce.md)
- [Block](./block.md)
- [Data Submission](./data_submission.md)
- [Events](./events.md)
- [Storage](./storage.md)
- [Batch](./batch.md)
- [RPC](./rpc.md)

63 changes: 63 additions & 0 deletions documentation/src/enum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Enum

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.

## Simple Enums
Example:
```go
{{#include ./../../examples/enum.go:simpleenum}}
```

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.

In order to set the enum to a variant, we just need to set `VariantIndex` to the desired and correct value.

Example of correct setup:
```go
enum := SimpleEnum{}
enum.VariantIndex = 1
```

Example of incorrect setup:
```go
// VariantIndex out of range
enum := SimpleEnum{}
enum.VariantIndex = 200
```

## Complex Enums
Example:
```go
{{#include ./../../examples/enum.go:complexenum}}
```

When at least one variant has additional data attach to it, we are forced to created on our encode and decode methods.

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.

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.

Example of correct setup:
```go
enum := ComplexEnum{}
enum.VariantIndex = 2
enum.Month.Set(12)
```

Example of incorrect setup:
```go
// VariantIndex out of range
enum := ComplexEnum{}
enum.VariantIndex = 125

// VariantIndex and data not matching
enum.VariantIndex = 0
enum.Year.Set(1990)

// Too many data fields are set
enum.VariantIndex = 1
enum.Day.Set(24)
enum.Year.Set(1990)
```

There isn't much room for errors in the Decode method unless the devs messed it up.
2 changes: 1 addition & 1 deletion documentation/src/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module mymodule

go 1.23.4

require github.com/availproject/avail-go-sdk v0.2.0-rc3
require github.com/availproject/avail-go-sdk v0.2.0-rc4
```

3. Fetch dependencies:
Expand Down
5 changes: 5 additions & 0 deletions documentation/src/rpc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# RPC

```go
{{#include ./../../examples/rpc.go}}
```
7 changes: 0 additions & 7 deletions examples/README

This file was deleted.

2 changes: 1 addition & 1 deletion examples/account_nonce.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
SDK "github.com/availproject/avail-go-sdk/sdk"
)

func run_account_nonce() {
func Run_account_nonce() {
sdk := SDK.NewSDK(SDK.LocalEndpoint)

nonce, err := sdk.Client.Rpc.System.AccountNextIndex("5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY")
Expand Down
2 changes: 1 addition & 1 deletion examples/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/itering/scale.go/utiles/uint128"
)

func run_batch() {
func Run_batch() {
sdk := SDK.NewSDK(SDK.LocalEndpoint)

// Use SDK.Account.NewKeyPair("Your key") to use a different account than Alice
Expand Down
2 changes: 1 addition & 1 deletion examples/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
SDK "github.com/availproject/avail-go-sdk/sdk"
)

func run_block() {
func Run_block() {
sdk := SDK.NewSDK(SDK.LocalEndpoint)

acc, err := SDK.Account.Alice()
Expand Down
2 changes: 1 addition & 1 deletion examples/data_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
SDK "github.com/availproject/avail-go-sdk/sdk"
)

func run_data_submission() {
func Run_data_submission() {
sdk := SDK.NewSDK(SDK.LocalEndpoint)

// Use SDK.Account.NewKeyPair("Your key") to use a different account than Alice
Expand Down
120 changes: 120 additions & 0 deletions examples/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package examples

import (
"errors"
"fmt"

prim "github.com/availproject/avail-go-sdk/primitives"
)

// ANCHOR: simpleenum
type SimpleEnum struct {
VariantIndex uint8
}

func (this SimpleEnum) ToString() string {
switch this.VariantIndex {
case 0:
return "Nothing"
case 1:
return "Day"
case 2:
return "Month"
case 3:
return "Year"
default:
panic("Unknown SimpleEnum Variant Index")
}
}

// ANCHOR_END: simpleenum

// ANCHOR: complexenum
type ComplexEnum struct {
VariantIndex uint8
Day prim.Option[uint16]
Month prim.Option[uint8]
Year prim.Option[uint32]
}

func (this ComplexEnum) ToString() string {
switch this.VariantIndex {
case 0:
return "Nothing"
case 1:
return fmt.Sprintf("Set: %v", this.Day.Unwrap())
case 2:
return fmt.Sprintf("Set: %v", this.Month.Unwrap())
case 3:
return fmt.Sprintf("Set: %v", this.Year.Unwrap())
default:
panic("Unknown ComplexEnum Variant Index")
}
}

func (this *ComplexEnum) EncodeTo(dest *string) {
prim.Encoder.EncodeTo(this.VariantIndex, dest)

if this.Day.IsSome() {
prim.Encoder.EncodeTo(this.Day.Unwrap(), dest)
}

if this.Month.IsSome() {
prim.Encoder.EncodeTo(this.Month.Unwrap(), dest)
}

if this.Year.IsSome() {
prim.Encoder.EncodeTo(this.Year.Unwrap(), dest)
}
}

func (this *ComplexEnum) Decode(decoder *prim.Decoder) error {
*this = ComplexEnum{}

if err := decoder.Decode(&this.VariantIndex); err != nil {
return err
}

switch this.VariantIndex {
case 0:
case 1:
var t uint16
if err := decoder.Decode(&t); err != nil {
return err
}
this.Day.Set(t)
case 2:
var t uint8
if err := decoder.Decode(&t); err != nil {
return err
}
this.Month.Set(t)
case 3:
var t uint32
if err := decoder.Decode(&t); err != nil {
return err
}
this.Year.Set(t)
default:
return errors.New("Unknown ComplexEnum Variant Index while Decoding")
}

return nil
}

// ANCHOR_END: complexenum

func tst() {
// VariantIndex out of range
enum := ComplexEnum{}
enum.VariantIndex = 125

// VariantIndex and data not matching
enum.VariantIndex = 0
enum.Year.Set(1990)

// Too many data fields are set
enum.VariantIndex = 1
enum.Day.Set(24)
enum.Year.Set(1990)
}
2 changes: 1 addition & 1 deletion examples/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
SDK "github.com/availproject/avail-go-sdk/sdk"
)

func run_events() {
func Run_events() {
sdk := SDK.NewSDK(SDK.LocalEndpoint)

acc, err := SDK.Account.Alice()
Expand Down
12 changes: 7 additions & 5 deletions examples/mod.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package examples

func Run() {
run_block()
run_events()
run_account_nonce()
run_data_submission()
run_storage()
Run_account_nonce()
Run_batch()
Run_block()
Run_data_submission()
Run_events()
Run_storage()
Run_rpc()
}
Loading

0 comments on commit 40d63bf

Please sign in to comment.