@swapkit/helpers 1.0.0-rc.1 → 1.0.0-rc.11

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
11
  "@internal/config": "0.0.0-internal.0",
9
- "@swapkit/tokens": "1.0.0-rc.0",
10
- "@swapkit/types": "1.0.0-rc.0"
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/tokens": "1.0.0-rc.0",
17
- "@swapkit/types": "1.0.0-rc.0"
19
+ "@swapkit/tokens": "1.0.0-rc.6",
20
+ "@swapkit/types": "1.0.0-rc.6"
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.1",
45
+ "version": "1.0.0-rc.11",
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,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 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: { 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
- { accept: '*/*', 'cache-control': 'no-cache', 'content-type': 'application/json' },
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' : 'GAIA';
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' : 'AVAX';
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
+ );
@@ -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 { 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';
@@ -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');
@@ -26,6 +30,8 @@ describe('AssetValue', () => {
26
30
 
27
31
  expect(ethSynth.assetValue).toBe('1234567890 ETH/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,