Last active
March 19, 2025 14:06
-
-
Save drewstaylor/541520654e77e8a083d662fe3979e84f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | |
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( | |
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( | |
makeTags(`wasm._contract_address=${CONTRACT}&wasm.token_id=${tokenId}`), | |
) | |
.then((results) => { | |
console.log(results); | |
}); | |
} | |
module.exports = { | |
eventsOfContract: eventsOfContract, | |
eventsOfAction: eventsOfContract, | |
eventsOfTokenId: eventsOfTokenId | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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