@swapkit/helpers 1.0.0-rc.52 → 1.0.0-rc.54
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/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +35 -36
- package/dist/index.es.js +439 -397
- package/dist/index.es.js.map +1 -1
- package/package.json +3 -3
- package/src/helpers/liquidity.ts +35 -29
- package/src/helpers/memo.ts +2 -11
- package/src/helpers/others.ts +6 -6
- package/src/helpers/validators.ts +4 -3
- package/src/modules/assetValue.ts +80 -110
- package/src/types.ts +28 -0
package/src/helpers/liquidity.ts
CHANGED
|
@@ -25,11 +25,11 @@ type PoolParams<T = {}> = T & {
|
|
|
25
25
|
* share = (s * A * (2 * T^2 - 2 * T * s + s^2))/T^3
|
|
26
26
|
* (part1 * (part2 - part3 + part4)) / part5
|
|
27
27
|
*/
|
|
28
|
-
export
|
|
28
|
+
export function getAsymmetricRuneShare({
|
|
29
29
|
liquidityUnits,
|
|
30
30
|
poolUnits,
|
|
31
31
|
runeDepth,
|
|
32
|
-
}: ShareParams<{ runeDepth: string }>)
|
|
32
|
+
}: ShareParams<{ runeDepth: string }>) {
|
|
33
33
|
const s = toTCSwapKitNumber(liquidityUnits);
|
|
34
34
|
const T = toTCSwapKitNumber(poolUnits);
|
|
35
35
|
const A = toTCSwapKitNumber(runeDepth);
|
|
@@ -43,13 +43,13 @@ export const getAsymmetricRuneShare = ({
|
|
|
43
43
|
const numerator = part1.mul(part2.sub(part3).add(part4));
|
|
44
44
|
|
|
45
45
|
return numerator.div(part5);
|
|
46
|
-
}
|
|
46
|
+
}
|
|
47
47
|
|
|
48
|
-
export
|
|
48
|
+
export function getAsymmetricAssetShare({
|
|
49
49
|
liquidityUnits,
|
|
50
50
|
poolUnits,
|
|
51
51
|
assetDepth,
|
|
52
|
-
}: ShareParams<{ assetDepth: string }>)
|
|
52
|
+
}: ShareParams<{ assetDepth: string }>) {
|
|
53
53
|
const s = toTCSwapKitNumber(liquidityUnits);
|
|
54
54
|
const T = toTCSwapKitNumber(poolUnits);
|
|
55
55
|
const A = toTCSwapKitNumber(assetDepth);
|
|
@@ -62,28 +62,31 @@ export const getAsymmetricAssetShare = ({
|
|
|
62
62
|
const part5 = T.mul(T).mul(T);
|
|
63
63
|
|
|
64
64
|
return numerator.div(part5);
|
|
65
|
-
}
|
|
65
|
+
}
|
|
66
66
|
|
|
67
|
-
export
|
|
67
|
+
export function getAsymmetricRuneWithdrawAmount({
|
|
68
68
|
percent,
|
|
69
69
|
runeDepth,
|
|
70
70
|
liquidityUnits,
|
|
71
71
|
poolUnits,
|
|
72
|
-
}: ShareParams<{ percent: number; runeDepth: string }>)
|
|
73
|
-
getAsymmetricRuneShare({ runeDepth, liquidityUnits, poolUnits }).mul(percent);
|
|
72
|
+
}: ShareParams<{ percent: number; runeDepth: string }>) {
|
|
73
|
+
return getAsymmetricRuneShare({ runeDepth, liquidityUnits, poolUnits }).mul(percent);
|
|
74
|
+
}
|
|
74
75
|
|
|
75
|
-
export
|
|
76
|
+
export function getAsymmetricAssetWithdrawAmount({
|
|
76
77
|
percent,
|
|
77
78
|
assetDepth,
|
|
78
79
|
liquidityUnits,
|
|
79
80
|
poolUnits,
|
|
80
|
-
}: ShareParams<{ percent: number; assetDepth: string }>)
|
|
81
|
-
getAsymmetricAssetShare({ assetDepth, liquidityUnits, poolUnits }).mul(percent);
|
|
81
|
+
}: ShareParams<{ percent: number; assetDepth: string }>) {
|
|
82
|
+
return getAsymmetricAssetShare({ assetDepth, liquidityUnits, poolUnits }).mul(percent);
|
|
83
|
+
}
|
|
82
84
|
|
|
83
|
-
|
|
84
|
-
SwapKitNumber.fromBigInt(BigInt(value), BaseDecimal.THOR);
|
|
85
|
+
function toTCSwapKitNumber(value: string) {
|
|
86
|
+
return SwapKitNumber.fromBigInt(BigInt(value), BaseDecimal.THOR);
|
|
87
|
+
}
|
|
85
88
|
|
|
86
|
-
export
|
|
89
|
+
export function getSymmetricPoolShare({
|
|
87
90
|
liquidityUnits,
|
|
88
91
|
poolUnits,
|
|
89
92
|
runeDepth,
|
|
@@ -91,12 +94,14 @@ export const getSymmetricPoolShare = ({
|
|
|
91
94
|
}: ShareParams<{
|
|
92
95
|
runeDepth: string;
|
|
93
96
|
assetDepth: string;
|
|
94
|
-
}>)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
}>) {
|
|
98
|
+
return {
|
|
99
|
+
assetAmount: toTCSwapKitNumber(assetDepth).mul(liquidityUnits).div(poolUnits),
|
|
100
|
+
runeAmount: toTCSwapKitNumber(runeDepth).mul(liquidityUnits).div(poolUnits),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function getSymmetricWithdraw({
|
|
100
105
|
liquidityUnits,
|
|
101
106
|
poolUnits,
|
|
102
107
|
runeDepth,
|
|
@@ -106,14 +111,15 @@ export const getSymmetricWithdraw = ({
|
|
|
106
111
|
runeDepth: string;
|
|
107
112
|
assetDepth: string;
|
|
108
113
|
percent: number;
|
|
109
|
-
}>)
|
|
110
|
-
Object.fromEntries(
|
|
114
|
+
}>) {
|
|
115
|
+
return Object.fromEntries(
|
|
111
116
|
Object.entries(getSymmetricPoolShare({ liquidityUnits, poolUnits, runeDepth, assetDepth })).map(
|
|
112
117
|
([name, value]) => [name, value.mul(percent)],
|
|
113
118
|
),
|
|
114
119
|
);
|
|
120
|
+
}
|
|
115
121
|
|
|
116
|
-
export
|
|
122
|
+
export function getEstimatedPoolShare({
|
|
117
123
|
runeDepth,
|
|
118
124
|
poolUnits,
|
|
119
125
|
assetDepth,
|
|
@@ -125,7 +131,7 @@ export const getEstimatedPoolShare = ({
|
|
|
125
131
|
assetAmount: string;
|
|
126
132
|
runeDepth: string;
|
|
127
133
|
assetDepth: string;
|
|
128
|
-
}>)
|
|
134
|
+
}>) {
|
|
129
135
|
const R = new SwapKitNumber({ value: runeDepth, decimal: 8 });
|
|
130
136
|
const A = new SwapKitNumber({ value: assetDepth, decimal: 8 });
|
|
131
137
|
const P = new SwapKitNumber({ value: poolUnits, decimal: 8 });
|
|
@@ -150,14 +156,14 @@ export const getEstimatedPoolShare = ({
|
|
|
150
156
|
const newPoolUnits = P.add(estimatedLiquidityUnits);
|
|
151
157
|
|
|
152
158
|
return estimatedLiquidityUnits.div(newPoolUnits).getBaseValue('number');
|
|
153
|
-
}
|
|
159
|
+
}
|
|
154
160
|
|
|
155
|
-
export
|
|
161
|
+
export function getLiquiditySlippage({
|
|
156
162
|
runeAmount,
|
|
157
163
|
assetAmount,
|
|
158
164
|
runeDepth,
|
|
159
165
|
assetDepth,
|
|
160
|
-
}: PoolParams)
|
|
166
|
+
}: PoolParams) {
|
|
161
167
|
if (runeAmount === '0' || assetAmount === '0' || runeDepth === '0' || assetDepth === '0')
|
|
162
168
|
return 0;
|
|
163
169
|
// formula: (t * R - T * r)/ (T*r + R*T)
|
|
@@ -171,4 +177,4 @@ export const getLiquiditySlippage = ({
|
|
|
171
177
|
|
|
172
178
|
// set absolute value of percent, no negative allowed
|
|
173
179
|
return Math.abs(numerator.div(denominator).getBaseValue('number'));
|
|
174
|
-
}
|
|
180
|
+
}
|
package/src/helpers/memo.ts
CHANGED
|
@@ -9,16 +9,6 @@ export type ThornameRegisterParam = {
|
|
|
9
9
|
expiryBlock?: string;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
const getShortenedSymbol = ({
|
|
13
|
-
symbol,
|
|
14
|
-
ticker,
|
|
15
|
-
chain,
|
|
16
|
-
}: {
|
|
17
|
-
ticker: string;
|
|
18
|
-
symbol: string;
|
|
19
|
-
chain: string | Chain;
|
|
20
|
-
}) => (chain === 'ETH' && ticker !== 'ETH' ? `${ticker}-${symbol.slice(-3)}` : symbol);
|
|
21
|
-
|
|
22
12
|
type WithAddress<T = {}> = T & { address: string };
|
|
23
13
|
type WithChain<T = {}> = T & { chain: Chain };
|
|
24
14
|
|
|
@@ -82,8 +72,9 @@ export const getMemoFor = <T extends MemoType>(memoType: T, options: MemoOptions
|
|
|
82
72
|
const { chain, ticker, symbol, basisPoints, targetAssetString, singleSide } =
|
|
83
73
|
options as MemoOptions<MemoType.WITHDRAW>;
|
|
84
74
|
|
|
75
|
+
const shortenedSymbol =
|
|
76
|
+
chain === 'ETH' && ticker !== 'ETH' ? `${ticker}-${symbol.slice(-3)}` : symbol;
|
|
85
77
|
const target = !singleSide && targetAssetString ? `:${targetAssetString}` : '';
|
|
86
|
-
const shortenedSymbol = getShortenedSymbol({ chain, symbol, ticker });
|
|
87
78
|
const assetDivider = singleSide ? '/' : '.';
|
|
88
79
|
|
|
89
80
|
return `${memoType}:${chain}${assetDivider}${shortenedSymbol}:${basisPoints}${target}`;
|
package/src/helpers/others.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
// 10 rune for register, 1 rune per year
|
|
2
2
|
// MINIMUM_REGISTRATION_FEE = 11
|
|
3
|
-
export
|
|
3
|
+
export function getTHORNameCost(year: number) {
|
|
4
4
|
if (year < 0) throw new Error('Invalid number of year');
|
|
5
5
|
return 10 + year;
|
|
6
|
-
}
|
|
6
|
+
}
|
|
7
7
|
|
|
8
|
-
export
|
|
8
|
+
export function validateTHORName(name: string) {
|
|
9
9
|
if (name.length > 30) return false;
|
|
10
10
|
|
|
11
11
|
const regex = /^[a-zA-Z0-9+_-]+$/g;
|
|
12
12
|
|
|
13
13
|
return !!name.match(regex);
|
|
14
|
-
}
|
|
14
|
+
}
|
|
15
15
|
|
|
16
|
-
export
|
|
16
|
+
export function derivationPathToString([network, chainId, account, change, index]: number[]) {
|
|
17
17
|
const shortPath = typeof index !== 'number';
|
|
18
18
|
|
|
19
19
|
return `${network}'/${chainId}'/${account}'/${change}${shortPath ? '' : `/${index}`}`;
|
|
20
|
-
}
|
|
20
|
+
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Chain } from '@swapkit/types';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// Backward compatibility
|
|
4
|
+
const supportedChains = [...Object.values(Chain), 'TERRA'];
|
|
4
5
|
|
|
5
|
-
export
|
|
6
|
+
export function validateIdentifier(identifier: string = '') {
|
|
6
7
|
const uppercasedIdentifier = identifier.toUpperCase();
|
|
7
8
|
|
|
8
9
|
const [chain] = uppercasedIdentifier.split('.') as [Chain, string];
|
|
@@ -14,4 +15,4 @@ export const validateIdentifier = (identifier: string = '') => {
|
|
|
14
15
|
throw new Error(
|
|
15
16
|
`Invalid identifier: ${identifier}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`,
|
|
16
17
|
);
|
|
17
|
-
}
|
|
18
|
+
}
|
|
@@ -1,74 +1,18 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CoinGeckoList,
|
|
3
|
-
MayaList,
|
|
4
|
-
PancakeswapETHList,
|
|
5
|
-
PancakeswapList,
|
|
6
|
-
PangolinList,
|
|
7
|
-
StargateARBList,
|
|
8
|
-
SushiswapList,
|
|
9
|
-
ThorchainList,
|
|
10
|
-
TraderjoeList,
|
|
11
|
-
UniswapList,
|
|
12
|
-
WoofiList,
|
|
13
|
-
} from '@swapkit/tokens';
|
|
14
1
|
import { BaseDecimal, Chain } from '@swapkit/types';
|
|
15
2
|
|
|
16
3
|
import type { CommonAssetString } from '../helpers/asset.ts';
|
|
17
4
|
import { getAssetType, getCommonAssetInfo, getDecimal, isGasAsset } from '../helpers/asset.ts';
|
|
18
5
|
import { validateIdentifier } from '../helpers/validators.ts';
|
|
6
|
+
import type { TokenNames, TokenTax } from '../types.ts';
|
|
19
7
|
|
|
20
8
|
import type { NumberPrimitives } from './bigIntArithmetics.ts';
|
|
21
9
|
import { BigIntArithmetics, formatBigIntToSafeValue } from './bigIntArithmetics.ts';
|
|
22
10
|
import type { SwapKitValueType } from './swapKitNumber.ts';
|
|
23
11
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
? formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal })
|
|
29
|
-
: value;
|
|
30
|
-
|
|
31
|
-
type AssetValueParams = { decimal: number; value: SwapKitValueType; tax?: TokenTax } & (
|
|
32
|
-
| { chain: Chain; symbol: string }
|
|
33
|
-
| { identifier: string }
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
type TCTokenNames = (typeof ThorchainList)['tokens'][number]['identifier'];
|
|
37
|
-
|
|
38
|
-
type TokenNames =
|
|
39
|
-
| TCTokenNames
|
|
40
|
-
| (typeof CoinGeckoList)['tokens'][number]['identifier']
|
|
41
|
-
| (typeof MayaList)['tokens'][number]['identifier']
|
|
42
|
-
| (typeof PancakeswapETHList)['tokens'][number]['identifier']
|
|
43
|
-
| (typeof PancakeswapList)['tokens'][number]['identifier']
|
|
44
|
-
| (typeof PangolinList)['tokens'][number]['identifier']
|
|
45
|
-
| (typeof StargateARBList)['tokens'][number]['identifier']
|
|
46
|
-
| (typeof SushiswapList)['tokens'][number]['identifier']
|
|
47
|
-
| (typeof TraderjoeList)['tokens'][number]['identifier']
|
|
48
|
-
| (typeof WoofiList)['tokens'][number]['identifier']
|
|
49
|
-
| (typeof UniswapList)['tokens'][number]['identifier'];
|
|
50
|
-
|
|
51
|
-
let staticTokensMap:
|
|
52
|
-
| Map<TokenNames, { tax?: TokenTax; decimal: number; identifier: string }>
|
|
53
|
-
| undefined;
|
|
54
|
-
|
|
55
|
-
const getStaticToken = (identifier: TokenNames) => {
|
|
56
|
-
if (!staticTokensMap) {
|
|
57
|
-
throw new Error('Static assets not loaded, call await AssetValue.loadStaticAssets() first');
|
|
58
|
-
}
|
|
59
|
-
const tokenInfo = staticTokensMap.get(identifier.toUpperCase() as TokenNames);
|
|
60
|
-
|
|
61
|
-
return tokenInfo || { decimal: BaseDecimal.THOR, identifier: '' };
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const createAssetValue = async (assetString: string, value: NumberPrimitives = 0) => {
|
|
65
|
-
validateIdentifier(assetString);
|
|
66
|
-
|
|
67
|
-
const decimal = await getDecimal(getAssetInfo(assetString));
|
|
68
|
-
const parsedValue = safeValue(value, decimal);
|
|
69
|
-
|
|
70
|
-
return new AssetValue({ decimal, value: parsedValue, identifier: assetString });
|
|
71
|
-
};
|
|
12
|
+
const staticTokensMap = new Map<
|
|
13
|
+
TokenNames,
|
|
14
|
+
{ tax?: TokenTax; decimal: number; identifier: string }
|
|
15
|
+
>();
|
|
72
16
|
|
|
73
17
|
export class AssetValue extends BigIntArithmetics {
|
|
74
18
|
address?: string;
|
|
@@ -80,27 +24,29 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
80
24
|
ticker: string;
|
|
81
25
|
type: ReturnType<typeof getAssetType>;
|
|
82
26
|
|
|
83
|
-
constructor(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
27
|
+
constructor({
|
|
28
|
+
value,
|
|
29
|
+
decimal,
|
|
30
|
+
tax,
|
|
31
|
+
chain,
|
|
32
|
+
symbol,
|
|
33
|
+
identifier,
|
|
34
|
+
}: { decimal: number; value: SwapKitValueType; tax?: TokenTax } & (
|
|
35
|
+
| { chain: Chain; symbol: string; identifier?: never }
|
|
36
|
+
| { identifier: string; chain?: never; symbol?: never }
|
|
37
|
+
)) {
|
|
38
|
+
super(typeof value === 'object' ? value : { decimal, value });
|
|
92
39
|
|
|
93
|
-
const assetInfo = getAssetInfo(identifier);
|
|
40
|
+
const assetInfo = getAssetInfo(identifier || `${chain}.${symbol}`);
|
|
94
41
|
|
|
95
42
|
this.type = getAssetType(assetInfo);
|
|
43
|
+
this.tax = tax;
|
|
96
44
|
this.chain = assetInfo.chain;
|
|
97
45
|
this.ticker = assetInfo.ticker;
|
|
98
46
|
this.symbol = assetInfo.symbol;
|
|
99
47
|
this.address = assetInfo.address;
|
|
100
48
|
this.isSynthetic = assetInfo.isSynthetic;
|
|
101
49
|
this.isGasAsset = assetInfo.isGasAsset;
|
|
102
|
-
|
|
103
|
-
this.tax = params.tax;
|
|
104
50
|
}
|
|
105
51
|
|
|
106
52
|
toString() {
|
|
@@ -131,45 +77,54 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
131
77
|
static fromString(assetString: string, value: NumberPrimitives = 0) {
|
|
132
78
|
return createAssetValue(assetString, value);
|
|
133
79
|
}
|
|
80
|
+
static fromIdentifier(
|
|
81
|
+
assetString:
|
|
82
|
+
| `${Chain}.${string}`
|
|
83
|
+
| `${Chain}/${string}`
|
|
84
|
+
| `${Chain}.${string}-${string}`
|
|
85
|
+
| TokenNames,
|
|
86
|
+
value: NumberPrimitives = 0,
|
|
87
|
+
) {
|
|
88
|
+
return createAssetValue(assetString, value);
|
|
89
|
+
}
|
|
134
90
|
|
|
135
91
|
static fromStringSync(assetString: string, value: NumberPrimitives = 0) {
|
|
136
92
|
const { isSynthetic } = getAssetInfo(assetString);
|
|
137
|
-
const
|
|
138
|
-
tax,
|
|
139
|
-
decimal,
|
|
140
|
-
identifier: tokenIdentifier,
|
|
141
|
-
} = getStaticToken(assetString as unknown as TokenNames);
|
|
93
|
+
const tokenInfo = staticTokensMap.get(assetString.toUpperCase() as TokenNames);
|
|
142
94
|
|
|
143
|
-
|
|
95
|
+
if (!tokenInfo) {
|
|
96
|
+
console.error(
|
|
97
|
+
`Asset ${assetString} is not loaded. Use AssetValue.loadStaticAssets() to load it`,
|
|
98
|
+
);
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
144
101
|
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
102
|
+
const { tax, decimal, identifier } = tokenInfo;
|
|
103
|
+
return new AssetValue({
|
|
104
|
+
tax,
|
|
105
|
+
value: safeValue(value, decimal),
|
|
106
|
+
identifier: isSynthetic ? assetString : identifier,
|
|
107
|
+
decimal: isSynthetic ? 8 : decimal,
|
|
108
|
+
});
|
|
152
109
|
}
|
|
153
110
|
|
|
154
|
-
static
|
|
155
|
-
|
|
156
|
-
value: NumberPrimitives = 0,
|
|
157
|
-
) {
|
|
158
|
-
return createAssetValue(assetString, value);
|
|
159
|
-
}
|
|
111
|
+
static fromIdentifierSync(assetString: TokenNames, value: NumberPrimitives = 0) {
|
|
112
|
+
const tokenInfo = staticTokensMap.get(assetString);
|
|
160
113
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
114
|
+
if (!tokenInfo) {
|
|
115
|
+
console.error(
|
|
116
|
+
`Asset ${assetString} is not loaded. Use AssetValue.loadStaticAssets() to load it`,
|
|
117
|
+
);
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
164
120
|
|
|
165
|
-
|
|
121
|
+
const { tax, decimal, identifier } = tokenInfo;
|
|
122
|
+
return new AssetValue({ tax, decimal, identifier, value: safeValue(value, decimal) });
|
|
166
123
|
}
|
|
167
124
|
|
|
168
125
|
static fromChainOrSignature(assetString: CommonAssetString, value: NumberPrimitives = 0) {
|
|
169
126
|
const { decimal, identifier } = getCommonAssetInfo(assetString);
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return new AssetValue({ value: parsedValue, decimal, identifier });
|
|
127
|
+
return new AssetValue({ value: safeValue(value, decimal), decimal, identifier });
|
|
173
128
|
}
|
|
174
129
|
|
|
175
130
|
static loadStaticAssets() {
|
|
@@ -177,18 +132,15 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
177
132
|
async (resolve, reject) => {
|
|
178
133
|
try {
|
|
179
134
|
const tokenPackages = await import('@swapkit/tokens');
|
|
180
|
-
|
|
135
|
+
|
|
136
|
+
Object.values(tokenPackages).forEach((tokenList) => {
|
|
181
137
|
tokenList?.tokens?.forEach(({ identifier, chain, ...rest }) => {
|
|
182
|
-
|
|
138
|
+
staticTokensMap.set(identifier.toUpperCase() as TokenNames, {
|
|
183
139
|
identifier,
|
|
184
140
|
decimal: 'decimals' in rest ? rest.decimals : BaseDecimal[chain as Chain],
|
|
185
141
|
});
|
|
186
142
|
});
|
|
187
|
-
|
|
188
|
-
return acc;
|
|
189
|
-
}, new Map<TokenNames, { decimal: number; identifier: string }>());
|
|
190
|
-
|
|
191
|
-
staticTokensMap = tokensMap;
|
|
143
|
+
});
|
|
192
144
|
|
|
193
145
|
resolve({ ok: true });
|
|
194
146
|
} catch (error) {
|
|
@@ -205,7 +157,7 @@ export class AssetValue extends BigIntArithmetics {
|
|
|
205
157
|
}
|
|
206
158
|
}
|
|
207
159
|
|
|
208
|
-
export
|
|
160
|
+
export function getMinAmountByChain(chain: Chain) {
|
|
209
161
|
const asset = AssetValue.fromChainOrSignature(chain);
|
|
210
162
|
|
|
211
163
|
switch (chain) {
|
|
@@ -228,9 +180,27 @@ export const getMinAmountByChain = (chain: Chain) => {
|
|
|
228
180
|
default:
|
|
229
181
|
return asset.set(0.00000001);
|
|
230
182
|
}
|
|
231
|
-
}
|
|
183
|
+
}
|
|
232
184
|
|
|
233
|
-
|
|
185
|
+
async function createAssetValue(identifier: string, value: NumberPrimitives = 0) {
|
|
186
|
+
validateIdentifier(identifier);
|
|
187
|
+
|
|
188
|
+
const staticToken = staticTokensMap.get(identifier.toUpperCase() as TokenNames);
|
|
189
|
+
const decimal = staticToken?.decimal || (await getDecimal(getAssetInfo(identifier)));
|
|
190
|
+
if (!staticToken) {
|
|
191
|
+
staticTokensMap.set(identifier.toUpperCase() as TokenNames, { identifier, decimal });
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return new AssetValue({ decimal, value: safeValue(value, decimal), identifier });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function safeValue(value: NumberPrimitives, decimal: number) {
|
|
198
|
+
return typeof value === 'bigint'
|
|
199
|
+
? formatBigIntToSafeValue({ value, bigIntDecimal: decimal, decimal })
|
|
200
|
+
: value;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function getAssetInfo(identifier: string) {
|
|
234
204
|
const isSynthetic = identifier.slice(0, 14).includes('/');
|
|
235
205
|
const [synthChain, synthSymbol] = identifier.split('.').pop()!.split('/');
|
|
236
206
|
const adjustedIdentifier =
|
|
@@ -249,4 +219,4 @@ const getAssetInfo = (identifier: string) => {
|
|
|
249
219
|
(address ? `${ticker}-${address?.toLowerCase() ?? ''}` : symbol),
|
|
250
220
|
ticker,
|
|
251
221
|
};
|
|
252
|
-
}
|
|
222
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CoinGeckoList,
|
|
3
|
+
MayaList,
|
|
4
|
+
PancakeswapETHList,
|
|
5
|
+
PancakeswapList,
|
|
6
|
+
PangolinList,
|
|
7
|
+
StargateARBList,
|
|
8
|
+
SushiswapList,
|
|
9
|
+
ThorchainList,
|
|
10
|
+
TraderjoeList,
|
|
11
|
+
UniswapList,
|
|
12
|
+
WoofiList,
|
|
13
|
+
} from '@swapkit/tokens';
|
|
14
|
+
|
|
15
|
+
export type TokenTax = { buy: number; sell: number };
|
|
16
|
+
|
|
17
|
+
export type TokenNames =
|
|
18
|
+
| (typeof ThorchainList)['tokens'][number]['identifier']
|
|
19
|
+
| (typeof CoinGeckoList)['tokens'][number]['identifier']
|
|
20
|
+
| (typeof MayaList)['tokens'][number]['identifier']
|
|
21
|
+
| (typeof PancakeswapETHList)['tokens'][number]['identifier']
|
|
22
|
+
| (typeof PancakeswapList)['tokens'][number]['identifier']
|
|
23
|
+
| (typeof PangolinList)['tokens'][number]['identifier']
|
|
24
|
+
| (typeof StargateARBList)['tokens'][number]['identifier']
|
|
25
|
+
| (typeof SushiswapList)['tokens'][number]['identifier']
|
|
26
|
+
| (typeof TraderjoeList)['tokens'][number]['identifier']
|
|
27
|
+
| (typeof WoofiList)['tokens'][number]['identifier']
|
|
28
|
+
| (typeof UniswapList)['tokens'][number]['identifier'];
|