@swapkit/helpers 2.4.3 → 3.0.0-beta.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.
Files changed (106) hide show
  1. package/dist/api/index.cjs +3 -0
  2. package/dist/api/index.cjs.map +16 -0
  3. package/dist/api/index.js +3 -0
  4. package/dist/api/index.js.map +16 -0
  5. package/dist/chunk-fazw0jvt.js +3 -0
  6. package/dist/chunk-fazw0jvt.js.map +9 -0
  7. package/dist/chunk-nm00d52v.js +4 -0
  8. package/dist/chunk-nm00d52v.js.map +9 -0
  9. package/dist/contracts/index.cjs +3 -0
  10. package/dist/contracts/index.cjs.map +38 -0
  11. package/dist/contracts/index.js +3 -0
  12. package/dist/contracts/index.js.map +38 -0
  13. package/dist/index.cjs +6 -0
  14. package/dist/index.cjs.map +29 -0
  15. package/dist/index.js +3 -3
  16. package/dist/index.js.map +19 -20
  17. package/dist/tokens/index.cjs +3 -0
  18. package/dist/tokens/index.cjs.map +23 -0
  19. package/dist/tokens/index.js +3 -0
  20. package/dist/tokens/index.js.map +23 -0
  21. package/package.json +31 -15
  22. package/src/api/index.ts +19 -0
  23. package/src/api/microgard/endpoints.ts +83 -0
  24. package/src/api/microgard/types.ts +60 -0
  25. package/src/api/midgard/endpoints.ts +166 -0
  26. package/src/api/midgard/types.ts +155 -0
  27. package/src/api/swapkitApi/endpoints.ts +266 -0
  28. package/src/api/swapkitApi/types.ts +772 -0
  29. package/src/api/thornode/endpoints.ts +78 -0
  30. package/src/api/thornode/types.ts +241 -0
  31. package/src/api/thorswapStatic/endpoints.ts +30 -0
  32. package/src/api/thorswapStatic/types.ts +18 -0
  33. package/src/contracts/abis/avaxGeneric.ts +92 -0
  34. package/src/contracts/abis/avaxWoofi.ts +145 -0
  35. package/src/contracts/abis/bscGeneric.ts +106 -0
  36. package/src/contracts/abis/chainflipGateway.ts +330 -0
  37. package/src/contracts/abis/erc20.ts +99 -0
  38. package/src/contracts/abis/ethGeneric.ts +92 -0
  39. package/src/contracts/abis/mayaEvmVaults.ts +331 -0
  40. package/src/contracts/abis/pancakeV2.ts +145 -0
  41. package/src/contracts/abis/pangolin.ts +120 -0
  42. package/src/contracts/abis/sushiswap.ts +120 -0
  43. package/src/contracts/abis/tcEthVault.ts +650 -0
  44. package/src/contracts/abis/traderJoe.ts +120 -0
  45. package/src/contracts/abis/uniswapV2.ts +120 -0
  46. package/src/contracts/abis/uniswapV2Leg.ts +128 -0
  47. package/src/contracts/abis/uniswapV3_100.ts +128 -0
  48. package/src/contracts/abis/uniswapV3_10000.ts +128 -0
  49. package/src/contracts/abis/uniswapV3_3000.ts +128 -0
  50. package/src/contracts/abis/uniswapV3_500.ts +128 -0
  51. package/src/contracts/index.ts +95 -0
  52. package/src/contracts/routers/index.ts +58 -0
  53. package/src/contracts/routers/kyber.ts +402 -0
  54. package/src/contracts/routers/oneinch.ts +2188 -0
  55. package/src/contracts/routers/pancakeswap.ts +340 -0
  56. package/src/contracts/routers/pangolin.ts +340 -0
  57. package/src/contracts/routers/sushiswap.ts +340 -0
  58. package/src/contracts/routers/traderJoe.ts +340 -0
  59. package/src/contracts/routers/uniswapv2.ts +340 -0
  60. package/src/contracts/routers/uniswapv3.ts +254 -0
  61. package/src/contracts/routers/woofi.ts +171 -0
  62. package/src/index.ts +11 -12
  63. package/src/modules/__tests__/assetValue.test.ts +0 -2
  64. package/src/modules/__tests__/swapKitConfig.test.ts +37 -0
  65. package/src/modules/assetValue.ts +26 -92
  66. package/src/modules/bigIntArithmetics.ts +6 -2
  67. package/src/modules/requestClient.ts +26 -35
  68. package/src/modules/swapKitConfig.ts +131 -0
  69. package/src/modules/swapKitError.ts +6 -6
  70. package/src/tokens/index.ts +15 -0
  71. package/src/tokens/lists/camelot_v3.ts +15920 -0
  72. package/src/tokens/lists/caviar_v1.ts +1694 -0
  73. package/src/tokens/lists/chainflip.ts +104 -0
  74. package/src/tokens/lists/index.ts +13 -0
  75. package/src/tokens/lists/jupiter.ts +29606 -0
  76. package/src/tokens/lists/mayachain.ts +513 -0
  77. package/src/tokens/lists/oneinch.ts +14238 -0
  78. package/src/tokens/lists/openocean_v2.ts +11514 -0
  79. package/src/tokens/lists/pancakeswap.ts +4296 -0
  80. package/src/tokens/lists/pangolin_v1.ts +175 -0
  81. package/src/tokens/lists/sushiswap_v2.ts +965 -0
  82. package/src/tokens/lists/thorchain.ts +669 -0
  83. package/src/tokens/lists/traderjoe_v2.ts +1384 -0
  84. package/src/tokens/lists/uniswap_v2.ts +5596 -0
  85. package/src/tokens/lists/uniswap_v3.ts +5946 -0
  86. package/src/types/chains.ts +26 -46
  87. package/src/types/commonTypes.ts +3 -90
  88. package/src/types/index.ts +3 -4
  89. package/src/types/sdk.ts +0 -24
  90. package/src/types/tokens.ts +1 -1
  91. package/src/types/wallet.ts +37 -21
  92. package/src/{helpers → utils}/asset.ts +49 -19
  93. package/src/{helpers → utils}/derivationPath.ts +7 -2
  94. package/src/{helpers → utils}/memo.ts +0 -65
  95. package/src/{helpers → utils}/others.ts +12 -5
  96. package/src/utils/plugin.ts +13 -0
  97. package/src/{helpers/web3wallets.ts → utils/wallets.ts} +175 -148
  98. package/src/modules/walletUtils.ts +0 -30
  99. package/src/types/network.ts +0 -49
  100. /package/src/{helpers → utils}/__tests__/asset.test.ts +0 -0
  101. /package/src/{helpers/__tests__/derivationPath.ts → utils/__tests__/derivationPath.test.ts} +0 -0
  102. /package/src/{helpers → utils}/__tests__/memo.test.ts +0 -0
  103. /package/src/{helpers → utils}/__tests__/others.test.ts +0 -0
  104. /package/src/{helpers → utils}/__tests__/validators.test.ts +0 -0
  105. /package/src/{helpers → utils}/liquidity.ts +0 -0
  106. /package/src/{helpers → utils}/validators.ts +0 -0
