From 1027068f8f4e1163b9d03db8616d9f0f29fa9750 Mon Sep 17 00:00:00 2001 From: Andrei Popescu Date: Tue, 21 May 2019 16:46:08 +0300 Subject: [PATCH 1/4] update go-events.md update go-events.md --- docs/en/go-events.md | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/docs/en/go-events.md b/docs/en/go-events.md index d0c437b38..7736586ad 100644 --- a/docs/en/go-events.md +++ b/docs/en/go-events.md @@ -4,16 +4,43 @@ 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). \ No newline at end of file From f4fc1f4de8a78b71585c434b36aa230ec249c651 Mon Sep 17 00:00:00 2001 From: Andrei Popescu Date: Wed, 29 May 2019 07:43:30 +0300 Subject: [PATCH 2/4] added go and js examples added go and js examples --- docs/en/go-events.md | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/docs/en/go-events.md b/docs/en/go-events.md index 7736586ad..3fa804243 100644 --- a/docs/en/go-events.md +++ b/docs/en/go-events.md @@ -43,4 +43,41 @@ EventStore: DBBackend: "goleveldb" ``` -Finally, you can query the events using [something like this](https://plasma.dappchains.com/query/contractevents?fromBlock=5216332&toBlock=5216352). \ No newline at end of file +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: + +```golang +func (c *DAppChainRPCClient) GetContractEvents(fromBlock, toBlock uint64, contractName string) (ptypes.ContractEventsResult, error) { + var result ptypes.ContractEventsResult + params := map[string]interface{}{ + "fromBlock": strconv.FormatUint(fromBlock, 10), + "toBlock": strconv.FormatUint(toBlock, 10), + "contract": contractName, + } + if err := c.queryClient.Call("contractevents", params, c.getNextRequestID(), &result); err != nil { + return result, err + } + return result, nil +} +``` + +## 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). \ No newline at end of file From 4faabe1a70b8864123a01cec9f3b0655b4e9754e Mon Sep 17 00:00:00 2001 From: Andrei Popescu Date: Wed, 29 May 2019 11:59:14 +0300 Subject: [PATCH 3/4] updating the go example updating the go example --- docs/en/go-events.md | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/docs/en/go-events.md b/docs/en/go-events.md index 3fa804243..ba406c6e9 100644 --- a/docs/en/go-events.md +++ b/docs/en/go-events.md @@ -49,18 +49,25 @@ Finally, you can query the events using [something like this](https://plasma.dap Use the following example to query and decode events in Go: -```golang -func (c *DAppChainRPCClient) GetContractEvents(fromBlock, toBlock uint64, contractName string) (ptypes.ContractEventsResult, error) { - var result ptypes.ContractEventsResult - params := map[string]interface{}{ - "fromBlock": strconv.FormatUint(fromBlock, 10), - "toBlock": strconv.FormatUint(toBlock, 10), - "contract": contractName, - } - if err := c.queryClient.Call("contractevents", params, c.getNextRequestID(), &result); err != nil { - return result, err +```go +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) } - return result, nil } ``` From fb6ad202000cdc4af72f9cb4aaf9ee53d1f7055c Mon Sep 17 00:00:00 2001 From: Andrei Popescu Date: Wed, 29 May 2019 19:26:46 +0300 Subject: [PATCH 4/4] updated the go example updated the go example --- docs/en/go-events.md | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/docs/en/go-events.md b/docs/en/go-events.md index ba406c6e9..533996a56 100644 --- a/docs/en/go-events.md +++ b/docs/en/go-events.md @@ -50,23 +50,35 @@ Finally, you can query the events using [something like this](https://plasma.dap 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 } -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 { +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) + + } + } } ```