You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// This should be handling raw events and should get meta from the chain to be able to parse events correctly
func processBlockEvents2(ctx context.Context, api *gsrpc.SubstrateAPI, ret retriever.EventRetriever, blockNumber gstypes.BlockNumber) {
h, err := api.RPC.Chain.GetBlockHash(uint64(blockNumber))
if err != nil {
log.Printf("Couldn't retrieve block hash for block number %d: %s\n", blockNumber, err)
return
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
log.Fatal(err)
}
f := registry.NewFactory()
ereg, err := f.CreateEventRegistry(meta)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Event Registry: %+v\n", ereg)
// Create the storage key for the System.Events
key, err := gstypes.CreateStorageKey(meta, "System", "Events", nil, nil)
if err != nil {
log.Fatal("Failed to create storage key for System.Events:", err)
}
// Fetch the events data for the specified block
rawEvents, err := api.RPC.State.GetStorageRaw(key, h)
if err != nil {
log.Fatal("Failed to get storage raw for key:", err)
}
// Decode the events
events := gstypes.EventRecords{}
if err = gstypes.EventRecordsRaw(*rawEvents).DecodeEventRecords(meta, &events); err != nil {
log.Fatal("Failed to decode event records:", err)
}
//Failed to decode event records:unable to find field Cards_CardRedeemed for event #4 with EventID [40 4]
//exit status 1
for _, event := range events.Balances_Transfer {
fmt.Printf("Balances.Transfer Event: %+v\n", event)
}
}
As can be seen in the example above, I am creating a registry based on the meta I get from the chain:
f := registry.NewFactory()
ereg, err := f.CreateEventRegistry(meta)
if err != nil {
log.Fatal(err)
}
Now if I wanted to use this to actually decode the events coming from the chain, how would I do that?
I have also tried
func processBlockEvents2(ctx context.Context, api *gsrpc.SubstrateAPI, ret retriever.EventRetriever, blockNumber gstypes.BlockNumber) {
h, err := api.RPC.Chain.GetBlockHash(uint64(blockNumber))
if err != nil {
log.Printf("Couldn't retrieve block hash for block number %d: %s\n", blockNumber, err)
return
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
log.Fatal(err)
}
f := registry.NewFactory()
ereg, err := f.CreateEventRegistry(meta)
if err != nil {
log.Fatal(err)
}
ep := parser.NewEventParser()
// Create the storage key for the System.Events
key, err := gstypes.CreateStorageKey(meta, "System", "Events", nil, nil)
if err != nil {
log.Fatal("Failed to create storage key for System.Events:", err)
}
// Fetch the events data for the specified block
rawEvents, err := api.RPC.State.GetStorageRaw(key, h)
if err != nil {
log.Fatal("Failed to get storage raw for key:", err)
}
// Decode the events
events, err := ep.ParseEvents(ereg, rawEvents)
if err != nil {
log.Fatal(err)
}
for _, event := range events {
fmt.Printf("Event: %+v\n", event)
}
}
I am wondering how I can handle reading custom events from a chain using this client.
I have looked at
go-substrate-rpc-client/registry/factory.go
Line 165 in 8f01d19
I then looked at https://github.com/centrifuge/pod/blob/main/centchain/api.go#L529 which was referenced in another issue as an example for how to use the registry above but I'm not understanding how exactly it is supposed to work.
As can be seen in the example above, I am creating a registry based on the meta I get from the chain:
Now if I wanted to use this to actually decode the events coming from the chain, how would I do that?
I have also tried
I end up getting
How would I decode these SCALE fields so I can actually read it?
The text was updated successfully, but these errors were encountered: