Skip to content

Instantly share code, notes, and snippets.

@giansalex
Last active June 24, 2022 08:50
Show Gist options
  • Save giansalex/44ec1848124ca0b531792c679e38591b to your computer and use it in GitHub Desktop.
Save giansalex/44ec1848124ca0b531792c679e38591b to your computer and use it in GitHub Desktop.
Listen cw20 transactions (Cosmwasm v1)
package main
import (
"context"
"fmt"
"log"
"time"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
)
func main() {
client, err := rpchttp.New("tcp://127.0.0.1:26657", "/websocket")
cw20Contract := "juno1suhgf5svhu4usrurvxzlgn54ksxmn8gljarjtxqnapv8kjnp4nrsf8smqw"
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 := fmt.Sprintf("message.module='wasm' AND wasm._contract_address='%s' AND wasm.action='transfer'", cw20Contract)
txs, err := client.Subscribe(ctx, "wasm-client", query)
if err != nil {
log.Fatal(err)
}
for e := range txs {
from := e.Events["wasm.from"][0]
to := e.Events["wasm.to"][0]
amount := e.Events["wasm.amount"][0]
fmt.Println("Tx: " + e.Events["tx.hash"][0])
fmt.Printf("Sender: %s \nRecipient: %s \nAmount: %s \n", from, to, amount)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment