@xchainjs/xchain-util
Version:
Helper utilities for XChain clients
242 lines (241 loc) • 8.57 kB
TypeScript
import BigNumber from 'bignumber.js';
import { Amount, AnyAsset, AssetAmount, BaseAmount, Denomination, SecuredAsset, SynthAsset, TokenAsset, TradeAsset } from './types';
export type Address = string;
/**
* Guard to check whether value is a BigNumber.Value or not
*
* @param {unknown} v
* @returns {boolean} `true` or `false`.
* */
export declare const isBigNumberValue: (v: unknown) => v is BigNumber.Value;
/**
* Native asset delimiter
*/
export declare const NATIVE_ASSET_DELIMITER = ".";
/**
* Token asset delimiter
*/
export declare const TOKEN_ASSET_DELIMITER = ".";
/**
* Synth asset delimiter
*/
export declare const SYNTH_ASSET_DELIMITER = "/";
/**
* Trade asset delimiter
*/
export declare const TRADE_ASSET_DELIMITER = "~";
/**
* Secured asset delimiter
*/
export declare const SECURED_ASSET_DELIMITER = "-";
/**
* Factory to create values of assets (e.g. RUNE)
*
* @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.
*
**/
export declare const assetAmount: (value: BigNumber.Value | undefined, decimal?: number) => AssetAmount;
/**
* Factory to create base amounts (e.g. tor)
*
* @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: BigNumber.Value | undefined, decimal?: number) => BaseAmount;
/**
* 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.
* */
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.
* */
export declare const assetToBase: (asset: AssetAmount) => BaseAmount;
/**
* Guard to check whether value is an amount of asset or not
*
* @param {Amount<Denomination>} v
* @returns {boolean} `true` or `false`.
* */
export declare const isAssetAmount: (v: Amount<Denomination>) => v is {
type: Denomination.Asset;
amount: () => BigNumber;
plus: (value: BigNumber.Value | any, decimal?: number | undefined) => any;
minus: (value: BigNumber.Value | any, decimal?: number | undefined) => any;
times: (value: BigNumber.Value | any, decimal?: number | undefined) => any;
div: (value: BigNumber.Value | any, decimal?: number | undefined) => any;
gt: (value: BigNumber.Value | any) => boolean;
gte: (value: BigNumber.Value | any) => boolean;
lt: (value: BigNumber.Value | any) => boolean;
lte: (value: BigNumber.Value | any) => boolean;
eq: (value: BigNumber.Value | any) => boolean;
decimal: number;
};
/**
* Guard to check whether value is an amount of a base value or not
*
* @param {Amount<Denomination>} v
* @returns {boolean} `true` or `false`.
* */
export declare const isBaseAmount: (v: Amount<Denomination>) => v is {
type: Denomination.Base;
amount: () => BigNumber;
plus: (value: BigNumber.Value | any, decimal?: number | undefined) => any;
minus: (value: BigNumber.Value | any, decimal?: number | undefined) => any;
times: (value: BigNumber.Value | any, decimal?: number | undefined) => any;
div: (value: BigNumber.Value | any, decimal?: number | undefined) => any;
gt: (value: BigNumber.Value | any) => boolean;
gte: (value: BigNumber.Value | any) => boolean;
lt: (value: BigNumber.Value | any) => boolean;
lte: (value: BigNumber.Value | any) => boolean;
eq: (value: BigNumber.Value | any) => boolean;
decimal: number;
};
/**
* Formats an `AssetAmount` into `string` based on decimal places
*
* If `decimal` is not set, `amount.decimal` is used
* Note: `trimZeros` wins over `decimal`
*
* @param {Params} param The asset amount format options.
* @returns {string} The formatted asset amount string from the given options.
*/
export declare const formatAssetAmount: ({ amount, decimal, trimZeros, }: {
amount: AssetAmount;
decimal?: number | undefined;
trimZeros?: boolean | undefined;
}) => string;
/**
* 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;
/**
* Helper to check whether asset is valid
*
* @param {Asset} asset
* @returns {boolean} `true` or `false`
*/
export declare const isValidAsset: (asset: AnyAsset) => boolean;
/**
* Helper to check whether an asset is synth asset
*
* @param {Asset} asset
* @returns {boolean} `true` or `false`
*/
export declare const isSynthAsset: (asset: AnyAsset) => asset is SynthAsset;
/**
* Helper to check whether an asset is trade asset
*
* @param {AnyAsset} asset
* @returns {boolean} `true` or `false`
*/
export declare const isTradeAsset: (asset: AnyAsset) => asset is TradeAsset;
/**
* Helper to check whether an asset is secured asset
*
* @param {AnyAsset} asset
* @returns {boolean} `true` or `false`
*/
export declare const isSecuredAsset: (asset: AnyAsset) => asset is SecuredAsset;
/**
*
* @param {AnyAsset} asset
* @returns {boolean} `true` or `false`
*/
export declare const isTokenAsset: (asset: AnyAsset) => asset is TokenAsset;
export declare const assetFromString: (s: string) => AnyAsset | null;
/**
* Similar to an `assetFromString`, but throws an exception for invalid asset strings
*/
export declare const assetFromStringEx: (s: string) => AnyAsset;
/**
* Returns an `Asset` as a string using following naming convention:
*
* `AAA.BBB-CCC`
* where
* chain: `AAA`
* ticker (optional): `BBB`
* symbol: `BBB-CCC` or `CCC` (if no ticker available)
* symbol (synth): `BBB/CCC` or `CCC` (if no ticker available)
*
* @see https://docs.thorchain.org/developers/transaction-memos#asset-notation
*
* @param {Asset} asset The given asset.
* @returns {string} The string from the given asset.
*/
export declare const assetToString: ({ chain, symbol, type }: AnyAsset) => string;
/**
* Currency symbols currently supported
*/
export declare enum AssetCurrencySymbol {
RUNE = "\u16B1",
BTC = "\u20BF",
SATOSHI = "\u26A1",
ETH = "\u039E",
USD = "$",
DASH = "\u0110",
LTC = "\u0141",
DOGE = "\u00D0",
CACAO = "\uD800\uDF02"
}
/**
* Returns currency symbols by given `Asset`
*
* @param {Asset} asset The given asset.
* @returns {string} The currency symbol from the given asset.
*/
export declare const currencySymbolByAsset: ({ ticker }: AnyAsset) => string;
/**
* Formats an asset amount using its currency symbol
*
* If `decimal` is not set, `amount.decimal` is used
* If `asset` is not set, `$` will be used as currency symbol by default
* `trimZeros` is `false` by default
* 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.
*/
export declare const formatAssetAmountCurrency: ({ amount, asset, decimal, trimZeros: shouldTrimZeros, }: {
amount: AssetAmount;
asset?: AnyAsset | undefined;
decimal?: number | undefined;
trimZeros?: boolean | undefined;
}) => string;
/**
* Formats a `BaseAmount` into a string of an `AssetAmount`
*
* If `decimal` is not set, `amount.decimal` is used
* 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.
*/
export declare const formatBaseAsAssetAmount: ({ amount, decimal, trimZeros, }: {
amount: BaseAmount;
decimal?: number | undefined;
trimZeros?: boolean | undefined;
}) => string;
/**
* Checks equality of two `Assets`
* @param {Asset} a Asset one
* @param {Asset} b Asset two
* @return {boolean} Result of equality check
*/
export declare const eqAsset: (a: AnyAsset, b: AnyAsset) => boolean;
/**
* Removes `0x` or `0X` from address
*/
export declare const strip0x: (addr: Address) => string;
export declare const getContractAddressFromAsset: (asset: TokenAsset) => Address;