Skip to content

Instantly share code, notes, and snippets.

@drewstaylor
Last active May 12, 2023 12:44
Show Gist options
  • Save drewstaylor/541520654e77e8a083d662fe3979e84f to your computer and use it in GitHub Desktop.
Save drewstaylor/541520654e77e8a083d662fe3979e84f to your computer and use it in GitHub Desktop.
const CwStargate = require("@cosmjs/cosmwasm-stargate");
const RPC = process.env.RPC;
const CONTRACT = process.env.CONTRACT;
async function cwClient() {
const cwClient = await CwStargate.SigningCosmWasmClient.connectWithSigner(RPC, null);
return cwClient;
}
// This handler parses incoming events filters
function makeTags(oneLiner) {
return oneLiner.split("&").map((pair) => {
if (pair.indexOf("=") === -1) throw new Error("Parsing error: Equal sign missing");
const parts = pair.split("=");
if (parts.length > 2) {
throw new Error(
"Parsing error: multiple equal signs found.",
);
}
const [key, value] = parts;
if (!key) throw new Error("Parsing error: key must not be empty");
return { key, value };
});
}
// Get all txs to a contract
async function eventsOfContract() {
const client = await cwClient();
client.searchTx({
tags: makeTags(`wasm._contract_address=${CONTRACT}`),
})
.then((results) => {
console.log(results);
});
}
// Get all txs to a contract of a specific `action` type
// e.g.: eventsOfAction("mint"), eventsOfAction("burn"), etc.
async function eventsOfAction(action = null) {
if (!action) return;
const client = await cwClient();
client.searchTx({
tags: makeTags(`wasm._contract_address=${CONTRACT}&wasm.action=${action}`),
})
.then((results) => {
console.log(results);
});
}
// Get all txs to a cw721 contract involving a specific `token_id`
async function eventsOfTokenId(tokenId = null) {
if (!tokenId) return;
const client = await cwClient();
client.searchTx({
tags: makeTags(`wasm._contract_address=${CONTRACT}&wasm.token_id=${tokenId}`),
})
.then((results) => {
console.log(results);
});
}
module.exports = {
eventsOfContract: eventsOfContract,
eventsOfAction: eventsOfContract,
eventsOfTokenId: eventsOfTokenId
};
{
"name": "searchTx-test",
"version": "1.0.0",
"dependencies": {
"@cosmjs/cosmwasm-stargate": "^0.30.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment