@swapkit/helpers 1.1.3 → 1.2.1

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.
@@ -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
+ });
@@ -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
  /**
@@ -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
  };
@@ -57,6 +57,7 @@ const errorCodes = {
57
57
  core_transaction_invalid_sender_address: 10313,
58
58
  core_transaction_deposit_server_error: 10314,
59
59
  core_transaction_user_rejected: 10315,
60
+ core_transaction_failed: 10316,
60
61
  /**
61
62
  * Wallets
62
63
  */
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