@swapkit/helpers 1.1.2 → 1.2.0

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
@@ -3,17 +3,16 @@
3
3
  "description": "SwapKit - Helpers",
4
4
  "dependencies": {
5
5
  "@swapkit/tokens": "1.0.3",
6
- "ky": "1.4.0",
7
6
  "picocolors": "1.0.1",
8
7
  "zod": "3.23.8"
9
8
  },
10
9
  "devDependencies": {
11
- "@swapkit/toolbox-cosmos": "1.0.7",
12
- "@swapkit/toolbox-evm": "1.1.2",
13
- "@swapkit/toolbox-solana": "1.0.7",
14
- "@swapkit/toolbox-radix": "1.0.7",
15
- "@swapkit/toolbox-substrate": "1.1.2",
16
- "@swapkit/toolbox-utxo": "1.0.7"
10
+ "@swapkit/toolbox-cosmos": "1.0.9",
11
+ "@swapkit/toolbox-evm": "1.1.4",
12
+ "@swapkit/toolbox-solana": "1.0.9",
13
+ "@swapkit/toolbox-radix": "1.0.9",
14
+ "@swapkit/toolbox-substrate": "1.1.4",
15
+ "@swapkit/toolbox-utxo": "1.0.9"
17
16
  },
18
17
  "files": [
19
18
  "src/",
@@ -38,5 +37,5 @@
38
37
  },
39
38
  "type": "module",
40
39
  "types": "./src/index.ts",
41
- "version": "1.1.2"
40
+ "version": "1.2.0"
42
41
  }
@@ -6,6 +6,8 @@ import {
6
6
  getMemoForLeaveAndBond,
7
7
  getMemoForNamePreferredAssetRegister,
8
8
  getMemoForNameRegister,
9
+ getMemoForRunePoolDeposit,
10
+ getMemoForRunePoolWithdraw,
9
11
  getMemoForSaverDeposit,
10
12
  getMemoForSaverWithdraw,
11
13
  getMemoForWithdraw,
@@ -73,6 +75,16 @@ describe("getMemoForDeposit", () => {
73
75
  const result = getMemoForDeposit({ chain: Chain.Ethereum, symbol: "ETH" });
74
76
  expect(result).toBe("+:ETH.ETH");
75
77
  });
78
+
79
+ test("returns correct memo when paired address is not available but affiliate info is present", () => {
80
+ const result = getMemoForDeposit({
81
+ chain: Chain.Ethereum,
82
+ symbol: "ETH",
83
+ affiliateAddress: "thor1abc123",
84
+ affiliateBasisPoints: 500,
85
+ });
86
+ expect(result).toBe("+:ETH.ETH::thor1abc123:500");
87
+ });
76
88
  });
77
89
 
78
90
  describe("getMemoForWithdraw", () => {
@@ -86,3 +98,26 @@ describe("getMemoForWithdraw", () => {
86
98
  expect(result).toBe("-:ETH.ETH:100");
87
99
  });
88
100
  });
101
+
102
+ describe("getMemoForRunePoolDeposit", () => {
103
+ test("returns correct memo for single side", () => {
104
+ const result = getMemoForRunePoolDeposit();
105
+ expect(result).toBe("POOL+");
106
+ });
107
+ });
108
+
109
+ describe("getMemoForRunePoolWithdraw", () => {
110
+ test("returns correct memo for single side", () => {
111
+ const result = getMemoForRunePoolWithdraw({ basisPoints: 500 });
112
+ expect(result).toBe("POOL-:500");
113
+ });
114
+
115
+ test("returns correct memo when affiliate info is present", () => {
116
+ const result = getMemoForRunePoolWithdraw({
117
+ basisPoints: 500,
118
+ affiliateAddress: "thor1abc123",
119
+ affiliateBasisPoints: 500,
120
+ });
121
+ expect(result).toBe("POOL-:500:thor1abc123:500");
122
+ });
123
+ });
@@ -171,7 +171,7 @@ export const getCommonAssetInfo = (
171
171
  case Chain.BinanceSmartChain:
172
172
  return { identifier: `${assetString}.BNB`, decimal: BaseDecimal[assetString] };
173
173
  case Chain.Maya:
174
- return { identifier: `${assetString}.CACAO`, decimal: BaseDecimal[assetString] };
174
+ return { identifier: `${assetString}.CACAO`, decimal: 10 };
175
175
  case Chain.Radix:
176
176
  return { identifier: `${Chain.Radix}.XRD`, decimal: BaseDecimal[assetString] };
177
177
 
@@ -128,7 +128,8 @@ export function getMemoForDeposit({
128
128
  address?: string;
129
129
  }>) {
130
130
  const poolIdentifier = getPoolIdentifier({ chain, symbol });
131
- const addressPart = address ? `:${address}` : "";
131
+ const hasAffiliateInfo = !!affiliate.affiliateAddress;
132
+ const addressPart = address ? `:${address}` : hasAffiliateInfo ? ":" : "";
132
133
 
133
134
  return addAffiliate(`${MemoType.DEPOSIT}:${poolIdentifier}${addressPart}`, affiliate);
134
135
  }
@@ -137,9 +138,8 @@ export function getMemoForSaverWithdraw({
137
138
  chain,
138
139
  symbol,
139
140
  basisPoints,
140
- ...affiliate
141
- }: WithAffiliate<{ chain: Chain; symbol: string; basisPoints: number }>) {
142
- return addAffiliate(`${MemoType.WITHDRAW}:${chain}/${symbol}:${basisPoints}`, affiliate);
141
+ }: { chain: Chain; symbol: string; basisPoints: number }) {
142
+ return `${MemoType.WITHDRAW}:${chain}/${symbol}:${basisPoints}`;
143
143
  }
