@swapkit/helpers 1.0.0-rc.0 → 1.0.0-rc.10
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.d.ts +76 -47
- package/dist/index.es.js +816 -403
- package/package.json +8 -4
- package/src/helpers/asset.ts +25 -13
- package/src/helpers/liquidity.ts +19 -4
- package/src/helpers/others.ts +0 -39
- package/src/helpers/request.ts +16 -0
- package/src/index.ts +5 -4
- package/src/modules/__tests__/assetValue.test.ts +24 -8
- package/src/modules/__tests__/swapKitNumber.test.ts +155 -76
- package/src/modules/assetValue.ts +46 -40
- package/src/modules/bigIntArithmetics.ts +210 -99
- package/src/modules/swapKitNumber.ts +0 -4
- package/dist/index.es-8503fb04.js +0 -34669
- package/dist/index.es-903b9173.cjs +0 -1
- package/src/helpers/number.ts +0 -40
package/package.json
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "swapkit-oss-team",
|
|
3
3
|
"description": "SwapKit Lib swapkit-helpers",
|
|
4
|
+
"dependencies": {
|
|
5
|
+
"ky": "1.1.2"
|
|
6
|
+
},
|
|
4
7
|
"devDependencies": {
|
|
5
8
|
"@vitest/coverage-istanbul": "0.34.4",
|
|
6
9
|
"vite": "4.4.9",
|
|
7
10
|
"vitest": "0.34.4",
|
|
8
11
|
"@internal/config": "0.0.0-internal.0",
|
|
9
|
-
"@swapkit/tokens": "1.0.0-rc.
|
|
10
|
-
"@swapkit/types": "1.0.0-rc.
|
|
12
|
+
"@swapkit/tokens": "1.0.0-rc.6",
|
|
13
|
+
"@swapkit/types": "1.0.0-rc.6"
|
|
11
14
|
},
|
|
12
15
|
"eslintConfig": {
|
|
13
16
|
"extends": "../../../internal/eslint-config"
|
|
14
17
|
},
|
|
15
18
|
"peerDependencies": {
|
|
16
|
-
"@swapkit/
|
|
19
|
+
"@swapkit/tokens": "1.0.0-rc.6",
|
|
20
|
+
"@swapkit/types": "1.0.0-rc.6"
|
|
17
21
|
},
|
|
18
22
|
"exports": {
|
|
19
23
|
".": {
|
|
@@ -38,7 +42,7 @@
|
|
|
38
42
|
"repository": "https://github.com/thorswap/SwapKit.git",
|
|
39
43
|
"type": "module",
|
|
40
44
|
"types": "./dist/index.d.ts",
|
|
41
|
-
"version": "1.0.0-rc.
|
|
45
|
+
"version": "1.0.0-rc.10",
|
|
42
46
|
"scripts": {
|
|
43
47
|
"build": "vite build",
|
|
44
48
|
"clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
|
package/src/helpers/asset.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EVMChain } from '@swapkit/types';
|
|
2
2
|
import { BaseDecimal, Chain, ChainToRPC, FeeOption } from '@swapkit/types';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { type AssetValue, RequestClient } from '../index.ts';
|
|
5
5
|
|
|
6
6
|
const getDecimalMethodHex = '0x313ce567';
|
|
7
7
|
|
|
@@ -9,19 +9,15 @@ export type CommonAssetString = 'MAYA.MAYA' | 'ETH.THOR' | 'ETH.vTHOR' | Chain;
|
|
|
9
9
|
|
|
10
10
|
const getContractDecimals = async ({ chain, to }: { chain: EVMChain; to: string }) => {
|
|
11
11
|
try {
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
JSON.stringify({
|
|
15
|
-
method: 'eth_call',
|
|
16
|
-
params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, 'latest'],
|
|
12
|
+
const { result } = await RequestClient.post<{ result: string }>(ChainToRPC[chain], {
|
|
13
|
+
headers: { accept: '*/*', 'cache-control': 'no-cache' },
|
|
14
|
+
body: JSON.stringify({
|
|
17
15
|
id: 44,
|
|
18
16
|
jsonrpc: '2.0',
|
|
17
|
+
method: 'eth_call',
|
|
18
|
+
params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, 'latest'],
|
|
19
19
|
}),
|
|
20
|
-
|
|
21
|
-
true,
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
const { result } = JSON.parse(response) as { result: string };
|
|
20
|
+
});
|
|
25
21
|
|
|
26
22
|
return parseInt(BigInt(result).toString());
|
|
27
23
|
} catch (error) {
|
|
@@ -90,6 +86,9 @@ export const isGasAsset = ({ chain, symbol }: { chain: Chain; symbol: string })
|
|
|
90
86
|
case Chain.Maya:
|
|
91
87
|
return symbol === 'CACAO';
|
|
92
88
|
|
|
89
|
+
case Chain.Kujira:
|
|
90
|
+
return symbol === 'KUJI';
|
|
91
|
+
|
|
93
92
|
case Chain.Cosmos:
|
|
94
93
|
return symbol === 'ATOM';
|
|
95
94
|
case Chain.Polygon:
|
|
@@ -121,6 +120,7 @@ export const getCommonAssetInfo = (
|
|
|
121
120
|
case 'MAYA.MAYA':
|
|
122
121
|
return { identifier: 'MAYA.MAYA', decimal: 4 };
|
|
123
122
|
|
|
123
|
+
case Chain.Kujira:
|
|
124
124
|
case Chain.Arbitrum:
|
|
125
125
|
case Chain.Optimism:
|
|
126
126
|
case Chain.BitcoinCash:
|
|
@@ -148,7 +148,9 @@ export const getAssetType = ({ chain, symbol }: { chain: Chain; symbol: string }
|
|
|
148
148
|
return 'Native';
|
|
149
149
|
|
|
150
150
|
case Chain.Cosmos:
|
|
151
|
-
return symbol === 'ATOM' ? 'Native' :
|
|
151
|
+
return symbol === 'ATOM' ? 'Native' : Chain.Cosmos;
|
|
152
|
+
case Chain.Kujira:
|
|
153
|
+
return symbol === Chain.Kujira ? 'Native' : Chain.Kujira;
|
|
152
154
|
case Chain.Binance:
|
|
153
155
|
return symbol === Chain.Binance ? 'Native' : 'BEP2';
|
|
154
156
|
case Chain.BinanceSmartChain:
|
|
@@ -156,7 +158,7 @@ export const getAssetType = ({ chain, symbol }: { chain: Chain; symbol: string }
|
|
|
156
158
|
case Chain.Ethereum:
|
|
157
159
|
return symbol === Chain.Ethereum ? 'Native' : 'ERC20';
|
|
158
160
|
case Chain.Avalanche:
|
|
159
|
-
return symbol === Chain.Avalanche ? 'Native' :
|
|
161
|
+
return symbol === Chain.Avalanche ? 'Native' : Chain.Avalanche;
|
|
160
162
|
case Chain.Polygon:
|
|
161
163
|
return symbol === Chain.Polygon ? 'Native' : 'POLYGON';
|
|
162
164
|
|
|
@@ -175,3 +177,13 @@ export const assetFromString = (assetString: string) => {
|
|
|
175
177
|
|
|
176
178
|
return { chain, symbol, ticker, synth };
|
|
177
179
|
};
|
|
180
|
+
|
|
181
|
+
const potentialScamRegex = new RegExp(
|
|
182
|
+
/(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/,
|
|
183
|
+
'gmi',
|
|
184
|
+
);
|
|
185
|
+
export const filterAssets = (assets: AssetValue[]) =>
|
|
186
|
+
assets.filter(
|
|
187
|
+
(asset) =>
|
|
188
|
+
!potentialScamRegex.test(asset.toString()) && !asset.toString().includes('undefined'),
|
|
189
|
+
);
|
package/src/helpers/liquidity.ts
CHANGED
|
@@ -83,6 +83,19 @@ export const getAsymmetricAssetWithdrawAmount = ({
|
|
|
83
83
|
const toTCSwapKitNumber = (value: string) =>
|
|
84
84
|
new SwapKitNumber({ value, decimal: BaseDecimal.THOR });
|
|
85
85
|
|
|
86
|
+
export const getSymmetricPoolShare = ({
|
|
87
|
+
liquidityUnits,
|
|
88
|
+
poolUnits,
|
|
89
|
+
runeDepth,
|
|
90
|
+
assetDepth,
|
|
91
|
+
}: ShareParams<{
|
|
92
|
+
runeDepth: string;
|
|
93
|
+
assetDepth: string;
|
|
94
|
+
}>) => ({
|
|
95
|
+
assetAmount: toTCSwapKitNumber(assetDepth).mul(liquidityUnits).div(poolUnits),
|
|
96
|
+
runeAmount: toTCSwapKitNumber(runeDepth).mul(liquidityUnits).div(poolUnits),
|
|
97
|
+
});
|
|
98
|
+
|
|
86
99
|
export const getSymmetricWithdraw = ({
|
|
87
100
|
liquidityUnits,
|
|
88
101
|
poolUnits,
|
|
@@ -93,10 +106,12 @@ export const getSymmetricWithdraw = ({
|
|
|
93
106
|
runeDepth: string;
|
|
94
107
|
assetDepth: string;
|
|
95
108
|
percent: number;
|
|
96
|
-
}>) =>
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
109
|
+
}>) =>
|
|
110
|
+
Object.fromEntries(
|
|
111
|
+
Object.entries(getSymmetricPoolShare({ liquidityUnits, poolUnits, runeDepth, assetDepth })).map(
|
|
112
|
+
([name, value]) => [name, value.mul(percent)],
|
|
113
|
+
),
|
|
114
|
+
);
|
|
100
115
|
|
|
101
116
|
export const getEstimatedPoolShare = ({
|
|
102
117
|
runeDepth,
|
package/src/helpers/others.ts
CHANGED
|
@@ -18,42 +18,3 @@ export const derivationPathToString = ([network, chainId, account, change, index
|
|
|
18
18
|
|
|
19
19
|
return `${network}'/${chainId}'/${account}'/${change}${shortPath ? '' : `/${index}`}`;
|
|
20
20
|
};
|
|
21
|
-
|
|
22
|
-
export const getRequest = async <T>(
|
|
23
|
-
url: string,
|
|
24
|
-
params?: { [key in string]?: any },
|
|
25
|
-
): Promise<T> => {
|
|
26
|
-
const queryParams = Object.entries(params || {}).reduce(
|
|
27
|
-
(acc, [key, value]) => {
|
|
28
|
-
if (value) {
|
|
29
|
-
acc[key] = value;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return acc;
|
|
33
|
-
},
|
|
34
|
-
{} as { [key in string]: any },
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
const response = await fetch(
|
|
38
|
-
`${url}${params ? `?${new URLSearchParams(queryParams).toString()}` : ''}`,
|
|
39
|
-
{ method: 'GET', mode: 'cors', credentials: 'omit', referrer: 'https://sk.thorswap.net' },
|
|
40
|
-
);
|
|
41
|
-
|
|
42
|
-
return response.json();
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export const postRequest = async <T>(
|
|
46
|
-
url: string,
|
|
47
|
-
body: string,
|
|
48
|
-
headers?: Record<string, string>,
|
|
49
|
-
parseAsString = false,
|
|
50
|
-
): Promise<T> => {
|
|
51
|
-
const response = await fetch(`${url}`, {
|
|
52
|
-
body,
|
|
53
|
-
headers,
|
|
54
|
-
method: 'POST',
|
|
55
|
-
referrer: 'https://sk.thorswap.net',
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
return parseAsString ? response.text() : response.json();
|
|
59
|
-
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Options } from 'ky';
|
|
2
|
+
import ky from 'ky';
|
|
3
|
+
|
|
4
|
+
const kyClient = ky.create({
|
|
5
|
+
headers: {
|
|
6
|
+
referrer: 'https://sk.thorswap.net',
|
|
7
|
+
referer: 'https://sk.thorswap.net',
|
|
8
|
+
'Content-Type': 'application/json',
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export const RequestClient = {
|
|
13
|
+
get: <T>(url: string | URL | Request, options?: Options) => kyClient.get(url, options).json<T>(),
|
|
14
|
+
post: <T>(url: string | URL | Request, options?: Options) =>
|
|
15
|
+
kyClient.post(url, options).json<T>(),
|
|
16
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -4,12 +4,13 @@
|
|
|
4
4
|
export * from './helpers/asset.ts';
|
|
5
5
|
export * from './helpers/liquidity.ts';
|
|
6
6
|
export * from './helpers/memo.ts';
|
|
7
|
-
export * from './helpers/number.ts';
|
|
8
7
|
export * from './helpers/others.ts';
|
|
8
|
+
export * from './helpers/request.ts';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Modules
|
|
12
12
|
*/
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
13
|
+
export * from './modules/assetValue.ts';
|
|
14
|
+
export * from './modules/bigIntArithmetics.ts';
|
|
15
|
+
export * from './modules/swapKitError.ts';
|
|
16
|
+
export * from './modules/swapKitNumber.ts';
|
|
@@ -13,6 +13,10 @@ describe('AssetValue', () => {
|
|
|
13
13
|
symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
14
14
|
});
|
|
15
15
|
expect(fakeAvaxUSDCAsset.assetValue).toBe('1234567890 USDC');
|
|
16
|
+
expect(fakeAvaxUSDCAsset.toString()).toBe(
|
|
17
|
+
'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
18
|
+
);
|
|
19
|
+
expect(fakeAvaxUSDCAsset.toString(true)).toBe('AVAX.USDC');
|
|
16
20
|
|
|
17
21
|
const thor = AssetValue.fromChainOrSignature('ETH.THOR');
|
|
18
22
|
expect(thor.assetValue).toBe('0 THOR');
|
|
@@ -20,11 +24,24 @@ describe('AssetValue', () => {
|
|
|
20
24
|
const ethSynth = new AssetValue({
|
|
21
25
|
chain: Chain.THORChain,
|
|
22
26
|
symbol: 'ETH/ETH',
|
|
23
|
-
decimal:
|
|
27
|
+
decimal: 8,
|
|
24
28
|
value: 1234567890,
|
|
25
29
|
});
|
|
30
|
+
|
|
26
31
|
expect(ethSynth.assetValue).toBe('1234567890 ETH/ETH');
|
|
27
32
|
expect(ethSynth.toString()).toBe('THOR.ETH/ETH');
|
|
33
|
+
expect(ethSynth.toString(true)).toBe('ETH/ETH');
|
|
34
|
+
expect(ethSynth.mul(21.37).getValue('string')).toBe('26382715809.3');
|
|
35
|
+
|
|
36
|
+
const atomDerived = new AssetValue({
|
|
37
|
+
identifier: 'THOR.ATOM',
|
|
38
|
+
decimal: 6,
|
|
39
|
+
value: 123456789,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect(atomDerived.assetValue).toBe('123456789 ATOM');
|
|
43
|
+
expect(atomDerived.toString()).toBe('THOR.ATOM');
|
|
44
|
+
expect(atomDerived.toString(true)).toBe('THOR.ATOM');
|
|
28
45
|
});
|
|
29
46
|
});
|
|
30
47
|
|
|
@@ -62,15 +79,13 @@ describe('AssetValue', () => {
|
|
|
62
79
|
|
|
63
80
|
describe('toString', () => {
|
|
64
81
|
test('returns asset value string/identifier', async () => {
|
|
65
|
-
const
|
|
82
|
+
const avaxUSDCAsset = new AssetValue({
|
|
66
83
|
decimal: 6,
|
|
67
84
|
value: 1234567890,
|
|
68
85
|
chain: Chain.Avalanche,
|
|
69
86
|
symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
70
87
|
});
|
|
71
|
-
expect(
|
|
72
|
-
'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
73
|
-
);
|
|
88
|
+
expect(avaxUSDCAsset.toString()).toBe('AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e');
|
|
74
89
|
|
|
75
90
|
const thor = AssetValue.fromChainOrSignature('ETH.THOR');
|
|
76
91
|
expect(thor.toString()).toBe('ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044');
|
|
@@ -82,10 +97,11 @@ describe('AssetValue', () => {
|
|
|
82
97
|
|
|
83
98
|
describe('fromIdentifier', () => {
|
|
84
99
|
test('creates AssetValue from string', async () => {
|
|
85
|
-
const
|
|
86
|
-
|
|
100
|
+
const avaxUSDCAsset = await AssetValue.fromIdentifier(
|
|
101
|
+
'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
102
|
+
);
|
|
87
103
|
|
|
88
|
-
expect(
|
|
104
|
+
expect(avaxUSDCAsset).toEqual(
|
|
89
105
|
expect.objectContaining({
|
|
90
106
|
address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
|
|
91
107
|
chain: Chain.Avalanche,
|