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.

115 lines (114 loc) 4.83 kB
import { logger } from '../utils/logger.js'; import { format } from '../utils/formatting.js'; import { Crc721, EthMethod } from '../lib/interfaces/ethMethods.js'; import { constructEthMethodPayload } from '../utils/ethCall.js'; /** * CRC721 integration for managing Ethereum RPC requests. * * @fileoverview This file provides helper functions for Ethereum JSON-RPC interactions. * @namespace crc721 */ export const crc721 = { /** * Fetches the token balance of the crc721 token. * * @param {string} accountAddress - The account address to fetch the balance from. * @param {string} contractAddress - The contract address of the crc721 instance. * @param {AxiosInstance} cronosInstance - The cronos client to initialize the crc721 instance * @returns {Promise<string>} The crc721 token balance of the account. * * @example * const data = crc721.getBalanceOf('0xACCOUNT_ADDRESS', '0xCONTRACT_ADDRESS'); */ getBalanceOf: async (accountAddress, contractAddress, cronosInstance) => { const paddedAddress = accountAddress.substring(2).padStart(64, '0'); const param = `${Crc721.BalanceOf}${paddedAddress}`; const data = constructEthMethodPayload({ to: contractAddress, data: param, }, EthMethod.Call); try { const response = await cronosInstance.post('', data); if (response.data.error) { logger.error('[crc721/getBalanceOf] error:', response.data.error.message); throw new Error(`[crc721/getBalanceOf] error: ${response.data.error.message}`); } if (!response.data.result) { logger.error('[crc721/getBalanceOf] error: No result returned'); throw new Error('[crc721/getBalanceOf] error: No result returned'); } const result = format.formatTokenAmount(response.data.result, 0); return result; } catch (e) { logger.error('[crc721/getBalanceOf] error:', e); throw e; } }, /** * Fetches the owner address of the crc721 token. * * @param {string} contractAddress - The contract address of the crc721 instance. * @param {AxiosInstance} cronosInstance - The cronos client to initialize the crc721 instance * @returns {Promise<string>} The owner address of the crc721 token. * * @example * const data = crc721.getOwnerOf('0xCONTRACT_ADDRESS'); */ getOwnerOf: async (contractAddress, cronosInstance) => { const data = constructEthMethodPayload({ to: contractAddress, data: Crc721.OwnerOf, }, EthMethod.Call); try { const response = await cronosInstance.post('', data); if (response.data.error) { logger.error('[crc721/getOwnerOf] error:', response.data.error.message); throw new Error(`[crc721/getOwnerOf] error: ${response.data.error.message}`); } if (!response.data.result) { logger.error('[crc721/getOwnerOf] error: No result returned'); throw new Error('[crc721/getOwnerOf] error: No result returned'); } const result = response.data.result; return result; } catch (e) { logger.error('[crc721/getOwnerOf] error:', e); throw e; } }, /** * Fetches the uri of the crc721 token. * * @param {string} contractAddress - The contract address of the crc721 instance. * @param {AxiosInstance} cronosInstance - The cronos client to initialize the crc721 instance * @returns {Promise<string>} The uri of the crc721 token. * * @example * const data = crc721.getTokenUri('0xCONTRACT_ADDRESS'); */ getTokenUri: async (contractAddress, cronosInstance) => { const data = constructEthMethodPayload({ to: contractAddress, data: Crc721.TokenURI, }, EthMethod.Call); try { const response = await cronosInstance.post('', data); if (response.data.error) { logger.error('[crc721/getBalanceOf] error:', response.data.error.message); throw new Error(`[crc721/getBalanceOf] error: ${response.data.error.message}`); } if (!response.data.result) { logger.error('[crc721/getBalanceOf] error: No result returned'); throw new Error('[crc721/getBalanceOf] error: No result returned'); } const result = format.decodeHexString(response.data.result); return result; } catch (e) { logger.error('[crc721/getBalanceOf] error:', e); throw e; } }, };