144
144
 
145
145
  export function getMemoForWithdraw({
@@ -148,22 +148,29 @@ export function getMemoForWithdraw({
148
148
  ticker,
149
149
  basisPoints,
150
150
  targetAsset,
151
- ...affiliate
152
- }: WithAffiliate<{
151
+ }: {
153
152
  chain: Chain;
154
153
  symbol: string;
155
154
  ticker: string;
156
155
  basisPoints: number;
157
156
  targetAsset?: string;
158
- }>) {
157
+ }) {
159
158
  const shortenedSymbol =
160
159
  chain === "ETH" && ticker !== "ETH" ? `${ticker}-${symbol.slice(-3)}` : symbol;
161
160
  const targetPart = targetAsset ? `:${targetAsset}` : "";
162
161
 
163
- return addAffiliate(
164
- `${MemoType.WITHDRAW}:${chain}.${shortenedSymbol}:${basisPoints}${targetPart}`,
165
- affiliate,
166
- );
162
+ return `${MemoType.WITHDRAW}:${chain}.${shortenedSymbol}:${basisPoints}${targetPart}`;
163
+ }
164
+
165
+ export function getMemoForRunePoolDeposit() {
166
+ return `${MemoType.RUNEPOOL_DEPOSIT}`;
167
+ }
168
+
169
+ export function getMemoForRunePoolWithdraw({
170
+ basisPoints,
171
+ ...affiliate
172
+ }: WithAffiliate<{ basisPoints: number }>) {
173
+ return addAffiliate(`${MemoType.RUNEPOOL_WITHDRAW}:${basisPoints}`, affiliate);
167
174
  }
168
175
 
169
176
  /**
@@ -188,6 +195,12 @@ export type MemoOptions<T extends MemoType> = {
188
195
  singleSide?: boolean;
189
196
  }>;
190
197
  [MemoType.NAME_REGISTER]: { name: string; chain: string; address: string };
198
+ [MemoType.RUNEPOOL_DEPOSIT]: {};
199
+ [MemoType.RUNEPOOL_WITHDRAW]: {
200
+ basisPoints: number;
201
+ affiliateAddress?: string;
202
+ affiliateBasisPoints?: number;
203
+ };
191
204
  }[T];
192
205
 
193
206
  /**
@@ -567,7 +567,7 @@ describe("AssetValue", () => {
567
567
  expect.objectContaining({
568
568
  address: undefined,
569
569
  chain: Chain.Maya,
570
- decimal: BaseDecimal.MAYA,
570
+ decimal: 10,
571
571
  isGasAsset: true,
572
572
  isSynthetic: false,
573
573
  symbol: "CACAO",
@@ -151,7 +151,7 @@ or by passing asyncTokenLookup: true to the from() function, which will make it
151
151
  const assetValue = asyncTokenLookup
152
152
  ? createAssetValue(identifier, fromBaseDecimal ? adjustedValue : parsedValue)
153
153
  : isSynthetic
154
- ? createSyntheticAssetValue(identifier, parsedValue)
154
+ ? createSyntheticAssetValue(identifier, adjustedValue)
155
155
  : new AssetValue({ tax, decimal, identifier, value: adjustedValue });
156
156
 
157
157
  return assetValue as ConditionalAssetValueReturn<T>;
@@ -1,39 +1,56 @@
1
- import type { KyInstance, Options } from "ky";
2
- import ky from "ky";
1
+ type Options = {
2
+ headers?: Record<string, string>;
3
+ apiKey?: string;
4
+ method?: "GET" | "POST";
5
+ onError?: (error: NotWorth) => NotWorth;
6
+ responseHandler?: (response: NotWorth) => NotWorth;
7
+ [key: string]: NotWorth;
8
+ };
3
9
 
4
- let kyClientConfig: Options & { apiKey?: string } = {};
10
+ let clientConfig: Options = {};
5
11
 
6
12
  export const defaultRequestHeaders =
7
13
  typeof window !== "undefined"
8
14
  ? ({} as Record<string, string>)
9
15
  : { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" };
10
16
 
11
- export function setRequestClientConfig({ apiKey, ...config }: Options & { apiKey?: string }) {
12
- kyClientConfig = { ...config, apiKey };
17
+ export function setRequestClientConfig({ apiKey, ...config }: Options) {
18
+ clientConfig = { ...config, apiKey };
13
19
  }
14
20
 
15
- function getKyClient() {
16
- const { apiKey, ...config } = kyClientConfig;
17
- return ky.create({
18
- ...config,
19
- headers: { ...defaultRequestHeaders, ...config.headers, "x-api-key": apiKey },
20
- });
21
- }
21
+ async function fetchWithConfig(url: string, options: Options = {}) {
22
+ const { apiKey, ...config } = clientConfig;
23
+ const headers = { ...defaultRequestHeaders, ...config.headers, ...options.headers };
24
+
25
+ if (apiKey) headers["x-api-key"] = apiKey;
26
+
27
+ try {
28
+ const response = await fetch(url, { ...config, ...options, headers });
29
+ const body = await response.json();
22
30
 
23
- const getTypedBaseRequestClient = (ky: KyInstance) => ({
24
- get: async <T>(url: string | URL | Request, options?: Options) =>
25
- (await ky.get(url, options)).json<T>(),
26
- post: async <T>(url: string | URL | Request, options?: Options) =>
27
- (await ky.post(url, options)).json<T>(),
28
- });
31
+ if (options.responseHandler) return options.responseHandler(body);
32
+
33
+ return body;
34
+ } catch (error) {
35
+ if (options.onError) return options.onError(error);
36
+
37
+ console.error(error);
38
+ }
39
+ }
29
40
 
30
41
  export const RequestClient = {
31
- ...getTypedBaseRequestClient(getKyClient()),
42
+ get: async <T>(url: string, options?: Options): Promise<T> =>
43
+ fetchWithConfig(url, { ...options, method: "GET" }),
44
+ post: async <T>(url: string, options?: Options): Promise<T> =>
45
+ fetchWithConfig(url, { ...options, method: "POST" }),
32
46
  extend: (options: Options) => {
33
- const extendedClient = getKyClient().extend(options);
47
+ const extendedConfig = { ...clientConfig, ...options };
34
48
  return {
35
- ...getTypedBaseRequestClient(extendedClient),
36
- extend: RequestClient.extend,
49
+ get: async <T>(url: string, options?: Options): Promise<T> =>
50
+ fetchWithConfig(url, { ...extendedConfig, ...options, method: "GET" }),
51
+ post: async <T>(url: string, options?: Options): Promise<T> =>
52
+ fetchWithConfig(url, { ...extendedConfig, ...options, method: "POST" }),
53
+ extend: (newOptions: Options) => RequestClient.extend({ ...extendedConfig, ...newOptions }),
37
54
  };
38
55
  },
39
56
  };
@@ -103,7 +103,7 @@ export enum BaseDecimal {
103
103
  KUJI = 6,
104
104
  LTC = 8,
105
105
  MATIC = 18,
106
- MAYA = 10,
106
+ MAYA = 8,
107
107
  OP = 18,
108
108
  SOL = 9,
109
109
  THOR = 8,
package/src/types/sdk.ts CHANGED
@@ -62,6 +62,8 @@ export enum MemoType {
62
62
  WITHDRAW = "-",
63
63
  OPEN_LOAN = "$+",
64
64
  CLOSE_LOAN = "$-",
65
+ RUNEPOOL_DEPOSIT = "POOL+",
66
+ RUNEPOOL_WITHDRAW = "POOL-",
65
67
  }
66
68
 
67
69
  export const QuoteRequestSchema = z