@rarcifa/cronos-evm-client
Version:
A Node.js client library for interacting with the Cronos EVM, facilitating operations on both CRC20 and CRC721 tokens.
273 lines (272 loc) • 11.9 kB
JavaScript
import { crc20 } from '../../integrations/crc20.js';
import { crc721 } from '../..//integrations/crc721.js';
import axios from 'axios';
import { cronosInstance } from '../../integrations/cronosInstance.js';
import { txs } from '../../utils/txs.js';
/**
* 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 const createClient = ({ endpoint, apiKey, additionalHeaders = {}, }) => {
const instance = axios.create({
baseURL: endpoint,
headers: {
...additionalHeaders,
...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
},
params: {
apikey: apiKey,
},
});
return {
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.
*
* @example
* const client = createClient({
* endpoint: 'RPC_ENDPOINT',
* apiKey: 'RPC_API_KEY'
* });
*
* async function getBalance() {
* try {
* const balance = await client.crc20.getBalance('0xACCOUNT_ADDRESS');
* console.log('Balance in Ether:', balance);
* } catch (e) {
* console.error('Error fetching main balance:', e);
* }
* }
*
* getBalance();
*/
getBalance: (accountAddress) => crc20.getBalance(accountAddress, instance),
/**
* 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.
*
* @example
* const client = createClient({
* endpoint: 'RPC_ENDPOINT',
* apiKey: 'RPC_API_KEY'
* });
*
* async function getBalanceOf() {
* try {
* const balanceOf = await client.crc20.getBalanceOf('0xACCOUNT_ADDRESS', '0xCONTRACT_ADDRESS');
* console.log('Balance of crc20 token:', balanceOf);
* } catch (e) {
* console.error('Error fetching balance of token:', e);
* }
* }
*
* getBalanceOf();
*/
getBalanceOf: (accountAddress, contractAddress) => crc20.getBalanceOf(accountAddress, contractAddress, instance),
/**
* 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.
*
* @example
* const client = createClient({
* endpoint: 'RPC_ENDPOINT',
* apiKey: 'RPC_API_KEY'
* });
*
* async function getName() {
* try {
* const name = await client.crc20.getName('0xCONTRACT_ADDRESS');
* console.log('Name of crc20 token:', name);
* } catch (e) {
* console.error('Error fetching name of token:', e);
* }
* }
*
* getName();
*/
getName: (contractAddress) => crc20.getName(contractAddress, instance),
/**
* 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.
*
* @example
* const client = createClient({
* endpoint: 'RPC_ENDPOINT',
* apiKey: 'RPC_API_KEY'
* });
*
* async function getSymbol() {
* try {
* const symbol = await client.crc20.getSymbol('0xCONTRACT_ADDRESS');
* console.log('Symbol of crc20 token:', symbol);
* } catch (e) {
* console.error('Error fetching symbol of token:', e);
* }
* }
*
* getSymbol();
*/
getSymbol: (contractAddress) => crc20.getSymbol(contractAddress, instance),
/**
* 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.
*
* @example
* const client = createClient({
* endpoint: 'RPC_ENDPOINT',
* apiKey: 'RPC_API_KEY'
* });
*
* async function getTotalSupply() {
* try {
* const totalSupply = await client.crc20.getTotalSupply('0xCONTRACT_ADDRESS');
* console.log('Total supply of crc20 token:', log);
* } catch (e) {
* console.error('Error fetching total supply of token:', e);
* }
* }
*
* getTotalSupply();
*/
getTotalSupply: (contractAddress) => crc20.getTotalSupply(contractAddress, instance),
},
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.
*
* @example
* const client = createClient({
* endpoint: 'RPC_ENDPOINT',
* apiKey: 'RPC_API_KEY'
* });
*
* async function getBalanceOf() {
* try {
* const balanceOf = await client.crc721.getBalanceOf('0xACCOUNT_ADDRESS', '0xCONTRACT_ADDRESS');
* console.log('Balance of crc721 token:', balanceOf);
* } catch (e) {
* console.error('Error fetching balance of token:', e);
* }
* }
*
* getBalanceOf();
*/
getBalanceOf: (accountAddress, contractAddress) => crc721.getBalanceOf(accountAddress, contractAddress, instance),
/**
* 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.
*
* @example
* const client = createClient({
* endpoint: 'RPC_ENDPOINT',
* apiKey: 'RPC_API_KEY'
* });
*
* async function getOwnerOf() {
* try {
* const ownerOf = await client.crc721.getOwnerOf('0xCONTRACT_ADDRESS');
* console.log('Owner of crc721 token:', ownerOf);
* } catch (e) {
* console.error('Error fetching owner of token:', e);
* }
* }
*
* getOwnerOf();
*/
getOwnerOf: async (contractAddress) => crc721.getOwnerOf(contractAddress, instance),
/**
* 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.
*
* @example
* const client = createClient({
* endpoint: 'RPC_ENDPOINT',
* apiKey: 'RPC_API_KEY'
* });
*
* async function getTokenUri() {
* try {
* const uri = await client.crc721.getTokenUri('0xCONTRACT_ADDRESS');
* console.log('Uri of crc721 token:', uri);
* } catch (e) {
* console.error('Error fetching uri of token:', e);
* }
* }
*
* getTokenUri();
*/
getTokenUri: async (contractAddress) => crc721.getTokenUri(contractAddress, instance),
},
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) => cronosInstance.getBlockByTimestamp(timestamp, instance),
/**
* 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, startBlock, endBlock, chunkSize) => txs.getUniqueTransactions(contractAddresses, startBlock, endBlock, chunkSize, instance),
},
};
};