loom-sdk-documentation
loom-sdk-documentation copied to clipboard
update go-events.md
update go-events.md
So far so good, can we additionally add some golang and js examples of polling and watching events?
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)
}
}
Ref PR: https://github.com/loomnetwork/go-loom/pull/383
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.
Change as per above comment
Updated the example. @pathornteng can you help with these:
- 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.
@pathornteng Could you help?
Not super high priority but when we get a chance lets see if we can fix this