@@ -0,0 +1,266 @@
1
+ import crypto from "crypto";
2
+ import { ProviderName, RequestClient, SKConfig, SwapKitError, warnOnce } from "@swapkit/helpers";
3
+
4
+ import {
5
+ type BrokerDepositChannelParams,
6
+ type DepositChannelResponse,
7
+ DepositChannelResponseSchema,
8
+ type GasResponse,
9
+ GasResponseSchema,
10
+ type PriceRequest,
11
+ type PriceResponse,
12
+ PriceResponseSchema,
13
+ type QuoteRequest,
14
+ type QuoteResponse,
15
+ QuoteResponseSchema,
16
+ type TokenListProvidersResponse,
17
+ type TokensResponseV2,
18
+ type TrackerParams,
19
+ type TrackerResponse,
20
+ } from "./types";
21
+
22
+ function getApiUrl(path?: `/${string}`) {
23
+ const { isDev, apiUrl, devApiUrl } = SKConfig.get("envs");
24
+
25
+ return `${isDev ? devApiUrl : apiUrl}${path}`;
26
+ }
27
+
28
+ function getAuthHeaders(hash?: string) {
29
+ const { swapKit } = SKConfig.get("apiKeys");
30
+ const { referer } = SKConfig.get("envs");
31
+
32
+ return {
33
+ ...(swapKit && !hash ? { "x-api-key": swapKit } : {}),
34
+ ...(hash && referer ? { "x-payload-hash": hash, referer } : {}),
35
+ };
36
+ }
37
+
38
+ export const computeHash = (
39
+ hashParams: { method: "POST"; payload: any } | { method: "GET"; url: string },
40
+ ) => {
41
+ const { swapKit } = SKConfig.get("apiKeys");
42
+ const { referer } = SKConfig.get("envs");
43
+
44
+ if (!(referer && swapKit)) return;
45
+
46
+ if (!["POST", "GET"].includes(hashParams.method)) {
47
+ throw new SwapKitError("api_v2_invalid_method_key_hash", {
48
+ message: `Invalid method for params: ${JSON.stringify(hashParams)}`,
49
+ });
50
+ }
51
+
52
+ const data =
53
+ hashParams.method === "POST"
54
+ ? JSON.stringify(hashParams.payload)
55
+ : `${hashParams.url}${swapKit}`;
56
+
57
+ return crypto.createHash("sha256").update(data, "utf8").digest("hex");
58
+ };
59
+
60
+ export function getTrackerDetails(payload: TrackerParams) {
61
+ return RequestClient.post<TrackerResponse>(getApiUrl("/track"), {
62
+ json: payload,
63
+ headers: getAuthHeaders(computeHash({ method: "POST", payload })),
64
+ });
65
+ }
66
+
67
+ export async function getSwapQuote(searchParams: QuoteRequest) {
68
+ const response = await RequestClient.post<QuoteResponse>(getApiUrl("/quote"), {
69
+ json: searchParams,
70
+ headers: getAuthHeaders(computeHash({ method: "POST", payload: searchParams })),
71
+ });
72
+
73
+ if (response.error) {
74
+ throw new SwapKitError("api_v2_server_error", { message: response.error });
75
+ }
76
+
77
+ try {
78
+ const parsedResponse = QuoteResponseSchema.safeParse(response);
79
+
80
+ if (!parsedResponse.success) {
81
+ throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
82
+ }
83
+
84
+ return parsedResponse.data;
85
+ } catch (error) {
86
+ // throw new SwapKitError("api_v2_invalid_response", error);
87
+ console.warn(error);
88
+ return response;
89
+ }
90
+ }
91
+
92
+ /**
93
+ * @deprecated use getTokenListProviders instead
94
+ */
95
+ export function getTokenListProvidersV2() {
96
+ warnOnce(true, "getTokenListProvidersV2 is deprecated, use getTokenListProviders instead");
97
+ return getTokenListProviders();
98
+ }
99
+
100
+ export function getTokenListProviders() {
101
+ const url = getApiUrl("/providers");
102
+ return RequestClient.get<TokenListProvidersResponse>(url, {
103
+ headers: getAuthHeaders(computeHash({ method: "GET", url })),
104
+ });
105
+ }
106
+
107
+ export function getTokenList(provider: ProviderName) {
108
+ const url = getApiUrl(`/tokens?provider=${provider}`);
109
+ return RequestClient.get<TokensResponseV2>(url, {
110
+ headers: getAuthHeaders(computeHash({ method: "GET", url })),
111
+ });
112
+ }
113
+
114
+ export async function getPrice(body: PriceRequest) {
115
+ const url = getApiUrl("/price");
116
+ const response = await RequestClient.post<PriceResponse>(url, {
117
+ json: body,
118
+ headers: getAuthHeaders(computeHash({ method: "POST", payload: body })),
119
+ });
120
+
121
+ try {
122
+ const parsedResponse = PriceResponseSchema.safeParse(response);
123
+
124
+ if (!parsedResponse.success) {
125
+ throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
126
+ }
127
+
128
+ return parsedResponse.data;
129
+ } catch (error) {
130
+ throw new SwapKitError("api_v2_invalid_response", error);
131
+ }
132
+ }
133
+
134
+ export async function getGasRate() {
135
+ const url = getApiUrl("/gas");
136
+
137
+ const response = await RequestClient.get<GasResponse>(url, {
138
+ headers: getAuthHeaders(computeHash({ method: "GET", url })),
139
+ });
140
+
141
+ try {
142
+ const parsedResponse = GasResponseSchema.safeParse(response);
143
+
144
+ if (!parsedResponse.success) {
145
+ throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
146
+ }
147
+
148
+ return parsedResponse.data;
149
+ } catch (error) {
150
+ throw new SwapKitError("api_v2_invalid_response", error);
151
+ }
152
+ }
153
+
154
+ const UNCHAINABLE_PROVIDERS = [
155
+ ProviderName.CAVIAR_V1,
156
+ ProviderName.CHAINFLIP,
157
+ ProviderName.CHAINFLIP_STREAMING,
158
+ ProviderName.MAYACHAIN,
159
+ ProviderName.MAYACHAIN_STREAMING,
160
+ ];
161
+
162
+ const CHAINABLE_PROVIDERS = [
163
+ ProviderName.ONEINCH,
164
+ ProviderName.PANCAKESWAP,
165
+ ProviderName.PANGOLIN_V1,
166
+ ProviderName.SUSHISWAP_V2,
167
+ ProviderName.THORCHAIN,
168
+ ProviderName.THORCHAIN_STREAMING,
169
+ ProviderName.TRADERJOE_V2,
170
+ ProviderName.UNISWAP_V2,
171
+ ProviderName.UNISWAP_V3,
172
+ ];
173
+
174
+ // TODO update this once the trading pairs are supported by BE api
175
+ export async function getTokenTradingPairs(providers: ProviderName[]) {
176
+ const tradingPairs = new Map<
177
+ string,
178
+ {
179
+ tokens: TokensResponseV2["tokens"];
180
+ providers: ProviderName[];
181
+ }
182
+ >();
183
+
184
+ if (!providers.length) return tradingPairs;
185
+
186
+ const providerRequests = providers.map(async (provider) => {
187
+ const tokenList = await getTokenList(provider);
188
+ return tokenList;
189
+ });
190
+
191
+ const providersData = (await Promise.all(providerRequests))
192
+ .filter((provider) => !!provider)
193
+ .map(({ tokens, ...rest }) => ({
194
+ data: {
195
+ ...(rest || {}),
196
+ tokens: tokens.map(({ address, ...rest }) => ({
197
+ ...rest,
198
+ ...(address &&
199
+ [
200
+ "resource_rdx1tknxxxxxxxxxradxrdxxxxxxxxx009923554798xxxxxxxxxradxrd",
201
+ "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
202
+ ].includes(address.toLowerCase())
203
+ ? {}
204
+ : { address }),
205
+ })),
206
+ },
207
+ ...rest,
208
+ }));
209
+
210
+ const chainableTokens = providersData
211
+ .filter(({ data }) => {
212
+ return !UNCHAINABLE_PROVIDERS.includes((data?.provider || "") as ProviderName);
213
+ })
214
+ .reduce(
215
+ (acc, { data }) => (data?.tokens ? acc.concat(data.tokens) : acc),
216
+ [] as TokensResponseV2["tokens"],
217
+ );
218
+
219
+ for (const { data } of providersData) {
220
+ if (!data?.tokens) return;
221
+
222
+ const isProviderChainable =
223
+ data.provider && !UNCHAINABLE_PROVIDERS.includes(data.provider as ProviderName);
224
+
225
+ for (const token of data.tokens) {
226
+ const existingTradingPairs = tradingPairs.get(token.identifier.toLowerCase()) || {
227
+ tokens: [],
228
+ providers: [],
229
+ };
230
+
231
+ const tradingPairsForToken = isProviderChainable
232
+ ? { tokens: chainableTokens, providers: CHAINABLE_PROVIDERS }
233
+ : { tokens: data.tokens, providers: data.provider };
234
+
235
+ tradingPairs.set(token.identifier.toLowerCase(), {
236
+ tokens: existingTradingPairs.tokens.concat(tradingPairsForToken.tokens),
237
+ providers: existingTradingPairs.providers.concat(tradingPairsForToken.providers),
238
+ });
239
+ }
240
+ }
241
+
242
+ return tradingPairs;
243
+ }
244
+
245
+ export async function getChainflipDepositChannel(body: BrokerDepositChannelParams) {
246
+ const { destinationAddress } = body;
247
+
248
+ if (!destinationAddress) {
249
+ throw new SwapKitError("chainflip_broker_invalid_params");
250
+ }
251
+ const url = SKConfig.get("integrations").chainflip?.brokerUrl || getApiUrl("/channel");
252
+
253
+ const response = await RequestClient.post<DepositChannelResponse>(url, { json: body });
254
+
255
+ try {
256
+ const parsedResponse = DepositChannelResponseSchema.safeParse(response);
257
+
258
+ if (!parsedResponse.success) {
259
+ throw new SwapKitError("api_v2_invalid_response", parsedResponse.error);
260
+ }
261
+
262
+ return parsedResponse.data;
263
+ } catch (error) {
264
+ throw new SwapKitError("api_v2_invalid_response", error);
265
+ }
266
+ }