Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading Custom Events #376

Open
danbrown1893 opened this issue Apr 10, 2024 · 1 comment
Open

Reading Custom Events #376

danbrown1893 opened this issue Apr 10, 2024 · 1 comment

Comments

@danbrown1893
Copy link

danbrown1893 commented Apr 10, 2024

I am wondering how I can handle reading custom events from a chain using this client.

I have looked at

func (f *factory) CreateEventRegistry(meta *types.Metadata) (EventRegistry, error) {
which seems to create a custom event registry based on the metadata from the chain.

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.

// 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 end up getting

Event: &{Name:Balances.Transfer Fields:[0x1400060e030 0x1400060e090 0x1400060e0c0] EventID:[10 2] Phase:0x14000524050 Topics:[]}

How would I decode these SCALE fields so I can actually read it?

@khssnv
Copy link

khssnv commented May 8, 2024

Hi, try ProcessDecodedFieldValue and GetDecodedFieldAsType helpers, you may find how to use them here: #370.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants