@swapkit/helpers 1.0.0-rc.2 → 1.0.0-rc.21

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/package.json CHANGED
@@ -1,20 +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
- "@internal/config": "0.0.0-internal.0",
9
- "@swapkit/tokens": "1.0.0-rc.1",
10
- "@swapkit/types": "1.0.0-rc.1"
11
+ "@internal/config": "0.0.0-rc.1",
12
+ "@swapkit/tokens": "1.0.0-rc.8",
13
+ "@swapkit/types": "1.0.0-rc.8"
11
14
  },
12
15
  "eslintConfig": {
13
16
  "extends": "../../../internal/eslint-config"
14
17
  },
15
18
  "peerDependencies": {
16
- "@swapkit/tokens": "1.0.0-rc.1",
17
- "@swapkit/types": "1.0.0-rc.1"
19
+ "@swapkit/tokens": "1.0.0-rc.8",
20
+ "@swapkit/types": "1.0.0-rc.8"
18
21
  },
19
22
  "exports": {
20
23
  ".": {
@@ -39,7 +42,7 @@
39
42
  "repository": "https://github.com/thorswap/SwapKit.git",
40
43
  "type": "module",
41
44
  "types": "./dist/index.d.ts",
42
- "version": "1.0.0-rc.2",
45
+ "version": "1.0.0-rc.21",
43
46
  "scripts": {
44
47
  "build": "vite build",
45
48
  "clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
@@ -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 { postRequest } from './others.ts';
4
+ import { type AssetValue, RequestClient } from '../index.ts';
5
5
 
6
6
  const getDecimalMethodHex = '0x313ce567';
7
7
 
@@ -9,19 +9,19 @@ 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 response = await postRequest<string>(
13
- ChainToRPC[chain],
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: {
14
+ accept: '*/*',
15
+ 'content-type': 'application/json',
16
+ 'cache-control': 'no-cache',
17
+ },
18
+ body: JSON.stringify({
17
19
  id: 44,
18
20
  jsonrpc: '2.0',
21
+ method: 'eth_call',
22
+ params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, 'latest'],
19
23
  }),
20
- { accept: '*/*', 'cache-control': 'no-cache', 'content-type': 'application/json' },
21
- true,
22
- );
23
-
24
- const { result } = JSON.parse(response) as { result: string };
24
+ });
25
25
 
26
26
  return parseInt(BigInt(result).toString());
