@xchainjs/xchain-util
Advanced tools
Comparing version 0.6.0 to 0.6.1
@@ -0,1 +1,7 @@ | ||
# v.0.6.1 (2022-04-04) | ||
### Fix | ||
- Support `UST` in `currencySymbolByAsset` | ||
# v.0.6.0 (2022-02-04) | ||
@@ -2,0 +8,0 @@ |
@@ -26,3 +26,3 @@ import BigNumber from 'bignumber.js'; | ||
* Helper to validate a possible BigNumber | ||
* If the given valie is invalid or undefined, 0 is returned as a BigNumber | ||
* If the given value is invalid or undefined, 0 is returned as a BigNumber | ||
* | ||
@@ -29,0 +29,0 @@ * @param {BigNumber|undefined} value |
@@ -21,3 +21,3 @@ import BigNumber from 'bignumber.js'; | ||
*/ | ||
var delay = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); }; | ||
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
@@ -30,3 +30,3 @@ /** | ||
*/ | ||
var bn = function (value) { return new BigNumber(value); }; | ||
const bn = (value) => new BigNumber(value); | ||
/** | ||
@@ -38,3 +38,3 @@ * Helper to check whether a BigNumber is valid or not | ||
* */ | ||
var isValidBN = function (value) { return !value.isNaN(); }; | ||
const isValidBN = (value) => !value.isNaN(); | ||
/** | ||
@@ -47,4 +47,4 @@ * Helper to create a big number from string or number | ||
* */ | ||
var bnOrZero = function (value) { | ||
var b = value ? bn(value) : bn(0); | ||
const bnOrZero = (value) => { | ||
const b = value ? bn(value) : bn(0); | ||
return isValidBN(b) ? b : bn(0); | ||
@@ -54,3 +54,3 @@ }; | ||
* Helper to validate a possible BigNumber | ||
* If the given valie is invalid or undefined, 0 is returned as a BigNumber | ||
* If the given value is invalid or undefined, 0 is returned as a BigNumber | ||
* | ||
@@ -60,3 +60,3 @@ * @param {BigNumber|undefined} value | ||
*/ | ||
var validBNOrZero = function (value) { return (value && isValidBN(value) ? value : bn(0)); }; | ||
const validBNOrZero = (value) => (value && isValidBN(value) ? value : bn(0)); | ||
/** | ||
@@ -69,6 +69,3 @@ * Format a BaseNumber to a string depending on given decimal places | ||
* */ | ||
var formatBN = function (value, decimal) { | ||
if (decimal === void 0) { decimal = 2; } | ||
return value.toFormat(decimal); | ||
}; | ||
const formatBN = (value, decimal = 2) => value.toFormat(decimal); | ||
/** | ||
@@ -92,11 +89,8 @@ * The enumuration for symbol position. | ||
*/ | ||
var formatBNCurrency = function (n, decimalPlaces, symbol, position) { | ||
if (decimalPlaces === void 0) { decimalPlaces = 2; } | ||
if (symbol === void 0) { symbol = '$'; } | ||
if (position === void 0) { position = SymbolPosition.BEFORE; } | ||
var value = formatBN(n, decimalPlaces); | ||
const formatBNCurrency = (n, decimalPlaces = 2, symbol = '$', position = SymbolPosition.BEFORE) => { | ||
const value = formatBN(n, decimalPlaces); | ||
if (position === SymbolPosition.BEFORE) { | ||
return "" + symbol + value; | ||
return `${symbol}${value}`; | ||
} | ||
return "" + value + symbol; | ||
return `${value}${symbol}`; | ||
}; | ||
@@ -111,10 +105,8 @@ /** | ||
* */ | ||
var fixedBN = function (value, decimalPlaces) { | ||
if (decimalPlaces === void 0) { decimalPlaces = 2; } | ||
var n = bn(value || 0); | ||
var fixedBN = isValidBN(n) ? n.toFixed(decimalPlaces) : bn(0).toFixed(decimalPlaces); | ||
const fixedBN = (value, decimalPlaces = 2) => { | ||
const n = bn(value || 0); | ||
const fixedBN = isValidBN(n) ? n.toFixed(decimalPlaces) : bn(0).toFixed(decimalPlaces); | ||
return bn(fixedBN); | ||
}; | ||
var _a; | ||
var Chain; | ||
@@ -133,12 +125,12 @@ (function (Chain) { | ||
})(Chain || (Chain = {})); | ||
var BNBChain = Chain.Binance; | ||
var BTCChain = Chain.Bitcoin; | ||
var ETHChain = Chain.Ethereum; | ||
var THORChain = Chain.THORChain; | ||
var CosmosChain = Chain.Cosmos; | ||
var PolkadotChain = Chain.Polkadot; | ||
var BCHChain = Chain.BitcoinCash; | ||
var LTCChain = Chain.Litecoin; | ||
var TerraChain = Chain.Terra; | ||
var DOGEChain = Chain.Doge; | ||
const BNBChain = Chain.Binance; | ||
const BTCChain = Chain.Bitcoin; | ||
const ETHChain = Chain.Ethereum; | ||
const THORChain = Chain.THORChain; | ||
const CosmosChain = Chain.Cosmos; | ||
const PolkadotChain = Chain.Polkadot; | ||
const BCHChain = Chain.BitcoinCash; | ||
const LTCChain = Chain.Litecoin; | ||
const TerraChain = Chain.Terra; | ||
const DOGEChain = Chain.Doge; | ||
/** | ||
@@ -150,3 +142,3 @@ * Type guard to check whether string is based on type `Chain` | ||
*/ | ||
var isChain = function (c) { return Object.values(Chain).includes(c); }; | ||
const isChain = (c) => Object.values(Chain).includes(c); | ||
/** | ||
@@ -158,18 +150,18 @@ * Convert chain to string. | ||
*/ | ||
var chainToString = Object.assign(function (chainId) { | ||
const chainToString = Object.assign((chainId) => { | ||
if (!(chainId in chainToString)) | ||
return 'unknown chain'; | ||
return chainToString[chainId]; | ||
}, (_a = {}, | ||
_a[Chain.THORChain] = 'Thorchain', | ||
_a[Chain.Bitcoin] = 'Bitcoin', | ||
_a[Chain.BitcoinCash] = 'Bitcoin Cash', | ||
_a[Chain.Litecoin] = 'Litecoin', | ||
_a[Chain.Ethereum] = 'Ethereum', | ||
_a[Chain.Binance] = 'Binance Chain', | ||
_a[Chain.Cosmos] = 'Cosmos', | ||
_a[Chain.Polkadot] = 'Polkadot', | ||
_a[Chain.Terra] = 'Terra', | ||
_a[Chain.Doge] = 'Dogecoin', | ||
_a)); | ||
}, { | ||
[Chain.THORChain]: 'Thorchain', | ||
[Chain.Bitcoin]: 'Bitcoin', | ||
[Chain.BitcoinCash]: 'Bitcoin Cash', | ||
[Chain.Litecoin]: 'Litecoin', | ||
[Chain.Ethereum]: 'Ethereum', | ||
[Chain.Binance]: 'Binance Chain', | ||
[Chain.Cosmos]: 'Cosmos', | ||
[Chain.Polkadot]: 'Polkadot', | ||
[Chain.Terra]: 'Terra', | ||
[Chain.Doge]: 'Dogecoin', | ||
}); | ||
@@ -184,9 +176,7 @@ /** | ||
*/ | ||
var trimZeros = function (value) { | ||
return value | ||
// (1) remove trailing zeros | ||
.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, '$1') | ||
// (2) remove leading zeros | ||
.replace(/\b0*([1-9][0-9]*|0)\b/, '$1'); | ||
}; | ||
const trimZeros = (value) => value | ||
// (1) remove trailing zeros | ||
.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, '$1') | ||
// (2) remove leading zeros | ||
.replace(/\b0*([1-9][0-9]*|0)\b/, '$1'); | ||
@@ -211,5 +201,3 @@ var Denomination; | ||
* */ | ||
var isBigNumberValue = function (v) { | ||
return typeof v === 'string' || typeof v === 'number' || v instanceof BigNumber; | ||
}; | ||
const isBigNumberValue = (v) => typeof v === 'string' || typeof v === 'number' || v instanceof BigNumber; | ||
/** | ||
@@ -225,3 +213,3 @@ * Default number of asset decimals | ||
* */ | ||
var ASSET_DECIMAL = 8; | ||
const ASSET_DECIMAL = 8; | ||
/** | ||
@@ -235,30 +223,17 @@ * Factory to create values of assets (e.g. RUNE) | ||
**/ | ||
var assetAmount = function (value, decimal) { | ||
if (decimal === void 0) { decimal = ASSET_DECIMAL; } | ||
var amount = fixedBN(value, decimal); | ||
const assetAmount = (value, decimal = ASSET_DECIMAL) => { | ||
const amount = fixedBN(value, decimal); | ||
return { | ||
type: Denomination.Asset, | ||
amount: function () { return amount; }, | ||
plus: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return assetAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
minus: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return assetAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
times: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return assetAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
div: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return assetAmount(amount.div(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
lt: function (v) { return amount.lt(isBigNumberValue(v) ? v : v.amount()); }, | ||
lte: function (v) { return amount.lte(isBigNumberValue(v) ? v : v.amount()); }, | ||
gt: function (v) { return amount.gt(isBigNumberValue(v) ? v : v.amount()); }, | ||
gte: function (v) { return amount.gte(isBigNumberValue(v) ? v : v.amount()); }, | ||
eq: function (v) { return amount.eq(isBigNumberValue(v) ? v : v.amount()); }, | ||
decimal: decimal, | ||
amount: () => amount, | ||
plus: (v, d = decimal) => assetAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d), | ||
minus: (v, d = decimal) => assetAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d), | ||
times: (v, d = decimal) => assetAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d), | ||
div: (v, d = decimal) => assetAmount(amount.div(isBigNumberValue(v) ? v : v.amount()), d), | ||
lt: (v) => amount.lt(isBigNumberValue(v) ? v : v.amount()), | ||
lte: (v) => amount.lte(isBigNumberValue(v) ? v : v.amount()), | ||
gt: (v) => amount.gt(isBigNumberValue(v) ? v : v.amount()), | ||
gte: (v) => amount.gte(isBigNumberValue(v) ? v : v.amount()), | ||
eq: (v) => amount.eq(isBigNumberValue(v) ? v : v.amount()), | ||
decimal, | ||
}; | ||
@@ -273,30 +248,17 @@ }; | ||
**/ | ||
var baseAmount = function (value, decimal) { | ||
if (decimal === void 0) { decimal = ASSET_DECIMAL; } | ||
var amount = fixedBN(value, 0); | ||
const baseAmount = (value, decimal = ASSET_DECIMAL) => { | ||
const amount = fixedBN(value, 0); | ||
return { | ||
type: Denomination.Base, | ||
amount: function () { return amount; }, | ||
plus: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return baseAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
minus: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return baseAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
times: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return baseAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
div: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return baseAmount(amount.div(isBigNumberValue(v) ? v : v.amount()).decimalPlaces(0, BigNumber.ROUND_DOWN), d); | ||
}, | ||
lt: function (v) { return amount.lt(isBigNumberValue(v) ? v : v.amount()); }, | ||
lte: function (v) { return amount.lte(isBigNumberValue(v) ? v : v.amount()); }, | ||
gt: function (v) { return amount.gt(isBigNumberValue(v) ? v : v.amount()); }, | ||
gte: function (v) { return amount.gte(isBigNumberValue(v) ? v : v.amount()); }, | ||
eq: function (v) { return amount.eq(isBigNumberValue(v) ? v : v.amount()); }, | ||
decimal: decimal, | ||
amount: () => amount, | ||
plus: (v, d = decimal) => baseAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d), | ||
minus: (v, d = decimal) => baseAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d), | ||
times: (v, d = decimal) => baseAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d), | ||
div: (v, d = decimal) => baseAmount(amount.div(isBigNumberValue(v) ? v : v.amount()).decimalPlaces(0, BigNumber.ROUND_DOWN), d), | ||
lt: (v) => amount.lt(isBigNumberValue(v) ? v : v.amount()), | ||
lte: (v) => amount.lte(isBigNumberValue(v) ? v : v.amount()), | ||
gt: (v) => amount.gt(isBigNumberValue(v) ? v : v.amount()), | ||
gte: (v) => amount.gte(isBigNumberValue(v) ? v : v.amount()), | ||
eq: (v) => amount.eq(isBigNumberValue(v) ? v : v.amount()), | ||
decimal, | ||
}; | ||
@@ -310,5 +272,5 @@ }; | ||
* */ | ||
var baseToAsset = function (base) { | ||
var decimal = base.decimal; | ||
var value = base | ||
const baseToAsset = (base) => { | ||
const decimal = base.decimal; | ||
const value = base | ||
.amount() | ||
@@ -325,4 +287,4 @@ .div(Math.pow(10, decimal)) | ||
* */ | ||
var assetToBase = function (asset) { | ||
var value = asset | ||
const assetToBase = (asset) => { | ||
const value = asset | ||
.amount() | ||
@@ -339,3 +301,3 @@ .multipliedBy(Math.pow(10, asset.decimal)) | ||
* */ | ||
var isAssetAmount = function (v) { return v.type === Denomination.Asset; }; | ||
const isAssetAmount = (v) => v.type === Denomination.Asset; | ||
/** | ||
@@ -347,3 +309,3 @@ * Guard to check whether value is an amount of a base value or not | ||
* */ | ||
var isBaseAmount = function (v) { return v.type === Denomination.Base; }; | ||
const isBaseAmount = (v) => v.type === Denomination.Base; | ||
/** | ||
@@ -358,6 +320,5 @@ * Formats an `AssetAmount` into `string` based on decimal places | ||
*/ | ||
var formatAssetAmount = function (_a) { | ||
var amount = _a.amount, decimal = _a.decimal, _b = _a.trimZeros, trimZeros$1 = _b === void 0 ? false : _b; | ||
const formatAssetAmount = ({ amount, decimal, trimZeros: trimZeros$1 = false, }) => { | ||
// strict check for `undefined` value as negate of 0 will return true and passed decimal value will be ignored | ||
var formatted = formatBN(amount.amount(), decimal === undefined ? amount.decimal : decimal); | ||
const formatted = formatBN(amount.amount(), decimal === undefined ? amount.decimal : decimal); | ||
// Note: `trimZeros` wins over `decimal` | ||
@@ -372,3 +333,3 @@ return trimZeros$1 ? trimZeros(formatted) : formatted; | ||
*/ | ||
var formatBaseAmount = function (amount) { return formatBN(amount.amount(), 0); }; | ||
const formatBaseAmount = (amount) => formatBN(amount.amount(), 0); | ||
/** | ||
@@ -380,3 +341,3 @@ * Base "chain" asset of Binance chain. | ||
*/ | ||
var AssetBNB = { chain: Chain.Binance, symbol: 'BNB', ticker: 'BNB', synth: false }; | ||
const AssetBNB = { chain: Chain.Binance, symbol: 'BNB', ticker: 'BNB', synth: false }; | ||
/** | ||
@@ -388,3 +349,3 @@ * Base "chain" asset on bitcoin main net. | ||
*/ | ||
var AssetBTC = { chain: Chain.Bitcoin, symbol: 'BTC', ticker: 'BTC', synth: false }; | ||
const AssetBTC = { chain: Chain.Bitcoin, symbol: 'BTC', ticker: 'BTC', synth: false }; | ||
/** | ||
@@ -396,3 +357,3 @@ * Base "chain" asset on bitcoin cash main net. | ||
*/ | ||
var AssetBCH = { chain: Chain.BitcoinCash, symbol: 'BCH', ticker: 'BCH', synth: false }; | ||
const AssetBCH = { chain: Chain.BitcoinCash, symbol: 'BCH', ticker: 'BCH', synth: false }; | ||
/** | ||
@@ -404,3 +365,3 @@ * Base "chain" asset on litecoin main net. | ||
*/ | ||
var AssetLTC = { chain: Chain.Litecoin, symbol: 'LTC', ticker: 'LTC', synth: false }; | ||
const AssetLTC = { chain: Chain.Litecoin, symbol: 'LTC', ticker: 'LTC', synth: false }; | ||
/** | ||
@@ -411,3 +372,3 @@ * Dogecoin asset | ||
*/ | ||
var AssetDOGE = { chain: Chain.Doge, symbol: 'DOGE', ticker: 'DOGE', synth: false }; | ||
const AssetDOGE = { chain: Chain.Doge, symbol: 'DOGE', ticker: 'DOGE', synth: false }; | ||
/** | ||
@@ -418,4 +379,4 @@ * Luna asset | ||
*/ | ||
var AssetLUNA = { chain: Chain.Terra, symbol: 'LUNA', ticker: 'LUNA', synth: false }; | ||
var RUNE_TICKER = 'RUNE'; | ||
const AssetLUNA = { chain: Chain.Terra, symbol: 'LUNA', ticker: 'LUNA', synth: false }; | ||
const RUNE_TICKER = 'RUNE'; | ||
/** | ||
@@ -427,3 +388,3 @@ * Base "chain" asset on ethereum main net. | ||
*/ | ||
var AssetETH = { chain: Chain.Ethereum, symbol: 'ETH', ticker: 'ETH', synth: false }; | ||
const AssetETH = { chain: Chain.Ethereum, symbol: 'ETH', ticker: 'ETH', synth: false }; | ||
/** | ||
@@ -435,3 +396,3 @@ * Base "chain" asset for RUNE-67C on Binance test net. | ||
*/ | ||
var AssetRune67C = { chain: Chain.Binance, symbol: 'RUNE-67C', ticker: RUNE_TICKER, synth: false }; | ||
const AssetRune67C = { chain: Chain.Binance, symbol: 'RUNE-67C', ticker: RUNE_TICKER, synth: false }; | ||
/** | ||
@@ -443,3 +404,3 @@ * Base "chain" asset for RUNE-B1A on Binance main net. | ||
*/ | ||
var AssetRuneB1A = { chain: Chain.Binance, symbol: 'RUNE-B1A', ticker: RUNE_TICKER, synth: false }; | ||
const AssetRuneB1A = { chain: Chain.Binance, symbol: 'RUNE-B1A', ticker: RUNE_TICKER, synth: false }; | ||
/** | ||
@@ -451,3 +412,3 @@ * Base "chain" asset on thorchain main net. | ||
*/ | ||
var AssetRuneNative = { chain: Chain.THORChain, symbol: RUNE_TICKER, ticker: RUNE_TICKER, synth: false }; | ||
const AssetRuneNative = { chain: Chain.THORChain, symbol: RUNE_TICKER, ticker: RUNE_TICKER, synth: false }; | ||
/** | ||
@@ -459,5 +420,5 @@ * Base "chain" asset for RUNE on ethereum main net. | ||
*/ | ||
var AssetRuneERC20 = { | ||
const AssetRuneERC20 = { | ||
chain: Chain.Ethereum, | ||
symbol: RUNE_TICKER + "-0x3155ba85d5f96b2d030a4966af206230e46849cb", | ||
symbol: `${RUNE_TICKER}-0x3155ba85d5f96b2d030a4966af206230e46849cb`, | ||
ticker: RUNE_TICKER, | ||
@@ -472,5 +433,5 @@ synth: false, | ||
*/ | ||
var AssetRuneERC20Testnet = { | ||
const AssetRuneERC20Testnet = { | ||
chain: Chain.Ethereum, | ||
symbol: RUNE_TICKER + "-0xd601c6A3a36721320573885A8d8420746dA3d7A0", | ||
symbol: `${RUNE_TICKER}-0xd601c6A3a36721320573885A8d8420746dA3d7A0`, | ||
ticker: RUNE_TICKER, | ||
@@ -485,3 +446,3 @@ synth: false, | ||
*/ | ||
var isValidAsset = function (asset) { return !!asset.chain && !!asset.ticker && !!asset.symbol; }; | ||
const isValidAsset = (asset) => !!asset.chain && !!asset.ticker && !!asset.symbol; | ||
/** | ||
@@ -493,8 +454,5 @@ * Helper to check whether an asset is synth asset | ||
*/ | ||
var isSynthAsset = function (_a) { | ||
var synth = _a.synth; | ||
return synth; | ||
}; | ||
var SYNTH_DELIMITER = '/'; | ||
var NON_SYNTH_DELIMITER = '.'; | ||
const isSynthAsset = ({ synth }) => synth; | ||
const SYNTH_DELIMITER = '/'; | ||
const NON_SYNTH_DELIMITER = '.'; | ||
/** | ||
@@ -517,17 +475,17 @@ * Creates an `Asset` by a given string | ||
*/ | ||
var assetFromString = function (s) { | ||
const assetFromString = (s) => { | ||
var _a; | ||
var isSynth = s.includes(SYNTH_DELIMITER); | ||
var delimiter = isSynth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
var data = s.split(delimiter); | ||
const isSynth = s.includes(SYNTH_DELIMITER); | ||
const delimiter = isSynth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
const data = s.split(delimiter); | ||
if (data.length <= 1 || ((_a = data[1]) === null || _a === void 0 ? void 0 : _a.length) < 1) { | ||
return null; | ||
} | ||
var chain = data[0]; | ||
const chain = data[0]; | ||
// filter out not supported string of chains | ||
if (!chain || !isChain(chain)) | ||
return null; | ||
var symbol = data[1]; | ||
var ticker = symbol.split('-')[0]; | ||
return { chain: chain, symbol: symbol, ticker: ticker, synth: isSynth }; | ||
const symbol = data[1]; | ||
const ticker = symbol.split('-')[0]; | ||
return { chain, symbol, ticker, synth: isSynth }; | ||
}; | ||
@@ -549,6 +507,5 @@ /** | ||
*/ | ||
var assetToString = function (_a) { | ||
var chain = _a.chain, symbol = _a.symbol, synth = _a.synth; | ||
var delimiter = synth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
return "" + chain + delimiter + symbol; | ||
const assetToString = ({ chain, symbol, synth }) => { | ||
const delimiter = synth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
return `${chain}${delimiter}${symbol}`; | ||
}; | ||
@@ -572,4 +529,3 @@ /** | ||
*/ | ||
var currencySymbolByAsset = function (_a) { | ||
var ticker = _a.ticker; | ||
const currencySymbolByAsset = ({ ticker }) => { | ||
switch (true) { | ||
@@ -582,3 +538,3 @@ case ticker === RUNE_TICKER: | ||
return AssetCurrencySymbol.ETH; | ||
case ticker.includes('USD'): | ||
case ticker.includes('USD') || ticker.includes('UST'): | ||
return AssetCurrencySymbol.USD; | ||
@@ -600,7 +556,6 @@ default: | ||
*/ | ||
var formatAssetAmountCurrency = function (_a) { | ||
var _b; | ||
var amount = _a.amount, asset = _a.asset, decimal = _a.decimal, _c = _a.trimZeros, shouldTrimZeros = _c === void 0 ? false : _c; | ||
var amountFormatted = formatAssetAmount({ | ||
amount: amount, | ||
const formatAssetAmountCurrency = ({ amount, asset, decimal, trimZeros: shouldTrimZeros = false, }) => { | ||
var _a; | ||
const amountFormatted = formatAssetAmount({ | ||
amount, | ||
// strict check for `undefined` value as negate of 0 will return true and passed decimal value will be ignored | ||
@@ -610,16 +565,16 @@ decimal: decimal === undefined ? amount.decimal : decimal, | ||
}); | ||
var ticker = (_b = asset === null || asset === void 0 ? void 0 : asset.ticker) !== null && _b !== void 0 ? _b : ''; | ||
const ticker = (_a = asset === null || asset === void 0 ? void 0 : asset.ticker) !== null && _a !== void 0 ? _a : ''; | ||
if (ticker) { | ||
// RUNE | ||
if (ticker === RUNE_TICKER) | ||
return AssetCurrencySymbol.RUNE + " " + amountFormatted; | ||
return `${AssetCurrencySymbol.RUNE} ${amountFormatted}`; | ||
// BTC | ||
var regex = new RegExp(AssetBTC.ticker, 'i'); | ||
let regex = new RegExp(AssetBTC.ticker, 'i'); | ||
if (ticker.match(new RegExp(AssetBTC.ticker, 'i'))) { | ||
var base = assetToBase(amount); | ||
const base = assetToBase(amount); | ||
// format all < ₿ 0.01 in statoshi | ||
if (base.amount().isLessThanOrEqualTo('1000000')) { | ||
return AssetCurrencySymbol.SATOSHI + " " + formatBaseAmount(base); | ||
return `${AssetCurrencySymbol.SATOSHI} ${formatBaseAmount(base)}`; | ||
} | ||
return AssetCurrencySymbol.BTC + " " + amountFormatted; | ||
return `${AssetCurrencySymbol.BTC} ${amountFormatted}`; | ||
} | ||
@@ -629,10 +584,10 @@ // ETH | ||
if (ticker.match(regex)) | ||
return AssetCurrencySymbol.ETH + " " + amountFormatted; | ||
return `${AssetCurrencySymbol.ETH} ${amountFormatted}`; | ||
// USD | ||
regex = new RegExp('USD', 'i'); | ||
if (ticker.match('USD')) | ||
return AssetCurrencySymbol.USD + " " + amountFormatted; | ||
return amountFormatted + " " + ticker; | ||
return `${AssetCurrencySymbol.USD} ${amountFormatted}`; | ||
return `${amountFormatted} ${ticker}`; | ||
} | ||
return "$ " + amountFormatted; | ||
return `$ ${amountFormatted}`; | ||
}; | ||
@@ -648,8 +603,5 @@ /** | ||
*/ | ||
var formatBaseAsAssetAmount = function (_a) { | ||
var amount = _a.amount, decimal = _a.decimal, _b = _a.trimZeros, trimZeros = _b === void 0 ? false : _b; | ||
return formatAssetAmount({ amount: baseToAsset(amount), decimal: decimal, trimZeros: trimZeros }); | ||
}; | ||
const formatBaseAsAssetAmount = ({ amount, decimal, trimZeros = false, }) => formatAssetAmount({ amount: baseToAsset(amount), decimal, trimZeros }); | ||
export { AssetBCH, AssetBNB, AssetBTC, AssetCurrencySymbol, AssetDOGE, AssetETH, AssetLTC, AssetLUNA, AssetRune67C, AssetRuneB1A, AssetRuneERC20, AssetRuneERC20Testnet, AssetRuneNative, BCHChain, BNBChain, BTCChain, Chain, CosmosChain, DOGEChain, Denomination, ETHChain, LTCChain, PolkadotChain, RUNE_TICKER, THORChain, TerraChain, assetAmount, assetFromString, assetToBase, assetToString, baseAmount, baseToAsset, bn, bnOrZero, chainToString, currencySymbolByAsset, delay, fixedBN, formatAssetAmount, formatAssetAmountCurrency, formatBN, formatBNCurrency, formatBaseAmount, formatBaseAsAssetAmount, isAssetAmount, isBaseAmount, isBigNumberValue, isChain, isSynthAsset, isValidAsset, isValidBN, trimZeros, validBNOrZero }; | ||
//# sourceMappingURL=index.esm.js.map |
306
lib/index.js
@@ -29,3 +29,3 @@ 'use strict'; | ||
*/ | ||
var delay = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); }; | ||
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
@@ -38,3 +38,3 @@ /** | ||
*/ | ||
var bn = function (value) { return new BigNumber__default['default'](value); }; | ||
const bn = (value) => new BigNumber__default['default'](value); | ||
/** | ||
@@ -46,3 +46,3 @@ * Helper to check whether a BigNumber is valid or not | ||
* */ | ||
var isValidBN = function (value) { return !value.isNaN(); }; | ||
const isValidBN = (value) => !value.isNaN(); | ||
/** | ||
@@ -55,4 +55,4 @@ * Helper to create a big number from string or number | ||
* */ | ||
var bnOrZero = function (value) { | ||
var b = value ? bn(value) : bn(0); | ||
const bnOrZero = (value) => { | ||
const b = value ? bn(value) : bn(0); | ||
return isValidBN(b) ? b : bn(0); | ||
@@ -62,3 +62,3 @@ }; | ||
* Helper to validate a possible BigNumber | ||
* If the given valie is invalid or undefined, 0 is returned as a BigNumber | ||
* If the given value is invalid or undefined, 0 is returned as a BigNumber | ||
* | ||
@@ -68,3 +68,3 @@ * @param {BigNumber|undefined} value | ||
*/ | ||
var validBNOrZero = function (value) { return (value && isValidBN(value) ? value : bn(0)); }; | ||
const validBNOrZero = (value) => (value && isValidBN(value) ? value : bn(0)); | ||
/** | ||
@@ -77,6 +77,3 @@ * Format a BaseNumber to a string depending on given decimal places | ||
* */ | ||
var formatBN = function (value, decimal) { | ||
if (decimal === void 0) { decimal = 2; } | ||
return value.toFormat(decimal); | ||
}; | ||
const formatBN = (value, decimal = 2) => value.toFormat(decimal); | ||
/** | ||
@@ -100,11 +97,8 @@ * The enumuration for symbol position. | ||
*/ | ||
var formatBNCurrency = function (n, decimalPlaces, symbol, position) { | ||
if (decimalPlaces === void 0) { decimalPlaces = 2; } | ||
if (symbol === void 0) { symbol = '$'; } | ||
if (position === void 0) { position = SymbolPosition.BEFORE; } | ||
var value = formatBN(n, decimalPlaces); | ||
const formatBNCurrency = (n, decimalPlaces = 2, symbol = '$', position = SymbolPosition.BEFORE) => { | ||
const value = formatBN(n, decimalPlaces); | ||
if (position === SymbolPosition.BEFORE) { | ||
return "" + symbol + value; | ||
return `${symbol}${value}`; | ||
} | ||
return "" + value + symbol; | ||
return `${value}${symbol}`; | ||
}; | ||
@@ -119,10 +113,8 @@ /** | ||
* */ | ||
var fixedBN = function (value, decimalPlaces) { | ||
if (decimalPlaces === void 0) { decimalPlaces = 2; } | ||
var n = bn(value || 0); | ||
var fixedBN = isValidBN(n) ? n.toFixed(decimalPlaces) : bn(0).toFixed(decimalPlaces); | ||
const fixedBN = (value, decimalPlaces = 2) => { | ||
const n = bn(value || 0); | ||
const fixedBN = isValidBN(n) ? n.toFixed(decimalPlaces) : bn(0).toFixed(decimalPlaces); | ||
return bn(fixedBN); | ||
}; | ||
var _a; | ||
(function (Chain) { | ||
@@ -140,12 +132,12 @@ Chain["Binance"] = "BNB"; | ||
})(exports.Chain || (exports.Chain = {})); | ||
var BNBChain = exports.Chain.Binance; | ||
var BTCChain = exports.Chain.Bitcoin; | ||
var ETHChain = exports.Chain.Ethereum; | ||
var THORChain = exports.Chain.THORChain; | ||
var CosmosChain = exports.Chain.Cosmos; | ||
var PolkadotChain = exports.Chain.Polkadot; | ||
var BCHChain = exports.Chain.BitcoinCash; | ||
var LTCChain = exports.Chain.Litecoin; | ||
var TerraChain = exports.Chain.Terra; | ||
var DOGEChain = exports.Chain.Doge; | ||
const BNBChain = exports.Chain.Binance; | ||
const BTCChain = exports.Chain.Bitcoin; | ||
const ETHChain = exports.Chain.Ethereum; | ||
const THORChain = exports.Chain.THORChain; | ||
const CosmosChain = exports.Chain.Cosmos; | ||
const PolkadotChain = exports.Chain.Polkadot; | ||
const BCHChain = exports.Chain.BitcoinCash; | ||
const LTCChain = exports.Chain.Litecoin; | ||
const TerraChain = exports.Chain.Terra; | ||
const DOGEChain = exports.Chain.Doge; | ||
/** | ||
@@ -157,3 +149,3 @@ * Type guard to check whether string is based on type `Chain` | ||
*/ | ||
var isChain = function (c) { return Object.values(exports.Chain).includes(c); }; | ||
const isChain = (c) => Object.values(exports.Chain).includes(c); | ||
/** | ||
@@ -165,18 +157,18 @@ * Convert chain to string. | ||
*/ | ||
var chainToString = Object.assign(function (chainId) { | ||
const chainToString = Object.assign((chainId) => { | ||
if (!(chainId in chainToString)) | ||
return 'unknown chain'; | ||
return chainToString[chainId]; | ||
}, (_a = {}, | ||
_a[exports.Chain.THORChain] = 'Thorchain', | ||
_a[exports.Chain.Bitcoin] = 'Bitcoin', | ||
_a[exports.Chain.BitcoinCash] = 'Bitcoin Cash', | ||
_a[exports.Chain.Litecoin] = 'Litecoin', | ||
_a[exports.Chain.Ethereum] = 'Ethereum', | ||
_a[exports.Chain.Binance] = 'Binance Chain', | ||
_a[exports.Chain.Cosmos] = 'Cosmos', | ||
_a[exports.Chain.Polkadot] = 'Polkadot', | ||
_a[exports.Chain.Terra] = 'Terra', | ||
_a[exports.Chain.Doge] = 'Dogecoin', | ||
_a)); | ||
}, { | ||
[exports.Chain.THORChain]: 'Thorchain', | ||
[exports.Chain.Bitcoin]: 'Bitcoin', | ||
[exports.Chain.BitcoinCash]: 'Bitcoin Cash', | ||
[exports.Chain.Litecoin]: 'Litecoin', | ||
[exports.Chain.Ethereum]: 'Ethereum', | ||
[exports.Chain.Binance]: 'Binance Chain', | ||
[exports.Chain.Cosmos]: 'Cosmos', | ||
[exports.Chain.Polkadot]: 'Polkadot', | ||
[exports.Chain.Terra]: 'Terra', | ||
[exports.Chain.Doge]: 'Dogecoin', | ||
}); | ||
@@ -191,9 +183,7 @@ /** | ||
*/ | ||
var trimZeros = function (value) { | ||
return value | ||
// (1) remove trailing zeros | ||
.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, '$1') | ||
// (2) remove leading zeros | ||
.replace(/\b0*([1-9][0-9]*|0)\b/, '$1'); | ||
}; | ||
const trimZeros = (value) => value | ||
// (1) remove trailing zeros | ||
.replace(/(\.[0-9]*[1-9])0+$|\.0*$/, '$1') | ||
// (2) remove leading zeros | ||
.replace(/\b0*([1-9][0-9]*|0)\b/, '$1'); | ||
@@ -217,5 +207,3 @@ (function (Denomination) { | ||
* */ | ||
var isBigNumberValue = function (v) { | ||
return typeof v === 'string' || typeof v === 'number' || v instanceof BigNumber__default['default']; | ||
}; | ||
const isBigNumberValue = (v) => typeof v === 'string' || typeof v === 'number' || v instanceof BigNumber__default['default']; | ||
/** | ||
@@ -231,3 +219,3 @@ * Default number of asset decimals | ||
* */ | ||
var ASSET_DECIMAL = 8; | ||
const ASSET_DECIMAL = 8; | ||
/** | ||
@@ -241,30 +229,17 @@ * Factory to create values of assets (e.g. RUNE) | ||
**/ | ||
var assetAmount = function (value, decimal) { | ||
if (decimal === void 0) { decimal = ASSET_DECIMAL; } | ||
var amount = fixedBN(value, decimal); | ||
const assetAmount = (value, decimal = ASSET_DECIMAL) => { | ||
const amount = fixedBN(value, decimal); | ||
return { | ||
type: exports.Denomination.Asset, | ||
amount: function () { return amount; }, | ||
plus: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return assetAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
minus: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return assetAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
times: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return assetAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
div: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return assetAmount(amount.div(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
lt: function (v) { return amount.lt(isBigNumberValue(v) ? v : v.amount()); }, | ||
lte: function (v) { return amount.lte(isBigNumberValue(v) ? v : v.amount()); }, | ||
gt: function (v) { return amount.gt(isBigNumberValue(v) ? v : v.amount()); }, | ||
gte: function (v) { return amount.gte(isBigNumberValue(v) ? v : v.amount()); }, | ||
eq: function (v) { return amount.eq(isBigNumberValue(v) ? v : v.amount()); }, | ||
decimal: decimal, | ||
amount: () => amount, | ||
plus: (v, d = decimal) => assetAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d), | ||
minus: (v, d = decimal) => assetAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d), | ||
times: (v, d = decimal) => assetAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d), | ||
div: (v, d = decimal) => assetAmount(amount.div(isBigNumberValue(v) ? v : v.amount()), d), | ||
lt: (v) => amount.lt(isBigNumberValue(v) ? v : v.amount()), | ||
lte: (v) => amount.lte(isBigNumberValue(v) ? v : v.amount()), | ||
gt: (v) => amount.gt(isBigNumberValue(v) ? v : v.amount()), | ||
gte: (v) => amount.gte(isBigNumberValue(v) ? v : v.amount()), | ||
eq: (v) => amount.eq(isBigNumberValue(v) ? v : v.amount()), | ||
decimal, | ||
}; | ||
@@ -279,30 +254,17 @@ }; | ||
**/ | ||
var baseAmount = function (value, decimal) { | ||
if (decimal === void 0) { decimal = ASSET_DECIMAL; } | ||
var amount = fixedBN(value, 0); | ||
const baseAmount = (value, decimal = ASSET_DECIMAL) => { | ||
const amount = fixedBN(value, 0); | ||
return { | ||
type: exports.Denomination.Base, | ||
amount: function () { return amount; }, | ||
plus: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return baseAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
minus: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return baseAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
times: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return baseAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d); | ||
}, | ||
div: function (v, d) { | ||
if (d === void 0) { d = decimal; } | ||
return baseAmount(amount.div(isBigNumberValue(v) ? v : v.amount()).decimalPlaces(0, BigNumber__default['default'].ROUND_DOWN), d); | ||
}, | ||
lt: function (v) { return amount.lt(isBigNumberValue(v) ? v : v.amount()); }, | ||
lte: function (v) { return amount.lte(isBigNumberValue(v) ? v : v.amount()); }, | ||
gt: function (v) { return amount.gt(isBigNumberValue(v) ? v : v.amount()); }, | ||
gte: function (v) { return amount.gte(isBigNumberValue(v) ? v : v.amount()); }, | ||
eq: function (v) { return amount.eq(isBigNumberValue(v) ? v : v.amount()); }, | ||
decimal: decimal, | ||
amount: () => amount, | ||
plus: (v, d = decimal) => baseAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d), | ||
minus: (v, d = decimal) => baseAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d), | ||
times: (v, d = decimal) => baseAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d), | ||
div: (v, d = decimal) => baseAmount(amount.div(isBigNumberValue(v) ? v : v.amount()).decimalPlaces(0, BigNumber__default['default'].ROUND_DOWN), d), | ||
lt: (v) => amount.lt(isBigNumberValue(v) ? v : v.amount()), | ||
lte: (v) => amount.lte(isBigNumberValue(v) ? v : v.amount()), | ||
gt: (v) => amount.gt(isBigNumberValue(v) ? v : v.amount()), | ||
gte: (v) => amount.gte(isBigNumberValue(v) ? v : v.amount()), | ||
eq: (v) => amount.eq(isBigNumberValue(v) ? v : v.amount()), | ||
decimal, | ||
}; | ||
@@ -316,5 +278,5 @@ }; | ||
* */ | ||
var baseToAsset = function (base) { | ||
var decimal = base.decimal; | ||
var value = base | ||
const baseToAsset = (base) => { | ||
const decimal = base.decimal; | ||
const value = base | ||
.amount() | ||
@@ -331,4 +293,4 @@ .div(Math.pow(10, decimal)) | ||
* */ | ||
var assetToBase = function (asset) { | ||
var value = asset | ||
const assetToBase = (asset) => { | ||
const value = asset | ||
.amount() | ||
@@ -345,3 +307,3 @@ .multipliedBy(Math.pow(10, asset.decimal)) | ||
* */ | ||
var isAssetAmount = function (v) { return v.type === exports.Denomination.Asset; }; | ||
const isAssetAmount = (v) => v.type === exports.Denomination.Asset; | ||
/** | ||
@@ -353,3 +315,3 @@ * Guard to check whether value is an amount of a base value or not | ||
* */ | ||
var isBaseAmount = function (v) { return v.type === exports.Denomination.Base; }; | ||
const isBaseAmount = (v) => v.type === exports.Denomination.Base; | ||
/** | ||
@@ -364,6 +326,5 @@ * Formats an `AssetAmount` into `string` based on decimal places | ||
*/ | ||
var formatAssetAmount = function (_a) { | ||
var amount = _a.amount, decimal = _a.decimal, _b = _a.trimZeros, trimZeros$1 = _b === void 0 ? false : _b; | ||
const formatAssetAmount = ({ amount, decimal, trimZeros: trimZeros$1 = false, }) => { | ||
// strict check for `undefined` value as negate of 0 will return true and passed decimal value will be ignored | ||
var formatted = formatBN(amount.amount(), decimal === undefined ? amount.decimal : decimal); | ||
const formatted = formatBN(amount.amount(), decimal === undefined ? amount.decimal : decimal); | ||
// Note: `trimZeros` wins over `decimal` | ||
@@ -378,3 +339,3 @@ return trimZeros$1 ? trimZeros(formatted) : formatted; | ||
*/ | ||
var formatBaseAmount = function (amount) { return formatBN(amount.amount(), 0); }; | ||
const formatBaseAmount = (amount) => formatBN(amount.amount(), 0); | ||
/** | ||
@@ -386,3 +347,3 @@ * Base "chain" asset of Binance chain. | ||
*/ | ||
var AssetBNB = { chain: exports.Chain.Binance, symbol: 'BNB', ticker: 'BNB', synth: false }; | ||
const AssetBNB = { chain: exports.Chain.Binance, symbol: 'BNB', ticker: 'BNB', synth: false }; | ||
/** | ||
@@ -394,3 +355,3 @@ * Base "chain" asset on bitcoin main net. | ||
*/ | ||
var AssetBTC = { chain: exports.Chain.Bitcoin, symbol: 'BTC', ticker: 'BTC', synth: false }; | ||
const AssetBTC = { chain: exports.Chain.Bitcoin, symbol: 'BTC', ticker: 'BTC', synth: false }; | ||
/** | ||
@@ -402,3 +363,3 @@ * Base "chain" asset on bitcoin cash main net. | ||
*/ | ||
var AssetBCH = { chain: exports.Chain.BitcoinCash, symbol: 'BCH', ticker: 'BCH', synth: false }; | ||
const AssetBCH = { chain: exports.Chain.BitcoinCash, symbol: 'BCH', ticker: 'BCH', synth: false }; | ||
/** | ||
@@ -410,3 +371,3 @@ * Base "chain" asset on litecoin main net. | ||
*/ | ||
var AssetLTC = { chain: exports.Chain.Litecoin, symbol: 'LTC', ticker: 'LTC', synth: false }; | ||
const AssetLTC = { chain: exports.Chain.Litecoin, symbol: 'LTC', ticker: 'LTC', synth: false }; | ||
/** | ||
@@ -417,3 +378,3 @@ * Dogecoin asset | ||
*/ | ||
var AssetDOGE = { chain: exports.Chain.Doge, symbol: 'DOGE', ticker: 'DOGE', synth: false }; | ||
const AssetDOGE = { chain: exports.Chain.Doge, symbol: 'DOGE', ticker: 'DOGE', synth: false }; | ||
/** | ||
@@ -424,4 +385,4 @@ * Luna asset | ||
*/ | ||
var AssetLUNA = { chain: exports.Chain.Terra, symbol: 'LUNA', ticker: 'LUNA', synth: false }; | ||
var RUNE_TICKER = 'RUNE'; | ||
const AssetLUNA = { chain: exports.Chain.Terra, symbol: 'LUNA', ticker: 'LUNA', synth: false }; | ||
const RUNE_TICKER = 'RUNE'; | ||
/** | ||
@@ -433,3 +394,3 @@ * Base "chain" asset on ethereum main net. | ||
*/ | ||
var AssetETH = { chain: exports.Chain.Ethereum, symbol: 'ETH', ticker: 'ETH', synth: false }; | ||
const AssetETH = { chain: exports.Chain.Ethereum, symbol: 'ETH', ticker: 'ETH', synth: false }; | ||
/** | ||
@@ -441,3 +402,3 @@ * Base "chain" asset for RUNE-67C on Binance test net. | ||
*/ | ||
var AssetRune67C = { chain: exports.Chain.Binance, symbol: 'RUNE-67C', ticker: RUNE_TICKER, synth: false }; | ||
const AssetRune67C = { chain: exports.Chain.Binance, symbol: 'RUNE-67C', ticker: RUNE_TICKER, synth: false }; | ||
/** | ||
@@ -449,3 +410,3 @@ * Base "chain" asset for RUNE-B1A on Binance main net. | ||
*/ | ||
var AssetRuneB1A = { chain: exports.Chain.Binance, symbol: 'RUNE-B1A', ticker: RUNE_TICKER, synth: false }; | ||
const AssetRuneB1A = { chain: exports.Chain.Binance, symbol: 'RUNE-B1A', ticker: RUNE_TICKER, synth: false }; | ||
/** | ||
@@ -457,3 +418,3 @@ * Base "chain" asset on thorchain main net. | ||
*/ | ||
var AssetRuneNative = { chain: exports.Chain.THORChain, symbol: RUNE_TICKER, ticker: RUNE_TICKER, synth: false }; | ||
const AssetRuneNative = { chain: exports.Chain.THORChain, symbol: RUNE_TICKER, ticker: RUNE_TICKER, synth: false }; | ||
/** | ||
@@ -465,5 +426,5 @@ * Base "chain" asset for RUNE on ethereum main net. | ||
*/ | ||
var AssetRuneERC20 = { | ||
const AssetRuneERC20 = { | ||
chain: exports.Chain.Ethereum, | ||
symbol: RUNE_TICKER + "-0x3155ba85d5f96b2d030a4966af206230e46849cb", | ||
symbol: `${RUNE_TICKER}-0x3155ba85d5f96b2d030a4966af206230e46849cb`, | ||
ticker: RUNE_TICKER, | ||
@@ -478,5 +439,5 @@ synth: false, | ||
*/ | ||
var AssetRuneERC20Testnet = { | ||
const AssetRuneERC20Testnet = { | ||
chain: exports.Chain.Ethereum, | ||
symbol: RUNE_TICKER + "-0xd601c6A3a36721320573885A8d8420746dA3d7A0", | ||
symbol: `${RUNE_TICKER}-0xd601c6A3a36721320573885A8d8420746dA3d7A0`, | ||
ticker: RUNE_TICKER, | ||
@@ -491,3 +452,3 @@ synth: false, | ||
*/ | ||
var isValidAsset = function (asset) { return !!asset.chain && !!asset.ticker && !!asset.symbol; }; | ||
const isValidAsset = (asset) => !!asset.chain && !!asset.ticker && !!asset.symbol; | ||
/** | ||
@@ -499,8 +460,5 @@ * Helper to check whether an asset is synth asset | ||
*/ | ||
var isSynthAsset = function (_a) { | ||
var synth = _a.synth; | ||
return synth; | ||
}; | ||
var SYNTH_DELIMITER = '/'; | ||
var NON_SYNTH_DELIMITER = '.'; | ||
const isSynthAsset = ({ synth }) => synth; | ||
const SYNTH_DELIMITER = '/'; | ||
const NON_SYNTH_DELIMITER = '.'; | ||
/** | ||
@@ -523,17 +481,17 @@ * Creates an `Asset` by a given string | ||
*/ | ||
var assetFromString = function (s) { | ||
const assetFromString = (s) => { | ||
var _a; | ||
var isSynth = s.includes(SYNTH_DELIMITER); | ||
var delimiter = isSynth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
var data = s.split(delimiter); | ||
const isSynth = s.includes(SYNTH_DELIMITER); | ||
const delimiter = isSynth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
const data = s.split(delimiter); | ||
if (data.length <= 1 || ((_a = data[1]) === null || _a === void 0 ? void 0 : _a.length) < 1) { | ||
return null; | ||
} | ||
var chain = data[0]; | ||
const chain = data[0]; | ||
// filter out not supported string of chains | ||
if (!chain || !isChain(chain)) | ||
return null; | ||
var symbol = data[1]; | ||
var ticker = symbol.split('-')[0]; | ||
return { chain: chain, symbol: symbol, ticker: ticker, synth: isSynth }; | ||
const symbol = data[1]; | ||
const ticker = symbol.split('-')[0]; | ||
return { chain, symbol, ticker, synth: isSynth }; | ||
}; | ||
@@ -555,6 +513,5 @@ /** | ||
*/ | ||
var assetToString = function (_a) { | ||
var chain = _a.chain, symbol = _a.symbol, synth = _a.synth; | ||
var delimiter = synth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
return "" + chain + delimiter + symbol; | ||
const assetToString = ({ chain, symbol, synth }) => { | ||
const delimiter = synth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
return `${chain}${delimiter}${symbol}`; | ||
}; | ||
@@ -574,4 +531,3 @@ (function (AssetCurrencySymbol) { | ||
*/ | ||
var currencySymbolByAsset = function (_a) { | ||
var ticker = _a.ticker; | ||
const currencySymbolByAsset = ({ ticker }) => { | ||
switch (true) { | ||
@@ -584,3 +540,3 @@ case ticker === RUNE_TICKER: | ||
return exports.AssetCurrencySymbol.ETH; | ||
case ticker.includes('USD'): | ||
case ticker.includes('USD') || ticker.includes('UST'): | ||
return exports.AssetCurrencySymbol.USD; | ||
@@ -602,7 +558,6 @@ default: | ||
*/ | ||
var formatAssetAmountCurrency = function (_a) { | ||
var _b; | ||
var amount = _a.amount, asset = _a.asset, decimal = _a.decimal, _c = _a.trimZeros, shouldTrimZeros = _c === void 0 ? false : _c; | ||
var amountFormatted = formatAssetAmount({ | ||
amount: amount, | ||
const formatAssetAmountCurrency = ({ amount, asset, decimal, trimZeros: shouldTrimZeros = false, }) => { | ||
var _a; | ||
const amountFormatted = formatAssetAmount({ | ||
amount, | ||
// strict check for `undefined` value as negate of 0 will return true and passed decimal value will be ignored | ||
@@ -612,16 +567,16 @@ decimal: decimal === undefined ? amount.decimal : decimal, | ||
}); | ||
var ticker = (_b = asset === null || asset === void 0 ? void 0 : asset.ticker) !== null && _b !== void 0 ? _b : ''; | ||
const ticker = (_a = asset === null || asset === void 0 ? void 0 : asset.ticker) !== null && _a !== void 0 ? _a : ''; | ||
if (ticker) { | ||
// RUNE | ||
if (ticker === RUNE_TICKER) | ||
return exports.AssetCurrencySymbol.RUNE + " " + amountFormatted; | ||
return `${exports.AssetCurrencySymbol.RUNE} ${amountFormatted}`; | ||
// BTC | ||
var regex = new RegExp(AssetBTC.ticker, 'i'); | ||
let regex = new RegExp(AssetBTC.ticker, 'i'); | ||
if (ticker.match(new RegExp(AssetBTC.ticker, 'i'))) { | ||
var base = assetToBase(amount); | ||
const base = assetToBase(amount); | ||
// format all < ₿ 0.01 in statoshi | ||
if (base.amount().isLessThanOrEqualTo('1000000')) { | ||
return exports.AssetCurrencySymbol.SATOSHI + " " + formatBaseAmount(base); | ||
return `${exports.AssetCurrencySymbol.SATOSHI} ${formatBaseAmount(base)}`; | ||
} | ||
return exports.AssetCurrencySymbol.BTC + " " + amountFormatted; | ||
return `${exports.AssetCurrencySymbol.BTC} ${amountFormatted}`; | ||
} | ||
@@ -631,10 +586,10 @@ // ETH | ||
if (ticker.match(regex)) | ||
return exports.AssetCurrencySymbol.ETH + " " + amountFormatted; | ||
return `${exports.AssetCurrencySymbol.ETH} ${amountFormatted}`; | ||
// USD | ||
regex = new RegExp('USD', 'i'); | ||
if (ticker.match('USD')) | ||
return exports.AssetCurrencySymbol.USD + " " + amountFormatted; | ||
return amountFormatted + " " + ticker; | ||
return `${exports.AssetCurrencySymbol.USD} ${amountFormatted}`; | ||
return `${amountFormatted} ${ticker}`; | ||
} | ||
return "$ " + amountFormatted; | ||
return `$ ${amountFormatted}`; | ||
}; | ||
@@ -650,6 +605,3 @@ /** | ||
*/ | ||
var formatBaseAsAssetAmount = function (_a) { | ||
var amount = _a.amount, decimal = _a.decimal, _b = _a.trimZeros, trimZeros = _b === void 0 ? false : _b; | ||
return formatAssetAmount({ amount: baseToAsset(amount), decimal: decimal, trimZeros: trimZeros }); | ||
}; | ||
const formatBaseAsAssetAmount = ({ amount, decimal, trimZeros = false, }) => formatAssetAmount({ amount: baseToAsset(amount), decimal, trimZeros }); | ||
@@ -656,0 +608,0 @@ exports.AssetBCH = AssetBCH; |
{ | ||
"name": "@xchainjs/xchain-util", | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"description": "Helper utilities for XChain clients", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
157
README.md
# `@xchainjs/xchain-util` | ||
Utitity helpers for XChain clients | ||
Utility helpers for XChain clients | ||
@@ -8,31 +8,22 @@ ## Modules (in alphabetical order) | ||
- `asset` - Utilities for handling assets | ||
- `async` - Utitilies for `async` handling | ||
- `bn` - Utitilies for using `bignumber.js` | ||
- `async` - Utilities for `async` handling | ||
- `bn` - Utilities for using `bignumber.js` | ||
- `chain` - Utilities for multi-chain | ||
- `string` - Utilities for strings | ||
## Usage | ||
## Installation | ||
**BigNumber** | ||
``` | ||
import bn, { | ||
isValidBN, | ||
bnOrZero, | ||
validBNOrZero, | ||
formatBN, | ||
formatBNCurrency, | ||
fixedBN | ||
} from '@xchainjs/xchain-util' | ||
yarn add @xchainjs/xchain-util | ||
``` | ||
The following peer dependencies have to be installed into your project. These are not included in `@xchainjs/xchain-util`. | ||
**Async** | ||
``` | ||
import delay from '@xchainjs/xchain-util' | ||
yarn add bignumber.js | ||
``` | ||
**Asset Helpers** | ||
## Basic example usage for Asset Helpers | ||
``` | ||
Imports | ||
```ts | ||
import { | ||
@@ -51,12 +42,132 @@ assetAmount, | ||
## Installation | ||
## Convert between Asset and base vice versa | ||
Use the helper to convert between base amounts and Assets. | ||
The Decimal represents the denomination of the asset. i.e BTC_DECIMAL = 8. | ||
These are normally a constant exported by the client. | ||
```ts | ||
const assetConversions = async () => { | ||
let amountToTransfer = 0.01 | ||
// convert to type baseAmount | ||
let amount = assetToBase(assetAmount(amountToTransfer, BTC_DECIMAL)) | ||
console.log(`Amount has been converted to type base amount: ${JSON.stringify(amount)}`) | ||
// Convert back to asset amount | ||
let asset = (baseToAsset(amount).amount()) | ||
console.log(`Converted back to ${asset}`) | ||
} | ||
``` | ||
yarn add @xchainjs/xchain-util | ||
## Format helpers for assets | ||
```ts | ||
// Imports | ||
import { | ||
assetToBase, | ||
assetAmount, | ||
formatAssetAmount, | ||
formatBaseAmount, | ||
formatBNCurrency, | ||
AssetBTC, | ||
AssetCurrencySymbol, | ||
formatAssetAmountCurrency, | ||
currencySymbolByAsset, | ||
AssetRuneNative, | ||
AssetETH, | ||
AssetLTC } from "@xchainjs/xchain-util" | ||
// usage Example | ||
const formatHelpers = async () => { | ||
let amountToTransfer = 0.01 | ||
let Amount = assetToBase(assetAmount(amountToTransfer, DECIMAL)) | ||
// Formats an `AssetAmount` into `string` based on decimal places | ||
let formatAsset = formatAssetAmount({ amount: assetAmount(amountToTransfer, BTC_DECIMAL), decimal: BTC_DECIMAL}) | ||
console.log(formatAsset) | ||
// Formats a `BaseAmount` value into a `string` | ||
let formatBase = formatBaseAmount(Amount) | ||
console.log(formatBase) | ||
// Formats a big number value by prefixing it with `$` | ||
let formatBN = formatBNCurrency(Amount.amount()) | ||
console.log(formatBN) | ||
// Formats an asset amount using its currency symbol | ||
let formatAssetCurrency = formatAssetAmountCurrency({ amount: assetAmount(amountToTransfer, BTC_DECIMAL), asset: AssetBTC, decimal: BTC_DECIMAL}) | ||
console.log(formatAssetCurrency) | ||
// Returns Asset symbol from ticker. | ||
let assetA = currencySymbolByAsset(AssetRuneNative) | ||
let assetB = currencySymbolByAsset(AssetBTC) | ||
let assetC = currencySymbolByAsset(AssetETH) | ||
let assetD = currencySymbolByAsset(AssetLTC) | ||
console.log(assetA, assetB, assetC, assetD) | ||
// Respective outputs | ||
/*0.01000000 | ||
1,000,000 | ||
$1,000,000.00 | ||
⚡ 1,000,000 | ||
ᚱ ₿ Ξ LTC */ | ||
} | ||
``` | ||
The following peer dependencies have to be installed into your project. These are not included in `@xchainjs/xchain-util`. | ||
## BigNumber helper functions | ||
```ts | ||
// Imports | ||
import { | ||
bnOrZero, | ||
validBNOrZero, | ||
fixedBN} from "@xchainjs/xchain-util" | ||
const bigNumberhelpers = async () => { | ||
let amountToTransfer = 0.01 | ||
let Amount = assetToBase(assetAmount(amountToTransfer, DECIMAL)) | ||
// Helper to create a big number from string or number If it fails to create a big number | ||
console.log(bnOrZero(amountToTransfer)) | ||
// Helper to validate a possible BigNumber If the given value is invalid or undefined | ||
console.log(validBNOrZero(Amount.amount())) | ||
// Helper to get a fixed BigNumber | ||
console.log(fixedBN(Amount.amount())) | ||
} | ||
``` | ||
yarn add bignumber.js | ||
## Chain helper | ||
```ts | ||
//Imports | ||
import { isChain } from from "@xchainjs/xchain-util" | ||
const chain = async () => { | ||
// Type guard to check whether string is based on type Chain | ||
let thor = "THOR" | ||
console.log(isChain(thor)) // Returns true | ||
} | ||
``` | ||
## Async helper | ||
```ts | ||
//Imports | ||
import delay from '@xchainjs/xchain-util' | ||
// Helper to delay anything within an async function | ||
const anyAsyncFunc = async () => { | ||
// do something | ||
console.log('before delay') | ||
// wait for 200ms | ||
await delay(200) | ||
// and do other things | ||
console.log('after delay') | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
172
137513
1620