Skip to content

Instantly share code, notes, and snippets.

@raddy
Last active June 22, 2022 02:46
Show Gist options
  • Save raddy/800247131bbfa1feae29e892f263e352 to your computer and use it in GitHub Desktop.
Save raddy/800247131bbfa1feae29e892f263e352 to your computer and use it in GitHub Desktop.
Fetch logs from graphql endpoint
const { ethers, utils } = require("ethers");
const { request, gql } = require("graphql-request");
const { program } = require("commander");
const query = gql`
query blockLogs($start : Long, $end : Long, $addresses : [Address!], $topics: [[Bytes32!]!]) {
logs(filter: {fromBlock: $start, toBlock: $end, addresses: $addresses, topics: $topics}) {
account {
address
}
topics
data
transaction {
hash
}
}
}`;
const fetch = async(
start,
end,
address,
topic,
rpc
) => {
const startBlock = utils.hexlify(+start);
const endBlock = utils.hexlify(+end);
let vars = { start: startBlock, end: endBlock};
if (address) vars.addresses = [address];
if (topic) vars.topics = [[topic]];
const resp = await request(rpc, query, vars);
console.log(JSON.stringify(resp));
};
function common_functions(name) {
const ln = name.toLowerCase();
if (ln == "transfer") {
return utils.keccak256(utils.toUtf8Bytes("Transfer(address,address,uint256)"));
}
if (ln == "approve") {
return utils.keccak256(utils.toUtf8Bytes("Approval(address,address,uint256)"));
}
if (ln == "univ2_sync") {
return utils.keccak256(utils.toUtf8Bytes("Sync(uint112,uint112)"));
}
if (ln == "univ2_swap") {
return utils.keccak256(utils.toUtf8Bytes("Swap(address,uint256,uint256,uint256,uint256,address)"));
}
if (ln == "univ2_swap") {
return utils.keccak256(utils.toUtf8Bytes("Swap(address,uint256,uint256,uint256,uint256,address)"));
}
if (ln == "univ3_swap") {
return utils.keccak256(utils.toUtf8Bytes("Swap(address,address,int256,int256,uint160,uint128,int24)"));
}
if (ln == "opensea_match") {
return utils.keccak256(utils.toUtf8Bytes("OrdersMatched(bytes32,bytes32,address,address,uint256,bytes32)"));
}
if (ln == "celer_relay") {
return utils.keccak256(utils.toUtf8Bytes("Relay(bytes32,address,address,address,uint256,uint64,bytes32)"));
}
}
const main = async () => {
program
.argument("<fromBlock>", "Starting Block")
.argument("<toBlock>", "Ending Block")
.option("--address <address>", "Address Filter")
.option("--topic <topic>", "Topic Filter (First Position)")
.option("--rpc <url>", "GRAPHQL RPC URL", "http://localhost:8545/graphql")
.option("--function <func>", "Function Name + Args")
.option("--common <name>", "Common Operations (Transfer)")
.action(async (fromBlock, toBlock, options) => {
let topic = options.topic;
if (options.function && !topic) {
topic = utils.keccak256(utils.toUtf8Bytes(options.function));
}
if (options.common && !topic) {
topic = common_functions(options.common);
}
console.log(topic);
await fetch(fromBlock, toBlock, options.address, topic, options.rpc);
});
program.parseAsync();
}
main().catch((e) => console.error(e.toString()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment