@swapkit/helpers 2.6.0 → 3.0.0-beta.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.
Files changed (167) 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-1gwdyyeh.js +4 -0
  6. package/dist/chunk-1gwdyyeh.js.map +10 -0
  7. package/dist/chunk-2bhvezjh.js +4 -0
  8. package/dist/chunk-2bhvezjh.js.map +10 -0
  9. package/dist/chunk-3ptwhtpd.js +3 -0
  10. package/dist/chunk-3ptwhtpd.js.map +10 -0
  11. package/dist/chunk-3wnfcm30.js +4 -0
  12. package/dist/chunk-3wnfcm30.js.map +9 -0
  13. package/dist/chunk-40epvqbm.js +3 -0
  14. package/dist/chunk-40epvqbm.js.map +10 -0
  15. package/dist/chunk-49nsjaay.js +3 -0
  16. package/dist/chunk-49nsjaay.js.map +10 -0
  17. package/dist/chunk-4b66tryh.js +4 -0
  18. package/dist/chunk-4b66tryh.js.map +10 -0
  19. package/dist/chunk-4kzjrrjv.js +3 -0
  20. package/dist/chunk-4kzjrrjv.js.map +10 -0
  21. package/dist/chunk-5m49s8z8.js +4 -0
  22. package/dist/chunk-5m49s8z8.js.map +10 -0
  23. package/dist/chunk-9b6y7r75.js +4 -0
  24. package/dist/chunk-9b6y7r75.js.map +10 -0
  25. package/dist/chunk-9zcbvqq2.js +3 -0
  26. package/dist/chunk-9zcbvqq2.js.map +10 -0
  27. package/dist/chunk-a0bpx1q3.js +4 -0
  28. package/dist/chunk-a0bpx1q3.js.map +10 -0
  29. package/dist/chunk-a0fxkkfh.js +4 -0
  30. package/dist/chunk-a0fxkkfh.js.map +10 -0
  31. package/dist/chunk-a6bjd9ar.js +3 -0
  32. package/dist/chunk-a6bjd9ar.js.map +10 -0
  33. package/dist/chunk-bg6pz5qh.js +3 -0
  34. package/dist/chunk-bg6pz5qh.js.map +10 -0
  35. package/dist/chunk-cpdcnp2x.js +4 -0
  36. package/dist/chunk-cpdcnp2x.js.map +10 -0
  37. package/dist/chunk-fazw0jvt.js +3 -0
  38. package/dist/chunk-fazw0jvt.js.map +9 -0
  39. package/dist/chunk-fr86y3rx.js +4 -0
  40. package/dist/chunk-fr86y3rx.js.map +10 -0
  41. package/dist/chunk-ftdtdkhk.js +3 -0
  42. package/dist/chunk-ftdtdkhk.js.map +10 -0
  43. package/dist/chunk-gjeaw024.js +4 -0
  44. package/dist/chunk-gjeaw024.js.map +10 -0
  45. package/dist/chunk-hdcdd2cf.js +4 -0
  46. package/dist/chunk-hdcdd2cf.js.map +10 -0
  47. package/dist/chunk-q2pb6ggs.js +3 -0
  48. package/dist/chunk-q2pb6ggs.js.map +10 -0
  49. package/dist/chunk-t9s9811k.js +3 -0
  50. package/dist/chunk-t9s9811k.js.map +10 -0
  51. package/dist/chunk-v4dvhh90.js +4 -0
  52. package/dist/chunk-v4dvhh90.js.map +10 -0
  53. package/dist/chunk-vzptz52h.js +4 -0
  54. package/dist/chunk-vzptz52h.js.map +10 -0
  55. package/dist/chunk-wyr5d8ad.js +3 -0
  56. package/dist/chunk-wyr5d8ad.js.map +10 -0
  57. package/dist/chunk-x0jsq6ag.js +3 -0
  58. package/dist/chunk-x0jsq6ag.js.map +10 -0
  59. package/dist/chunk-x2fe9scs.js +3 -0
  60. package/dist/chunk-x2fe9scs.js.map +10 -0
  61. package/dist/chunk-xm5jkehh.js +4 -0
  62. package/dist/chunk-xm5jkehh.js.map +10 -0
  63. package/dist/chunk-xzb1nshn.js +3 -0
  64. package/dist/chunk-xzb1nshn.js.map +10 -0
  65. package/dist/contracts/index.cjs +3 -0
  66. package/dist/contracts/index.cjs.map +38 -0
  67. package/dist/contracts/index.js +3 -0
  68. package/dist/contracts/index.js.map +38 -0
  69. package/dist/index.cjs +6 -0
  70. package/dist/index.cjs.map +30 -0
  71. package/dist/index.js +3 -3
  72. package/dist/index.js.map +23 -23
  73. package/dist/tokens/index.cjs +3 -0
  74. package/dist/tokens/index.cjs.map +10 -0
  75. package/dist/tokens/index.js +3 -0
  76. package/dist/tokens/index.js.map +10 -0
  77. package/package.json +35 -16
  78. package/src/api/index.ts +19 -0
  79. package/src/api/microgard/endpoints.ts +83 -0
  80. package/src/api/microgard/types.ts +60 -0
  81. package/src/api/midgard/endpoints.ts +154 -0
  82. package/src/api/midgard/types.ts +155 -0
  83. package/src/api/swapkitApi/endpoints.ts +161 -0
  84. package/src/api/swapkitApi/types.ts +576 -0
  85. package/src/api/thornode/endpoints.ts +78 -0
  86. package/src/api/thornode/types.ts +241 -0
  87. package/src/api/thorswapStatic/endpoints.ts +46 -0
  88. package/src/api/thorswapStatic/types.ts +18 -0
  89. package/src/contracts/abis/avaxGeneric.ts +92 -0
  90. package/src/contracts/abis/avaxWoofi.ts +145 -0
  91. package/src/contracts/abis/bscGeneric.ts +106 -0
  92. package/src/contracts/abis/chainflipGateway.ts +330 -0
  93. package/src/contracts/abis/erc20.ts +99 -0
  94. package/src/contracts/abis/ethGeneric.ts +92 -0
  95. package/src/contracts/abis/mayaEvmVaults.ts +331 -0
  96. package/src/contracts/abis/pancakeV2.ts +145 -0
  97. package/src/contracts/abis/pangolin.ts +120 -0
  98. package/src/contracts/abis/sushiswap.ts +120 -0
  99. package/src/contracts/abis/tcEthVault.ts +650 -0
  100. package/src/contracts/abis/traderJoe.ts +120 -0
  101. package/src/contracts/abis/uniswapV2.ts +120 -0
  102. package/src/contracts/abis/uniswapV2Leg.ts +128 -0
  103. package/src/contracts/abis/uniswapV3_100.ts +128 -0
  104. package/src/contracts/abis/uniswapV3_10000.ts +128 -0
  105. package/src/contracts/abis/uniswapV3_3000.ts +128 -0
  106. package/src/contracts/abis/uniswapV3_500.ts +128 -0
  107. package/src/contracts/index.ts +95 -0
  108. package/src/contracts/routers/index.ts +58 -0
  109. package/src/contracts/routers/kyber.ts +402 -0
  110. package/src/contracts/routers/oneinch.ts +2188 -0
  111. package/src/contracts/routers/pancakeswap.ts +340 -0
  112. package/src/contracts/routers/pangolin.ts +340 -0
  113. package/src/contracts/routers/sushiswap.ts +340 -0
  114. package/src/contracts/routers/traderJoe.ts +340 -0
  115. package/src/contracts/routers/uniswapv2.ts +340 -0
  116. package/src/contracts/routers/uniswapv3.ts +254 -0
  117. package/src/contracts/routers/woofi.ts +171 -0
  118. package/src/index.ts +19 -15
  119. package/src/modules/__tests__/assetValue.test.ts +50 -24
  120. package/src/modules/__tests__/swapKitConfig.test.ts +37 -0
  121. package/src/modules/assetValue.ts +82 -141
  122. package/src/modules/bigIntArithmetics.ts +6 -2
  123. package/src/modules/requestClient.ts +53 -65
  124. package/src/modules/swapKitConfig.ts +134 -0
  125. package/src/modules/swapKitError.ts +19 -14
  126. package/src/tokens/index.ts +15 -0
  127. package/src/tokens/lists/camelot_v3.ts +15920 -0
  128. package/src/tokens/lists/caviar_v1.ts +1694 -0
  129. package/src/tokens/lists/chainflip.ts +104 -0
  130. package/src/tokens/lists/index.ts +14 -0
  131. package/src/tokens/lists/jupiter.ts +29606 -0
  132. package/src/tokens/lists/mayachain.ts +513 -0
  133. package/src/tokens/lists/oneinch.ts +14238 -0
  134. package/src/tokens/lists/openocean_v2.ts +11514 -0
  135. package/src/tokens/lists/pancakeswap.ts +4296 -0
  136. package/src/tokens/lists/pangolin_v1.ts +175 -0
  137. package/src/tokens/lists/sushiswap_v2.ts +965 -0
  138. package/src/tokens/lists/thorchain.ts +669 -0
  139. package/src/tokens/lists/traderjoe_v2.ts +1384 -0
  140. package/src/tokens/lists/uniswap_v2.ts +5596 -0
  141. package/src/tokens/lists/uniswap_v3.ts +5946 -0
  142. package/src/types/chains.ts +48 -51
  143. package/src/types/commonTypes.ts +3 -90
  144. package/src/types/derivationPath.ts +11 -24
  145. package/src/types/index.ts +3 -4
  146. package/src/types/quotes.ts +0 -1
  147. package/src/types/radix.ts +2 -2
  148. package/src/types/sdk.ts +0 -40
  149. package/src/types/tokens.ts +14 -17
  150. package/src/types/wallet.ts +70 -33
  151. package/src/{helpers → utils}/__tests__/asset.test.ts +0 -27
  152. package/src/utils/asset.ts +235 -0
  153. package/src/{helpers → utils}/derivationPath.ts +31 -9
  154. package/src/{helpers → utils}/memo.ts +0 -82
  155. package/src/{helpers → utils}/others.ts +12 -5
  156. package/src/utils/plugin.ts +13 -0
  157. package/src/utils/tokens.ts +73 -0
  158. package/src/{helpers → utils}/validators.ts +7 -5
  159. package/src/{helpers/web3wallets.ts → utils/wallets.ts} +189 -145
  160. package/src/helpers/asset.ts +0 -324
  161. package/src/modules/walletUtils.ts +0 -30
  162. package/src/types/network.ts +0 -49
  163. /package/src/{helpers/__tests__/derivationPath.ts → utils/__tests__/derivationPath.test.ts} +0 -0
  164. /package/src/{helpers → utils}/__tests__/memo.test.ts +0 -0
  165. /package/src/{helpers → utils}/__tests__/others.test.ts +0 -0
  166. /package/src/{helpers → utils}/__tests__/validators.test.ts +0 -0
  167. /package/src/{helpers → utils}/liquidity.ts +0 -0
@@ -1,23 +1,22 @@
1
+ import { BaseDecimal, Chain, type ChainId, ChainToChainId } from "../types/chains";
2
+ import type { TokenNames, TokenTax } from "../types/tokens";
1
3
  import {
2
4
  type CommonAssetString,
3
- CommonAssetStrings,
5
+ assetFromString,
4
6
  getAssetType,
5
7
  getCommonAssetInfo,
6
8
  getDecimal,
7
9
  isGasAsset,
8
- } from "../helpers/asset";
9
- import { warnOnce } from "../helpers/others";
10
- import { validateIdentifier } from "../helpers/validators";
11
- import { BaseDecimal, Chain, type ChainId, ChainToChainId } from "../types/chains";
12
- import type { TokenNames, TokenTax } from "../types/tokens";
10
+ } from "../utils/asset";
11
+ import { warnOnce } from "../utils/others";
12
+ import { type TokenListName, loadTokenLists } from "../utils/tokens";
13
+ import { validateIdentifier } from "../utils/validators";
13
14
 
14
15
  import type { NumberPrimitives } from "./bigIntArithmetics";
15
16
  import { BigIntArithmetics, formatBigIntToSafeValue } from "./bigIntArithmetics";
16
17
  import { SwapKitError } from "./swapKitError";
17
18
  import type { SwapKitValueType } from "./swapKitNumber";
18
19
 
19
- export const TRADE_OR_SYNTH_ASSET_SEPERATOR = /~|\//;
20
-
21
20
  const staticTokensMap = new Map<
22
21
  TokenNames,
23
22
  { tax?: TokenTax; decimal: number; identifier: string }
@@ -27,8 +26,7 @@ type ConditionalAssetValueReturn<T extends { asyncTokenLookup?: boolean }> =
27
26
  T["asyncTokenLookup"] extends true ? Promise<AssetValue> : AssetValue;
28
27
 
29
28
  type AssetIdentifier =
30
- | { asset: CommonAssetString }
31
- | { asset: TokenNames }
29
+ | { asset: CommonAssetString | TokenNames }
32
30
  | { asset: string }
33
31
  | { chain: Chain };
34
32
 
@@ -123,16 +121,11 @@ export class AssetValue extends BigIntArithmetics {
123
121
  ...fromAssetOrChain
124
122
  }: T & AssetValueFromParams): ConditionalAssetValueReturn<T> {
125
123
  const parsedValue = value instanceof BigIntArithmetics ? value.getValue("string") : value;
126
- const isFromChain = "chain" in fromAssetOrChain;
127
- const assetOrChain = isFromChain ? fromAssetOrChain.chain : fromAssetOrChain.asset;
128
-
129
- const isFromCommonAssetOrChain =
130
- isFromChain ||
131
- CommonAssetStrings.includes(assetOrChain as (typeof CommonAssetStrings)[number]);
124
+ const assetOrChain = getAssetString(fromAssetOrChain);
132
125
 
133
- const { identifier: unsafeIdentifier, decimal: commonAssetDecimal } = isFromCommonAssetOrChain
134
- ? getCommonAssetInfo(assetOrChain as CommonAssetString)
135
- : { identifier: assetOrChain, decimal: undefined };
126
+ const { identifier: unsafeIdentifier, decimal: commonAssetDecimal } = getCommonAssetInfo(
127
+ assetOrChain as CommonAssetString,
128
+ );
136
129
 
137
130
  const { chain, isSynthetic, isTradeAsset } = getAssetInfo(unsafeIdentifier);
138
131
  const token = staticTokensMap.get(
@@ -140,11 +133,12 @@ export class AssetValue extends BigIntArithmetics {
140
133
  ? (unsafeIdentifier as TokenNames)
141
134
  : (unsafeIdentifier.toUpperCase() as TokenNames),
142
135
  );
136
+
143
137
  const tokenDecimal = token?.decimal || commonAssetDecimal;
144
138
 
145
139
  warnOnce(
146
140
  !(asyncTokenLookup || tokenDecimal),
147
- `Couldn't find static decimal for a token on chain (Using default ${BaseDecimal[chain]} decimal as fallback).
141
+ `Couldn't find static decimal for one or more tokens on ${chain} (Using default ${BaseDecimal[chain]} decimal as fallback).
148
142
  This can result in incorrect calculations and mess with amount sent on transactions.
149
143
  You can load static assets by installing @swapkit/tokens package and calling AssetValue.loadStaticAssets()
150
144
  or by passing asyncTokenLookup: true to the from() function, which will make it async and return a promise.`,
@@ -155,106 +149,34 @@ or by passing asyncTokenLookup: true to the from() function, which will make it
155
149
  identifier: unsafeIdentifier,
156
150
  };
157
151
 
152
+ const isSynthOrTrade = isSynthetic || isTradeAsset;
153
+
158
154
  const adjustedValue = fromBaseDecimal
159
155
  ? safeValue(BigInt(parsedValue), fromBaseDecimal)
160
156
  : safeValue(parsedValue, decimal);
161
157
 
162
158
  const assetValue = asyncTokenLookup
163
159
  ? createAssetValue(identifier, fromBaseDecimal ? adjustedValue : parsedValue)
164
- : isSynthetic || isTradeAsset
160
+ : isSynthOrTrade
165
161
  ? createSyntheticAssetValue(identifier, adjustedValue)
166
162
  : new AssetValue({ tax, decimal, identifier, value: adjustedValue });
167
163
 
168
164
  return assetValue as ConditionalAssetValueReturn<T>;
169
165
  }
170
166
 
171
- static loadStaticAssets() {
172
- return new Promise<{ ok: true } | { ok: false; message: string; error: any }>(
173
- (resolve, reject) => {
174
- try {
175
- import("@swapkit/tokens").then((tokenPackage) => {
176
- for (const tokenList of Object.values(tokenPackage.tokenLists)) {
177
- for (const { identifier, chain, ...rest } of tokenList.tokens) {
178
- staticTokensMap.set(
179
- chain === "SOL" ? identifier : (identifier.toUpperCase() as TokenNames),
180
- {
181
- identifier,
182
- decimal: "decimals" in rest ? rest.decimals : BaseDecimal[chain as Chain],
183
- },
184
- );
185
- }
186
- }
187
-
188
- resolve({ ok: true });
189
- });
190
- } catch (error) {
191
- console.error(error);
192
- reject({
193
- ok: false,
194
- error,
195
- message:
196
- "Couldn't load static assets. Ensure you have installed @swapkit/tokens package",
197
- });
198
- }
199
- },
200
- );
201
- }
167
+ static async loadStaticAssets(listNames?: TokenListName[]) {
168
+ const lists = await loadTokenLists(listNames);
202
169
 
203
- /**
204
- * @deprecated use AssetValue.from({ asset, value, asyncTokenLookup: true })
205
- */
206
- static fromString(asset: string, value: NumberPrimitives = 0) {
207
- return AssetValue.from({ asset, value, asyncTokenLookup: true });
208
- }
209
- /**
210
- * @deprecated use AssetValue.from({ asset, value, asyncTokenLookup: true })
211
- */
212
- static fromIdentifier(
213
- asset: `${Chain}.${string}` | `${Chain}/${string}` | TokenNames,
214
- value: NumberPrimitives = 0,
215
- ) {
216
- return AssetValue.from({ asset: asset as TokenNames, value, asyncTokenLookup: true });
217
- }
218
- /**
219
- * @deprecated use AssetValue.from({ asset, value })
220
- */
221
- static fromStringSync(asset: string, value: NumberPrimitives = 0) {
222
- return AssetValue.from({ asset, value });
223
- }
224
- /**
225
- * @deprecated use AssetValue.from({ asset, value, fromBaseDecimal, asyncTokenLookup: true })
226
- */
227
- static fromStringWithBase(
228
- asset: string,
229
- value: string | bigint = 0n,
230
- fromBaseDecimal: number = BaseDecimal.THOR,
231
- ) {
232
- return AssetValue.from({ asyncTokenLookup: true, asset, value, fromBaseDecimal });
233
- }
234
- /**
235
- * @deprecated use AssetValue.from({ asset, value, fromBaseDecimal, asyncTokenLookup: true })
236
- */
237
- static fromStringWithBaseSync(
238
- asset: string,
239
- value: string | bigint = 0n,
240
- fromBaseDecimal: number = BaseDecimal.THOR,
241
- ) {
242
- return AssetValue.from({ asset, value, fromBaseDecimal });
243
- }
244
- /**
245
- * @deprecated use AssetValue.from({ asset, value })
246
- */
247
- static fromIdentifierSync(asset: TokenNames, value: NumberPrimitives = 0) {
248
- return AssetValue.from({ asset, value });
249
- }
250
- /**
251
- * @deprecated use AssetValue.from({ asset, value }) or AssetValue.from({ chain, value })
252
- */
253
- static fromChainOrSignature(assetOrChain: CommonAssetString, value: NumberPrimitives = 0) {
254
- if (Object.values(Chain).includes(assetOrChain as Chain)) {
255
- return AssetValue.from({ chain: assetOrChain as Chain, value });
170
+ for (const { tokens } of Object.values(lists)) {
171
+ for (const { identifier, chain, ...rest } of tokens) {
172
+ const tokenKey = (chain === "SOL" ? identifier : identifier.toUpperCase()) as TokenNames;
173
+ const tokenDecimal = "decimals" in rest ? rest.decimals : BaseDecimal[chain as Chain];
174
+
175
+ staticTokensMap.set(tokenKey, { identifier, decimal: tokenDecimal });
176
+ }
256
177
  }
257
- return AssetValue.from({ asset: assetOrChain, value });
178
+
179
+ return true;
258
180
  }
259
181
  }
260
182
 
@@ -305,7 +227,11 @@ async function createAssetValue(identifier: string, value: NumberPrimitives = 0)
305
227
  staticTokensMap.set(modifiedIdentifier, { identifier, decimal });
306
228
  }
307
229
 
308
- return new AssetValue({ decimal, value: safeValue(value, decimal), identifier });
230
+ return new AssetValue({
231
+ decimal,
232
+ value: safeValue(value, decimal),
233
+ identifier,
234
+ });
309
235
  }
310
236
 
311
237
  function createSyntheticAssetValue(identifier: string, value: NumberPrimitives = 0) {
@@ -314,11 +240,11 @@ function createSyntheticAssetValue(identifier: string, value: NumberPrimitives =
314
240
  : undefined;
315
241
  const isMayaOrThor = chain ? [Chain.Maya, Chain.THORChain].includes(chain) : false;
316
242
 
317
- const assetSeperator = identifier.slice(0, 14).includes("~") ? "~" : "/";
243
+ const assetSeparator = identifier.slice(0, 14).includes("~") ? "~" : "/";
318
244
 
319
245
  const [synthChain, symbol] = isMayaOrThor
320
- ? identifier.split(".").slice(1).join().split(assetSeperator)
321
- : identifier.split(assetSeperator);
246
+ ? identifier.split(".").slice(1).join().split(assetSeparator)
247
+ : identifier.split(assetSeparator);
322
248
 
323
249
  if (!(synthChain && symbol)) {
324
250
  throw new SwapKitError({
@@ -330,7 +256,7 @@ function createSyntheticAssetValue(identifier: string, value: NumberPrimitives =
330
256
  return new AssetValue({
331
257
  decimal: 8,
332
258
  value: safeValue(value, 8),
333
- identifier: `${chain || Chain.THORChain}.${synthChain}${assetSeperator}${symbol}`,
259
+ identifier: `${chain || Chain.THORChain}.${synthChain}${assetSeparator}${symbol}`,
334
260
  });
335
261
  }
336
262
 
@@ -340,54 +266,69 @@ function safeValue(value: NumberPrimitives, decimal: number) {
340
266
  : value;
341
267
  }
342
268
 
343
- // TODO refactor & split into smaller functions
269
+ function getAssetString(assetOrChain: AssetIdentifier) {
270
+ if ("chain" in assetOrChain) return assetOrChain.chain;
271
+
272
+ const { chain, symbol } = assetFromString(assetOrChain.asset);
273
+ const isNativeChain = getAssetType({ chain, symbol }) === "Native";
274
+
275
+ return isNativeChain ? chain : assetOrChain.asset;
276
+ }
277
+
344
278
  function getAssetInfo(identifier: string) {
345
- const isSynthetic = identifier.slice(0, 14).includes("/");
346
- const isTradeAsset = identifier.slice(0, 14).includes("~");
347
- const isSynthOrTradeAsset = isSynthetic || isTradeAsset;
348
- const assetSeperator = identifier.slice(0, 14).includes("~") ? "~" : "/";
279
+ const shortIdentifier = identifier.slice(0, 14);
280
+ const splitIdentifier = identifier.split(".");
281
+ const identifierChain = splitIdentifier[0]?.toUpperCase() as Chain;
282
+ const isThorOrMaya = [Chain.THORChain, Chain.Maya].includes(identifierChain);
349
283
 
350
- const isThorchain = identifier.split(".")?.[0]?.toUpperCase() === Chain.THORChain;
351
- const isMaya = identifier.split(".")?.[0]?.toUpperCase() === Chain.Maya;
284
+ const isSynthetic = shortIdentifier.includes("/");
285
+ const isTradeAsset = shortIdentifier.includes("~");
286
+ const isSynthOrTrade = isSynthetic || isTradeAsset;
287
+ const assetSeparator = isTradeAsset ? "~" : "/";
352
288
 
353
- const [synthChain, synthSymbol = ""] =
354
- isThorchain || isMaya
355
- ? identifier.split(".").slice(1).join().split(assetSeperator)
356
- : identifier.split(assetSeperator);
289
+ const [synthChain, synthSymbol = ""] = isThorOrMaya
290
+ ? splitIdentifier.slice(1).join().split(assetSeparator)
291
+ : identifier.split(assetSeparator);
357
292
 
358
- if (isSynthOrTradeAsset && !(synthChain && synthSymbol)) {
293
+ if (isSynthOrTrade && !(synthChain && synthSymbol)) {
359
294
  throw new SwapKitError({
360
295
  errorKey: "helpers_invalid_asset_identifier",
361
296
  info: { identifier },
362
297
  });
363
298
  }
364
299
 
365
- const adjustedIdentifier =
366
- identifier.includes(".") && !isSynthOrTradeAsset
367
- ? identifier
368
- : `${isMaya ? Chain.Maya : Chain.THORChain}.${synthSymbol}`;
369
-
370
- const [chain, ...rest] = adjustedIdentifier.split(".") as [Chain, string];
300
+ const [chain, ...rest] = (
301
+ identifier.includes(".") && !isSynthOrTrade ? identifier : `${identifierChain}.${synthSymbol}`
302
+ ).split(".") as [Chain, string];
371
303
 
372
- const symbol = isSynthOrTradeAsset ? synthSymbol : rest.join(".");
373
- const splitSymbol = symbol.split("-");
374
- const ticker = (
375
- splitSymbol.length === 1 ? splitSymbol[0] : splitSymbol.slice(0, -1).join("-")
376
- ) as string;
377
- const unformattedAddress =
378
- splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1];
304
+ const assetSymbol = isSynthOrTrade ? synthSymbol : rest.join(".");
379
305
 
380
- const address = chain === Chain.Solana ? unformattedAddress : unformattedAddress?.toLowerCase();
306
+ const { address, ticker } = getAssetBaseInfo({ symbol: assetSymbol, chain });
307
+ const symbol =
308
+ (isSynthOrTrade ? `${synthChain}${assetSeparator}` : "") +
309
+ (address ? `${ticker}-${address ?? ""}` : assetSymbol);
381
310
 
382
311
  return {
383
312
  address,
384
313
  chain,
385
- isGasAsset: isGasAsset({ chain, symbol }),
314
+ isSynthOrTrade,
386
315
  isSynthetic,
387
316
  isTradeAsset,
388
317
  ticker,
389
- symbol:
390
- (isSynthOrTradeAsset ? `${synthChain}${assetSeperator}` : "") +
391
- (address ? `${ticker}-${address ?? ""}` : symbol),
318
+ symbol,
319
+ isGasAsset: isGasAsset({ chain, symbol: assetSymbol }),
392
320
  };
393
321
  }
322
+
323
+ function getAssetBaseInfo({ symbol, chain }: { symbol: string; chain: Chain }) {
324
+ const splitSymbol = symbol.split("-");
325
+ const unformattedAddress =
326
+ splitSymbol.length === 1 ? undefined : splitSymbol[splitSymbol.length - 1];
327
+
328
+ const address = chain === Chain.Solana ? unformattedAddress : unformattedAddress?.toLowerCase();
329
+ const ticker = (
330
+ splitSymbol.length === 1 ? splitSymbol[0] : splitSymbol.slice(0, -1).join("-")
331
+ ) as string;
332
+
333
+ return { address, ticker };
334
+ }
@@ -131,7 +131,6 @@ export class BigIntArithmetics {
131
131
  return this.#comparison("eqValue", value);
132
132
  }
133
133
 
134
- // @ts-expect-error False positive
135
134
  getValue<T extends AllowedNumberTypes>(type: T, decimal?: number): NumberPrimitivesType[T] {
136
135
  const value = this.formatBigIntToSafeValue(
137
136
  this.bigIntValue,
@@ -146,10 +145,11 @@ export class BigIntArithmetics {
146
145
  case "bigint":
147
146
  return ((this.bigIntValue * 10n ** BigInt(this.decimal || 8n)) /
148
147
  this.decimalMultiplier) as NumberPrimitivesType[T];
148
+ default:
149
+ return value as NumberPrimitivesType[T];
149
150
  }
150
151
  }
151
152
 
152
- // @ts-expect-error
153
153
  getBaseValue<T extends AllowedNumberTypes>(type: T, decimal?: number): NumberPrimitivesType[T] {
154
154
  const divisor =
155
155
  this.decimalMultiplier / toMultiplier(decimal || this.decimal || BaseDecimal.THOR);
@@ -162,6 +162,8 @@ export class BigIntArithmetics {
162
162
  return baseValue.toString() as NumberPrimitivesType[T];
163
163
  case "bigint":
164
164
  return baseValue as NumberPrimitivesType[T];
165
+ default:
166
+ return baseValue as NumberPrimitivesType[T];
165
167
  }
166
168
  }
167
169
 
@@ -360,6 +362,8 @@ export class BigIntArithmetics {
360
362
  return compareToValue <= value;
361
363
  case "eqValue":
362
364
  return compareToValue === value;
365
+ default:
366
+ return false;
363
367
  }
364
368
  }
365
369
 
@@ -1,75 +1,63 @@
1
- type Options = Parameters<typeof fetch>[1] & {
2
- headers?: Record<string, string>;
3
- apiKey?: string;
4
- method?: "GET" | "POST";
5
- onError?: (error: any) => any;
1
+ import { SKConfig } from "./swapKitConfig";
2
+
3
+ type Options = RequestInit & {
4
+ /**
5
+ * @deprecated Use onSuccess instead
6
+ */
6
7
  responseHandler?: (response: any) => any;
7
- searchParams?: Record<string, string>;
8
8
  json?: unknown;
9
+ onError?: (error: any) => any;
10
+ onSuccess?: (response: any) => any;
11
+ searchParams?: Record<string, string>;
9
12
  };
10
13
 
11
- let clientConfig: Options = {};
12
-
13
- export const defaultRequestHeaders =
14
- typeof window !== "undefined"
15
- ? ({} as Record<string, string>)
16
- : { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" };
17
-
18
- export function setRequestClientConfig({ apiKey, ...config }: Options) {
19
- clientConfig = { ...config, apiKey };
20
- }
21
-
22
- async function fetchWithConfig(url: string, options: Options) {
23
- const { apiKey, ...config } = clientConfig;
24
- const { searchParams, json, body } = options;
25
- const headers = {
26
- ...defaultRequestHeaders,
27
- ...config.headers,
28
- ...options.headers,
29
- ...(json ? { "Content-Type": "application/json" } : {}),
30
- } as Record<string, string>;
31
-
32
- const bodyToSend = json ? JSON.stringify(json) : body;
33
-
34
- const urlInstance = new URL(url);
35
- if (searchParams) {
36
- urlInstance.search = new URLSearchParams(searchParams).toString();
37
- }
14
+ export const RequestClient = {
15
+ get: fetchWithConfig("GET"),
16
+ post: fetchWithConfig("POST"),
17
+ extend: (extendOptions: Options) => ({
18
+ get: fetchWithConfig("GET", extendOptions),
19
+ post: fetchWithConfig("POST", extendOptions),
20
+ extend: (newOptions: Options) => RequestClient.extend({ ...extendOptions, ...newOptions }),
21
+ }),
22
+ };
38
23
 
39
- if (apiKey) headers["x-api-key"] = apiKey;
24
+ function fetchWithConfig(method: "GET" | "POST", extendOptions: Options = {}) {
25
+ return async <T>(url: string, options: Options = {}): Promise<T> => {
26
+ const { searchParams, json, body, headers: headersOptions } = { ...extendOptions, ...options };
27
+ const { swapKit } = SKConfig.get("apiKeys");
40
28
 
41
- try {
42
- const response = await fetch(urlInstance.toString(), {
43
- ...config,
44
- ...options,
45
- body: bodyToSend,
46
- headers,
47
- });
48
- const body = await response.json();
29
+ const isJson = json || url.endsWith(".json");
30
+ const bodyToSend = isJson ? JSON.stringify(json) : body;
31
+ const urlInstance = new URL(url);
49
32
 
50
- if (options.responseHandler) return options.responseHandler(body);
33
+ if (searchParams) {
34
+ urlInstance.search = new URLSearchParams(searchParams).toString();
35
+ }
51
36
 
52
- return body;
53
- } catch (error) {
54
- if (options.onError) return options.onError(error);
37
+ const headers = {
38
+ ...headersOptions,
39
+ ...(isJson ? { "Content-Type": "application/json" } : {}),
40
+ ...(swapKit ? { "x-api-key": swapKit } : {}),
41
+ };
55
42
 
56
- console.error(error);
57
- }
43
+ try {
44
+ const response = await fetch(urlInstance.toString(), {
45
+ ...options,
46
+ method,
47
+ body: bodyToSend,
48
+ headers,
49
+ });
50
+
51
+ if (!response.ok) {
52
+ const message = await response.text();
53
+ throw new Error(`${response.statusText}(${response.status}): ${message}`);
54
+ }
55
+
56
+ const body = await response.json();
57
+
58
+ return options.onSuccess?.(body) || options.responseHandler?.(body) || body;
59
+ } catch (error) {
60
+ return options.onError?.(error) || console.error(error);
61
+ }
62
+ };
58
63
  }
59
-
60
- export const RequestClient = {
61
- get: async <T>(url: string, options?: Options): Promise<T> =>
62
- fetchWithConfig(url, { ...options, method: "GET" }),
63
- post: async <T>(url: string, options?: Options): Promise<T> =>
64
- fetchWithConfig(url, { ...options, method: "POST" }),
65
- extend: (options: Options) => {
66
- const extendedConfig = { ...clientConfig, ...options };
67
- return {
68
- get: async <T>(url: string, options?: Options): Promise<T> =>
69
- fetchWithConfig(url, { ...extendedConfig, ...options, method: "GET" }),
70
- post: async <T>(url: string, options?: Options): Promise<T> =>
71
- fetchWithConfig(url, { ...extendedConfig, ...options, method: "POST" }),
72
- extend: (newOptions: Options) => RequestClient.extend({ ...extendedConfig, ...newOptions }),
73
- };
74
- },
75
- };
@@ -0,0 +1,134 @@
1
+ import { createStore } from "zustand/vanilla";
2
+ import { Chain, EXPLORER_URLS, NODE_URLS, RPC_URLS, WalletOption } from "../types";
3
+
4
+ export type SKConfigIntegrations = {
5
+ chainflip?: { useSDKBroker?: boolean; brokerUrl: string };
6
+ coinbase?: {
7
+ appName: string;
8
+ appLogoUrl?: string | null;
9
+ darkMode?: boolean;
10
+ linkAPIUrl?: string;
11
+ overrideIsMetaMask?: boolean;
12
+ overrideIsCoinbaseWallet?: boolean;
13
+ overrideIsCoinbaseBrowser?: boolean;
14
+ headlessMode?: boolean;
15
+ reloadOnDisconnect?: boolean;
16
+ };
17
+ trezor?: { email: string; appUrl: string };
18
+ keepKey?: { name: string; imageUrl: string; basePath: string; url: string };
19
+ radix: {
20
+ dAppDefinitionAddress: string;
21
+ applicationName: string;
22
+ applicationVersion: string;
23
+ network: { networkId: number; networkName: string; dashboardBase: string };
24
+ };
25
+ };
26
+
27
+ const initialState = {
28
+ // TODO: figure out how to type apis without using toolbox directly
29
+ // Maybe move rpc/toolbox apis to helpers?
30
+ apis: {} as { [key in Chain]: any },
31
+ chains: Object.values(Chain),
32
+ wallets: Object.values(WalletOption),
33
+ explorerUrls: EXPLORER_URLS,
34
+ nodeUrls: NODE_URLS,
35
+ rpcUrls: RPC_URLS,
36
+
37
+ apiKeys: {
38
+ blockchair: "",
39
+ kado: "",
40
+ keepKey: "",
41
+ swapKit: "",
42
+ walletConnectProjectId: "",
43
+ },
44
+
45
+ envs: {
46
+ apiUrl: "https://api.swapkit.dev",
47
+ devApiUrl: "https://dev-api.swapkit.dev",
48
+ isDev: false,
49
+ isStagenet: false,
50
+ },
51
+
52
+ integrations: {
53
+ radix: {
54
+ applicationName: "Swapkit Playground",
55
+ applicationVersion: "0.0.1",
56
+ dAppDefinitionAddress: "account_rdx128r289p58222hcvev7frs6kue76pl7pdcnw8725aw658v0zggkh9ws",
57
+ network: {
58
+ dashboardBase: "https://dashboard.radixdlt.com",
59
+ networkId: 1,
60
+ networkName: "mainnet",
61
+ },
62
+ },
63
+ } as SKConfigIntegrations,
64
+ };
65
+ type SKState = typeof initialState;
66
+
67
+ export type SKConfigState = {
68
+ apiKeys?: Partial<SKState["apiKeys"]>;
69
+ chains?: SKState["chains"];
70
+ envs?: Partial<SKState["envs"]>;
71
+ explorerUrls?: Partial<SKState["explorerUrls"]>;
72
+ integrations?: Partial<SKConfigIntegrations>;
73
+ nodeUrls?: Partial<SKState["nodeUrls"]>;
74
+ rpcUrls?: Partial<SKState["rpcUrls"]>;
75
+ wallets?: SKState["wallets"];
76
+ };
77
+
78
+ type SwapKitConfigStore = SKState & {
79
+ setApiKey: (key: keyof SKState["apiKeys"], apiKey: string) => void;
80
+ setConfig: (config: SKConfigState) => void;
81
+ setEnv: <T extends keyof SKState["envs"]>(key: T, value: SKState["envs"][T]) => void;
82
+ setExplorerUrl: (chain: keyof SKState["explorerUrls"], url: string) => void;
83
+ setNodeUrl: (chain: keyof SKState["nodeUrls"], url: string) => void;
84
+ setRpcUrl: (chain: keyof SKState["rpcUrls"], url: string) => void;
85
+ setIntegrationConfig: (
86
+ integration: keyof SKState["integrations"],
87
+ config: SKConfigIntegrations[keyof SKConfigIntegrations],
88
+ ) => void;
89
+ };
90
+
91
+ const swapKitState = createStore<SwapKitConfigStore>((set) => ({
92
+ ...initialState,
93
+
94
+ setApiKey: (key, apiKey) => set((s) => ({ apiKeys: { ...s.apiKeys, [key]: apiKey } })),
95
+ setEnv: (key, value) => set((s) => ({ envs: { ...s.envs, [key]: value } })),
96
+ setExplorerUrl: (chain, url) =>
97
+ set((s) => ({ explorerUrls: { ...s.explorerUrls, [chain]: url } })),
98
+ setNodeUrl: (chain, url) => set((s) => ({ nodeUrls: { ...s.nodeUrls, [chain]: url } })),
99
+ setRpcUrl: (chain, url) => set((s) => ({ rpcUrls: { ...s.rpcUrls, [chain]: url } })),
100
+ setIntegrationConfig: (integration, config) =>
101
+ set((s) => ({ integrations: { ...s.integrations, [integration]: config } })),
102
+ setConfig: (config) =>
103
+ set((s) => ({
104
+ apiKeys: { ...s.apiKeys, ...config.apiKeys },
105
+ envs: { ...s.envs, ...config.envs },
106
+ explorerUrls: { ...s.explorerUrls, ...config.explorerUrls },
107
+ integrations: { ...s.integrations, ...config.integrations },
108
+ nodeUrls: { ...s.nodeUrls, ...config.nodeUrls },
109
+ rpcUrls: { ...s.rpcUrls, ...config.rpcUrls },
110
+ chains: s.chains.concat(config.chains || []),
111
+ wallets: s.wallets.concat(config.wallets || []),
112
+ })),
113
+ }));
114
+
115
+ export const SKConfig = {
116
+ getState: swapKitState.getState,
117
+ get: <T extends keyof SKState>(key: T) => swapKitState.getState()[key],
118
+ set: <T extends SKConfigState>(config: T) => swapKitState.getState().setConfig(config),
119
+
120
+ setApiKey: <T extends keyof SKState["apiKeys"]>(key: T, apiKey: string) =>
121
+ swapKitState.getState().setApiKey(key, apiKey),
122
+ setEnv: <T extends keyof SKState["envs"]>(key: T, value: SKState["envs"][T]) =>
123
+ swapKitState.getState().setEnv(key, value),
124
+ setExplorerUrl: <T extends keyof SKState["explorerUrls"]>(chain: T, url: string) =>
125
+ swapKitState.getState().setExplorerUrl(chain, url),
126
+ setNodeUrl: <T extends keyof SKState["nodeUrls"]>(chain: T, url: string) =>
127
+ swapKitState.getState().setNodeUrl(chain, url),
128
+ setRpcUrl: <T extends keyof SKState["rpcUrls"]>(chain: T, url: string) =>
129
+ swapKitState.getState().setRpcUrl(chain, url),
130
+ setIntegrationConfig: <T extends keyof SKState["integrations"]>(
131
+ integration: T,
132
+ config: SKConfigIntegrations[T],
133
+ ) => swapKitState.getState().setIntegrationConfig(integration, config),
134
+ };