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

update go-events.md #214

Open
wants to merge 4 commits into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 87 additions & 4 deletions docs/en/go-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,99 @@ title: Go Event Indexing
sidebar_label: Go Event Indexing
---

Previously events emitted in Go would only go out to the websocket. Now they can be stored in a local database or redis also. Below is the config to store the events
## Go Event Indexing

Previously, events emitted in Go would only go out to the websocket. Now, they can be stored in a local database or Redis also.

## Enabling the Event Store

Here's a template you can use to enable the Event Store:

```yaml
EventStore:
DBName: {{.EventStore.DBName}} // Set DB name (can be anything, default is events)
DBBackend: {{.EventStore.DBBackend}} // Set DB type 'goleveldb' or 'cleveldb'
{{end}}

EventDispatcher:
Dispatcher: {{.EventDispatcher.Dispatcher}} // Available dispatcher: "db_indexer" | "log" | "redis"
# Redis will be use when Dispatcher is "redis"
Redis:
URI: "{{.EventDispatcher.Redis.URI}}"// Redis URI
```

## Example

As an example, to store the events in Redis, you should add the following to your `loom.yml` file:

## Event Store

```yaml
EventDispatcher:
# Available dispatcher: "db_indexer" | "log" | "redis"
Dispatcher: "db_indexer"
# Redis will be use when Dispatcher is "redis"
# Redis will be used when Dispatcher is "redis"
Redis:
URI: "redis_url_here"
URI: "redis_url_here"

EventStore:
DBName: "event"
DBBackend: "goleveldb"
```

Finally, you can query the events using [something like this](https://plasma.dappchains.com/query/contractevents?fromBlock=5216332&toBlock=5216352).

## Querying and Decoding Events in Go

Use the following example to query and decode events in Go:

```go
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)

}

}
}
```

## Querying and Decoding Events in JavaScript

To query and decode events you can use something like the following:


```js
this.simpleStoreInstance.events.NewValueSet({ filter: { _value: 10 }}, (err, event) => {
if (err) console.error('Error on event', err)
else {
if (this.onEvent) {
this.onEvent(event.returnValues)
}
}
})
```

For a simple example of Truffle interacting with Loom DappChain, check out [this repo](https://github.com/loomnetwork/truffle-dappchain-example).