aphoh / go-substrate-gen Goto Github PK
View Code? Open in Web Editor NEWhttps://github.com/Aphoh/go-substrate-gen
https://github.com/Aphoh/go-substrate-gen
Currently, variants are constructed by embedding every variant into a single parent struct.
In the case of the runtime calls, this can result in some incredibly large structs, as the runtime call struct contains every single field of every single possible call that could be constructed.
As a future TODO, update this struct embedding to store booleans for what variety it is, and a pointer to each different struct for memory efficiency.
Hey, this library seems to be a godsend if you actually want to use gsrpc. Is it possible you could show an example of how to decode all the events in a block. Can't seem to figure it out. Thanks!
*sorry if this is a dumb question
When I'm trying use this on historical metadata of a chain, it does not work:
error generating calls for pallet System: no call type found. Expected a path like *_runtime::RuntimeCall
How to reproduce:
curl -L -X POST -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "state_getMetadata", "params": ["0x6c51377632106ddba379416f2ef5e24450e90cee8c78a70daa262b2ae6f27e93"]}' https://rpc.polkadot.io > polkadot-meta.json
Note: I've tried this on two chains already.
This type currently has garbage metadata. It should either be a []byte
or a pointer to it's second field element. It has custom scale encoding, so that needs to be replicated here.
Currently, go-substrate-gen
encodes variants pretty inefficiently by having an empty entry in every variant struct. This could be solved by having methods that return a struct containing each variant's information, and only storing a uint8
and the actual information of the variant in go
. It would look something like the below (omitting the err != nil
checks for brevity)
type VariantXYZ struct {
index uint8
data interface{}
}
func (ty VariantXYZ) Encode(encoder scale.Encoder) (err error) {
err = encoder.PushByte(ty.index)
switch ty.index {
case 0:
err = encode.Encode(ty.data.(X))
return err
case 1:
err = encode.Encode(ty.data.(Y))
return err
case 2:
err = encode.Encode(ty.data.(Z))
return err
}
return fmt.Errorf("Unrecognized variant")
}
func (ty *VariantXYZ) Decode(decoder scale.Decoder) (err error) {
variant, err := decoder.ReadOneByte()
ty.variant = variant
switch variant {
case 0:
var tmp X
err = decoder.Decode(&tmp)
ty.data = &tmp
return
case 1:
var tmp Y
err = decoder.Decode(&tmp)
ty.data = &tmp
return
case 2:
var tmp Z
err = decoder.Decode(&tmp)
ty.data = &tmp
return
}
}
func (ty *VariantXYZ) IsX() bool {
return ty.index == 0
}
func (ty *VariantXYZ) IsY() bool {
return ty.index == 1
}
func (ty *VariantXYZ) IsZ() bool {
return ty.index == 2
}
// Optionally these could just use go semantics and return the zero struct if it fails for easier chaining
func (ty *VariantXYZ) X() (d X, err error) {
if !ty.IsX() {
err = fmt.Errorf("Variant is not X")
}
return ty.data.(X), nil
}
func (ty *VariantXYZ) Y() (d Y, err error) {
if !ty.IsY() {
err = fmt.Errorf("Variant is not Y")
}
return ty.data.(Y), nil
}
func (ty *VariantXYZ) X() (d Z, err error) {
if !ty.IsZ() {
err = fmt.Errorf("Variant is not Z")
}
return ty.data.(X), nil
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.