@xchainjs/xchain-util
Advanced tools
Comparing version 0.13.7 to 1.0.0
import BigNumber from 'bignumber.js'; | ||
import { Amount, Asset, AssetAmount, BaseAmount, Denomination } from './types'; | ||
import { Amount, AnyAsset, AssetAmount, BaseAmount, Denomination, SynthAsset, TokenAsset, TradeAsset } from './types'; | ||
export type Address = string; | ||
@@ -12,2 +12,18 @@ /** | ||
/** | ||
* 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 = "~"; | ||
/** | ||
* Factory to create values of assets (e.g. RUNE) | ||
@@ -51,3 +67,8 @@ * | ||
type: Denomination.Asset; | ||
amount: () => BigNumber; | ||
amount: () => BigNumber; /** | ||
* Guard to check whether value is a BigNumber.Value or not | ||
* | ||
* @param {unknown} v | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
plus: (value: BigNumber.Value | any, decimal?: number | undefined) => any; | ||
@@ -60,3 +81,5 @@ minus: (value: BigNumber.Value | any, decimal?: number | undefined) => any; | ||
lt: (value: BigNumber.Value | any) => boolean; | ||
lte: (value: BigNumber.Value | any) => boolean; | ||
lte: (value: BigNumber.Value | any) => boolean; /** | ||
* Native asset delimiter | ||
*/ | ||
eq: (value: BigNumber.Value | any) => boolean; | ||
@@ -73,3 +96,8 @@ decimal: number; | ||
type: Denomination.Base; | ||
amount: () => BigNumber; | ||
amount: () => BigNumber; /** | ||
* Guard to check whether value is a BigNumber.Value or not | ||
* | ||
* @param {unknown} v | ||
* @returns {boolean} `true` or `false`. | ||
* */ | ||
plus: (value: BigNumber.Value | any, decimal?: number | undefined) => any; | ||
@@ -82,3 +110,5 @@ minus: (value: BigNumber.Value | any, decimal?: number | undefined) => any; | ||
lt: (value: BigNumber.Value | any) => boolean; | ||
lte: (value: BigNumber.Value | any) => boolean; | ||
lte: (value: BigNumber.Value | any) => boolean; /** | ||
* Native asset delimiter | ||
*/ | ||
eq: (value: BigNumber.Value | any) => boolean; | ||
@@ -114,3 +144,3 @@ decimal: number; | ||
*/ | ||
export declare const isValidAsset: (asset: Asset) => boolean; | ||
export declare const isValidAsset: (asset: AnyAsset) => boolean; | ||
/** | ||
@@ -122,4 +152,11 @@ * Helper to check whether an asset is synth asset | ||
*/ | ||
export declare const isSynthAsset: ({ synth }: Asset) => boolean; | ||
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; | ||
/** | ||
* Creates an `Asset` by a given string | ||
@@ -141,7 +178,7 @@ * | ||
*/ | ||
export declare const assetFromString: (s: string) => Asset | null; | ||
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) => Asset; | ||
export declare const assetFromStringEx: (s: string) => AnyAsset; | ||
/** | ||
@@ -162,3 +199,3 @@ * Returns an `Asset` as a string using following naming convention: | ||
*/ | ||
export declare const assetToString: ({ chain, symbol, synth }: Asset) => string; | ||
export declare const assetToString: ({ chain, symbol, type }: AnyAsset) => string; | ||
/** | ||
@@ -184,3 +221,3 @@ * Currency symbols currently supported | ||
*/ | ||
export declare const currencySymbolByAsset: ({ ticker }: Asset) => string; | ||
export declare const currencySymbolByAsset: ({ ticker }: AnyAsset) => string; | ||
/** | ||
@@ -199,3 +236,3 @@ * Formats an asset amount using its currency symbol | ||
amount: AssetAmount; | ||
asset?: Asset | undefined; | ||
asset?: AnyAsset | undefined; | ||
decimal?: number | undefined; | ||
@@ -224,3 +261,3 @@ trimZeros?: boolean | undefined; | ||
*/ | ||
export declare const eqAsset: (a: Asset, b: Asset) => boolean; | ||
export declare const eqAsset: (a: AnyAsset, b: AnyAsset) => boolean; | ||
/** | ||
@@ -230,2 +267,2 @@ * Removes `0x` or `0X` from address | ||
export declare const strip0x: (addr: Address) => string; | ||
export declare const getContractAddressFromAsset: (asset: Asset) => Address; | ||
export declare const getContractAddressFromAsset: (asset: TokenAsset) => Address; |
import { BigNumber } from 'bignumber.js'; | ||
import { Asset, AssetAmount, BaseAmount } from './'; | ||
type CryptoNumeric = CryptoAmount | number | BigNumber; | ||
import { AnyAsset, Asset, AssetAmount, BaseAmount, SynthAsset, TokenAsset, TradeAsset } from './'; | ||
/** | ||
@@ -8,15 +7,15 @@ * Utility Class to combine an amount (asset/base) with the Asset | ||
*/ | ||
export declare class CryptoAmount { | ||
declare class BaseCryptoAmount<T extends AnyAsset> { | ||
baseAmount: BaseAmount; | ||
readonly asset: Asset; | ||
constructor(amount: BaseAmount, asset: Asset); | ||
plus(v: CryptoAmount): CryptoAmount; | ||
minus(v: CryptoAmount): CryptoAmount; | ||
times(v: CryptoNumeric): CryptoAmount; | ||
div(v: CryptoNumeric): CryptoAmount; | ||
lt(v: CryptoAmount): boolean; | ||
lte(v: CryptoAmount): boolean; | ||
gt(v: CryptoAmount): boolean; | ||
gte(v: CryptoAmount): boolean; | ||
eq(v: CryptoAmount): boolean; | ||
readonly asset: T; | ||
constructor(amount: BaseAmount, asset: T); | ||
plus(v: BaseCryptoAmount<T>): BaseCryptoAmount<T>; | ||
minus(v: BaseCryptoAmount<T>): BaseCryptoAmount<T>; | ||
times(v: BaseCryptoAmount<T> | number | BigNumber): BaseCryptoAmount<T>; | ||
div(v: BaseCryptoAmount<T> | number | BigNumber): BaseCryptoAmount<T>; | ||
lt(v: BaseCryptoAmount<T>): boolean; | ||
lte(v: BaseCryptoAmount<T>): boolean; | ||
gt(v: BaseCryptoAmount<T>): boolean; | ||
gte(v: BaseCryptoAmount<T>): boolean; | ||
eq(v: BaseCryptoAmount<T>): boolean; | ||
formatedAssetString(): string; | ||
@@ -38,2 +37,12 @@ assetAmountFixedString(): string; | ||
} | ||
export declare class CryptoAmount<T extends AnyAsset = AnyAsset> extends BaseCryptoAmount<T> { | ||
} | ||
export declare class AssetCryptoAmount extends BaseCryptoAmount<Asset> { | ||
} | ||
export declare class TokenCryptoAmount extends BaseCryptoAmount<TokenAsset> { | ||
} | ||
export declare class SynthCryptoAmount extends BaseCryptoAmount<SynthAsset> { | ||
} | ||
export declare class TradeCryptoAmount extends BaseCryptoAmount<TradeAsset> { | ||
} | ||
export {}; |
@@ -130,2 +130,13 @@ import BigNumber from 'bignumber.js'; | ||
/** | ||
* Asset type | ||
*/ | ||
var AssetType; | ||
(function (AssetType) { | ||
AssetType[AssetType["NATIVE"] = 0] = "NATIVE"; | ||
AssetType[AssetType["TOKEN"] = 1] = "TOKEN"; | ||
AssetType[AssetType["SYNTH"] = 2] = "SYNTH"; | ||
AssetType[AssetType["TRADE"] = 3] = "TRADE"; | ||
})(AssetType || (AssetType = {})); | ||
/** | ||
* Guard to check whether value is a BigNumber.Value or not | ||
@@ -149,2 +160,18 @@ * | ||
/** | ||
* Native asset delimiter | ||
*/ | ||
const NATIVE_ASSET_DELIMITER = '.'; | ||
/** | ||
* Token asset delimiter | ||
*/ | ||
const TOKEN_ASSET_DELIMITER = '.'; | ||
/** | ||
* Synth asset delimiter | ||
*/ | ||
const SYNTH_ASSET_DELIMITER = '/'; | ||
/** | ||
* Trade asset delimiter | ||
*/ | ||
const TRADE_ASSET_DELIMITER = '~'; | ||
/** | ||
* Factory to create values of assets (e.g. RUNE) | ||
@@ -267,3 +294,3 @@ * | ||
*/ | ||
const AssetBTC = { chain: 'BTC', symbol: 'BTC', ticker: 'BTC', synth: false }; | ||
const AssetBTC = { chain: 'BTC', symbol: 'BTC', ticker: 'BTC', type: AssetType.NATIVE }; | ||
/** | ||
@@ -275,3 +302,3 @@ * Base "chain" asset on ethereum main net. | ||
*/ | ||
const AssetETH = { chain: 'ETH', symbol: 'ETH', ticker: 'ETH', synth: false }; | ||
const AssetETH = { chain: 'ETH', symbol: 'ETH', ticker: 'ETH', type: AssetType.NATIVE }; | ||
/** | ||
@@ -290,6 +317,11 @@ * Helper to check whether asset is valid | ||
*/ | ||
const isSynthAsset = ({ synth }) => synth; | ||
const SYNTH_DELIMITER = '/'; | ||
const NON_SYNTH_DELIMITER = '.'; | ||
const isSynthAsset = (asset) => asset.type === AssetType.SYNTH; | ||
/** | ||
* Helper to check whether an asset is trade asset | ||
* | ||
* @param {AnyAsset} asset | ||
* @returns {boolean} `true` or `false` | ||
*/ | ||
const isTradeAsset = (asset) => asset.type === AssetType.TRADE; | ||
/** | ||
* Creates an `Asset` by a given string | ||
@@ -312,5 +344,6 @@ * | ||
const assetFromString = (s) => { | ||
var _a; | ||
const isSynth = s.includes(SYNTH_DELIMITER); | ||
const delimiter = isSynth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
var _a, _b; | ||
const isSynth = s.includes(SYNTH_ASSET_DELIMITER); | ||
const isTrade = s.includes(TRADE_ASSET_DELIMITER); | ||
const delimiter = isSynth ? SYNTH_ASSET_DELIMITER : isTrade ? TRADE_ASSET_DELIMITER : NATIVE_ASSET_DELIMITER; | ||
const data = s.split(delimiter); | ||
@@ -326,5 +359,12 @@ if (data.length <= 1 || ((_a = data[1]) === null || _a === void 0 ? void 0 : _a.length) < 1) { | ||
const ticker = symbol.split('-')[0]; | ||
const isToken = ((_b = symbol.split('-')[1]) === null || _b === void 0 ? void 0 : _b.length) > 1; | ||
if (!symbol) | ||
return null; | ||
return { chain, symbol, ticker, synth: isSynth }; | ||
if (isSynth) | ||
return { chain, symbol, ticker, type: AssetType.SYNTH }; | ||
if (isTrade) | ||
return { chain, symbol, ticker, type: AssetType.TRADE }; | ||
if (isToken) | ||
return { chain, symbol, ticker, type: AssetType.TOKEN }; | ||
return { chain, symbol, ticker, type: AssetType.NATIVE }; | ||
}; | ||
@@ -355,5 +395,13 @@ /** | ||
*/ | ||
const assetToString = ({ chain, symbol, synth }) => { | ||
const delimiter = synth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
return `${chain}${delimiter}${symbol}`; | ||
const assetToString = ({ chain, symbol, type }) => { | ||
switch (type) { | ||
case AssetType.SYNTH: | ||
return `${chain}${SYNTH_ASSET_DELIMITER}${symbol}`; | ||
case AssetType.TOKEN: | ||
return `${chain}${TOKEN_ASSET_DELIMITER}${symbol}`; | ||
case AssetType.TRADE: | ||
return `${chain}${TRADE_ASSET_DELIMITER}${symbol}`; | ||
default: | ||
return `${chain}${NATIVE_ASSET_DELIMITER}${symbol}`; | ||
} | ||
}; | ||
@@ -475,3 +523,3 @@ /** | ||
*/ | ||
const eqAsset = (a, b) => a.chain === b.chain && a.symbol === b.symbol && a.ticker === b.ticker && a.synth === b.synth; | ||
const eqAsset = (a, b) => a.chain === b.chain && a.symbol === b.symbol && a.ticker === b.ticker && a.type === b.type; | ||
/** | ||
@@ -529,3 +577,3 @@ * Removes `0x` or `0X` from address | ||
*/ | ||
class CryptoAmount { | ||
class BaseCryptoAmount { | ||
constructor(amount, asset) { | ||
@@ -538,3 +586,3 @@ this.asset = asset; | ||
const assetAmountResult = assetToBase(this.assetAmount.plus(v.assetAmount)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
@@ -544,13 +592,13 @@ minus(v) { | ||
const assetAmountResult = assetToBase(this.assetAmount.minus(v.assetAmount)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
times(v) { | ||
this.check(v); | ||
if (v instanceof CryptoAmount) { | ||
if (v instanceof BaseCryptoAmount) { | ||
const assetAmountResult = assetToBase(this.assetAmount.times(v.assetAmount)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
else { | ||
const assetAmountResult = assetToBase(this.assetAmount.times(v)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
@@ -560,9 +608,9 @@ } | ||
this.check(v); | ||
if (v instanceof CryptoAmount) { | ||
if (v instanceof BaseCryptoAmount) { | ||
const assetAmountResult = assetToBase(this.assetAmount.div(v.assetAmount)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
else { | ||
const assetAmountResult = assetToBase(this.assetAmount.div(v)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
@@ -615,3 +663,3 @@ } | ||
check(v) { | ||
if (v instanceof CryptoAmount) { | ||
if (v instanceof BaseCryptoAmount) { | ||
if (!eqAsset(this.asset, v.asset)) { | ||
@@ -623,2 +671,12 @@ throw Error(`cannot perform math on 2 diff assets ${assetToString(this.asset)} ${assetToString(v.asset)}`); | ||
} | ||
class CryptoAmount extends BaseCryptoAmount { | ||
} | ||
class AssetCryptoAmount extends BaseCryptoAmount { | ||
} | ||
class TokenCryptoAmount extends BaseCryptoAmount { | ||
} | ||
class SynthCryptoAmount extends BaseCryptoAmount { | ||
} | ||
class TradeCryptoAmount extends BaseCryptoAmount { | ||
} | ||
@@ -695,3 +753,3 @@ /****************************************************************************** | ||
export { AssetCurrencySymbol, CachedValue, CryptoAmount, Denomination, NINE_REALMS_CLIENT_HEADER, XCHAINJS_IDENTIFIER, add9Rheader, assetAmount, assetFromString, assetFromStringEx, assetToBase, assetToString, baseAmount, baseToAsset, bn, bnOrZero, currencySymbolByAsset, delay, eqAsset, fixedBN, formatAssetAmount, formatAssetAmountCurrency, formatBN, formatBNCurrency, formatBaseAmount, formatBaseAsAssetAmount, getContractAddressFromAsset, isAssetAmount, isBaseAmount, isBigNumberValue, isSynthAsset, isValidAsset, isValidBN, register9Rheader, strip0x, trimZeros, validBNOrZero }; | ||
export { AssetCryptoAmount, AssetCurrencySymbol, AssetType, CachedValue, CryptoAmount, Denomination, NATIVE_ASSET_DELIMITER, NINE_REALMS_CLIENT_HEADER, SYNTH_ASSET_DELIMITER, SynthCryptoAmount, TOKEN_ASSET_DELIMITER, TRADE_ASSET_DELIMITER, TokenCryptoAmount, TradeCryptoAmount, XCHAINJS_IDENTIFIER, add9Rheader, assetAmount, assetFromString, assetFromStringEx, assetToBase, assetToString, baseAmount, baseToAsset, bn, bnOrZero, currencySymbolByAsset, delay, eqAsset, fixedBN, formatAssetAmount, formatAssetAmountCurrency, formatBN, formatBNCurrency, formatBaseAmount, formatBaseAsAssetAmount, getContractAddressFromAsset, isAssetAmount, isBaseAmount, isBigNumberValue, isSynthAsset, isTradeAsset, isValidAsset, isValidBN, register9Rheader, strip0x, trimZeros, validBNOrZero }; | ||
//# sourceMappingURL=index.esm.js.map |
113
lib/index.js
@@ -138,2 +138,13 @@ 'use strict'; | ||
/** | ||
* Asset type | ||
*/ | ||
exports.AssetType = void 0; | ||
(function (AssetType) { | ||
AssetType[AssetType["NATIVE"] = 0] = "NATIVE"; | ||
AssetType[AssetType["TOKEN"] = 1] = "TOKEN"; | ||
AssetType[AssetType["SYNTH"] = 2] = "SYNTH"; | ||
AssetType[AssetType["TRADE"] = 3] = "TRADE"; | ||
})(exports.AssetType || (exports.AssetType = {})); | ||
/** | ||
* Guard to check whether value is a BigNumber.Value or not | ||
@@ -157,2 +168,18 @@ * | ||
/** | ||
* Native asset delimiter | ||
*/ | ||
const NATIVE_ASSET_DELIMITER = '.'; | ||
/** | ||
* Token asset delimiter | ||
*/ | ||
const TOKEN_ASSET_DELIMITER = '.'; | ||
/** | ||
* Synth asset delimiter | ||
*/ | ||
const SYNTH_ASSET_DELIMITER = '/'; | ||
/** | ||
* Trade asset delimiter | ||
*/ | ||
const TRADE_ASSET_DELIMITER = '~'; | ||
/** | ||
* Factory to create values of assets (e.g. RUNE) | ||
@@ -275,3 +302,3 @@ * | ||
*/ | ||
const AssetBTC = { chain: 'BTC', symbol: 'BTC', ticker: 'BTC', synth: false }; | ||
const AssetBTC = { chain: 'BTC', symbol: 'BTC', ticker: 'BTC', type: exports.AssetType.NATIVE }; | ||
/** | ||
@@ -283,3 +310,3 @@ * Base "chain" asset on ethereum main net. | ||
*/ | ||
const AssetETH = { chain: 'ETH', symbol: 'ETH', ticker: 'ETH', synth: false }; | ||
const AssetETH = { chain: 'ETH', symbol: 'ETH', ticker: 'ETH', type: exports.AssetType.NATIVE }; | ||
/** | ||
@@ -298,6 +325,11 @@ * Helper to check whether asset is valid | ||
*/ | ||
const isSynthAsset = ({ synth }) => synth; | ||
const SYNTH_DELIMITER = '/'; | ||
const NON_SYNTH_DELIMITER = '.'; | ||
const isSynthAsset = (asset) => asset.type === exports.AssetType.SYNTH; | ||
/** | ||
* Helper to check whether an asset is trade asset | ||
* | ||
* @param {AnyAsset} asset | ||
* @returns {boolean} `true` or `false` | ||
*/ | ||
const isTradeAsset = (asset) => asset.type === exports.AssetType.TRADE; | ||
/** | ||
* Creates an `Asset` by a given string | ||
@@ -320,5 +352,6 @@ * | ||
const assetFromString = (s) => { | ||
var _a; | ||
const isSynth = s.includes(SYNTH_DELIMITER); | ||
const delimiter = isSynth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
var _a, _b; | ||
const isSynth = s.includes(SYNTH_ASSET_DELIMITER); | ||
const isTrade = s.includes(TRADE_ASSET_DELIMITER); | ||
const delimiter = isSynth ? SYNTH_ASSET_DELIMITER : isTrade ? TRADE_ASSET_DELIMITER : NATIVE_ASSET_DELIMITER; | ||
const data = s.split(delimiter); | ||
@@ -334,5 +367,12 @@ if (data.length <= 1 || ((_a = data[1]) === null || _a === void 0 ? void 0 : _a.length) < 1) { | ||
const ticker = symbol.split('-')[0]; | ||
const isToken = ((_b = symbol.split('-')[1]) === null || _b === void 0 ? void 0 : _b.length) > 1; | ||
if (!symbol) | ||
return null; | ||
return { chain, symbol, ticker, synth: isSynth }; | ||
if (isSynth) | ||
return { chain, symbol, ticker, type: exports.AssetType.SYNTH }; | ||
if (isTrade) | ||
return { chain, symbol, ticker, type: exports.AssetType.TRADE }; | ||
if (isToken) | ||
return { chain, symbol, ticker, type: exports.AssetType.TOKEN }; | ||
return { chain, symbol, ticker, type: exports.AssetType.NATIVE }; | ||
}; | ||
@@ -363,5 +403,13 @@ /** | ||
*/ | ||
const assetToString = ({ chain, symbol, synth }) => { | ||
const delimiter = synth ? SYNTH_DELIMITER : NON_SYNTH_DELIMITER; | ||
return `${chain}${delimiter}${symbol}`; | ||
const assetToString = ({ chain, symbol, type }) => { | ||
switch (type) { | ||
case exports.AssetType.SYNTH: | ||
return `${chain}${SYNTH_ASSET_DELIMITER}${symbol}`; | ||
case exports.AssetType.TOKEN: | ||
return `${chain}${TOKEN_ASSET_DELIMITER}${symbol}`; | ||
case exports.AssetType.TRADE: | ||
return `${chain}${TRADE_ASSET_DELIMITER}${symbol}`; | ||
default: | ||
return `${chain}${NATIVE_ASSET_DELIMITER}${symbol}`; | ||
} | ||
}; | ||
@@ -483,3 +531,3 @@ /** | ||
*/ | ||
const eqAsset = (a, b) => a.chain === b.chain && a.symbol === b.symbol && a.ticker === b.ticker && a.synth === b.synth; | ||
const eqAsset = (a, b) => a.chain === b.chain && a.symbol === b.symbol && a.ticker === b.ticker && a.type === b.type; | ||
/** | ||
@@ -537,3 +585,3 @@ * Removes `0x` or `0X` from address | ||
*/ | ||
class CryptoAmount { | ||
class BaseCryptoAmount { | ||
constructor(amount, asset) { | ||
@@ -546,3 +594,3 @@ this.asset = asset; | ||
const assetAmountResult = assetToBase(this.assetAmount.plus(v.assetAmount)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
@@ -552,13 +600,13 @@ minus(v) { | ||
const assetAmountResult = assetToBase(this.assetAmount.minus(v.assetAmount)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
times(v) { | ||
this.check(v); | ||
if (v instanceof CryptoAmount) { | ||
if (v instanceof BaseCryptoAmount) { | ||
const assetAmountResult = assetToBase(this.assetAmount.times(v.assetAmount)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
else { | ||
const assetAmountResult = assetToBase(this.assetAmount.times(v)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
@@ -568,9 +616,9 @@ } | ||
this.check(v); | ||
if (v instanceof CryptoAmount) { | ||
if (v instanceof BaseCryptoAmount) { | ||
const assetAmountResult = assetToBase(this.assetAmount.div(v.assetAmount)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
else { | ||
const assetAmountResult = assetToBase(this.assetAmount.div(v)); | ||
return new CryptoAmount(assetAmountResult, this.asset); | ||
return new BaseCryptoAmount(assetAmountResult, this.asset); | ||
} | ||
@@ -623,3 +671,3 @@ } | ||
check(v) { | ||
if (v instanceof CryptoAmount) { | ||
if (v instanceof BaseCryptoAmount) { | ||
if (!eqAsset(this.asset, v.asset)) { | ||
@@ -631,2 +679,12 @@ throw Error(`cannot perform math on 2 diff assets ${assetToString(this.asset)} ${assetToString(v.asset)}`); | ||
} | ||
class CryptoAmount extends BaseCryptoAmount { | ||
} | ||
class AssetCryptoAmount extends BaseCryptoAmount { | ||
} | ||
class TokenCryptoAmount extends BaseCryptoAmount { | ||
} | ||
class SynthCryptoAmount extends BaseCryptoAmount { | ||
} | ||
class TradeCryptoAmount extends BaseCryptoAmount { | ||
} | ||
@@ -703,5 +761,13 @@ /****************************************************************************** | ||
exports.AssetCryptoAmount = AssetCryptoAmount; | ||
exports.CachedValue = CachedValue; | ||
exports.CryptoAmount = CryptoAmount; | ||
exports.NATIVE_ASSET_DELIMITER = NATIVE_ASSET_DELIMITER; | ||
exports.NINE_REALMS_CLIENT_HEADER = NINE_REALMS_CLIENT_HEADER; | ||
exports.SYNTH_ASSET_DELIMITER = SYNTH_ASSET_DELIMITER; | ||
exports.SynthCryptoAmount = SynthCryptoAmount; | ||
exports.TOKEN_ASSET_DELIMITER = TOKEN_ASSET_DELIMITER; | ||
exports.TRADE_ASSET_DELIMITER = TRADE_ASSET_DELIMITER; | ||
exports.TokenCryptoAmount = TokenCryptoAmount; | ||
exports.TradeCryptoAmount = TradeCryptoAmount; | ||
exports.XCHAINJS_IDENTIFIER = XCHAINJS_IDENTIFIER; | ||
@@ -733,2 +799,3 @@ exports.add9Rheader = add9Rheader; | ||
exports.isSynthAsset = isSynthAsset; | ||
exports.isTradeAsset = isTradeAsset; | ||
exports.isValidAsset = isValidAsset; | ||
@@ -735,0 +802,0 @@ exports.isValidBN = isValidBN; |
import { Chain } from './chain'; | ||
/** | ||
* Asset type | ||
*/ | ||
export declare enum AssetType { | ||
NATIVE = 0, | ||
TOKEN = 1, | ||
SYNTH = 2, | ||
TRADE = 3 | ||
} | ||
/** | ||
* Any Asset XChainJS can work with | ||
*/ | ||
export type AnyAsset = { | ||
chain: Chain; | ||
symbol: string; | ||
ticker: string; | ||
type: AssetType; | ||
}; | ||
/** | ||
* Asset type for native assets | ||
*/ | ||
export type Asset = { | ||
@@ -6,3 +27,30 @@ chain: Chain; | ||
ticker: string; | ||
synth: boolean; | ||
type: AssetType.NATIVE; | ||
}; | ||
/** | ||
* Asset type for token assets. For example ERC20, BEP20 | ||
*/ | ||
export type TokenAsset = { | ||
chain: Chain; | ||
symbol: string; | ||
ticker: string; | ||
type: AssetType.TOKEN; | ||
}; | ||
/** | ||
* Asset type for synthetic assets which lives in networks like Thorchain or Mayachain | ||
*/ | ||
export type SynthAsset = { | ||
chain: Chain; | ||
symbol: string; | ||
ticker: string; | ||
type: AssetType.SYNTH; | ||
}; | ||
/** | ||
* Asset type for synthetic assets which lives in networks like Thorchain or Mayachain | ||
*/ | ||
export type TradeAsset = { | ||
chain: Chain; | ||
symbol: string; | ||
ticker: string; | ||
type: AssetType.TRADE; | ||
}; |
{ | ||
"name": "@xchainjs/xchain-util", | ||
"version": "0.13.7", | ||
"version": "1.0.0", | ||
"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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
173397
2035
0
20