centrifuge / go-substrate-rpc-client

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

Trying to decode EventRecords failed on testnet : field Indices_IndexAssigned: expected more bytes #317

Open bugbuilder opened 1 year ago

bugbuilder commented 1 year ago

When I tried to parse events records to get information about balance transfers its failed on tesnet (rococo and wested) but on mainnet works.

panic: unable to decode field 3 event #1 with EventID [3 0], field Indices_IndexAssigned: expected more bytes, but could not decode any more

There is a workaround or alternative to have the same code working on mainnet and testnet?

./main wss://rpc.polkadot.io <-- works
./main wss://rococo-rpc.polkadot.io <-- it doesn’t work
./main wss://westend-rpc.polkadot.io <-- it doesn’t work
package main

import (
    "os"

    gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
    "github.com/centrifuge/go-substrate-rpc-client/v4/types"
)

func main() {
    rpc := os.Args[1]
    api, err := gsrpc.NewSubstrateAPI(rpc)
    checkErr(err)

    meta, err := api.RPC.State.GetMetadataLatest()
    checkErr(err)

    head, err := api.RPC.Chain.GetFinalizedHead()
    checkErr(err)

    key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil)
    checkErr(err)

    raw, err := api.RPC.State.GetStorageRaw(key, head)
    checkErr(err)

    events := types.EventRecords{}
    err = types.EventRecordsRaw(*raw).DecodeEventRecords(meta, &events)
    checkErr(err)
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}
cdamian commented 11 months ago

Hey @bugbuilder,

We added a way to dynamically decode events. You can find an example here - https://github.com/centrifuge/go-substrate-rpc-client/blob/master/registry/retriever/event_retriever_live_test.go#L76