Skip to content

Instantly share code, notes, and snippets.

@giansalex
Created August 30, 2020 22:42
Show Gist options
  • Save giansalex/bb8d043021d0395cc69c83e8e5340f2d to your computer and use it in GitHub Desktop.
Save giansalex/bb8d043021d0395cc69c83e8e5340f2d to your computer and use it in GitHub Desktop.
Listen new blocks - Tendermint - Cosmos sdk
package main
import (
"context"
"encoding/hex"
"fmt"
"log"
"time"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"github.com/tendermint/tendermint/types"
)
func main() {
client, err := rpchttp.New("tcp://127.0.0.1:26657", "/websocket")
if err != nil {
log.Fatal(err)
}
err = client.Start()
if err != nil {
log.Fatal(err)
}
defer client.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
query := "tm.event = 'NewBlock'"
txs, err := client.Subscribe(ctx, "test-client", query)
if err != nil {
log.Fatal(err)
}
for e := range txs {
switch data := e.Data.(type) {
case types.EventDataNewBlock:
fmt.Printf("Block %s - Height: %d \n", hex.EncodeToString(data.Block.Hash()), data.Block.Height)
break
}
}
}
@david-zenx
Copy link

how to listen new blocks and transaction in nest js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment