@rarcifa/cronos-evm-client
Version:
A Node.js client library for interacting with the Cronos EVM, facilitating operations on both CRC20 and CRC721 tokens.
69 lines (68 loc) • 2.86 kB
JavaScript
import { cronosInstance } from '../integrations/cronosInstance.js';
/**
* Provides utility functions for blockchain data processing.
*
* @namespace txs
*/
export const txs = {
/**
* Splits a block range into smaller chunks for easier processing.
*
* @param {number} startBlock - The starting block number.
* @param {number} endBlock - The ending block number.
* @param {number} chunkSize - The size of each chunk.
* @returns {Array<{ start: number, end: number }>} An array of block range objects.
*
* @example
* const ranges = utils.splitBlockRange(1000000, 1100000, 10000);
* console.log(ranges);
*/
splitBlockRange: (startBlock, endBlock, chunkSize) => {
const ranges = [];
for (let i = startBlock; i <= endBlock; i += chunkSize + 1) {
const end = Math.min(i + chunkSize, endBlock);
ranges.push({ start: i, end: end });
}
return ranges;
},
/**
* Counts unique transactions for a list of contract addresses within a specified block range.
*
* @param {string[]} contractAddresses - An array of contract addresses to fetch transactions for.
* @param {number} startBlock - The starting block number.
* @param {number} endBlock - The ending block number.
* @param {number} chunkSize - The size of chunks to split the block range into for fetching.
* @param {AxiosInstance} instance - The Axios instance to use for making API requests.
* @returns {Promise<string[]>} A promise that resolves to an array of unique wallet addresses involved in the transactions.
*
* @example
* const uniqueWallets = await utils.countUniqueTransactions(
* ['0xCONTRACT_ADDRESS1', '0xCONTRACT_ADDRESS2'],
* 1000000,
* 1100000,
* 10000,
* axiosInstance
* );
* console.log('Unique wallets:', uniqueWallets);
*/
getUniqueTransactions: async (contractAddresses, startBlock, endBlock, chunkSize, instance) => {
const uniqueWallets = new Set();
for (const contractAddress of contractAddresses) {
const ranges = txs.splitBlockRange(startBlock, endBlock, chunkSize);
for (const range of ranges) {
const logs = await cronosInstance.getUniqueTransactions(contractAddress, range.start, range.end, instance);
logs.forEach((log) => {
if (log.from) {
const fromAddress = log.from.toLowerCase();
uniqueWallets.add(fromAddress);
}
if (log.to) {
const toAddress = log.to.toLowerCase();
uniqueWallets.add(toAddress);
}
});
}
}
return Array.from(uniqueWallets);
},
};