@rarcifa/cronos-evm-client
Version:
A Node.js client library for interacting with the Cronos EVM, facilitating operations on both CRC20 and CRC721 tokens.
189 lines (188 loc) • 7.67 kB
TypeScript
/**
* Configuration parameters for creating a blockchain client instance.
*
* @interface
* @property {string} endpoint - The base URL for the RPC server.
* @property {string} [apiKey] - Optional API key for accessing the RPC server if required.
*/
export interface ClientConfig {
endpoint: string;
apiKey?: string;
additionalHeaders?: Record<string, string>;
}
export type Transactions = {
/**
* Fetches the block number closest to a given timestamp.
*
* @param {number} timestamp - The timestamp to find the block for.
* @returns {Promise<number>} A promise that resolves to the block number closest to the given timestamp.
*
* @example
* const client = createClient({ endpoint: 'RPC_ENDPOINT', apiKey: 'RPC_API_KEY' });
* async function fetchBlockByTimestamp() {
* try {
* const blockNumber = await client.transactions.getBlockByTimestamp(1718196040);
* console.log('Block number:', blockNumber);
* } catch (e) {
* console.error('Error fetching block by timestamp:', e);
* }
* }
* fetchBlockByTimestamp();
*/
getBlockByTimestamp: (timestamp: number) => Promise<number>;
/**
* 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.
* @returns {Promise<string[]>} A promise that resolves to an array of unique wallet addresses involved in the transactions.
*
* @example
* const client = createClient({ endpoint: 'RPC_ENDPOINT', apiKey: 'RPC_API_KEY' });
* async function countUniqueTx() {
* try {
* const uniqueWallets = await client.transactions.countUniqueTransactions(['0xCONTRACT_ADDRESS1', '0xCONTRACT_ADDRESS2'], 1000000, 1100000, 10000);
* console.log('Unique wallets:', uniqueWallets);
* } catch (e) {
* console.error('Error counting unique transactions:', e);
* }
* }
* countUniqueTx();
*/
getUniqueTransactions: (contractAddresses: string[], startBlock: number, endBlock: number, chunkSize: number) => Promise<string[]>;
};
/**
* Represents the state of the blockchain interaction, including cached blocks and requests made.
*
* @interface
*/
export type Transaction = {
blockNumber: string;
timeStamp: string;
hash: string;
nonce: string;
blockHash: string;
transactionIndex: string;
from: string;
to: string;
value: string;
gas: string;
gasPrice: string;
isError: string;
txreceipt_status: string;
input: string;
contractAddress: string;
cumulativeGasUsed: string;
gasUsed: string;
confirmations: string;
methodId: string;
functionName: string;
};
/**
* Interface for interacting with CRC20 token methods.
*
* @interface
* @property {Function} getBalance - Fetches the balance of the main token.
* @property {Function} getBalanceOf - Fetches the balance of a specified CRC20 token.
* @property {Function} getName - Fetches the name of the CRC20 token.
* @property {Function} getSymbol - Fetches the symbol of the CRC20 token.
* @property {Function} getTotalSupply - Fetches the total supply of the CRC20 token.
*/
interface CRC20 {
/**
* Fetches the balance of an account.
*
* @param {string} accountAddress - The account address to fetch the balance from.
* @returns {Promise<string>} A promise that resolves to the main token balance of the account in Ether.
*/
getBalance: (accountAddress: string) => Promise<string>;
/**
* Fetches the token balance of an account for a specified crc20 contract.
*
* @param {string} accountAddress - The account address to fetch the balance from.
* @param {string} contractAddress - The contract address of the crc20 token.
* @returns {Promise<string>} A promise that resolves to the crc20 token balance of the account in Ether.
*/
getBalanceOf: (accountAddress: string, contractAddress: string) => Promise<string>;
/**
* Fetches the name of a crc20 token.
*
* @param {string} contractAddress - The contract address of the crc20 token.
* @returns {Promise<string>} A promise that resolves to the name of the crc20 token.
*/
getName: (contractAddress: string) => Promise<string>;
/**
* Fetches the symbol of a crc20 token.
*
* @param {string} contractAddress - The contract address of the crc20 token.
* @returns {Promise<string>} A promise that resolves to the symbol of the crc20 token.
*/
getSymbol: (contractAddress: string) => Promise<string>;
/**
* Fetches the total supply of a crc20 token.
*
* @param {string} contractAddress - The contract address of the crc20 token.
* @returns {Promise<string>} A promise that resolves to the total supply of the crc20 token.
*/
getTotalSupply: (contractAddress: string) => Promise<string>;
}
/**
* Interface for interacting with CRC721 token methods.
*
* @interface
* @property {Function} getBalanceOf - Fetches the balance of a specified CRC721 token.
* @property {Function} getOwnerOf - Fetches the owner address of a specific CRC721 token.
* @property {Function} getTokenUri - Fetches the URI pointing to the metadata of the CRC721 token.
*/
interface CRC721 {
/**
* Fetches the token balance of an account for a specified crc721 contract.
*
* @param {string} accountAddress - The account address to fetch the balance from.
* @param {string} contractAddress - The contract address of the crc721 token.
* @returns {Promise<string>} A promise that resolves to the crc721 token balance of the account.
*/
getBalanceOf: (accountAddress: string, contractAddress: string) => Promise<string>;
/**
* Fetches the owner address of a specific crc721 token from a specified contract.
*
* @param {string} contractAddress - The contract address of the crc721 token.
* @returns {Promise<string>} The owner address of the specified crc721 token.
*/
getOwnerOf: (contractAddress: string) => Promise<string>;
/**
* Fetches the URI (often a URL) that points to the metadata of the specified crc721 token.
*
* @param {string} contractAddress - The contract address of the crc721 token.
* @returns {Promise<string>} The URI of the specified crc721 token.
*/
getTokenUri: (contractAddress: string) => Promise<string>;
}
/**
* Defines the structure for a blockchain client that holds methods for interacting with both CRC20 and CRC721 tokens.
*
* @interface
* @property {CRC20} crc20 - Methods for interacting with CRC20 tokens.
* @property {CRC721} crc721 - Methods for interacting with CRC721 tokens.
*/
export interface BlockchainClient {
crc20: CRC20;
crc721: CRC721;
transactions: Transactions;
}
/**
* Creates a new client for interacting with the CRC20 contract over the Cronos blockchain.
*
* @param {ClientConfig} config - The configuration for setting up the client.
* @returns {Object} Returns an object with methods to interact with the blockchain.
*
* @example
* const cronosClient = createClient({
* endpoint: 'RPC_ENDPOINT',
* apiKey: 'RPC_API_KEY'
* });
*/
export declare const createClient: ({ endpoint, apiKey, additionalHeaders, }: ClientConfig) => BlockchainClient;
export {};