Skip to content

Instantly share code, notes, and snippets.

@the-frey
Forked from giansalex/listen-cw20-transfer.go
Created June 24, 2022 08:50
Show Gist options
  • Save the-frey/2e0ccba7a51ffed515fd6a1366c81a50 to your computer and use it in GitHub Desktop.
Save the-frey/2e0ccba7a51ffed515fd6a1366c81a50 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