@swapkit/helpers 1.0.0-rc.8 → 1.0.0-rc.9

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.5",
10
- "@swapkit/types": "1.0.0-rc.5"
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.5",
17
- "@swapkit/types": "1.0.0-rc.5"
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.8",
45
+ "version": "1.0.0-rc.9",
43
46
  "scripts": {
44
47
  "build": "vite build",
45
48
  "clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
@@ -1,9 +1,7 @@
1
1
  import type { EVMChain } from '@swapkit/types';
2
2
  import { BaseDecimal, Chain, ChainToRPC, FeeOption } from '@swapkit/types';
3
3
 
4
- import type { AssetValue } from '../index.ts';
5
-
6
- import { postRequest } from './others.ts';
4
+ import { type AssetValue, RequestClient } from '../index.ts';
7
5
 
8
6
  const getDecimalMethodHex = '0x313ce567';
9
7
 
@@ -11,19 +9,15 @@ export type CommonAssetString = 'MAYA.MAYA' | 'ETH.THOR' | 'ETH.vTHOR' | Chain;
11
9
 
12
10
  const getContractDecimals = async ({ chain, to }: { chain: EVMChain; to: string }) => {
13
11
  try {
14
- const response = await postRequest<string>(
15
- ChainToRPC[chain],
16
- JSON.stringify({
17
- method: 'eth_call',
18
- 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({
19
15
  id: 44,
20
16
  jsonrpc: '2.0',
17
+ method: 'eth_call',
18
+ params: [{ to: to.toLowerCase(), data: getDecimalMethodHex }, 'latest'],
21
19
  }),
22
- { accept: '*/*', 'cache-control': 'no-cache', 'content-type': 'application/json' },
23
- true,
24
- );
25
-
26
- const { result } = JSON.parse(response) as { result: string };
20
+ });
27
21
 
28
22
  return parseInt(BigInt(result).toString());
29
23
  } catch (error) {
@@ -18,51 +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
- try {
27
- const queryParams = Object.entries(params || {}).reduce(
28
- (acc, [key, value]) => {
29
- if (value) {
30
- acc[key] = value;
31
- }
32
-
33
- return acc;
34
- },
35
- {} as { [key in string]: any },
36
- );
37
-
38
- const response = await fetch(
39
- `${url}${params ? `?${new URLSearchParams(queryParams).toString()}` : ''}`,
40
- { method: 'GET', mode: 'cors', credentials: 'omit', referrer: 'https://sk.thorswap.net' },
41
- );
42
-
43
- return response.json();
44
- } catch (error) {
45
- console.error(error);
46
- return {} as T;
47
- }
48
- };
49
-
50
- export const postRequest = async <T>(
51
- url: string,
52
- body: string,
53
- headers?: Record<string, string>,
54
- parseAsString = false,
55
- ): Promise<T> => {
56
- try {
57
- const response = await fetch(`${url}`, {
58
- body,
59
- headers,
60
- method: 'POST',
61
- referrer: 'https://sk.thorswap.net',
62
- });
63
-
64
- return parseAsString ? response.text() : response.json();
65
- } catch (error) {
66
- return {} as T;
67
- }
68
- };
@@ -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
@@ -5,6 +5,7 @@ export * from './helpers/asset.ts';
5
5
  export * from './helpers/liquidity.ts';
6
6
  export * from './helpers/memo.ts';
7
7
  export * from './helpers/others.ts';
8
+ export * from './helpers/request.ts';
8
9
 
9
10
  /**
10
11
  * Modules
@@ -79,15 +79,13 @@ describe('AssetValue', () => {
79
79
 
80
80
  describe('toString', () => {
81
81
  test('returns asset value string/identifier', async () => {
82
- const fakeAvaxUSDCAsset = new AssetValue({
82
+ const avaxUSDCAsset = new AssetValue({
83
83
  decimal: 6,
84
84
  value: 1234567890,
85
85
  chain: Chain.Avalanche,
86
86
  symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
87
87
  });
88
- expect(fakeAvaxUSDCAsset.toString()).toBe(
89
- 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
90
- );
88
+ expect(avaxUSDCAsset.toString()).toBe('AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e');
91
89
 
92
90
  const thor = AssetValue.fromChainOrSignature('ETH.THOR');
93
91
  expect(thor.toString()).toBe('ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044');
@@ -99,10 +97,11 @@ describe('AssetValue', () => {
99
97
 
100
98
  describe('fromIdentifier', () => {
101
99
  test('creates AssetValue from string', async () => {
102
- const fakeAvaxUSDCAssetString = 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e';
103
- const fakeAvaxUSDCAsset = await AssetValue.fromIdentifier(fakeAvaxUSDCAssetString);
100
+ const avaxUSDCAsset = await AssetValue.fromIdentifier(
101
+ 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
102
+ );
104
103
 
105
- expect(fakeAvaxUSDCAsset).toEqual(
104
+ expect(avaxUSDCAsset).toEqual(
106
105
  expect.objectContaining({
107
106
  address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e',
108
107
  chain: Chain.Avalanche,