@swapkit/helpers 4.5.8 → 4.12.3

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.
Files changed (111) hide show
  1. package/dist/LICENSE +246 -0
  2. package/dist/api/index.cjs +0 -4
  3. package/dist/api/index.js +1 -4
  4. package/dist/chunk-1m30h0t9.js +1 -0
  5. package/dist/chunk-jsgxsr8a.js +4 -0
  6. package/dist/chunk-z5an6869.js +5 -0
  7. package/dist/contracts.cjs +0 -3
  8. package/dist/contracts.js +0 -3
  9. package/dist/index.cjs +1 -7
  10. package/dist/index.js +1 -7
  11. package/dist/tokens.cjs +0 -3
  12. package/dist/tokens.js +0 -3
  13. package/dist/types/api/index.d.ts +126 -153
  14. package/dist/types/api/index.d.ts.map +1 -1
  15. package/dist/types/api/midgard/endpoints.d.ts +2 -1
  16. package/dist/types/api/midgard/endpoints.d.ts.map +1 -1
  17. package/dist/types/api/swapkitApi/endpoints.d.ts +134 -159
  18. package/dist/types/api/swapkitApi/endpoints.d.ts.map +1 -1
  19. package/dist/types/api/swapkitApi/types.d.ts +1038 -23
  20. package/dist/types/api/swapkitApi/types.d.ts.map +1 -1
  21. package/dist/types/api/thornode/endpoints.d.ts +1 -1
  22. package/dist/types/api/thornode/endpoints.d.ts.map +1 -1
  23. package/dist/types/index.d.ts +5 -0
  24. package/dist/types/index.d.ts.map +1 -1
  25. package/dist/types/modules/assetValue.d.ts +6 -7
  26. package/dist/types/modules/assetValue.d.ts.map +1 -1
  27. package/dist/types/modules/bigIntArithmetics.d.ts +2 -1
  28. package/dist/types/modules/bigIntArithmetics.d.ts.map +1 -1
  29. package/dist/types/modules/requestClient.d.ts +1 -1
  30. package/dist/types/modules/requestClient.d.ts.map +1 -1
  31. package/dist/types/modules/swapKitConfig.d.ts +24 -78
  32. package/dist/types/modules/swapKitConfig.d.ts.map +1 -1
  33. package/dist/types/modules/swapKitError.d.ts +146 -14
  34. package/dist/types/modules/swapKitError.d.ts.map +1 -1
  35. package/dist/types/modules/widgetAuth.d.ts +6 -0
  36. package/dist/types/modules/widgetAuth.d.ts.map +1 -0
  37. package/dist/types/types/derivationPath.d.ts +1 -1
  38. package/dist/types/types/derivationPath.d.ts.map +1 -1
  39. package/dist/types/types/quotes.d.ts +2 -6
  40. package/dist/types/types/quotes.d.ts.map +1 -1
  41. package/dist/types/types/sdk.d.ts +6 -0
  42. package/dist/types/types/sdk.d.ts.map +1 -1
  43. package/dist/types/types/wallet.d.ts +12 -3
  44. package/dist/types/types/wallet.d.ts.map +1 -1
  45. package/dist/types/utils/asset.d.ts +17 -1
  46. package/dist/types/utils/asset.d.ts.map +1 -1
  47. package/dist/types/utils/chains.d.ts +5 -0
  48. package/dist/types/utils/chains.d.ts.map +1 -1
  49. package/dist/types/utils/derivationPath.d.ts +4 -2
  50. package/dist/types/utils/derivationPath.d.ts.map +1 -1
  51. package/dist/types/utils/wallets.d.ts +18 -2
  52. package/dist/types/utils/wallets.d.ts.map +1 -1
  53. package/package.json +12 -30
  54. package/dist/api/index.cjs.map +0 -14
  55. package/dist/api/index.js.map +0 -14
  56. package/dist/chunk-pfmeq01a.js +0 -5
  57. package/dist/chunk-pfmeq01a.js.map +0 -9
  58. package/dist/chunk-vb4wtm2w.js +0 -4
  59. package/dist/chunk-vb4wtm2w.js.map +0 -9
  60. package/dist/contracts.cjs.map +0 -10
  61. package/dist/contracts.js.map +0 -10
  62. package/dist/index.cjs.map +0 -30
  63. package/dist/index.js.map +0 -30
  64. package/dist/tokens.cjs.map +0 -10
  65. package/dist/tokens.js.map +0 -10
  66. package/src/api/index.ts +0 -9
  67. package/src/api/midgard/endpoints.ts +0 -348
  68. package/src/api/midgard/types.ts +0 -515
  69. package/src/api/swapkitApi/endpoints.ts +0 -247
  70. package/src/api/swapkitApi/types.ts +0 -624
  71. package/src/api/thornode/endpoints.ts +0 -105
  72. package/src/api/thornode/types.ts +0 -247
  73. package/src/contracts.ts +0 -1
  74. package/src/index.ts +0 -28
  75. package/src/modules/__tests__/assetValue.test.ts +0 -1892
  76. package/src/modules/__tests__/bigIntArithmetics.test.ts +0 -408
  77. package/src/modules/__tests__/feeMultiplier.test.ts +0 -125
  78. package/src/modules/__tests__/swapKitConfig.test.ts +0 -425
  79. package/src/modules/__tests__/swapKitNumber.test.ts +0 -435
  80. package/src/modules/assetValue.ts +0 -532
  81. package/src/modules/bigIntArithmetics.ts +0 -368
  82. package/src/modules/feeMultiplier.ts +0 -80
  83. package/src/modules/requestClient.ts +0 -110
  84. package/src/modules/swapKitConfig.ts +0 -174
  85. package/src/modules/swapKitError.ts +0 -470
  86. package/src/modules/swapKitNumber.ts +0 -13
  87. package/src/tokens.ts +0 -1
  88. package/src/types/commonTypes.ts +0 -10
  89. package/src/types/derivationPath.ts +0 -11
  90. package/src/types/errors/apiV1.ts +0 -0
  91. package/src/types/index.ts +0 -5
  92. package/src/types/quotes.ts +0 -174
  93. package/src/types/sdk.ts +0 -38
  94. package/src/types/wallet.ts +0 -124
  95. package/src/utils/__tests__/asset.test.ts +0 -186
  96. package/src/utils/__tests__/derivationPath.test.ts +0 -142
  97. package/src/utils/__tests__/explorerUrls.test.ts +0 -59
  98. package/src/utils/__tests__/liquidity.test.ts +0 -302
  99. package/src/utils/__tests__/memo.test.ts +0 -99
  100. package/src/utils/__tests__/others.test.ts +0 -165
  101. package/src/utils/__tests__/validators.test.ts +0 -84
  102. package/src/utils/__tests__/wallets.test.ts +0 -621
  103. package/src/utils/asset.ts +0 -399
  104. package/src/utils/chains.ts +0 -100
  105. package/src/utils/derivationPath.ts +0 -101
  106. package/src/utils/explorerUrls.ts +0 -32
  107. package/src/utils/liquidity.ts +0 -150
  108. package/src/utils/memo.ts +0 -102
  109. package/src/utils/others.ts +0 -62
  110. package/src/utils/validators.ts +0 -32
  111. package/src/utils/wallets.ts +0 -237
