import Dec from 'decimal.js'; import { getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens'; import { NetworkNumber } from '../types/common'; export const isLayer2Network = (networkId: NetworkNumber) => [NetworkNumber.Opt, NetworkNumber.Arb, NetworkNumber.Base, NetworkNumber.Linea, NetworkNumber.Plasma].includes(+networkId); export const addToObjectIf = (condition: any, item: any) => (condition ? item : {}); export const addToArrayIf = (condition: any, ...items: any[]) => (condition ? items : []); export const stEthToWstEth = (maybeStEth: any) => maybeStEth?.replace(/^stETH$/, 'wstETH'); export const wstEthToStEth = (maybeStEth: any) => maybeStEth?.replace(/^wstETH$/, 'stETH'); export const getAbiItem = (abi: any, methodName: string) => abi.find((i: any) => i.name === methodName); export const ADDRESS_REGEX = /0x[0-9a-fA-F]{40}/; export const isAddress = (address: string) => typeof address === 'string' && (new RegExp(ADDRESS_REGEX).test(address)); export const compareAddresses = (addr1 = '', addr2 = '') => addr1.toLowerCase() === addr2.toLowerCase(); export const shortenAddress = (address = '', start = 8, end = 4) => `${address.substring(0, start)}...${address.slice(-end)}`; export const getWeiAmountForDecimals = (amount: string | number, decimals: number) => new Dec(amount).mul(10 ** decimals).floor().toString(); export const getEthAmountForDecimals = (amount: string | number, decimals: string | number) => new Dec(amount).div(10 ** +decimals).toString(); export const handleWbtcLegacy = (asset: string) => (asset === 'WBTC Legacy' ? 'WBTC' : asset); export const bytesToString = (hex: string) => Buffer.from(hex.replace(/^0x/, ''), 'hex') .toString() // eslint-disable-next-line no-control-regex .replace(/\x00/g, ''); /** * Map an input value from one range (minInput, maxInput) to a value in another range (minOutput, maxOutput) */ export const mapRange = (input: number | string, minInput: number | string, maxInput: number | string, minOutput:number | string, maxOutput: number | string) => { // slope = 1.0 * (output_end - output_start) / (input_end - input_start) const inputDiff = new Dec(maxInput).minus(minInput); const outputDiff = new Dec(maxOutput).minus(minOutput); const slope = new Dec(outputDiff).div(inputDiff); // output = output_start + slope * (input - input_start) return new Dec(minOutput).plus(new Dec(slope).mul(new Dec(input).minus(minInput))).toDP(2).toNumber(); }; export const isEnabledOnBitmap = ( bitmap: string | bigint | number, assetId: string | bigint | number, ) => ( // eslint-disable-next-line no-bitwise ((BigInt(bitmap) >> BigInt(assetId)) & BigInt(1)) === BigInt(1) ); export const MAXUINT:string = '115792089237316195423570985008687907853269984665640564039457584007913129639935'; export const isMaxUint = (amount: string) => compareAddresses(MAXUINT, amount); export const isMainnetNetwork = (network: NetworkNumber) => network === NetworkNumber.Eth; export const DEFAULT_TIMEOUT = 2000; // 2 seconds export const LONGER_TIMEOUT = 5000; // 5 seconds /** * Converts web3 hybrid response (that can be used as objects and arrays, so has duplicated values) to objects. * @param value */ export const convertHybridArraysToObjects = (value: any): any => { if (Array.isArray(value)) { const keys = Object.keys(value); const hasNamedKeys = keys.some((key) => Number.isNaN(Number(key))); // If array has named keys, treat it as an object and keep only those if (hasNamedKeys) { const result: Record = {}; for (const [key, val] of Object.entries(value)) { if (Number.isNaN(Number(key))) { result[key] = convertHybridArraysToObjects(val); } } return result; } // Else, treat as a regular array return value.map(convertHybridArraysToObjects); } // If it's an object (but not an array), recurse into its values if (value && typeof value === 'object') { const result: Record = {}; for (const [key, val] of Object.entries(value)) { result[key] = convertHybridArraysToObjects(val); } return result; } return value; }; export const ethToWeth = (maybeEth: string) => maybeEth?.replace(/^ETH$/, 'WETH'); export const wethToEth = (maybeWeth: string) => maybeWeth?.replace(/^WETH$/, 'ETH'); export const wethToEthByAddress = (maybeWethAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(wethToEth(getAssetInfoByAddress(maybeWethAddr, chainId).symbol), chainId).address; export const ethToWethByAddress = (maybeEthAddr: string, chainId = NetworkNumber.Eth) => getAssetInfo(ethToWeth(getAssetInfoByAddress(maybeEthAddr, chainId).symbol), chainId).address; export const xplToWxpl = (maybeXpl: string) => maybeXpl?.replace(/^XPL$/, 'WXPL'); export const wxplToXpl = (maybeWxpl: string) => maybeWxpl?.replace(/^WXPL$/, 'XPL'); export const getWrappedNativeAssetFromUnwrapped = (symbol: string) => { if (symbol === 'ETH') return 'WETH'; if (symbol === 'XPL') return 'WXPL'; return symbol; }; export const getNativeAssetFromWrapped = (symbol: string) => { if (symbol === 'WETH') return 'ETH'; if (symbol === 'WXPL') return 'XPL'; return symbol; };