loom-sdk-documentation icon indicating copy to clipboard operation
loom-sdk-documentation copied to clipboard

update go-events.md

Open andreipope opened this issue 5 years ago • 7 comments

update go-events.md

andreipope avatar May 21 '19 13:05 andreipope

So far so good, can we additionally add some golang and js examples of polling and watching events?

gakonst avatar May 21 '19 17:05 gakonst

I just create a new method GetContractEvents in go-loom. It is not done yet. But once it is merged, the following is the example of how to query and decode events

type MyEvent struct {
	Owner  string
	Method string
	Addr   []byte
}

rpcClient := client.NewDAppChainRPCClient("default", "http://plasma.dappchains.com:80/rpc", "http://plasma.dappchains.com:80/query")
fromBlock := uint64(5216300)
toBlock := uint64(5216320)
result, err := rpcClient.GetContractEvents(fromBlock, toBlock, "")
if err != nil {
	panic(err)
}
for _, event := range result.Events {
	var decodedEvent MyEvent
	if err := json.Unmarshal(event.EncodedBody, &decodedEvent); err != nil {
		panic(err)
	}
}

pathornteng avatar May 22 '19 10:05 pathornteng

Ref PR: https://github.com/loomnetwork/go-loom/pull/383

pathornteng avatar May 22 '19 10:05 pathornteng

Good work so far! It'd be better if you could include a full code snippet that compiles, so change the example to:

package main

import (
	"encoding/json"
	"github.com/loomnetwork/go-loom/client"
)

type MyEvent struct {
	Owner  string
	Method string
	Addr   []byte
}

func main() {
	rpcClient := client.NewDAppChainRPCClient("default", "http://plasma.dappchains.com:80/rpc", "http://plasma.dappchains.com:80/query")
	fromBlock := uint64(5216300)
	toBlock := uint64(5216320)
	result, err := rpcClient.GetContractEvents(fromBlock, toBlock, "")
	if err != nil {
		panic(err)

	}
	for _, event := range result.Events {
		var decodedEvent MyEvent
		if err := json.Unmarshal(event.EncodedBody, &decodedEvent); err != nil {
			panic(err)

		}

	}
}

2 things:

  • Can we somehow filter for events that only come from a specific contract?
  • Your example also panics, so may want to give an example which provides some more intuitive result.

gakonst avatar May 29 '19 13:05 gakonst

Change as per above comment

Updated the example. @pathornteng can you help with these:

  1. Can we somehow filter for events that only come from a specific contract?
  2. Your example also panics, so may want to give an example which provides some more intuitive result.

andreipope avatar May 29 '19 16:05 andreipope

@pathornteng Could you help?

andreipope avatar Jul 09 '19 20:07 andreipope

Not super high priority but when we get a chance lets see if we can fix this

mattkanwisher avatar Aug 16 '19 13:08 mattkanwisher