Aphoh / go-substrate-gen

https://github.com/Aphoh/go-substrate-gen

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can you show an example of decoding events?

DragonDmoney opened this issue · comments

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

Glad to see the library getting some looks!

Assuming that you've generated the code for your node, and placed it under ./runtime in your repository, the rough codeflow to do this would look like the below. Of course, you'd generally share the api among different functions.

Do note that the current state of go-substrate-gen makes these events very large, as all events get embedded in one of these structs, so it's not yet particularly performant. That will eventually be fixed, but not super high priority for me yet.

import (
    gsrpc "github.com/centrifuge/go-susbtrate-rpc-client/v4"
    "github.com/centrifuge/go-susbtrate-rpc-client/v4/types"
    mytypes "path/to/my/repo/runtime/types"
    "path/to/my/repo/runtime/system"
)

func GetBlockEvents(blockHash types.Hash) ([]mytypes.EventRecord, error) {
    // url is the connection url
    api, err := gsrpc.NewSubstrateAPI(url)
    if err != nil {
        return err
    }
    
    return system.GetEvents(api.RPC.State, blockHash)
}

@DragonDmoney Were you able to figure this out with @elv-nate 's help?

Yes, forgot to close the issue. Thanks for the help!