Last active
July 10, 2024 18:52
-
-
Save alexx855/7acf2243b50058fe90d6659cc89fda35 to your computer and use it in GitHub Desktop.
axie-treasury-transfers.js
This file contains hidden or 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
import { defineChain, createPublicClient, http, erc20Abi } from 'viem' | |
const main = async () => { | |
const ronin = defineChain({ | |
id: 2020, | |
name: 'Ronin', | |
network: 'ronin', | |
nativeCurrency: { | |
decimals: 18, | |
name: 'RON', | |
symbol: 'RON' | |
}, | |
rpcUrls: { | |
public: { | |
http: ['RPC'] | |
}, | |
default: { http: ['RPC'] } | |
}, | |
blockExplorers: { | |
default: { name: 'Ronin Explorer', url: 'https://app.roninchain.com/' } | |
} | |
}) | |
const publicClient = createPublicClient({ | |
chain: ronin, | |
transport: http() | |
}) | |
const latestBlock = await publicClient.getBlock(); | |
const startBlock = 16377111n; // Example start block, replace with your start block | |
const walletAddress = "0x245db945c485b68fdc429e4f7085a1761aa4d45d"; | |
const contractAddresses = [ | |
"0xc99a6a985ed2cac1ef41640596c5a5f9f4e19ef5", // WETH | |
"0x97a9107c1793bc407d6f527b77e7fff4d812bece", // AXS | |
"0xa8754b9fa15fc18bb59458815510e40a12cd2014", // SLP | |
]; | |
if (BigInt(latestBlock.number) < startBlock) { | |
console.error(`Start block number must be less than or equal to the latest block number: ${latestBlock.number}`); | |
res.status(400).send('Invalid start block'); | |
return; | |
} | |
const transfers = []; | |
const blockRange = 5000n; | |
const filename = `transfers_${startBlock}_${latestBlock.number}.json`; | |
console.time("Total Operation Time"); | |
console.log(`Starting to fetch transfers from block ${startBlock} to ${latestBlock.number}`); | |
for (let blockNumber = startBlock;blockNumber <= BigInt(latestBlock.number);blockNumber += blockRange) { | |
let endBlock = blockNumber + blockRange - 1n; | |
if (endBlock > BigInt(latestBlock.number)) { | |
endBlock = BigInt(latestBlock.number); | |
} | |
console.log(`Fetching logs for blocks ${blockNumber} to ${endBlock}`); | |
console.time(`Fetching logs Time for blocks ${blockNumber} to ${endBlock}`); | |
const logs = await publicClient.getContractEvents({ | |
address: contractAddresses, // Assuming getContractEvents can accept multiple addresses | |
abi: erc20Abi, | |
eventName: 'Transfer', | |
fromBlock: blockNumber, | |
toBlock: endBlock | |
}); | |
console.timeEnd(`Fetching logs Time for blocks ${blockNumber} to ${endBlock}`); | |
logs.forEach(log => { | |
const from = log.topics[1]; | |
const to = log.topics[2]; | |
if (from === walletAddress || to === walletAddress) { | |
transfers.push({ | |
from, | |
to, | |
data: log.data, | |
parsed: log.data, | |
tokenAddress: log.address, | |
transactionHash: log.transactionHash, | |
block: blockNumber | |
}); | |
} | |
}); | |
console.log(`Completed fetching logs for blocks ${blockNumber} to ${endBlock}. Found ${logs.length} logs.`); | |
} | |
try { | |
const encodedTransfers = JSON.stringify(transfers); | |
fs.writeFileSync(filename, encodedTransfers); | |
console.log(`Transfers saved to ${filename}`); | |
res.send(`Transfers saved to ${filename}`); | |
} catch (error) { | |
console.error(`Failed to write to file: ${error}`); | |
} | |
console.timeEnd("Total Operation Time"); | |
} | |
main().then(() => { | |
console.log('Done'); | |
}).catch(error => { | |
console.error(`Error: ${error}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment