UNPKG

@rarcifa/cronos-evm-client

Version:

A Node.js client library for interacting with the Cronos EVM, facilitating operations on both CRC20 and CRC721 tokens.

55 lines (54 loc) 2.38 kB
/** * ethMethods integration for managing Ethereum RPC requests. * * @fileoverview This file provides helper functions for Ethereum JSON-RPC interactions. * @namespace ethMethods */ export const cronosInstance = { /** * Fetches block information for a given block number. * * @param {number | string} blockNumber - The block number to fetch. * @param {AxiosInstance} ethereumInstance - The Axios instance for making requests. * @returns {Promise<{ number: string; timestamp: string }>} A promise that resolves to the block information. * * @example * const blockInfo = await erc20.getBlockByNumber(123456, ethereumInstance); */ getUniqueTransactions: async (address, startBlock, endBlock, instance) => { const url = `/api?module=account&action=txlist&address=${address}&startblock=${startBlock}&endblock=${endBlock}&page=1&offset=10000&sort=asc`; try { const response = await instance.get(url); if (response.data.message !== 'OK') { console.error(`[cronosInstance/getUniqueTransactions] error for ${address}: ${response.data.message}`); return []; } const result = response.data.result; return result; } catch (e) { console.error('[cronosInstance/getUniqueTransactions] error:', e); throw e; } }, getBlockByTimestamp: async (timestamp, instance) => { const url = `/api?module=block&action=getblocknobytime&timestamp=${timestamp}&closest=before`; try { const response = await instance.get(url); if (response.data.error) { console.log('[erc20/getBlockByTimestamp] error:', response.data.error.message); throw new Error(`[erc20/getBlockByTimestamp] error: ${response.data.error.message}`); } if (!response.data.result) { console.error('[cronosInstance/getBlockByTimestamp] error: No result returned'); throw new Error('[cronosInstance/getBlockByTimestamp] error: No result returned'); } const result = Number(response.data.result); return result; } catch (e) { console.error('[cronosInstance/getBlockByTimestamp] error:', e); throw e; } }, };