@swapkit/helpers 4.0.0-beta.32 → 4.0.0-beta.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/index.js +2 -2
- package/dist/api/index.js.map +1 -1
- package/dist/{chunk-ganzk321.js → chunk-hm14bfyg.js} +3 -3
- package/dist/{chunk-fazw0jvt.js.map → chunk-hm14bfyg.js.map} +1 -1
- package/dist/chunk-vb4wtm2w.js +4 -0
- package/dist/{chunk-ganzk321.js.map → chunk-vb4wtm2w.js.map} +1 -1
- package/dist/contracts.cjs +4 -0
- package/dist/contracts.cjs.map +10 -0
- package/dist/contracts.js +4 -0
- package/dist/contracts.js.map +10 -0
- package/dist/index.cjs +3 -3
- package/dist/index.cjs.map +11 -11
- package/dist/index.js +3 -3
- package/dist/index.js.map +11 -11
- package/dist/tokens.cjs +4 -0
- package/dist/tokens.cjs.map +10 -0
- package/dist/tokens.js +4 -0
- package/dist/tokens.js.map +10 -0
- package/dist/types/api/index.d.ts +238 -0
- package/dist/types/api/index.d.ts.map +1 -0
- package/dist/types/api/microgard/endpoints.d.ts +25 -0
- package/dist/types/api/microgard/endpoints.d.ts.map +1 -0
- package/dist/types/api/microgard/types.d.ts +56 -0
- package/dist/types/api/microgard/types.d.ts.map +1 -0
- package/dist/types/api/midgard/endpoints.d.ts +35 -0
- package/dist/types/api/midgard/endpoints.d.ts.map +1 -0
- package/dist/types/api/midgard/types.d.ts +102 -0
- package/dist/types/api/midgard/types.d.ts.map +1 -0
- package/dist/types/api/swapkitApi/endpoints.d.ts +194 -0
- package/dist/types/api/swapkitApi/endpoints.d.ts.map +1 -0
- package/dist/types/api/swapkitApi/types.d.ts +7238 -0
- package/dist/types/api/swapkitApi/types.d.ts.map +1 -0
- package/dist/types/api/thornode/endpoints.d.ts +31 -0
- package/dist/types/api/thornode/endpoints.d.ts.map +1 -0
- package/dist/types/api/thornode/types.d.ts +264 -0
- package/dist/types/api/thornode/types.d.ts.map +1 -0
- package/dist/types/api/thorswapStatic/endpoints.d.ts +7 -0
- package/dist/types/api/thorswapStatic/endpoints.d.ts.map +1 -0
- package/dist/types/api/thorswapStatic/types.d.ts +22 -0
- package/dist/types/api/thorswapStatic/types.d.ts.map +1 -0
- package/dist/types/contracts.d.ts +2 -0
- package/dist/types/contracts.d.ts.map +1 -0
- package/{src/index.ts → dist/types/index.d.ts} +3 -4
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/modules/assetValue.d.ts +71 -0
- package/dist/types/modules/assetValue.d.ts.map +1 -0
- package/dist/types/modules/bigIntArithmetics.d.ts +56 -0
- package/dist/types/modules/bigIntArithmetics.d.ts.map +1 -0
- package/dist/types/modules/feeMultiplier.d.ts +45 -0
- package/dist/types/modules/feeMultiplier.d.ts.map +1 -0
- package/dist/types/modules/requestClient.d.ts +22 -0
- package/dist/types/modules/requestClient.d.ts.map +1 -0
- package/dist/types/modules/swapKitConfig.d.ts +98 -0
- package/dist/types/modules/swapKitConfig.d.ts.map +1 -0
- package/dist/types/modules/swapKitError.d.ts +829 -0
- package/dist/types/modules/swapKitError.d.ts.map +1 -0
- package/dist/types/modules/swapKitNumber.d.ts +7 -0
- package/dist/types/modules/swapKitNumber.d.ts.map +1 -0
- package/dist/types/tokens.d.ts +2 -0
- package/dist/types/tokens.d.ts.map +1 -0
- package/dist/types/types/chains.d.ts +187 -0
- package/dist/types/types/chains.d.ts.map +1 -0
- package/dist/types/types/commonTypes.d.ts +37 -0
- package/dist/types/types/commonTypes.d.ts.map +1 -0
- package/dist/types/types/derivationPath.d.ts +17 -0
- package/dist/types/types/derivationPath.d.ts.map +1 -0
- package/dist/types/types/errors/apiV1.d.ts +2 -0
- package/dist/types/types/errors/apiV1.d.ts.map +1 -0
- package/{src/types/index.ts → dist/types/types/index.d.ts} +1 -1
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/types/quotes.d.ts +170 -0
- package/dist/types/types/quotes.d.ts.map +1 -0
- package/dist/types/types/radix.d.ts +15 -0
- package/dist/types/types/radix.d.ts.map +1 -0
- package/dist/types/types/sdk.d.ts +35 -0
- package/dist/types/types/sdk.d.ts.map +1 -0
- package/dist/types/types/wallet.d.ts +121 -0
- package/dist/types/types/wallet.d.ts.map +1 -0
- package/dist/types/utils/asset.d.ts +40 -0
- package/dist/types/utils/asset.d.ts.map +1 -0
- package/dist/types/utils/derivationPath.d.ts +19 -0
- package/dist/types/utils/derivationPath.d.ts.map +1 -0
- package/dist/types/utils/explorerUrls.d.ts +10 -0
- package/dist/types/utils/explorerUrls.d.ts.map +1 -0
- package/dist/types/utils/liquidity.d.ts +59 -0
- package/dist/types/utils/liquidity.d.ts.map +1 -0
- package/dist/types/utils/memo.d.ts +65 -0
- package/dist/types/utils/memo.d.ts.map +1 -0
- package/dist/types/utils/others.d.ts +9 -0
- package/dist/types/utils/others.d.ts.map +1 -0
- package/dist/types/utils/validators.d.ts +3 -0
- package/dist/types/utils/validators.d.ts.map +1 -0
- package/dist/types/utils/wallets.d.ts +32 -0
- package/dist/types/utils/wallets.d.ts.map +1 -0
- package/package.json +14 -15
- package/dist/chunk-06q78xy3.js +0 -4
- package/dist/chunk-06q78xy3.js.map +0 -10
- package/dist/chunk-0q5vvg5c.js +0 -5
- package/dist/chunk-0q5vvg5c.js.map +0 -10
- package/dist/chunk-18bxh1n7.js +0 -5
- package/dist/chunk-18bxh1n7.js.map +0 -10
- package/dist/chunk-3qya5ng9.js +0 -4
- package/dist/chunk-3qya5ng9.js.map +0 -10
- package/dist/chunk-4yqymkbp.js +0 -5
- package/dist/chunk-4yqymkbp.js.map +0 -10
- package/dist/chunk-50gpwcq9.js +0 -4
- package/dist/chunk-50gpwcq9.js.map +0 -10
- package/dist/chunk-6atq9cs7.js +0 -4
- package/dist/chunk-6atq9cs7.js.map +0 -10
- package/dist/chunk-6m0d5pv4.js +0 -5
- package/dist/chunk-6m0d5pv4.js.map +0 -10
- package/dist/chunk-8x46g3wq.js +0 -5
- package/dist/chunk-8x46g3wq.js.map +0 -10
- package/dist/chunk-92sk0g47.js +0 -5
- package/dist/chunk-92sk0g47.js.map +0 -10
- package/dist/chunk-9f2b3enk.js +0 -5
- package/dist/chunk-9f2b3enk.js.map +0 -10
- package/dist/chunk-adrafsdk.js +0 -5
- package/dist/chunk-adrafsdk.js.map +0 -10
- package/dist/chunk-b1hang9z.js +0 -5
- package/dist/chunk-b1hang9z.js.map +0 -10
- package/dist/chunk-becdmywt.js +0 -4
- package/dist/chunk-becdmywt.js.map +0 -10
- package/dist/chunk-ehz0t9d1.js +0 -4
- package/dist/chunk-ehz0t9d1.js.map +0 -10
- package/dist/chunk-fazw0jvt.js +0 -4
- package/dist/chunk-fm9ycn0h.js +0 -5
- package/dist/chunk-fm9ycn0h.js.map +0 -10
- package/dist/chunk-g67e2r57.js +0 -4
- package/dist/chunk-g67e2r57.js.map +0 -10
- package/dist/chunk-he3vy8gt.js +0 -4
- package/dist/chunk-he3vy8gt.js.map +0 -10
- package/dist/chunk-jjayk2y5.js +0 -4
- package/dist/chunk-jjayk2y5.js.map +0 -10
- package/dist/chunk-mmyv2vsv.js +0 -5
- package/dist/chunk-mmyv2vsv.js.map +0 -10
- package/dist/chunk-phev7aw7.js +0 -4
- package/dist/chunk-phev7aw7.js.map +0 -10
- package/dist/chunk-rhqk87p2.js +0 -5
- package/dist/chunk-rhqk87p2.js.map +0 -10
- package/dist/chunk-rnv7fdjk.js +0 -4
- package/dist/chunk-rnv7fdjk.js.map +0 -10
- package/dist/chunk-rwh98p1c.js +0 -4
- package/dist/chunk-rwh98p1c.js.map +0 -10
- package/dist/chunk-v9zj2gsj.js +0 -5
- package/dist/chunk-v9zj2gsj.js.map +0 -10
- package/dist/chunk-vv5bgsn8.js +0 -4
- package/dist/chunk-vv5bgsn8.js.map +0 -10
- package/dist/chunk-vwh9jfxn.js +0 -5
- package/dist/chunk-vwh9jfxn.js.map +0 -10
- package/dist/chunk-x0rqy75t.js +0 -4
- package/dist/chunk-x0rqy75t.js.map +0 -10
- package/dist/contracts/index.cjs +0 -4
- package/dist/contracts/index.cjs.map +0 -38
- package/dist/contracts/index.js +0 -4
- package/dist/contracts/index.js.map +0 -38
- package/dist/tokens/index.cjs +0 -4
- package/dist/tokens/index.cjs.map +0 -10
- package/dist/tokens/index.js +0 -4
- package/dist/tokens/index.js.map +0 -10
- package/src/api/index.ts +0 -19
- package/src/api/microgard/endpoints.ts +0 -83
- package/src/api/microgard/types.ts +0 -57
- package/src/api/midgard/endpoints.ts +0 -90
- package/src/api/midgard/types.ts +0 -106
- package/src/api/swapkitApi/endpoints.ts +0 -193
- package/src/api/swapkitApi/types.ts +0 -635
- package/src/api/thornode/endpoints.ts +0 -116
- package/src/api/thornode/types.ts +0 -276
- package/src/api/thorswapStatic/endpoints.ts +0 -46
- package/src/api/thorswapStatic/types.ts +0 -18
- package/src/contracts/abis/avaxGeneric.ts +0 -92
- package/src/contracts/abis/avaxWoofi.ts +0 -145
- package/src/contracts/abis/bscGeneric.ts +0 -106
- package/src/contracts/abis/chainflipGateway.ts +0 -330
- package/src/contracts/abis/erc20.ts +0 -99
- package/src/contracts/abis/ethGeneric.ts +0 -92
- package/src/contracts/abis/mayaEvmVaults.ts +0 -331
- package/src/contracts/abis/pancakeV2.ts +0 -145
- package/src/contracts/abis/pangolin.ts +0 -120
- package/src/contracts/abis/sushiswap.ts +0 -120
- package/src/contracts/abis/tcEthVault.ts +0 -650
- package/src/contracts/abis/traderJoe.ts +0 -120
- package/src/contracts/abis/uniswapV2.ts +0 -120
- package/src/contracts/abis/uniswapV2Leg.ts +0 -128
- package/src/contracts/abis/uniswapV3_100.ts +0 -128
- package/src/contracts/abis/uniswapV3_10000.ts +0 -128
- package/src/contracts/abis/uniswapV3_3000.ts +0 -128
- package/src/contracts/abis/uniswapV3_500.ts +0 -128
- package/src/contracts/index.ts +0 -95
- package/src/contracts/routers/index.ts +0 -58
- package/src/contracts/routers/kyber.ts +0 -402
- package/src/contracts/routers/oneinch.ts +0 -2188
- package/src/contracts/routers/pancakeswap.ts +0 -340
- package/src/contracts/routers/pangolin.ts +0 -340
- package/src/contracts/routers/sushiswap.ts +0 -340
- package/src/contracts/routers/traderJoe.ts +0 -340
- package/src/contracts/routers/uniswapv2.ts +0 -340
- package/src/contracts/routers/uniswapv3.ts +0 -254
- package/src/contracts/routers/woofi.ts +0 -171
- package/src/modules/__tests__/assetValue.test.ts +0 -877
- package/src/modules/__tests__/bigIntArithmetics.test.ts +0 -30
- package/src/modules/__tests__/swapKitConfig.test.ts +0 -37
- package/src/modules/__tests__/swapKitNumber.test.ts +0 -560
- package/src/modules/assetValue.ts +0 -387
- package/src/modules/bigIntArithmetics.ts +0 -436
- package/src/modules/feeMultiplier.ts +0 -87
- package/src/modules/requestClient.ts +0 -83
- package/src/modules/swapKitConfig.ts +0 -143
- package/src/modules/swapKitError.ts +0 -442
- package/src/modules/swapKitNumber.ts +0 -16
- package/src/tokens/index.ts +0 -15
- package/src/tokens/lists/camelot_v3.ts +0 -16060
- package/src/tokens/lists/caviar_v1.ts +0 -1704
- package/src/tokens/lists/chainflip.ts +0 -104
- package/src/tokens/lists/index.ts +0 -14
- package/src/tokens/lists/jupiter.ts +0 -33563
- package/src/tokens/lists/mayachain.ts +0 -509
- package/src/tokens/lists/near.ts +0 -458
- package/src/tokens/lists/oneinch.ts +0 -15006
- package/src/tokens/lists/openocean_v2.ts +0 -12434
- package/src/tokens/lists/pancakeswap.ts +0 -6136
- package/src/tokens/lists/pangolin_v1.ts +0 -75
- package/src/tokens/lists/sushiswap_v2.ts +0 -595
- package/src/tokens/lists/thorchain.ts +0 -748
- package/src/tokens/lists/traderjoe_v2.ts +0 -594
- package/src/tokens/lists/uniswap_v2.ts +0 -11484
- package/src/tokens/lists/uniswap_v3.ts +0 -19424
- package/src/types/chains.ts +0 -555
- package/src/types/commonTypes.ts +0 -58
- package/src/types/derivationPath.ts +0 -58
- package/src/types/errors/apiV1.ts +0 -0
- package/src/types/quotes.ts +0 -172
- package/src/types/radix.ts +0 -14
- package/src/types/sdk.ts +0 -42
- package/src/types/tokens.ts +0 -17
- package/src/types/wallet.ts +0 -166
- package/src/utils/__tests__/asset.test.ts +0 -193
- package/src/utils/__tests__/derivationPath.test.ts +0 -16
- package/src/utils/__tests__/memo.test.ts +0 -103
- package/src/utils/__tests__/others.test.ts +0 -88
- package/src/utils/__tests__/validators.test.ts +0 -24
- package/src/utils/asset.ts +0 -231
- package/src/utils/derivationPath.ts +0 -98
- package/src/utils/liquidity.ts +0 -179
- package/src/utils/memo.ts +0 -143
- package/src/utils/others.ts +0 -86
- package/src/utils/plugin.ts +0 -13
- package/src/utils/tokens.ts +0 -73
- package/src/utils/validators.ts +0 -32
- package/src/utils/wallets.ts +0 -276
|
@@ -1,387 +0,0 @@
|
|
|
1
|
-
import { getAddress } from "ethers";
|
|
2
|
-
import { match } from "ts-pattern";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
BaseDecimal,
|
|
6
|
-
Chain,
|
|
7
|
-
type ChainId,
|
|
8
|
-
ChainToChainId,
|
|
9
|
-
type EVMChain,
|
|
10
|
-
EVMChains,
|
|
11
|
-
} from "../types/chains";
|
|
12
|
-
import type { TokenNames, TokenTax } from "../types/tokens";
|
|
13
|
-
import {
|
|
14
|
-
type CommonAssetString,
|
|
15
|
-
assetFromString,
|
|
16
|
-
getAssetType,
|
|
17
|
-
getCommonAssetInfo,
|
|
18
|
-
getDecimal,
|
|
19
|
-
isGasAsset,
|
|
20
|
-
} from "../utils/asset";
|
|
21
|
-
import { warnOnce } from "../utils/others";
|
|
22
|
-
import { type TokenListName, loadTokenLists } from "../utils/tokens";
|
|
23
|
-
import { validateIdentifier } from "../utils/validators";
|
|
24
|
-
|
|
25
|
-
import type { NumberPrimitives } from "./bigIntArithmetics";
|
|
26
|
-
import { BigIntArithmetics, formatBigIntToSafeValue } from "./bigIntArithmetics";
|
|
27
|
-
import { SwapKitError } from "./swapKitError";
|
|
28
|
-
import type { SwapKitValueType } from "./swapKitNumber";
|
|
29
|
-
|
|
30
|
-
const CASE_SENSITIVE_CHAINS = [Chain.Solana, Chain.Tron];
|
|
31
|
-
|
|
32
|
-
const staticTokensMap = new Map<
|
|
33
|
-
TokenNames | string,
|
|
34
|
-
{ tax?: TokenTax; decimal: number; identifier: string }
|
|
35
|
-
>();
|
|
36
|
-
|
|
37
|
-
type ConditionalAssetValueReturn<T extends { asyncTokenLookup?: boolean }> =
|
|
38
|
-
T["asyncTokenLookup"] extends true ? Promise<AssetValue> : AssetValue;
|
|
39
|
-
|
|
40
|
-
type AssetIdentifier =
|
|
41
|
-
| { asset: CommonAssetString | TokenNames }
|
|
42
|
-
| { asset: string }
|
|
43
|
-
| { chain: Chain };
|
|
44
|
-
|
|
45
|
-
type AssetValueFromParams = AssetIdentifier & {
|
|
46
|
-
value?: NumberPrimitives | SwapKitValueType;
|
|
47
|
-
fromBaseDecimal?: number;
|
|
48
|
-
asyncTokenLookup?: boolean;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export class AssetValue extends BigIntArithmetics {
|
|
52
|
-
address?: string;
|
|
53
|
-
chain: Chain;
|
|
54
|
-
isGasAsset = false;
|
|
55
|
-
isSynthetic = false;
|
|
56
|
-
isTradeAsset = false;
|
|
57
|
-
symbol: string;
|
|
58
|
-
tax?: TokenTax;
|
|
59
|
-
ticker: string;
|
|
60
|
-
type: ReturnType<typeof getAssetType>;
|
|
61
|
-
chainId: ChainId;
|
|
62
|
-
|
|
63
|
-
constructor({
|
|
64
|
-
value,
|
|
65
|
-
decimal,
|
|
66
|
-
tax,
|
|
67
|
-
chain,
|
|
68
|
-
symbol,
|
|
69
|
-
identifier,
|
|
70
|
-
}: { decimal: number; value: SwapKitValueType; tax?: TokenTax } & (
|
|
71
|
-
| { chain: Chain; symbol: string; identifier?: never }
|
|
72
|
-
| { identifier: string; chain?: never; symbol?: never }
|
|
73
|
-
)) {
|
|
74
|
-
super(typeof value === "object" ? value : { decimal, value });
|
|
75
|
-
|
|
76
|
-
const assetInfo = getAssetInfo(identifier || `${chain}.${symbol}`);
|
|
77
|
-
|
|
78
|
-
this.type = getAssetType(assetInfo);
|
|
79
|
-
this.tax = tax;
|
|
80
|
-
this.chain = assetInfo.chain;
|
|
81
|
-
this.ticker = assetInfo.ticker;
|
|
82
|
-
this.symbol = assetInfo.symbol;
|
|
83
|
-
this.address = assetInfo.address;
|
|
84
|
-
this.isSynthetic = assetInfo.isSynthetic;
|
|
85
|
-
this.isTradeAsset = assetInfo.isTradeAsset;
|
|
86
|
-
this.isGasAsset = assetInfo.isGasAsset;
|
|
87
|
-
this.chainId = ChainToChainId[assetInfo.chain];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
toString({ includeSynthProtocol }: { includeSynthProtocol?: boolean } = {}) {
|
|
91
|
-
return (this.isSynthetic || this.isTradeAsset) && !includeSynthProtocol
|
|
92
|
-
? this.symbol
|
|
93
|
-
: `${this.chain}.${this.symbol}`;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
toUrl() {
|
|
97
|
-
return this.isSynthetic
|
|
98
|
-
? `${this.chain}.${this.symbol.replace("/", ".")}`
|
|
99
|
-
: this.isTradeAsset
|
|
100
|
-
? `${this.chain}.${this.symbol.replace("~", "..")}`
|
|
101
|
-
: this.toString();
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
eqAsset({ chain, symbol }: { chain: Chain; symbol: string }) {
|
|
105
|
-
return this.chain === chain && this.symbol === symbol;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
eq(assetValue: AssetValue) {
|
|
109
|
-
return this.eqAsset(assetValue) && this.eqValue(assetValue);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// THOR.RUNE
|
|
113
|
-
// THOR.ETH.ETH
|
|
114
|
-
// ETH.THOR-0x1234567890
|
|
115
|
-
static fromUrl(urlAsset: string, value: NumberPrimitives = 0) {
|
|
116
|
-
const [chain, ticker, symbol] = urlAsset.split(".");
|
|
117
|
-
if (!(chain && ticker)) {
|
|
118
|
-
throw new SwapKitError({
|
|
119
|
-
errorKey: "helpers_invalid_asset_url",
|
|
120
|
-
info: { urlAsset },
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
const asset = chain === Chain.THORChain && symbol ? `${chain}.${ticker}/${symbol}` : urlAsset;
|
|
125
|
-
|
|
126
|
-
return AssetValue.from({ asset, value });
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
static from<T extends {}>({
|
|
130
|
-
value = 0,
|
|
131
|
-
fromBaseDecimal,
|
|
132
|
-
asyncTokenLookup,
|
|
133
|
-
...fromAssetOrChain
|
|
134
|
-
}: T & AssetValueFromParams): ConditionalAssetValueReturn<T> {
|
|
135
|
-
const parsedValue = value instanceof BigIntArithmetics ? value.getValue("string") : value;
|
|
136
|
-
const assetOrChain = getAssetString(fromAssetOrChain);
|
|
137
|
-
|
|
138
|
-
const { identifier: unsafeIdentifier, decimal: commonAssetDecimal } = getCommonAssetInfo(
|
|
139
|
-
assetOrChain as CommonAssetString,
|
|
140
|
-
);
|
|
141
|
-
|
|
142
|
-
const { chain, isSynthetic, isTradeAsset } = getAssetInfo(unsafeIdentifier);
|
|
143
|
-
const token = staticTokensMap.get(
|
|
144
|
-
CASE_SENSITIVE_CHAINS.includes(chain)
|
|
145
|
-
? (unsafeIdentifier as TokenNames)
|
|
146
|
-
: (unsafeIdentifier.toUpperCase() as TokenNames),
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
const tokenDecimal = token?.decimal || commonAssetDecimal;
|
|
150
|
-
|
|
151
|
-
warnOnce(
|
|
152
|
-
!(asyncTokenLookup || tokenDecimal),
|
|
153
|
-
`Couldn't find static decimal for one or more tokens on ${chain} (Using default ${BaseDecimal[chain]} decimal as fallback).
|
|
154
|
-
This can result in incorrect calculations and mess with amount sent on transactions.
|
|
155
|
-
You can load static assets by installing @swapkit/tokens package and calling AssetValue.loadStaticAssets()
|
|
156
|
-
or by passing asyncTokenLookup: true to the from() function, which will make it async and return a promise.`,
|
|
157
|
-
);
|
|
158
|
-
|
|
159
|
-
const { decimal, identifier, tax } = token || {
|
|
160
|
-
decimal: tokenDecimal || BaseDecimal[chain],
|
|
161
|
-
identifier: unsafeIdentifier,
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
const isSynthOrTrade = isSynthetic || isTradeAsset;
|
|
165
|
-
|
|
166
|
-
const adjustedValue = fromBaseDecimal
|
|
167
|
-
? safeValue(BigInt(parsedValue), fromBaseDecimal)
|
|
168
|
-
: safeValue(parsedValue, decimal);
|
|
169
|
-
|
|
170
|
-
const assetValue = asyncTokenLookup
|
|
171
|
-
? createAssetValue(identifier, fromBaseDecimal ? adjustedValue : parsedValue)
|
|
172
|
-
: isSynthOrTrade
|
|
173
|
-
? createSyntheticAssetValue(identifier, adjustedValue)
|
|
174
|
-
: new AssetValue({ tax, decimal, identifier, value: adjustedValue });
|
|
175
|
-
|
|
176
|
-
return assetValue as ConditionalAssetValueReturn<T>;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
static async loadStaticAssets(listNames?: TokenListName[]) {
|
|
180
|
-
const lists = await loadTokenLists(listNames);
|
|
181
|
-
|
|
182
|
-
for (const { tokens } of Object.values(lists)) {
|
|
183
|
-
for (const { identifier, chain, ...rest } of tokens) {
|
|
184
|
-
const tokenKey = (
|
|
185
|
-
CASE_SENSITIVE_CHAINS.includes(chain as Chain) ? identifier : identifier.toUpperCase()
|
|
186
|
-
) as TokenNames;
|
|
187
|
-
const tokenDecimal = "decimals" in rest ? rest.decimals : BaseDecimal[chain as Chain];
|
|
188
|
-
|
|
189
|
-
staticTokensMap.set(tokenKey, { identifier, decimal: tokenDecimal });
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return true;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
static setStaticAssets(
|
|
197
|
-
tokenMap: Map<
|
|
198
|
-
string,
|
|
199
|
-
{ tax?: TokenTax; identifier: string; chain: Chain } & (
|
|
200
|
-
| { decimal: number }
|
|
201
|
-
| { decimals: number }
|
|
202
|
-
)
|
|
203
|
-
>,
|
|
204
|
-
) {
|
|
205
|
-
staticTokensMap.clear();
|
|
206
|
-
for (const [key, value] of tokenMap.entries()) {
|
|
207
|
-
const tokenKey = (
|
|
208
|
-
CASE_SENSITIVE_CHAINS.includes(value.chain)
|
|
209
|
-
? value.identifier
|
|
210
|
-
: value.identifier.toUpperCase()
|
|
211
|
-
) as TokenNames;
|
|
212
|
-
const tokenDecimal = "decimals" in value ? value.decimals : value.decimal;
|
|
213
|
-
staticTokensMap.set(key, { ...value, decimal: tokenDecimal, identifier: tokenKey });
|
|
214
|
-
}
|
|
215
|
-
return true;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export function getMinAmountByChain(chain: Chain) {
|
|
220
|
-
const asset = AssetValue.from({ chain });
|
|
221
|
-
|
|
222
|
-
return match(chain)
|
|
223
|
-
.with(Chain.Bitcoin, Chain.Litecoin, Chain.BitcoinCash, Chain.Dash, () => asset.set(0.00010001))
|
|
224
|
-
.with(Chain.Dogecoin, () => asset.set(1.00000001))
|
|
225
|
-
.with(Chain.Avalanche, Chain.Ethereum, Chain.Arbitrum, Chain.BinanceSmartChain, () =>
|
|
226
|
-
asset.set(0.00000001),
|
|
227
|
-
)
|
|
228
|
-
.with(Chain.THORChain, Chain.Maya, () => asset.set(0))
|
|
229
|
-
.with(Chain.Cosmos, Chain.Kujira, () => asset.set(0.000001))
|
|
230
|
-
.otherwise(() => asset.set(0.00000001));
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
async function createAssetValue(identifier: string, value: NumberPrimitives = 0) {
|
|
234
|
-
validateIdentifier(identifier);
|
|
235
|
-
|
|
236
|
-
const isCaseSensitiveChain = identifier.includes("SOL.");
|
|
237
|
-
|
|
238
|
-
const modifiedIdentifier = isCaseSensitiveChain
|
|
239
|
-
? (identifier as TokenNames)
|
|
240
|
-
: (identifier.toUpperCase() as TokenNames);
|
|
241
|
-
|
|
242
|
-
const staticToken = staticTokensMap.get(modifiedIdentifier);
|
|
243
|
-
const decimal = staticToken?.decimal || (await getDecimal(getAssetInfo(identifier)));
|
|
244
|
-
if (!staticToken) {
|
|
245
|
-
staticTokensMap.set(modifiedIdentifier, { identifier, decimal });
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return new AssetValue({
|
|
249
|
-
decimal,
|
|
250
|
-
value: safeValue(value, decimal),
|
|
251
|
-
identifier,
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function createSyntheticAssetValue(identifier: string, value: NumberPrimitives = 0) {
|
|
256
|
-
const chain = identifier.includes(".")
|
|
257
|
-
? (identifier.split(".")?.[0]?.toUpperCase() as Chain)
|
|
258
|
-
: undefined;
|
|
259
|
-
const isMayaOrThor = chain ? [Chain.Maya, Chain.THORChain].includes(chain) : false;
|
|
260
|
-
|
|
261
|
-
const assetSeparator = identifier.slice(0, 14).includes("~") ? "~" : "/";
|
|
262
|
-
|
|
263
|
-
const [synthChain, symbol] = isMayaOrThor
|
|
264
|
-
? identifier.split(".").slice(1).join().split(assetSeparator)
|
|
265
|
-
: identifier.split(assetSeparator);
|
|
266
|
-
|
|
267
|
-
if (!(synthChain && symbol)) {
|
|
268
|
-
throw new SwapKitError({
|
|
269
|
-
errorKey: "helpers_invalid_asset_identifier",
|
|
270
|
-
info: { identifier },
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
return new AssetValue({
|
|
275
|
-
decimal: 8,
|
|
276
|
-
value: safeValue(value, 8),
|
|
277
|
-
identifier: `${chain || Chain.THORChain}.${synthChain}${assetSeparator}${symbol}`,
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function safeValue(value: NumberPrimitives, decimal: number) {
|
|
282
|
-
return typeof value === "bigint"
|
|
283
|
-
? formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal })
|
|
284
|
-
: value;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
function validateAssetChain(assetOrChain: AssetIdentifier) {
|
|
288
|
-
const chain = match(assetOrChain)
|
|
289
|
-
.when(
|
|
290
|
-
(x): x is { chain: Chain } => "chain" in x && x.chain !== undefined,
|
|
291
|
-
({ chain }) => chain,
|
|
292
|
-
)
|
|
293
|
-
.otherwise((x) => {
|
|
294
|
-
const assetInfo = assetFromString((x as { asset: string }).asset);
|
|
295
|
-
return assetInfo.synth ? Chain.THORChain : assetInfo.chain;
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
// TODO: move to SKConfig chains once we support it throughout sdk
|
|
299
|
-
if (!Object.values(Chain).includes(chain.toUpperCase() as Chain)) {
|
|
300
|
-
throw new SwapKitError({
|
|
301
|
-
errorKey: "helpers_invalid_asset_identifier",
|
|
302
|
-
info: { message: "Please use the AssetValue constructor for unsupported chains" },
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
function getAssetString(assetOrChain: AssetIdentifier) {
|
|
308
|
-
validateAssetChain(assetOrChain);
|
|
309
|
-
|
|
310
|
-
if ("chain" in assetOrChain) return assetOrChain.chain;
|
|
311
|
-
|
|
312
|
-
const { chain, symbol } = assetFromString(assetOrChain.asset);
|
|
313
|
-
const isNativeChain = getAssetType({ chain, symbol }) === "Native";
|
|
314
|
-
|
|
315
|
-
return isNativeChain ? chain : assetOrChain.asset;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
function getAssetInfo(identifier: string) {
|
|
319
|
-
const shortIdentifier = identifier.slice(0, 14);
|
|
320
|
-
const splitIdentifier = identifier.split(".");
|
|
321
|
-
const identifierChain = splitIdentifier[0]?.toUpperCase() as Chain;
|
|
322
|
-
const isThorOrMaya = [Chain.THORChain, Chain.Maya].includes(identifierChain);
|
|
323
|
-
|
|
324
|
-
const isSynthetic = shortIdentifier.includes("/");
|
|
325
|
-
const isTradeAsset = shortIdentifier.includes("~");
|
|
326
|
-
const isSynthOrTrade = isSynthetic || isTradeAsset;
|
|
327
|
-
const assetSeparator = isTradeAsset ? "~" : "/";
|
|
328
|
-
|
|
329
|
-
const [synthChain, synthSymbol = ""] = isThorOrMaya
|
|
330
|
-
? splitIdentifier.slice(1).join().split(assetSeparator)
|
|
331
|
-
: identifier.split(assetSeparator);
|
|
332
|
-
|
|
333
|
-
if (isSynthOrTrade && !(synthChain && synthSymbol)) {
|
|
334
|
-
throw new SwapKitError({
|
|
335
|
-
errorKey: "helpers_invalid_asset_identifier",
|
|
336
|
-
info: { identifier },
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
const [chain, ...rest] = (
|
|
341
|
-
identifier.includes(".") && !isSynthOrTrade ? identifier : `${identifierChain}.${synthSymbol}`
|
|
342
|
-
).split(".") as [Chain, string];
|
|
343
|
-
|
|
344
|
-
const assetSymbol = isSynthOrTrade ? synthSymbol : rest.join(".");
|
|
345
|
-
|
|
346
|
-
const { address, ticker } = getAssetBaseInfo({ symbol: assetSymbol, chain });
|
|
347
|
-
|
|
348
|
-
let formattedAddress: string | undefined;
|
|
349
|
-
try {
|
|
350
|
-
formattedAddress =
|
|
351
|
-
address && EVMChains.includes(chain as EVMChain) && getAddress(address)
|
|
352
|
-
? getAddress(address)
|
|
353
|
-
: address;
|
|
354
|
-
} catch (_error) {
|
|
355
|
-
formattedAddress = address;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
const symbol =
|
|
359
|
-
(isSynthOrTrade ? `${synthChain}${assetSeparator}` : "") +
|
|
360
|
-
(formattedAddress ? `${ticker}-${formattedAddress ?? ""}` : assetSymbol);
|
|
361
|
-
|
|
362
|
-
return {
|
|
363
|
-
address: formattedAddress,
|
|
364
|
-
chain,
|
|
365
|
-
isSynthOrTrade,
|
|
366
|
-
isSynthetic,
|
|
367
|
-
isTradeAsset,
|
|
368
|
-
ticker,
|
|
369
|
-
symbol,
|
|
370
|
-
isGasAsset: isGasAsset({ chain, symbol: assetSymbol }),
|
|
371
|
-
};
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
function getAssetBaseInfo({ symbol, chain }: { symbol: string; chain: Chain }) {
|
|
375
|
-
const splitSymbol = symbol.split("-");
|
|
376
|
-
const unformattedAddress =
|
|
377
|
-
splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1];
|
|
378
|
-
|
|
379
|
-
const address = CASE_SENSITIVE_CHAINS.includes(chain)
|
|
380
|
-
? unformattedAddress
|
|
381
|
-
: unformattedAddress?.toLowerCase();
|
|
382
|
-
const ticker = (
|
|
383
|
-
splitSymbol.length === 1 ? splitSymbol[0] : splitSymbol.slice(0, -1).join("-")
|
|
384
|
-
) as string;
|
|
385
|
-
|
|
386
|
-
return { address, ticker };
|
|
387
|
-
}
|