@rarcifa/cronos-evm-client
Version:
A Node.js client library for interacting with the Cronos EVM, facilitating operations on both CRC20 and CRC721 tokens.
178 lines (177 loc) • 7.46 kB
JavaScript
import { format } from '../utils/formatting.js';
import { logger } from '../utils/logger.js';
import { Crc20, EthMethod } from '../lib/interfaces/ethMethods.js';
import { constructEthMethodPayload } from '../utils/ethCall.js';
/**
* CRC20 integration for managing Ethereum RPC requests.
*
* @fileoverview This file provides helper functions for Ethereum JSON-RPC interactions.
* @namespace crc20
*/
export const crc20 = {
/**
* Fetches the balance of an account.
*
* @param {JsonRpcRequestPayload} accountAddress - The account address to fetch the balance from.
* @param {AxiosInstance} cronosInstance - The cronos client to initialize the crc20 instance
* @returns {Promise<string>} The main token balance of the account.
*
* @example
* const data = crc20.getBalance('0xACCOUNT_ADDRESS');
*/
getBalance: async (accountAddress, cronosInstance) => {
const data = constructEthMethodPayload(accountAddress, EthMethod.GetBalance);
try {
const response = await cronosInstance.post('', data);
if (response.data.error) {
logger.error('[crc20/getBalance] error:', response.data.error.message);
throw new Error(`[crc20/getBalance] error: ${response.data.error.message}`);
}
if (!response.data.result) {
logger.error('[crc20/getBalance] error: No result returned');
throw new Error('[crc20/getBalance] error: No result returned');
}
const result = format.weiToEther(response.data.result);
return result;
}
catch (e) {
logger.error('[crc20/getBalance] error:', e);
throw e;
}
},
/**
* Fetches the token balance of an account of the crc20 token.
*
* @param {string} accountAddress - The account address to fetch the balance from.
* @param {string} contractAddress - The contract address of the crc20 instance.
* @param {AxiosInstance} cronosInstance - The cronos client to initialize the crc20 instance
* @returns {Promise<string>} The crc20 token balance of the account.
*
* @example
* const data = crc20.getBalanceOf('0xACCOUNT_ADDRESS', '0xCONTRACT_ADDRESS');
*/
getBalanceOf: async (accountAddress, contractAddress, cronosInstance) => {
const paddedAddress = accountAddress.substring(2).padStart(64, '0');
const param = `${Crc20.BalanceOf}${paddedAddress}`;
const data = constructEthMethodPayload({
to: contractAddress,
data: param,
}, EthMethod.Call);
try {
const response = await cronosInstance.post('', data);
if (response.data.error) {
logger.error('[crc20/getBalanceOf] error:', response.data.error.message);
throw new Error(`[crc20/getBalanceOf] error: ${response.data.error.message}`);
}
if (!response.data.result) {
logger.error('[crc20/getBalanceOf] error: No result returned');
throw new Error('[crc20/getBalanceOf] error: No result returned');
}
const result = format.weiToEther(response.data.result);
return result;
}
catch (e) {
logger.error('[crc20/getBalanceOf] error:', e);
throw e;
}
},
/**
* Fetches the name of the crc20 token.
*
* @param {string} contractAddress - The contract address of the crc20 instance.
* @param {AxiosInstance} cronosInstance - The cronos client to initialize the crc20 instance
* @returns {Promise<string>} The name of the token.
*
* @example
* const data = crc20.getName('0xCONTRACT_ADDRESS');
*/
getName: async (contractAddress, cronosInstance) => {
const data = constructEthMethodPayload({
to: contractAddress,
data: Crc20.Name,
}, EthMethod.Call);
try {
const response = await cronosInstance.post('', data);
if (response.data.error) {
logger.error('[crc20/getName] error:', response.data.error.message);
throw new Error(`[crc20/getName] error: ${response.data.error.message}`);
}
if (!response.data.result) {
logger.error('[crc20/getName] error: No result returned');
throw new Error('[crc20/getName] error: No result returned');
}
const result = format.decodeHexString(response.data.result);
return result;
}
catch (e) {
logger.error('[crc20/getName] error:', e);
throw e;
}
},
/**
* Fetches the symbol of the crc20 token.
*
* @param {string} contractAddress - The contract address of the crc20 instance.
* @param {AxiosInstance} cronosInstance - The cronos client to initialize the crc20 instance
* @returns {Promise<string>} The symbol of the token.
*
* @example
* const data = crc20.getSymbol('0xCONTRACT_ADDRESS');
*/
getSymbol: async (contractAddress, cronosInstance) => {
const data = constructEthMethodPayload({
to: contractAddress,
data: Crc20.Symbol,
}, EthMethod.Call);
try {
const response = await cronosInstance.post('', data);
if (response.data.error) {
logger.error('[crc20/getSymbol] error:', response.data.error.message);
throw new Error(`[crc20/getSymbol] error: ${response.data.error.message}`);
}
if (!response.data.result) {
logger.error('[crc20/getSymbol] error: No result returned');
throw new Error('[crc20/getSymbol] error: No result returned');
}
const result = format.decodeHexString(response.data.result);
return result;
}
catch (e) {
logger.error('[crc20/getSymbol] error:', e);
throw e;
}
},
/**
* Fetches the total supply of the crc20 token.
*
* @param {string} contractAddress - The contract address of the crc20 instance.
* @param {AxiosInstance} cronosInstance - The cronos client to initialize the crc20 instance
* @returns {Promise<string>} The total supply of the token.
*
* @example
* const data = crc20.getTotalSupply('0xCONTRACT_ADDRESS');
*/
getTotalSupply: async (contractAddress, cronosInstance) => {
const data = constructEthMethodPayload({
to: contractAddress,
data: Crc20.TotalSupply,
}, EthMethod.Call);
try {
const response = await cronosInstance.post('', data);
if (response.data.error) {
logger.error('[crc20/getTotalSupply] error:', response.data.error.message);
throw new Error(`[crc20/getTotalSupply] error: ${response.data.error.message}`);
}
if (!response.data.result) {
logger.error('[crc20/getTotalSupply] error: No result returned');
throw new Error('[crc20/getTotalSupply] error: No result returned');
}
const result = format.formatTokenAmount(response.data.result, 6);
return result;
}
catch (e) {
logger.error('[crc20/getTotalSupply] error:', e);
throw e;
}
},
};