27
27
  } catch (error) {
@@ -152,9 +152,9 @@ export const getAssetType = ({ chain, symbol }: { chain: Chain; symbol: string }
152
152
  return 'Native';
153
153
 
154
154
  case Chain.Cosmos:
155
- return symbol === 'ATOM' ? 'Native' : 'GAIA';
155
+ return symbol === 'ATOM' ? 'Native' : Chain.Cosmos;
156
156
  case Chain.Kujira:
157
- return symbol === 'KUJI' ? 'Native' : 'KUJI';
157
+ return symbol === Chain.Kujira ? 'Native' : Chain.Kujira;
158
158
  case Chain.Binance:
159
159
  return symbol === Chain.Binance ? 'Native' : 'BEP2';
160
160
  case Chain.BinanceSmartChain:
@@ -162,7 +162,7 @@ export const getAssetType = ({ chain, symbol }: { chain: Chain; symbol: string }
162
162
  case Chain.Ethereum:
163
163
  return symbol === Chain.Ethereum ? 'Native' : 'ERC20';
164
164
  case Chain.Avalanche:
165
- return symbol === Chain.Avalanche ? 'Native' : 'AVAX';
165
+ return symbol === Chain.Avalanche ? 'Native' : Chain.Avalanche;
166
166
  case Chain.Polygon:
167
167
  return symbol === Chain.Polygon ? 'Native' : 'POLYGON';
168
168
 
@@ -181,3 +181,13 @@ export const assetFromString = (assetString: string) => {
181
181
 
182
182
  return { chain, symbol, ticker, synth };
183
183
  };
184
+
185
+ const potentialScamRegex = new RegExp(
186
+ /(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/,
187
+ 'gmi',
188
+ );
189
+ export const filterAssets = (assets: AssetValue[]) =>
190
+ assets.filter(
191
+ (asset) =>
192
+ !potentialScamRegex.test(asset.toString()) && !asset.toString().includes('undefined'),
193
+ );
@@ -81,7 +81,7 @@ export const getAsymmetricAssetWithdrawAmount = ({
81
81
  getAsymmetricAssetShare({ assetDepth, liquidityUnits, poolUnits }).mul(percent);
82
82
 
83
83
  const toTCSwapKitNumber = (value: string) =>
84
- new SwapKitNumber({ value, decimal: BaseDecimal.THOR });
84
+ SwapKitNumber.fromBigInt(BigInt(value), BaseDecimal.THOR);
85
85
 
86
86
  export const getSymmetricPoolShare = ({
87
87
  liquidityUnits,
@@ -126,11 +126,11 @@ export const getEstimatedPoolShare = ({
126
126
  runeDepth: string;
127
127
  assetDepth: string;
128
128
  }>) => {
129
- const R = toTCSwapKitNumber(runeDepth);
130
- const A = toTCSwapKitNumber(assetDepth);
131
- const P = toTCSwapKitNumber(poolUnits);
132
- const runeAddAmount = toTCSwapKitNumber(runeAmount);
133
- const assetAddAmount = toTCSwapKitNumber(assetAmount);
129
+ const R = new SwapKitNumber({ value: runeDepth, decimal: 8 });
130
+ const A = new SwapKitNumber({ value: assetDepth, decimal: 8 });
131
+ const P = new SwapKitNumber({ value: poolUnits, decimal: 8 });
132
+ const runeAddAmount = new SwapKitNumber({ value: runeAmount, decimal: 8 });
133
+ const assetAddAmount = new SwapKitNumber({ value: assetAmount, decimal: 8 });
134
134
 
135
135
  // liquidityUnits = P * (r*A + a*R + 2*r*a) / (r*A + a*R + 2*R*A)
136
136
  const rA = runeAddAmount.mul(A);
@@ -142,14 +142,14 @@ export const getEstimatedPoolShare = ({
142
142
  const liquidityUnitsAfterAdd = numerator.div(denominator);
143
143
  const estimatedLiquidityUnits = toTCSwapKitNumber(liquidityUnits).add(liquidityUnitsAfterAdd);
144
144
 
145
- if (liquidityUnitsAfterAdd.baseValueNumber === 0) {
146
- return estimatedLiquidityUnits.div(P).baseValueNumber;
145
+ if (liquidityUnitsAfterAdd.getBaseValue('number') === 0) {
146
+ return estimatedLiquidityUnits.div(P).getBaseValue('number');
147
147
  }
148
148
 
149
149
  // get pool units after add
150
150
  const newPoolUnits = P.add(estimatedLiquidityUnits);
151
151
 
152
- return estimatedLiquidityUnits.div(newPoolUnits).baseValueNumber;
152
+ return estimatedLiquidityUnits.div(newPoolUnits).getBaseValue('number');
153
153
  };
154
154
 
155
155
  export const getLiquiditySlippage = ({
@@ -158,6 +158,8 @@ export const getLiquiditySlippage = ({
158
158
  runeDepth,
159
159
  assetDepth,
160
160
  }: PoolParams) => {
161
+ if (runeAmount === '0' || assetAmount === '0' || runeDepth === '0' || assetDepth === '0')
162
+ return 0;
161
163
  // formula: (t * R - T * r)/ (T*r + R*T)
162
164
  const R = toTCSwapKitNumber(runeDepth);
163
165
  const T = toTCSwapKitNumber(assetDepth);
@@ -168,5 +170,5 @@ export const getLiquiditySlippage = ({
168
170
  const denominator = T.mul(runeAddAmount).add(R.mul(T));
169
171
 
170
172
  // set absolute value of percent, no negative allowed
171
- return Math.abs(numerator.div(denominator).baseValueNumber);
173
+ return Math.abs(numerator.div(denominator).getBaseValue('number'));
172
174
  };
@@ -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,15 @@
1
+ import type { Options } from 'ky';
2
+ import ky from 'ky';
3
+
4
+ const headers =
5
+ typeof window !== 'undefined'
6
+ ? {}
7
+ : { referrer: 'https://sk.thorswap.net', referer: 'https://sk.thorswap.net' };
8
+
9
+ const kyClient = ky.create({ headers });
10
+
11
+ export const RequestClient = {
12
+ get: <T>(url: string | URL | Request, options?: Options) => kyClient.get(url, options).json<T>(),
13
+ post: <T>(url: string | URL | Request, options?: Options) =>
14
+ kyClient.post(url, options).json<T>(),
15
+ };
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 { AssetValue, getMinAmountByChain } from './modules/assetValue.ts';
14
- export { type Keys, SwapKitError } from './modules/swapKitError.ts';
15
- export { SwapKitNumber } from './modules/swapKitNumber.ts';
13
+ export * from './modules/assetValue.ts';
14
+ export * from './modules/bigIntArithmetics.ts';
15
+ export * from './modules/swapKitError.ts';
16
+ export * from './modules/swapKitNumber.ts';
@@ -1,7 +1,7 @@
1
1
  import { BaseDecimal, Chain } from '@swapkit/types';
2
2
  import { describe, expect, test } from 'vitest';
3
3
 
4
- import { AssetValue } from '../assetValue.ts';
4
+ import { AssetValue, getMinAmountByChain } from '../assetValue.ts';
5
5
 
6
6
  describe('AssetValue', () => {
7
7
  describe('assetValue', () => {
@@ -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');
@@ -24,8 +28,10 @@ describe('AssetValue', () => {
24
28
  value: 1234567890,
25
29
  });
26
30
 
27
- expect(ethSynth.assetValue).toBe('1234567890 ETH/ETH');
31
+ expect(ethSynth.assetValue).toBe('1234567890 ETH');
28
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');
29
35
 
30
36
  const atomDerived = new AssetValue({
31
37
  identifier: 'THOR.ATOM',
@@ -35,6 +41,7 @@ describe('AssetValue', () => {
35
41
 
36
42
  expect(atomDerived.assetValue).toBe('123456789 ATOM');
37
43
  expect(atomDerived.toString()).toBe('THOR.ATOM');
44
+ expect(atomDerived.toString(true)).toBe('THOR.ATOM');
38
45
  });
39
46
  });
40
47
 
@@ -70,17 +77,25 @@ describe('AssetValue', () => {
70
77
  });
71
78
  });
72
79
 
80
+ describe('from bigint', () => {
81
+ test('returns asset value with correct decimal', async () => {
82
+ const avaxUSDCAsset = await AssetValue.fromIdentifier(
83
+ `${Chain.Avalanche}.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e`,
84
+ 1234567800n,
85
+ );
86
+ expect(avaxUSDCAsset.getValue('string')).toBe('1234.5678');
87
+ });
88
+ });
89
+
73
90
  describe('toString', () => {
74
91
  test('returns asset value string/identifier', async () => {
75
- const fakeAvaxUSDCAsset = new AssetValue({
92
+ const avaxUSDCAsset = new AssetValue({
76
93
  decimal: 6,
77
94
  value: 1234567890,
78
95
  chain: Chain.Avalanche,
79
96
  symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
80
97
  });
81
- expect(fakeAvaxUSDCAsset.toString()).toBe(
82
- 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
83
- );
98
+ expect(avaxUSDCAsset.toString()).toBe('AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e');
84
99
 
85
100
  const thor = AssetValue.fromChainOrSignature('ETH.THOR');
86
101
  expect(thor.toString()).toBe('ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044');
@@ -92,10 +107,11 @@ describe('AssetValue', () => {
92
107
 
93
108
  describe('fromIdentifier', () => {
94
109
  test('creates AssetValue from string', async () => {
95
- const fakeAvaxUSDCAssetString = 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e';
96
- const fakeAvaxUSDCAsset = await AssetValue.fromIdentifier(fakeAvaxUSDCAssetString);
110
+ const avaxUSDCAsset = await AssetValue.fromIdentifier(
111
+ 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
112
+ );
97
113
 
98
- expect(fakeAvaxUSDCAsset).toEqual(
114
+ expect(avaxUSDCAsset).toEqual(
99
115
  expect.objectContaining({
100
116
  address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
101
117
  chain: Chain.Avalanche,
@@ -293,3 +309,22 @@ describe('AssetValue', () => {
293
309
  });
294
310
  });
295
311
  });
312
+
313
+ describe('getMinAmountByChain', () => {
314
+ test('returns min amount for chain', () => {
315
+ expect(getMinAmountByChain(Chain.THORChain).getValue('string')).toBe('0');
316
+ expect(getMinAmountByChain(Chain.Maya).getValue('string')).toBe('0');
317
+ expect(getMinAmountByChain(Chain.Cosmos).getValue('string')).toBe('0');
318
+
319
+ expect(getMinAmountByChain(Chain.Bitcoin).getValue('string')).toBe('0.00010001');
320
+ expect(getMinAmountByChain(Chain.Litecoin).getValue('string')).toBe('0.00010001');
321
+ expect(getMinAmountByChain(Chain.BitcoinCash).getValue('string')).toBe('0.00010001');
322
+ expect(getMinAmountByChain(Chain.Dogecoin).getValue('string')).toBe('1.00000001');
323
+
324
+ expect(getMinAmountByChain(Chain.BinanceSmartChain).getValue('string')).toBe('0.00000001');
325
+ expect(getMinAmountByChain(Chain.Ethereum).getValue('string')).toBe('0.00000001');
326
+ expect(getMinAmountByChain(Chain.Avalanche).getValue('string')).toBe('0.00000001');
327
+ expect(getMinAmountByChain(Chain.Arbitrum).getValue('string')).toBe('0.00000001');
328
+ expect(getMinAmountByChain(Chain.Optimism).getValue('string')).toBe('0.00000001');
329
+ });
330
+ });