UNPKG

@goequitize/rwa-token-sdk

Version:

SDK for creating and managing RWA token transactions with compliance features

51 lines (50 loc) 1.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidAddress = isValidAddress; exports.formatTokenAmount = formatTokenAmount; exports.parseTokenAmount = parseTokenAmount; exports.normalizeChainId = normalizeChainId; const ethers_1 = require("ethers"); /** * Validates if a string is a valid Ethereum address * @param address The address to validate * @returns True if the address is valid, false otherwise */ function isValidAddress(address) { try { return ethers_1.ethers.isAddress(address); } catch (error) { return false; } } /** * Converts a number to a string with the correct decimal places for a token * @param amount The amount to convert * @param decimals The number of decimals the token has * @returns The formatted amount as a string */ function formatTokenAmount(amount, decimals) { return ethers_1.ethers.parseUnits(amount.toString(), decimals).toString(); } /** * Converts a string token amount from wei to a human-readable number * @param amount The amount in wei (smallest unit) * @param decimals The number of decimals the token has * @returns The formatted amount as a number */ function parseTokenAmount(amount, decimals) { return parseFloat(ethers_1.ethers.formatUnits(amount, decimals)); } /** * Normalizes a chain ID to its numerical value * @param chainId The chain ID as a string or number * @returns The chain ID as a number */ function normalizeChainId(chainId) { if (typeof chainId === 'string') { // Handle hexadecimal chain IDs from MetaMask return chainId.startsWith('0x') ? parseInt(chainId, 16) : parseInt(chainId, 10); } return chainId; }