UNPKG

@filfox/fnsjs

Version:

Library For FNS

113 lines (112 loc) 3.86 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { keccak256, toUtf8Bytes } from 'ethers/lib/utils'; import BigNumber from 'bignumber.js'; import { BigNumber as BN, utils } from 'ethers'; import moment from 'moment'; import axios from 'axios'; import { ethAddressFromDelegated, validateAddressString } from '@glif/filecoin-address'; export function trimSuffix(name) { return name.replace(/\.fil$/g, ''); } export function labelhash(label) { return keccak256(toUtf8Bytes(label)); } export function formatTime(timestamp, format = 'YYYY-MM-DD HH:mm') { return moment.unix(timestamp).utc().format(format); } export function isTimeBefore(t1, t2) { return moment.unix(t1).isBefore(moment.unix(t2)); } export function convertToEthAddr(addr) { if (!addr) return ''; const isEthAddr = utils.isAddress(addr); if (isEthAddr) return addr.toLowerCase(); const isFilAddr = validateAddressString(addr); const isF4Addr = isFilAddr && /^[fFtT]4/.test(addr); if (isF4Addr) { const ethAddr = ethAddressFromDelegated(addr); return ethAddr.toLowerCase(); } throw new TypeError('Invalid eth or f4 address'); } export function toFixed(value, digits = 0) { if (value < 1 && value > 0) { return new BigNumber(value) .toFixed() .replace(new RegExp(`^(0.0*[1-9][0-9]{${digits - 1}}).*`), '$1') .replace(/0+$/, ''); } return new BigNumber(value) .times(Math.pow(10, digits)) .dp(0, BigNumber.ROUND_DOWN) .div(Math.pow(10, digits)) .toFixed(); } export function formatUnits(value, decimals = 18, digits = decimals, simple = false) { // check for ethers' BigNumber if (BN.isBigNumber(value)) { value = value.toString(); } value = new BigNumber(value); // is 0 ? if (value.eq(0)) { return '0'; } const units = toFixed(value.div(Math.pow(10, decimals)), digits); if (digits < decimals && Number(units) < 1 / Math.pow(10, digits) && simple) { return '<' + `0.${'1'.padStart(digits, '0')}`; } return units; } export function formatAssetValue(balance, price, decimals = 18, digits = decimals) { if (balance === undefined) { return '0'; } if (BN.isBigNumber(balance)) { balance = balance.toString(); } return new BigNumber(balance) .div(Math.pow(10, decimals)) .times(price) .times(Math.pow(10, digits)) .dp(0) .div(Math.pow(10, digits)) .toFixed(digits); } export function getFilMarketPrice() { return __awaiter(this, void 0, void 0, function* () { let price = 0; try { const { data } = yield axios.get('https://api.coingecko.com/api/v3/simple/price', { params: { ids: 'filecoin', vs_currencies: 'usd' } }); price = data.filecoin.usd; } catch (error) { throw new Error('Get fil market price error'); } finally { return price; } }); } export default { trimSuffix, labelhash, formatTime, isTimeBefore, toFixed, formatUnits, formatAssetValue, getFilMarketPrice };