@xchainjs/xchain-util
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -1,3 +0,11 @@ | ||
# v.x.x.x (2020-XX-XX) | ||
# v.0.2.1 (2021-01-08) | ||
### Fix | ||
- `assetToBase` ignores `decimal` #174 | ||
### Update | ||
- Update comments for documentation | ||
# v.0.2.0 (2020-12-11) | ||
@@ -4,0 +12,0 @@ |
@@ -6,4 +6,5 @@ import BigNumber from 'bignumber.js'; | ||
* | ||
* @param value - Asset amount - If the value is undefined, AssetAmount with value `0` will be returned | ||
* @param decimal (optional) - Decimal places - default 8 | ||
* @param {string|number|BigNumber|undefined} value - The asset amount, If the value is undefined, AssetAmount with value `0` will be returned. | ||
* @param {number} decimal The decimal places. (optional) | ||
* @returns {AssetAmount} The asset amount from the given value and decimal. | ||
* | ||
@@ -13,11 +14,11 @@ **/ | ||
type: Denomination.ASSET; | ||
amount: () => BigNumber; | ||
/** | ||
amount: () => BigNumber; /** | ||
* Default number of asset decimals | ||
* | ||
* For history reason and by starting the project on Binance chain assets, it's 8 decimal. | ||
* | ||
* For example: | ||
* ``` | ||
* RUNE has a maximum of 8 digits of decimal | ||
* 0.00000001 RUNE == 1 ð (tor) | ||
* ``` | ||
* */ | ||
@@ -29,17 +30,17 @@ decimal: number; | ||
* | ||
* @param value - Base amount - If the value is undefined, BaseAmount with value `0` will be returned | ||
* @param decimal - Decimal places - default 8 | ||
* | ||
* @param {string|number|BigNumber|undefined} value - The base amount, If the value is undefined, BaseAmount with value `0` will be returned. | ||
* @param {number} decimal The decimal places of its associated AssetAmount. (optional) | ||
* @returns {BaseAmount} The base amount from the given value and decimal. | ||
**/ | ||
export declare const baseAmount: (value: string | number | BigNumber | undefined, decimal?: number) => { | ||
type: Denomination.BASE; | ||
amount: () => BigNumber; | ||
/** | ||
amount: () => BigNumber; /** | ||
* Default number of asset decimals | ||
* | ||
* For history reason and by starting the project on Binance chain assets, it's 8 decimal. | ||
* | ||
* For example: | ||
* ``` | ||
* RUNE has a maximum of 8 digits of decimal | ||
* 0.00000001 RUNE == 1 ð (tor) | ||
* ``` | ||
* */ | ||
@@ -50,2 +51,5 @@ decimal: number; | ||
* Helper to convert values for a asset from base values (e.g. RUNE from tor) | ||
* | ||
* @param {BaseAmount} base | ||
* @returns {AssetAmount} The asset amount from the given base amount. | ||
* */ | ||
@@ -55,2 +59,5 @@ export declare const baseToAsset: (base: BaseAmount) => AssetAmount; | ||
* Helper to convert asset to base values (e.g. tor -> RUNE) | ||
* | ||
* @param {AssetAmount} asset | ||
* @returns {BaseAmount} The base amount from the given AssetAmount. | ||
* */ | ||
@@ -60,14 +67,17 @@ export declare const assetToBase: (asset: AssetAmount) => BaseAmount; | ||
* Guard to check whether value is an amount of asset or not | ||
* | ||
* @param {BaseAmount|AssetAmount} v | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
export declare const isAssetAmount: (v: Amounts) => v is { | ||
type: Denomination.ASSET; | ||
amount: () => BigNumber; | ||
/** | ||
amount: () => BigNumber; /** | ||
* Default number of asset decimals | ||
* | ||
* For history reason and by starting the project on Binance chain assets, it's 8 decimal. | ||
* | ||
* For example: | ||
* ``` | ||
* RUNE has a maximum of 8 digits of decimal | ||
* 0.00000001 RUNE == 1 ð (tor) | ||
* ``` | ||
* */ | ||
@@ -78,14 +88,17 @@ decimal: number; | ||
* Guard to check whether value is an amount of a base value or not | ||
* | ||
* @param {BaseAmount|AssetAmount} v | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
export declare const isBaseAmount: (v: Amounts) => v is { | ||
type: Denomination.BASE; | ||
amount: () => BigNumber; | ||
/** | ||
amount: () => BigNumber; /** | ||
* Default number of asset decimals | ||
* | ||
* For history reason and by starting the project on Binance chain assets, it's 8 decimal. | ||
* | ||
* For example: | ||
* ``` | ||
* RUNE has a maximum of 8 digits of decimal | ||
* 0.00000001 RUNE == 1 ð (tor) | ||
* ``` | ||
* */ | ||
@@ -99,2 +112,5 @@ decimal: number; | ||
* Note: `trimZeros` wins over `decimal` | ||
* | ||
* @param {Params} param The asset amount format options. | ||
* @returns {string} The formatted asset amount string from the given options. | ||
*/ | ||
@@ -108,21 +124,57 @@ export declare const formatAssetAmount: ({ amount, decimal, trimZeros, }: { | ||
* Formats a `BaseAmount` value into a `string` | ||
* | ||
* @param {BaseAmount} amount | ||
* @returns {string} The formatted base amount string from the given base amount. | ||
*/ | ||
export declare const formatBaseAmount: (amount: BaseAmount) => string; | ||
/** | ||
* Base "chain" assets | ||
* Base "chain" asset of Binance chain. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
export declare const AssetBNB: Asset; | ||
/** | ||
* Base "chain" asset on bitcoin main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
export declare const AssetBTC: Asset; | ||
/** | ||
* Base "chain" asset on ethereum main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
export declare const AssetETH: Asset; | ||
export declare const RUNE_TICKER = "RUNE"; | ||
/** | ||
* Base "chain" asset for RUNE-67C on Binance test net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
export declare const AssetRune67C: Asset; | ||
/** | ||
* Base "chain" asset for RUNE-B1A on Binance main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
export declare const AssetRuneB1A: Asset; | ||
/** | ||
* Base "chain" asset on thorchain main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
export declare const AssetRuneNative: Asset; | ||
/** | ||
* Helper to check whether asset is valid | ||
* | ||
* @param {Asset} asset | ||
* @returns {boolean} `true` or `false` | ||
*/ | ||
export declare const isValidAsset: (a: Asset) => boolean; | ||
export declare const isValidAsset: (asset: Asset) => boolean; | ||
/** | ||
@@ -141,2 +193,5 @@ * Creates an `Asset` by a given string | ||
* If the naming convention fails, it returns null | ||
* | ||
* @param {string} s The given string. | ||
* @returns {Asset|null} The asset from the given string. | ||
*/ | ||
@@ -155,2 +210,4 @@ export declare const assetFromString: (s: string) => Asset | null; | ||
* | ||
* @param {Asset} asset The given asset. | ||
* @returns {string} The string from the given asset. | ||
*/ | ||
@@ -169,3 +226,6 @@ export declare const assetToString: ({ chain, symbol }: Asset) => string; | ||
/** | ||
* Returns currency symbols by givven `Asset` | ||
* Returns currency symbols by given `Asset` | ||
* | ||
* @param {Asset} asset The given asset. | ||
* @returns {string} The currency symbol from the given asset. | ||
*/ | ||
@@ -180,2 +240,5 @@ export declare const currencySymbolByAsset: ({ ticker }: Asset) => string; | ||
* Note: `trimZeros` wins over `decimal` | ||
* | ||
* @param {Params} params The asset amount currency format options. | ||
* @return {string} The formatted asset amount string using its currency format. | ||
*/ | ||
@@ -193,2 +256,5 @@ export declare const formatAssetAmountCurrency: ({ amount, asset, decimal, trimZeros: shouldTrimZeros, }: { | ||
* Note: `trimZeros` wins over `decimal` | ||
* | ||
* @param {Params} params The base amount currency format options. | ||
* @return {string} The formatted base amount string using its currency format. | ||
*/ | ||
@@ -195,0 +261,0 @@ export declare const formatBaseAsAssetAmount: ({ amount, decimal, trimZeros, }: { |
@@ -8,2 +8,3 @@ /** | ||
* | ||
* ``` | ||
* const anyAsyncFunc = async () => { | ||
@@ -17,3 +18,4 @@ * // do something | ||
* } | ||
* ``` | ||
*/ | ||
export declare const delay: (ms: number) => Promise<unknown>; |
import BigNumber from 'bignumber.js'; | ||
/** | ||
* Shortcut to create a BigNumber | ||
* | ||
* @param {string | number | BigNumber.Instance} value | ||
* @returns {BigNumber} The BigNumber interface from the given value. | ||
*/ | ||
@@ -8,2 +11,5 @@ declare const bn: (value: BigNumber.Value) => BigNumber; | ||
* Helper to check whether a BigNumber is valid or not | ||
* | ||
* @param {BigNumber} value | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
@@ -14,2 +20,5 @@ export declare const isValidBN: (value: BigNumber) => boolean; | ||
* If it fails to create a big number, a big number with value 0 will be returned instead | ||
* | ||
* @param {string|number|undefined} value | ||
* @returns {BigNumber} The BigNumber interface from the given value. If invalid one is provided, will return `0`. | ||
* */ | ||
@@ -20,2 +29,5 @@ export declare const bnOrZero: (value: string | number | undefined) => BigNumber; | ||
* If the given valie is invalid or undefined, 0 is returned as a BigNumber | ||
* | ||
* @param {BigNumber|undefined} value | ||
* @returns {boolean} `true` or `false`. | ||
*/ | ||
@@ -25,4 +37,12 @@ export declare const validBNOrZero: (value: BigNumber | undefined) => BigNumber; | ||
* Format a BaseNumber to a string depending on given decimal places | ||
* | ||
* @param {BigNumber} value | ||
* @param {number} decimal The decimal place. (optional) | ||
* @returns {string} The formatted string from the given BigNumber and decimal place. | ||
* */ | ||
export declare const formatBN: (value: BigNumber, decimalPlaces?: number) => string; | ||
export declare const formatBN: (value: BigNumber, decimal?: number) => string; | ||
/** | ||
* The enumuration for symbol position. | ||
* `before` or `after` | ||
*/ | ||
export declare enum SymbolPosition { | ||
@@ -34,2 +54,8 @@ BEFORE = "before", | ||
* Formats a big number value by prefixing it with `$` | ||
* | ||
* @param {BigNumber} n | ||
* @param {number} decimalPlaces The decimal place. (optional) | ||
* @param {string} symbol The currency symbol. (optional) | ||
* @param {position} position The symbol position. (optional) | ||
* @returns {string} The formatted string from the given BigNumber, decimal places, symbol and position. | ||
*/ | ||
@@ -40,4 +66,8 @@ export declare const formatBNCurrency: (n: BigNumber, decimalPlaces?: number, symbol?: string, position?: SymbolPosition) => string; | ||
* Returns zero `BigNumber` if `value` is invalid | ||
* | ||
* @param {number|string|BigNumber|undefined} value | ||
* @param {number} decimalPlaces The decimal place. (optional) | ||
* @returns {BigNumber} The BigNumber interface from the given value and decimal. | ||
* */ | ||
export declare const fixedBN: (value: number | string | BigNumber | undefined, decimalPlaces?: number) => BigNumber; | ||
export default bn; |
/** | ||
* Chains | ||
* Binance Chain | ||
*/ | ||
export declare const BNBChain = "BNB"; | ||
/** | ||
* Bitcoin Chain | ||
*/ | ||
export declare const BTCChain = "BTC"; | ||
/** | ||
* Ethereum Chain | ||
*/ | ||
export declare const ETHChain = "ETH"; | ||
/** | ||
* Thorchain | ||
*/ | ||
export declare const THORChain = "THOR"; | ||
/** | ||
* Cosmos Chain | ||
*/ | ||
export declare const CosmosChain = "GAIA"; | ||
/** | ||
* Polkadot Chain | ||
*/ | ||
export declare const PolkadotChain = "POLKA"; | ||
@@ -10,0 +25,0 @@ /** |
import { Chain } from './types'; | ||
/** | ||
* Type guard to check whether string is based on type `Chain` | ||
* | ||
* @param {string} c The chain string. | ||
* @returns {boolean} `true` or `false` | ||
*/ | ||
export declare const isChain: (c: string) => c is "BNB" | "BTC" | "ETH" | "THOR" | "GAIA" | "POLKA"; | ||
/** | ||
* Convert chain to string. | ||
* | ||
* @param {Chain} chainId. | ||
* @returns {string} The string based on the given chain type. | ||
*/ | ||
export declare const chainToString: (chainId: Chain) => "Thorchain" | "Bitcoin" | "Ethereum" | "Binance Chain" | "Cosmos" | "Polkadot" | "unknown chain"; |
@@ -10,2 +10,3 @@ import BigNumber from 'bignumber.js'; | ||
* | ||
* ``` | ||
* const anyAsyncFunc = async () => { | ||
@@ -19,2 +20,3 @@ * // do something | ||
* } | ||
* ``` | ||
*/ | ||
@@ -25,2 +27,5 @@ var delay = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); }; | ||
* Shortcut to create a BigNumber | ||
* | ||
* @param {string | number | BigNumber.Instance} value | ||
* @returns {BigNumber} The BigNumber interface from the given value. | ||
*/ | ||
@@ -30,2 +35,5 @@ var bn = function (value) { return new BigNumber(value); }; | ||
* Helper to check whether a BigNumber is valid or not | ||
* | ||
* @param {BigNumber} value | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
@@ -36,2 +44,5 @@ var isValidBN = function (value) { return !value.isNaN(); }; | ||
* If it fails to create a big number, a big number with value 0 will be returned instead | ||
* | ||
* @param {string|number|undefined} value | ||
* @returns {BigNumber} The BigNumber interface from the given value. If invalid one is provided, will return `0`. | ||
* */ | ||
@@ -45,2 +56,5 @@ var bnOrZero = function (value) { | ||
* If the given valie is invalid or undefined, 0 is returned as a BigNumber | ||
* | ||
* @param {BigNumber|undefined} value | ||
* @returns {boolean} `true` or `false`. | ||
*/ | ||
@@ -50,7 +64,15 @@ var validBNOrZero = function (value) { return (value && isValidBN(value) ? value : bn(0)); }; | ||
* Format a BaseNumber to a string depending on given decimal places | ||
* | ||
* @param {BigNumber} value | ||
* @param {number} decimal The decimal place. (optional) | ||
* @returns {string} The formatted string from the given BigNumber and decimal place. | ||
* */ | ||
var formatBN = function (value, decimalPlaces) { | ||
if (decimalPlaces === void 0) { decimalPlaces = 2; } | ||
return value.toFormat(decimalPlaces); | ||
var formatBN = function (value, decimal) { | ||
if (decimal === void 0) { decimal = 2; } | ||
return value.toFormat(decimal); | ||
}; | ||
/** | ||
* The enumuration for symbol position. | ||
* `before` or `after` | ||
*/ | ||
var SymbolPosition; | ||
@@ -63,2 +85,8 @@ (function (SymbolPosition) { | ||
* Formats a big number value by prefixing it with `$` | ||
* | ||
* @param {BigNumber} n | ||
* @param {number} decimalPlaces The decimal place. (optional) | ||
* @param {string} symbol The currency symbol. (optional) | ||
* @param {position} position The symbol position. (optional) | ||
* @returns {string} The formatted string from the given BigNumber, decimal places, symbol and position. | ||
*/ | ||
@@ -78,2 +106,6 @@ var formatBNCurrency = function (n, decimalPlaces, symbol, position) { | ||
* Returns zero `BigNumber` if `value` is invalid | ||
* | ||
* @param {number|string|BigNumber|undefined} value | ||
* @param {number} decimalPlaces The decimal place. (optional) | ||
* @returns {BigNumber} The BigNumber interface from the given value and decimal. | ||
* */ | ||
@@ -91,2 +123,5 @@ var fixedBN = function (value, decimalPlaces) { | ||
* (2) Regex to remove leading zeros https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch06s06.html | ||
* | ||
* @param {string} value | ||
* @returns {string} The result after removing trailing zeros. | ||
*/ | ||
@@ -102,9 +137,24 @@ var trimZeros = function (value) { | ||
/** | ||
* Chains | ||
* Binance Chain | ||
*/ | ||
var BNBChain = 'BNB'; | ||
/** | ||
* Bitcoin Chain | ||
*/ | ||
var BTCChain = 'BTC'; | ||
/** | ||
* Ethereum Chain | ||
*/ | ||
var ETHChain = 'ETH'; | ||
/** | ||
* Thorchain | ||
*/ | ||
var THORChain = 'THOR'; | ||
/** | ||
* Cosmos Chain | ||
*/ | ||
var CosmosChain = 'GAIA'; | ||
/** | ||
* Polkadot Chain | ||
*/ | ||
var PolkadotChain = 'POLKA'; | ||
@@ -118,4 +168,13 @@ /** | ||
* Type guard to check whether string is based on type `Chain` | ||
* | ||
* @param {string} c The chain string. | ||
* @returns {boolean} `true` or `false` | ||
*/ | ||
var isChain = function (c) { return chains.includes(c); }; | ||
/** | ||
* Convert chain to string. | ||
* | ||
* @param {Chain} chainId. | ||
* @returns {string} The string based on the given chain type. | ||
*/ | ||
var chainToString = function (chainId) { | ||
@@ -154,8 +213,9 @@ switch (chainId) { | ||
* Default number of asset decimals | ||
* | ||
* For history reason and by starting the project on Binance chain assets, it's 8 decimal. | ||
* | ||
* For example: | ||
* ``` | ||
* RUNE has a maximum of 8 digits of decimal | ||
* 0.00000001 RUNE == 1 ð (tor) | ||
* ``` | ||
* */ | ||
@@ -166,4 +226,5 @@ var ASSET_DECIMAL = 8; | ||
* | ||
* @param value - Asset amount - If the value is undefined, AssetAmount with value `0` will be returned | ||
* @param decimal (optional) - Decimal places - default 8 | ||
* @param {string|number|BigNumber|undefined} value - The asset amount, If the value is undefined, AssetAmount with value `0` will be returned. | ||
* @param {number} decimal The decimal places. (optional) | ||
* @returns {AssetAmount} The asset amount from the given value and decimal. | ||
* | ||
@@ -182,5 +243,5 @@ **/ | ||
* | ||
* @param value - Base amount - If the value is undefined, BaseAmount with value `0` will be returned | ||
* @param decimal - Decimal places - default 8 | ||
* | ||
* @param {string|number|BigNumber|undefined} value - The base amount, If the value is undefined, BaseAmount with value `0` will be returned. | ||
* @param {number} decimal The decimal places of its associated AssetAmount. (optional) | ||
* @returns {BaseAmount} The base amount from the given value and decimal. | ||
**/ | ||
@@ -197,2 +258,5 @@ var baseAmount = function (value, decimal) { | ||
* Helper to convert values for a asset from base values (e.g. RUNE from tor) | ||
* | ||
* @param {BaseAmount} base | ||
* @returns {AssetAmount} The asset amount from the given base amount. | ||
* */ | ||
@@ -209,2 +273,5 @@ var baseToAsset = function (base) { | ||
* Helper to convert asset to base values (e.g. tor -> RUNE) | ||
* | ||
* @param {AssetAmount} asset | ||
* @returns {BaseAmount} The base amount from the given AssetAmount. | ||
* */ | ||
@@ -216,6 +283,9 @@ var assetToBase = function (asset) { | ||
.integerValue(); | ||
return baseAmount(value); | ||
return baseAmount(value, asset.decimal); | ||
}; | ||
/** | ||
* Guard to check whether value is an amount of asset or not | ||
* | ||
* @param {BaseAmount|AssetAmount} v | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
@@ -225,2 +295,5 @@ var isAssetAmount = function (v) { return v.type === Denomination.ASSET; }; | ||
* Guard to check whether value is an amount of a base value or not | ||
* | ||
* @param {BaseAmount|AssetAmount} v | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
@@ -233,2 +306,5 @@ var isBaseAmount = function (v) { return v.type === Denomination.BASE; }; | ||
* Note: `trimZeros` wins over `decimal` | ||
* | ||
* @param {Params} param The asset amount format options. | ||
* @returns {string} The formatted asset amount string from the given options. | ||
*/ | ||
@@ -243,23 +319,57 @@ var formatAssetAmount = function (_a) { | ||
* Formats a `BaseAmount` value into a `string` | ||
* | ||
* @param {BaseAmount} amount | ||
* @returns {string} The formatted base amount string from the given base amount. | ||
*/ | ||
var formatBaseAmount = function (amount) { return formatBN(amount.amount(), 0); }; | ||
/** | ||
* Base "chain" assets | ||
* Base "chain" asset of Binance chain. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetBNB = { chain: BNBChain, symbol: 'BNB', ticker: 'BNB' }; | ||
/** | ||
* Base "chain" asset on bitcoin main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetBTC = { chain: BTCChain, symbol: 'BTC', ticker: 'BTC' }; | ||
/** | ||
* Base "chain" asset on ethereum main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetETH = { chain: ETHChain, symbol: 'ETH', ticker: 'ETH' }; | ||
var RUNE_TICKER = 'RUNE'; | ||
// Rune67CAsset RUNE on Binance test net | ||
/** | ||
* Base "chain" asset for RUNE-67C on Binance test net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetRune67C = { chain: BNBChain, symbol: 'RUNE-67C', ticker: RUNE_TICKER }; | ||
// RuneB1AAsset RUNE on Binance main net | ||
/** | ||
* Base "chain" asset for RUNE-B1A on Binance main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetRuneB1A = { chain: BNBChain, symbol: 'RUNE-B1A', ticker: RUNE_TICKER }; | ||
/** | ||
* Base "chain" asset on thorchain main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetRuneNative = { chain: THORChain, symbol: RUNE_TICKER, ticker: RUNE_TICKER }; | ||
/** | ||
* Helper to check whether asset is valid | ||
* | ||
* @param {Asset} asset | ||
* @returns {boolean} `true` or `false` | ||
*/ | ||
var isValidAsset = function (a) { return !!a.chain && !!a.ticker && !!a.symbol; }; | ||
var isValidAsset = function (asset) { return !!asset.chain && !!asset.ticker && !!asset.symbol; }; | ||
/** | ||
@@ -278,2 +388,5 @@ * Creates an `Asset` by a given string | ||
* If the naming convention fails, it returns null | ||
* | ||
* @param {string} s The given string. | ||
* @returns {Asset|null} The asset from the given string. | ||
*/ | ||
@@ -305,2 +418,4 @@ var assetFromString = function (s) { | ||
* | ||
* @param {Asset} asset The given asset. | ||
* @returns {string} The string from the given asset. | ||
*/ | ||
@@ -323,3 +438,6 @@ var assetToString = function (_a) { | ||
/** | ||
* Returns currency symbols by givven `Asset` | ||
* Returns currency symbols by given `Asset` | ||
* | ||
* @param {Asset} asset The given asset. | ||
* @returns {string} The currency symbol from the given asset. | ||
*/ | ||
@@ -348,2 +466,5 @@ var currencySymbolByAsset = function (_a) { | ||
* Note: `trimZeros` wins over `decimal` | ||
* | ||
* @param {Params} params The asset amount currency format options. | ||
* @return {string} The formatted asset amount string using its currency format. | ||
*/ | ||
@@ -387,2 +508,5 @@ var formatAssetAmountCurrency = function (_a) { | ||
* Note: `trimZeros` wins over `decimal` | ||
* | ||
* @param {Params} params The base amount currency format options. | ||
* @return {string} The formatted base amount string using its currency format. | ||
*/ | ||
@@ -389,0 +513,0 @@ var formatBaseAsAssetAmount = function (_a) { |
158
lib/index.js
@@ -18,2 +18,3 @@ 'use strict'; | ||
* | ||
* ``` | ||
* const anyAsyncFunc = async () => { | ||
@@ -27,2 +28,3 @@ * // do something | ||
* } | ||
* ``` | ||
*/ | ||
@@ -33,2 +35,5 @@ var delay = function (ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); }; | ||
* Shortcut to create a BigNumber | ||
* | ||
* @param {string | number | BigNumber.Instance} value | ||
* @returns {BigNumber} The BigNumber interface from the given value. | ||
*/ | ||
@@ -38,2 +43,5 @@ var bn = function (value) { return new BigNumber__default['default'](value); }; | ||
* Helper to check whether a BigNumber is valid or not | ||
* | ||
* @param {BigNumber} value | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
@@ -44,2 +52,5 @@ var isValidBN = function (value) { return !value.isNaN(); }; | ||
* If it fails to create a big number, a big number with value 0 will be returned instead | ||
* | ||
* @param {string|number|undefined} value | ||
* @returns {BigNumber} The BigNumber interface from the given value. If invalid one is provided, will return `0`. | ||
* */ | ||
@@ -53,2 +64,5 @@ var bnOrZero = function (value) { | ||
* If the given valie is invalid or undefined, 0 is returned as a BigNumber | ||
* | ||
* @param {BigNumber|undefined} value | ||
* @returns {boolean} `true` or `false`. | ||
*/ | ||
@@ -58,7 +72,15 @@ var validBNOrZero = function (value) { return (value && isValidBN(value) ? value : bn(0)); }; | ||
* Format a BaseNumber to a string depending on given decimal places | ||
* | ||
* @param {BigNumber} value | ||
* @param {number} decimal The decimal place. (optional) | ||
* @returns {string} The formatted string from the given BigNumber and decimal place. | ||
* */ | ||
var formatBN = function (value, decimalPlaces) { | ||
if (decimalPlaces === void 0) { decimalPlaces = 2; } | ||
return value.toFormat(decimalPlaces); | ||
var formatBN = function (value, decimal) { | ||
if (decimal === void 0) { decimal = 2; } | ||
return value.toFormat(decimal); | ||
}; | ||
/** | ||
* The enumuration for symbol position. | ||
* `before` or `after` | ||
*/ | ||
var SymbolPosition; | ||
@@ -71,2 +93,8 @@ (function (SymbolPosition) { | ||
* Formats a big number value by prefixing it with `$` | ||
* | ||
* @param {BigNumber} n | ||
* @param {number} decimalPlaces The decimal place. (optional) | ||
* @param {string} symbol The currency symbol. (optional) | ||
* @param {position} position The symbol position. (optional) | ||
* @returns {string} The formatted string from the given BigNumber, decimal places, symbol and position. | ||
*/ | ||
@@ -86,2 +114,6 @@ var formatBNCurrency = function (n, decimalPlaces, symbol, position) { | ||
* Returns zero `BigNumber` if `value` is invalid | ||
* | ||
* @param {number|string|BigNumber|undefined} value | ||
* @param {number} decimalPlaces The decimal place. (optional) | ||
* @returns {BigNumber} The BigNumber interface from the given value and decimal. | ||
* */ | ||
@@ -99,2 +131,5 @@ var fixedBN = function (value, decimalPlaces) { | ||
* (2) Regex to remove leading zeros https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch06s06.html | ||
* | ||
* @param {string} value | ||
* @returns {string} The result after removing trailing zeros. | ||
*/ | ||
@@ -110,9 +145,24 @@ var trimZeros = function (value) { | ||
/** | ||
* Chains | ||
* Binance Chain | ||
*/ | ||
var BNBChain = 'BNB'; | ||
/** | ||
* Bitcoin Chain | ||
*/ | ||
var BTCChain = 'BTC'; | ||
/** | ||
* Ethereum Chain | ||
*/ | ||
var ETHChain = 'ETH'; | ||
/** | ||
* Thorchain | ||
*/ | ||
var THORChain = 'THOR'; | ||
/** | ||
* Cosmos Chain | ||
*/ | ||
var CosmosChain = 'GAIA'; | ||
/** | ||
* Polkadot Chain | ||
*/ | ||
var PolkadotChain = 'POLKA'; | ||
@@ -126,4 +176,13 @@ /** | ||
* Type guard to check whether string is based on type `Chain` | ||
* | ||
* @param {string} c The chain string. | ||
* @returns {boolean} `true` or `false` | ||
*/ | ||
var isChain = function (c) { return chains.includes(c); }; | ||
/** | ||
* Convert chain to string. | ||
* | ||
* @param {Chain} chainId. | ||
* @returns {string} The string based on the given chain type. | ||
*/ | ||
var chainToString = function (chainId) { | ||
@@ -161,8 +220,9 @@ switch (chainId) { | ||
* Default number of asset decimals | ||
* | ||
* For history reason and by starting the project on Binance chain assets, it's 8 decimal. | ||
* | ||
* For example: | ||
* ``` | ||
* RUNE has a maximum of 8 digits of decimal | ||
* 0.00000001 RUNE == 1 ð (tor) | ||
* ``` | ||
* */ | ||
@@ -173,4 +233,5 @@ var ASSET_DECIMAL = 8; | ||
* | ||
* @param value - Asset amount - If the value is undefined, AssetAmount with value `0` will be returned | ||
* @param decimal (optional) - Decimal places - default 8 | ||
* @param {string|number|BigNumber|undefined} value - The asset amount, If the value is undefined, AssetAmount with value `0` will be returned. | ||
* @param {number} decimal The decimal places. (optional) | ||
* @returns {AssetAmount} The asset amount from the given value and decimal. | ||
* | ||
@@ -189,5 +250,5 @@ **/ | ||
* | ||
* @param value - Base amount - If the value is undefined, BaseAmount with value `0` will be returned | ||
* @param decimal - Decimal places - default 8 | ||
* | ||
* @param {string|number|BigNumber|undefined} value - The base amount, If the value is undefined, BaseAmount with value `0` will be returned. | ||
* @param {number} decimal The decimal places of its associated AssetAmount. (optional) | ||
* @returns {BaseAmount} The base amount from the given value and decimal. | ||
**/ | ||
@@ -204,2 +265,5 @@ var baseAmount = function (value, decimal) { | ||
* Helper to convert values for a asset from base values (e.g. RUNE from tor) | ||
* | ||
* @param {BaseAmount} base | ||
* @returns {AssetAmount} The asset amount from the given base amount. | ||
* */ | ||
@@ -216,2 +280,5 @@ var baseToAsset = function (base) { | ||
* Helper to convert asset to base values (e.g. tor -> RUNE) | ||
* | ||
* @param {AssetAmount} asset | ||
* @returns {BaseAmount} The base amount from the given AssetAmount. | ||
* */ | ||
@@ -223,6 +290,9 @@ var assetToBase = function (asset) { | ||
.integerValue(); | ||
return baseAmount(value); | ||
return baseAmount(value, asset.decimal); | ||
}; | ||
/** | ||
* Guard to check whether value is an amount of asset or not | ||
* | ||
* @param {BaseAmount|AssetAmount} v | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
@@ -232,2 +302,5 @@ var isAssetAmount = function (v) { return v.type === exports.Denomination.ASSET; }; | ||
* Guard to check whether value is an amount of a base value or not | ||
* | ||
* @param {BaseAmount|AssetAmount} v | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
@@ -240,2 +313,5 @@ var isBaseAmount = function (v) { return v.type === exports.Denomination.BASE; }; | ||
* Note: `trimZeros` wins over `decimal` | ||
* | ||
* @param {Params} param The asset amount format options. | ||
* @returns {string} The formatted asset amount string from the given options. | ||
*/ | ||
@@ -250,23 +326,57 @@ var formatAssetAmount = function (_a) { | ||
* Formats a `BaseAmount` value into a `string` | ||
* | ||
* @param {BaseAmount} amount | ||
* @returns {string} The formatted base amount string from the given base amount. | ||
*/ | ||
var formatBaseAmount = function (amount) { return formatBN(amount.amount(), 0); }; | ||
/** | ||
* Base "chain" assets | ||
* Base "chain" asset of Binance chain. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetBNB = { chain: BNBChain, symbol: 'BNB', ticker: 'BNB' }; | ||
/** | ||
* Base "chain" asset on bitcoin main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetBTC = { chain: BTCChain, symbol: 'BTC', ticker: 'BTC' }; | ||
/** | ||
* Base "chain" asset on ethereum main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetETH = { chain: ETHChain, symbol: 'ETH', ticker: 'ETH' }; | ||
var RUNE_TICKER = 'RUNE'; | ||
// Rune67CAsset RUNE on Binance test net | ||
/** | ||
* Base "chain" asset for RUNE-67C on Binance test net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetRune67C = { chain: BNBChain, symbol: 'RUNE-67C', ticker: RUNE_TICKER }; | ||
// RuneB1AAsset RUNE on Binance main net | ||
/** | ||
* Base "chain" asset for RUNE-B1A on Binance main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetRuneB1A = { chain: BNBChain, symbol: 'RUNE-B1A', ticker: RUNE_TICKER }; | ||
/** | ||
* Base "chain" asset on thorchain main net. | ||
* | ||
* Based on definition in Thorchain `common` | ||
* @see https://gitlab.com/thorchain/thornode/-/blob/master/common/asset.go#L12-24 | ||
*/ | ||
var AssetRuneNative = { chain: THORChain, symbol: RUNE_TICKER, ticker: RUNE_TICKER }; | ||
/** | ||
* Helper to check whether asset is valid | ||
* | ||
* @param {Asset} asset | ||
* @returns {boolean} `true` or `false` | ||
*/ | ||
var isValidAsset = function (a) { return !!a.chain && !!a.ticker && !!a.symbol; }; | ||
var isValidAsset = function (asset) { return !!asset.chain && !!asset.ticker && !!asset.symbol; }; | ||
/** | ||
@@ -285,2 +395,5 @@ * Creates an `Asset` by a given string | ||
* If the naming convention fails, it returns null | ||
* | ||
* @param {string} s The given string. | ||
* @returns {Asset|null} The asset from the given string. | ||
*/ | ||
@@ -312,2 +425,4 @@ var assetFromString = function (s) { | ||
* | ||
* @param {Asset} asset The given asset. | ||
* @returns {string} The string from the given asset. | ||
*/ | ||
@@ -326,3 +441,6 @@ var assetToString = function (_a) { | ||
/** | ||
* Returns currency symbols by givven `Asset` | ||
* Returns currency symbols by given `Asset` | ||
* | ||
* @param {Asset} asset The given asset. | ||
* @returns {string} The currency symbol from the given asset. | ||
*/ | ||
@@ -351,2 +469,5 @@ var currencySymbolByAsset = function (_a) { | ||
* Note: `trimZeros` wins over `decimal` | ||
* | ||
* @param {Params} params The asset amount currency format options. | ||
* @return {string} The formatted asset amount string using its currency format. | ||
*/ | ||
@@ -390,2 +511,5 @@ var formatAssetAmountCurrency = function (_a) { | ||
* Note: `trimZeros` wins over `decimal` | ||
* | ||
* @param {Params} params The base amount currency format options. | ||
* @return {string} The formatted base amount string using its currency format. | ||
*/ | ||
@@ -392,0 +516,0 @@ var formatBaseAsAssetAmount = function (_a) { |
@@ -5,3 +5,6 @@ /** | ||
* (2) Regex to remove leading zeros https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch06s06.html | ||
* | ||
* @param {string} value | ||
* @returns {string} The result after removing trailing zeros. | ||
*/ | ||
export declare const trimZeros: (value: string) => string; |
{ | ||
"name": "@xchainjs/xchain-util", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Helper utilities for XChain clients", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
102763
1463
0
21