@@ -1,247 +0,0 @@
1
- import {
2
- type Chain,
3
- type EVMChain,
4
- EVMChains,
5
- isGasAsset,
6
- type ProviderName,
7
- RequestClient,
8
- SKConfig,
9
- SwapKitError,
10
- } from "@swapkit/helpers";
11
- import { match, P } from "ts-pattern";
12
- import {
13
- type BalanceResponse,
14
- type BrokerDepositChannelParams,
15
- type DepositChannelResponse,
16
- DepositChannelResponseSchema,
17
- type GasResponse,
18
- GasResponseSchema,
19
- type NearDepositChannelParams,
20
- type NearSwapResponse,
21
- NearSwapResponseSchema,
22
- type PriceRequest,
23
- type PriceResponse,
24
- PriceResponseSchema,
25
- type QuoteRequest,
26
- type QuoteResponse,
27
- type QuoteResponseRoute,
28
- QuoteResponseRouteItem,
29
- QuoteResponseSchema,
30
- type TokenListProvidersResponse,
31
- type TokensResponseV2,
32
- type TrackerResponse,
33
- TrackerResponseSchema,
34
- type TrackingRequest,
35
- } from "./types";
36
-
37
- const SKRequestClient = RequestClient.extend({
38
- dynamicHeader: () => {
39
- const { swapKit } = SKConfig.get("apiKeys");
40
- return swapKit ? { "x-api-key": swapKit } : {};
41
- },
42
- });
43
-
44
- export async function getTrackerDetails(json: TrackingRequest) {
45
- const response = await SKRequestClient.post<TrackerResponse>(getApiUrl("/track"), { json });
46
-
47
- try {
48
- const parsedResponse = TrackerResponseSchema.safeParse(response);
49
-
50
- if (!parsedResponse.success) {
51
- throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
52
- }
53
-
54
- return parsedResponse.data;
55
- } catch (_error) {
56
- // throw new SwapKitError("api_v2_invalid_response", error);
57
- return response;
58
- }
59
- }
60
-
61
- export async function getSwapQuote(json: QuoteRequest) {
62
- const { getQuote } = SKConfig.get("endpoints");
63
-
64
- if (getQuote) return getQuote(json);
65
-
66
- const response = await SKRequestClient.post<QuoteResponse>(getApiUrl("/quote"), { json });
67
-
68
- if (response.error) {
69
- throw new SwapKitError("api_v2_server_error", { message: response.error });
70
- }
71
-
72
- try {
73
- const parsedResponse = QuoteResponseSchema.safeParse(response);
74
-
75
- if (!parsedResponse.success) {
76
- throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
77
- }
78
-
79
- return parsedResponse.data;
80
- } catch {
81
- // throw new SwapKitError("api_v2_invalid_response", error);
82
- return response;
83
- }
84
- }
85
-
86
- export async function getRouteWithTx(json: { routeId: string; sourceAddress: string; destinationAddress: string }) {
87
- const { getRouteWithTx } = SKConfig.get("endpoints");
88
-
89
- if (getRouteWithTx) return getRouteWithTx(json);
90
-
91
- const response = await SKRequestClient.post<QuoteResponseRoute>(getApiUrl("/swap"), { json });
92
-
93
- try {
94
- const parsedResponse = QuoteResponseRouteItem.safeParse(response);
95
-
96
- if (!parsedResponse.success) {
97
- throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
98
- }
99
-
100
- return parsedResponse.data;
101
- } catch (error) {
102
- console.error(new SwapKitError("api_v2_invalid_response", error));
103
- return response;
104
- }
105
- }
106
-
107
- export async function getChainBalance<T extends Chain>({
108
- chain,
109
- address,
110
- scamFilter = true,
111
- }: {
112
- chain: T;
113
- address: string;
114
- scamFilter?: boolean;
115
- }) {
116
- const { getBalance } = SKConfig.get("endpoints");
117
- if (getBalance) return getBalance({ address, chain });
118
-
119
- const url = getApiUrl(`/balance?chain=${chain}&address=${address}`);
120
- const balanceResponse = await SKRequestClient.get<BalanceResponse>(url);
121
- const balances = Array.isArray(balanceResponse) ? balanceResponse : [];
122
- return scamFilter ? filterAssets(balances) : balances;
123
- }
124
-
125
- export function getTokenListProviders() {
126
- const url = getApiUrl("/providers");
127
- return SKRequestClient.get<TokenListProvidersResponse>(url);
128
- }
129
-
130
- export function getTokenList(provider: ProviderName) {
131
- const url = getApiUrl(`/tokens?provider=${provider}`);
132
- return SKRequestClient.get<TokensResponseV2>(url);
133
- }
134
-
135
- export async function getPrice(body: PriceRequest) {
136
- const url = getApiUrl("/price");
137
- const response = await SKRequestClient.post<PriceResponse>(url, { json: body });
138
-
139
- try {
140
- const parsedResponse = PriceResponseSchema.safeParse(response);
141
-
142
- if (!parsedResponse.success) {
143
- throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
144
- }
145
-
146
- return parsedResponse.data;
147
- } catch (error) {
148
- throw new SwapKitError("api_v2_invalid_response", error);
149
- }
150
- }
151
-
152
- export async function getGasRate() {
153
- const url = getApiUrl("/gas");
154
- const response = await SKRequestClient.get<GasResponse>(url);
155
-
156
- try {
157
- const parsedResponse = GasResponseSchema.safeParse(response);
158
-
159
- if (!parsedResponse.success) {
160
- throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
161
- }
162
-
163
- const gasRates = Array.isArray(parsedResponse) ? parsedResponse : [parsedResponse];
164
- return gasRates;
165
- } catch (error) {
166
- throw new SwapKitError("api_v2_invalid_response", error);
167
- }
168
- }
169
-
170
- export async function getChainflipDepositChannel(body: BrokerDepositChannelParams) {
171
- const { destinationAddress } = body;
172
-
173
- if (!destinationAddress) {
174
- throw new SwapKitError("chainflip_broker_invalid_params");
175
- }
176
- const url = SKConfig.get("integrations").chainflip?.brokerUrl || getApiUrl("/chainflip/broker/channel");
177
-
178
- const response = await SKRequestClient.post<DepositChannelResponse>(url, { json: body });
179
-
180
- try {
181
- const parsedResponse = DepositChannelResponseSchema.safeParse(response);
182
-
183
- if (!parsedResponse.success) {
184
- throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
185
- }
186
-
187
- return parsedResponse.data;
188
- } catch (error) {
189
- throw new SwapKitError("api_v2_invalid_response", error);
190
- }
191
- }
192
-
193
- export async function getNearDepositChannel(body: NearDepositChannelParams) {
194
- const { destinationAddress } = body;
195
-
196
- if (!destinationAddress) {
197
- throw new SwapKitError("chainflip_broker_invalid_params");
198
- }
199
- const url = getApiUrl("/near/channel");
200
-
201
- const response = await SKRequestClient.post<NearSwapResponse>(url, { json: body });
202
-
203
- try {
204
- const parsedResponse = NearSwapResponseSchema.safeParse(response);
205
-
206
- if (!parsedResponse.success) {
207
- throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
208
- }
209
-
210
- return parsedResponse.data;
211
- } catch (error) {
212
- throw new SwapKitError("api_v2_invalid_response", error);
213
- }
214
- }
215
-
216
- function getApiUrl(path?: `/${string}`) {
217
- const { isDev, apiUrl, devApiUrl, experimental_apiUrlQuote, experimental_apiUrlSwap } = SKConfig.get("envs");
218
-
219
- const defaultUrl = `${isDev ? devApiUrl : apiUrl}${path}`;
220
-
221
- return match({ experimental_apiUrlQuote, experimental_apiUrlSwap, path })
222
- .with(
223
- { experimental_apiUrlQuote: P.string.startsWith("http"), path: "/quote" },
224
- ({ experimental_apiUrlQuote, path }) => `${experimental_apiUrlQuote}${path}`,
225
- )
226
- .with(
227
- { experimental_apiUrlSwap: P.string.startsWith("http"), path: "/swap" },
228
- ({ experimental_apiUrlSwap, path }) => `${experimental_apiUrlSwap}${path}`,
229
- )
230
- .otherwise(() => defaultUrl);
231
- }
232
-
233
- function evmAssetHasAddress(assetString: string) {
234
- const [chain, symbol] = assetString.split(".") as [EVMChain, string];
235
- if (!EVMChains.includes(chain as EVMChain)) return true;
236
- const splitSymbol = symbol.split("-");
237
- const address = splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1];
238
-
239
- return isGasAsset({ chain: chain as Chain, symbol }) || !!address;
240
- }
241
-
242
- const potentialScamRegex = new RegExp(/(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/, "gmi");
243
- function filterAssets(tokens: BalanceResponse) {
244
- return tokens.filter((token) => {
245
- return !potentialScamRegex.test(token.identifier) && evmAssetHasAddress(token.identifier);
246
- });
247
- }