centrifuge / go-substrate-rpc-client

Substrate RPC client for go aka GSRPC
Apache License 2.0
204 stars 179 forks source link

How to build event types using metadata? #264

Open csknk opened 2 years ago

csknk commented 2 years ago

Is it possible to retrieve data types for events from metadata?

The objective is to extend the types.EventRecords type for the given chain (in my case, Polkadot) to prevent unkown type errors when decoding events.

I'm attempting to list out the data types for events, and I'm unsure how si1Lookup works.

Sample code:

func TestGetEventsFromMeta(t *testing.T) {
    c, err := NewConnection("http://localhost:9934")
    assert.NoError(t, err)
    meta, err := c.getLatestMetadata()
    assert.NoError(t, err)

    for _, mod := range meta.AsMetadataV14.Pallets {
        if !mod.HasEvents {
            continue
        }
        if typ, ok := meta.AsMetadataV14.EfficientLookup[mod.Events.Type.Int64()]; ok {
            if len(typ.Def.Variant.Variants) > 0 {
                for _, vars := range typ.Def.Variant.Variants {
                    fmt.Printf("%s\t%s\n", mod.Name, vars.Name)
                    for _, field := range vars.Fields {

                        // How to decode field data here? <---------------------------------------------------
                        fmt.Printf("%#v\n", meta.AsMetadataV14.Lookup.Types[field.Type.Int64()])

                    }
                }
            }
        }
    }
}

Related to issue #220

cdamian commented 11 months ago

Hey @csknk,

Please refer to - https://github.com/centrifuge/go-substrate-rpc-client/issues/251#issuecomment-1840630293