@swapkit/toolboxes 1.0.0-beta.3 → 1.0.0-beta.30

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 (131) hide show
  1. package/dist/{chunk-fjfxga2v.js → chunk-5yxc1e69.js} +1 -1
  2. package/dist/{chunk-fjfxga2v.js.map → chunk-5yxc1e69.js.map} +1 -1
  3. package/dist/chunk-6f98phv2.js +4 -0
  4. package/dist/{chunk-0h4xdrwz.js.map → chunk-6f98phv2.js.map} +2 -2
  5. package/dist/{chunk-0f0249b1.js → chunk-9bqegm61.js} +1 -1
  6. package/dist/{chunk-p1kdg37m.js → chunk-s47y8512.js} +2 -2
  7. package/dist/{chunk-p1kdg37m.js.map → chunk-s47y8512.js.map} +1 -1
  8. package/dist/chunk-vtd17cje.js +3 -0
  9. package/dist/chunk-vtd17cje.js.map +10 -0
  10. package/dist/chunk-zcdeg6h9.js +4 -0
  11. package/dist/chunk-zcdeg6h9.js.map +10 -0
  12. package/dist/src/cosmos/index.cjs +3 -0
  13. package/dist/src/cosmos/index.cjs.map +16 -0
  14. package/dist/src/cosmos/index.js +3 -0
  15. package/dist/src/cosmos/index.js.map +16 -0
  16. package/dist/src/evm/index.cjs +3 -0
  17. package/dist/src/evm/index.cjs.map +18 -0
  18. package/dist/src/evm/index.js +3 -0
  19. package/dist/src/evm/index.js.map +18 -0
  20. package/dist/src/index.cjs +3 -0
  21. package/dist/src/index.cjs.map +10 -0
  22. package/dist/src/index.js +3 -0
  23. package/dist/src/index.js.map +10 -0
  24. package/dist/src/near/index.cjs +3 -0
  25. package/dist/src/near/index.cjs.map +13 -0
  26. package/dist/src/near/index.js +3 -0
  27. package/dist/src/near/index.js.map +13 -0
  28. package/dist/{radix → src/radix}/index.cjs +2 -2
  29. package/dist/src/radix/index.cjs.map +10 -0
  30. package/dist/src/radix/index.js +3 -0
  31. package/dist/src/radix/index.js.map +10 -0
  32. package/dist/src/ripple/index.cjs +3 -0
  33. package/dist/src/ripple/index.cjs.map +10 -0
  34. package/dist/src/ripple/index.js +3 -0
  35. package/dist/src/ripple/index.js.map +10 -0
  36. package/dist/src/solana/index.cjs +3 -0
  37. package/dist/src/solana/index.cjs.map +10 -0
  38. package/dist/src/solana/index.js +3 -0
  39. package/dist/src/solana/index.js.map +10 -0
  40. package/dist/src/substrate/index.cjs +3 -0
  41. package/dist/src/substrate/index.cjs.map +12 -0
  42. package/dist/src/substrate/index.js +3 -0
  43. package/dist/src/substrate/index.js.map +12 -0
  44. package/dist/src/tron/index.cjs +3 -0
  45. package/dist/src/tron/index.cjs.map +11 -0
  46. package/dist/src/tron/index.js +3 -0
  47. package/dist/src/tron/index.js.map +11 -0
  48. package/dist/src/utxo/index.cjs +5 -0
  49. package/dist/src/utxo/index.cjs.map +17 -0
  50. package/dist/src/utxo/index.js +5 -0
  51. package/dist/src/utxo/index.js.map +17 -0
  52. package/package.json +49 -37
  53. package/src/cosmos/thorchainUtils/addressFormat.ts +4 -1
  54. package/src/cosmos/thorchainUtils/messages.ts +2 -2
  55. package/src/cosmos/thorchainUtils/registry.ts +3 -3
  56. package/src/cosmos/toolbox/cosmos.ts +35 -16
  57. package/src/cosmos/toolbox/index.ts +2 -2
  58. package/src/cosmos/toolbox/thorchain.ts +11 -9
  59. package/src/cosmos/util.ts +76 -6
  60. package/src/evm/__tests__/address-validation.test.ts +86 -0
  61. package/src/evm/__tests__/ethereum.test.ts +1 -1
  62. package/src/evm/helpers.ts +4 -3
  63. package/src/evm/toolbox/baseEVMToolbox.ts +37 -25
  64. package/src/evm/toolbox/index.ts +2 -2
  65. package/src/evm/toolbox/op.ts +21 -7
  66. package/src/index.ts +118 -100
  67. package/src/near/__tests__/core.test.ts +80 -0
  68. package/src/near/helpers/contractFactory.ts +22 -0
  69. package/src/near/helpers/core.ts +89 -0
  70. package/src/near/helpers/gasEstimation.ts +110 -0
  71. package/src/near/helpers/index.ts +5 -0
  72. package/src/near/helpers/nep141.ts +110 -0
  73. package/src/near/index.ts +24 -0
  74. package/src/near/toolbox.ts +498 -0
  75. package/src/near/types/contract.ts +48 -0
  76. package/src/near/types/nep141.ts +66 -0
  77. package/src/near/types.ts +57 -0
  78. package/src/radix/index.ts +8 -2
  79. package/src/ripple/index.ts +15 -26
  80. package/src/solana/toolbox.ts +73 -2
  81. package/src/substrate/balance.ts +92 -0
  82. package/src/substrate/substrate.ts +7 -5
  83. package/src/tron/__tests__/toolbox.test.ts +147 -0
  84. package/src/tron/helpers/trc20.abi.ts +40 -0
  85. package/src/tron/index.ts +16 -0
  86. package/src/tron/toolbox.ts +336 -0
  87. package/src/tron/types.ts +31 -0
  88. package/src/utxo/__tests__/zcash-integration.test.ts +114 -0
  89. package/src/utxo/helpers/api.ts +66 -16
  90. package/src/utxo/helpers/bchaddrjs.ts +8 -8
  91. package/src/utxo/helpers/coinselect.ts +4 -2
  92. package/src/utxo/helpers/txSize.ts +4 -3
  93. package/src/utxo/index.ts +1 -0
  94. package/src/utxo/toolbox/bitcoinCash.ts +22 -14
  95. package/src/utxo/toolbox/index.ts +16 -4
  96. package/src/utxo/toolbox/utxo.ts +42 -27
  97. package/src/utxo/toolbox/zcash.ts +208 -0
  98. package/src/utxo/types.ts +2 -0
  99. package/dist/chunk-0h4xdrwz.js +0 -4
  100. package/dist/cosmos/index.cjs +0 -3
  101. package/dist/cosmos/index.cjs.map +0 -16
  102. package/dist/cosmos/index.js +0 -3
  103. package/dist/cosmos/index.js.map +0 -16
  104. package/dist/evm/index.cjs +0 -3
  105. package/dist/evm/index.cjs.map +0 -18
  106. package/dist/evm/index.js +0 -3
  107. package/dist/evm/index.js.map +0 -18
  108. package/dist/index.cjs +0 -3
  109. package/dist/index.cjs.map +0 -10
  110. package/dist/index.js +0 -3
  111. package/dist/index.js.map +0 -10
  112. package/dist/radix/index.cjs.map +0 -10
  113. package/dist/radix/index.js +0 -3
  114. package/dist/radix/index.js.map +0 -10
  115. package/dist/ripple/index.cjs +0 -3
  116. package/dist/ripple/index.cjs.map +0 -10
  117. package/dist/ripple/index.js +0 -3
  118. package/dist/ripple/index.js.map +0 -10
  119. package/dist/solana/index.cjs +0 -3
  120. package/dist/solana/index.cjs.map +0 -10
  121. package/dist/solana/index.js +0 -3
  122. package/dist/solana/index.js.map +0 -10
  123. package/dist/substrate/index.cjs +0 -3
  124. package/dist/substrate/index.cjs.map +0 -11
  125. package/dist/substrate/index.js +0 -3
  126. package/dist/substrate/index.js.map +0 -11
  127. package/dist/utxo/index.cjs +0 -3
  128. package/dist/utxo/index.cjs.map +0 -16
  129. package/dist/utxo/index.js +0 -3
  130. package/dist/utxo/index.js.map +0 -16
  131. /package/dist/{chunk-0f0249b1.js.map → chunk-9bqegm61.js.map} +0 -0
@@ -8,6 +8,7 @@ import {
8
8
  FeeOption,
9
9
  SwapKitError,
10
10
  SwapKitNumber,
11
+ applyFeeMultiplierToBigInt,
11
12
  isGasAsset,
12
13
  } from "@swapkit/helpers";
13
14
  import { erc20ABI } from "@swapkit/helpers/contracts";
@@ -208,7 +209,7 @@ export function getEstimateGasPrices({
208
209
  try {
209
210
  const { gasPrice } = await provider.getFeeData();
210
211
 
211
- if (!gasPrice) throw new Error("No fee data available");
212
+ if (!gasPrice) throw new SwapKitError("toolbox_evm_no_fee_data");
212
213
 
213
214
  return {
214
215
  [FeeOption.Average]: { gasPrice },
@@ -216,9 +217,9 @@ export function getEstimateGasPrices({
216
217
  [FeeOption.Fastest]: { gasPrice },
217
218
  };
218
219
  } catch (error) {
219
- throw new Error(
220
- `Failed to estimate gas price: ${(error as any).msg ?? (error as any).toString()}`,
221
- );
220
+ throw new SwapKitError("toolbox_evm_gas_estimation_error", {
221
+ error: (error as any).msg ?? (error as any).toString(),
222
+ });
222
223
  }
223
224
  };
224
225
  }
@@ -231,7 +232,7 @@ export function getEstimateGasPrices({
231
232
  const price = gasPrice as bigint;
232
233
 
233
234
  if (!(maxFeePerGas && maxPriorityFeePerGas)) {
234
- throw new Error("No fee data available");
235
+ throw new SwapKitError("toolbox_evm_no_fee_data");
235
236
  }
236
237
 
237
238
  return {
@@ -242,22 +243,25 @@ export function getEstimateGasPrices({
242
243
  maxPriorityFeePerGas,
243
244
  },
244
245
  [FeeOption.Fast]: {
245
- l1GasPrice: ((l1GasPrice || 0n) * 15n) / 10n,
246
- gasPrice: (price * 15n) / 10n,
246
+ l1GasPrice: applyFeeMultiplierToBigInt(l1GasPrice || 0n, FeeOption.Fast),
247
+ gasPrice: applyFeeMultiplierToBigInt(price, FeeOption.Fast),
247
248
  maxFeePerGas,
248
- maxPriorityFeePerGas: (maxPriorityFeePerGas * 15n) / 10n,
249
+ maxPriorityFeePerGas: applyFeeMultiplierToBigInt(maxPriorityFeePerGas, FeeOption.Fast),
249
250
  },
250
251
  [FeeOption.Fastest]: {
251
- l1GasPrice: (l1GasPrice || 0n) * 2n,
252
- gasPrice: price * 2n,
252
+ l1GasPrice: applyFeeMultiplierToBigInt(l1GasPrice || 0n, FeeOption.Fastest),
253
+ gasPrice: applyFeeMultiplierToBigInt(price, FeeOption.Fastest),
253
254
  maxFeePerGas,
254
- maxPriorityFeePerGas: maxPriorityFeePerGas * 2n,
255
+ maxPriorityFeePerGas: applyFeeMultiplierToBigInt(
256
+ maxPriorityFeePerGas,
257
+ FeeOption.Fastest,
258
+ ),
255
259
  },
256
260
  };
257
261
  } catch (error) {
258
- throw new Error(
259
- `Failed to estimate gas price: ${(error as any).msg ?? (error as any).toString()}`,
260
- );
262
+ throw new SwapKitError("toolbox_evm_gas_estimation_error", {
263
+ error: (error as any).msg ?? (error as any).toString(),
264
+ });
261
265
  }
262
266
  };
263
267
  }
@@ -273,12 +277,15 @@ export function getEstimateGasPrices({
273
277
  return {
274
278
  [FeeOption.Average]: { maxFeePerGas, maxPriorityFeePerGas },
275
279
  [FeeOption.Fast]: {
276
- maxFeePerGas: (maxFeePerGas * 15n) / 10n,
277
- maxPriorityFeePerGas: (maxPriorityFeePerGas * 15n) / 10n,
280
+ maxFeePerGas: applyFeeMultiplierToBigInt(maxFeePerGas, FeeOption.Fast),
281
+ maxPriorityFeePerGas: applyFeeMultiplierToBigInt(maxPriorityFeePerGas, FeeOption.Fast),
278
282
  },
279
283
  [FeeOption.Fastest]: {
280
- maxFeePerGas: maxFeePerGas * 2n,
281
- maxPriorityFeePerGas: maxPriorityFeePerGas * 2n,
284
+ maxFeePerGas: applyFeeMultiplierToBigInt(maxFeePerGas, FeeOption.Fastest),
285
+ maxPriorityFeePerGas: applyFeeMultiplierToBigInt(
286
+ maxPriorityFeePerGas,
287
+ FeeOption.Fastest,
288
+ ),
282
289
  },
283
290
  };
284
291
  }
@@ -286,13 +293,13 @@ export function getEstimateGasPrices({
286
293
 
287
294
  return {
288
295
  [FeeOption.Average]: { gasPrice },
289
- [FeeOption.Fast]: { gasPrice: (gasPrice * 15n) / 10n },
290
- [FeeOption.Fastest]: { gasPrice: gasPrice * 2n },
296
+ [FeeOption.Fast]: { gasPrice: applyFeeMultiplierToBigInt(gasPrice, FeeOption.Fast) },
297
+ [FeeOption.Fastest]: { gasPrice: applyFeeMultiplierToBigInt(gasPrice, FeeOption.Fastest) },
291
298
  };
292
299
  } catch (error) {
293
- throw new Error(
294
- `Failed to estimate gas price: ${(error as any).msg ?? (error as any).toString()}`,
295
- );
300
+ throw new SwapKitError("toolbox_evm_gas_estimation_error", {
301
+ error: (error as any).msg ?? (error as any).toString(),
302
+ });
296
303
  }
297
304
  };
298
305
  }
@@ -314,7 +321,10 @@ function getCall({ provider, isEIP1559Compatible, signer, chain }: ToolboxWrapPa
314
321
  feeOption = FeeOption.Fast,
315
322
  }: CallParams): Promise<T> {
316
323
  const contractProvider = callProvider || provider;
317
- if (!contractAddress) throw new Error("contractAddress must be provided");
324
+ if (!contractAddress)
325
+ throw new SwapKitError("toolbox_evm_invalid_params", {
326
+ error: "contractAddress must be provided",
327
+ });
318
328
 
319
329
  const isStateChanging = isStateChangingCall({ abi, funcName });
320
330
 
@@ -538,6 +548,7 @@ function getEstimateGasLimit({ provider, signer }: ToolboxWrapParams) {
538
548
  assetValue,
539
549
  recipient,
540
550
  memo,
551
+ data,
541
552
  sender,
542
553
  funcName,
543
554
  funcParams,
@@ -547,6 +558,7 @@ function getEstimateGasLimit({ provider, signer }: ToolboxWrapParams) {
547
558
  funcName?: string;
548
559
  funcParams?: unknown[];
549
560
  txOverrides?: EVMTxParams;
561
+ data?: string;
550
562
  }) {
551
563
  // const value = assetValue.getBaseValue("bigint");
552
564
  const value = assetValue.bigIntValue;
@@ -573,7 +585,7 @@ function getEstimateGasLimit({ provider, signer }: ToolboxWrapParams) {
573
585
  from: sender,
574
586
  to: recipient,
575
587
  value,
576
- data: memo ? hexlify(toUtf8Bytes(memo)) : undefined,
588
+ data: data ? data : memo ? hexlify(toUtf8Bytes(memo)) : undefined,
577
589
  });
578
590
  };
579
591
  }
@@ -1,4 +1,4 @@
1
- import { Chain, type EVMChain } from "@swapkit/helpers";
1
+ import { Chain, type EVMChain, SwapKitError } from "@swapkit/helpers";
2
2
 
3
3
  import { getProvider } from "../helpers";
4
4
  import type { EVMToolboxParams } from "../types";
@@ -27,7 +27,7 @@ export async function getEvmToolbox<T extends EVMChain>(chain: T, params?: EVMTo
27
27
  case Chain.Ethereum:
28
28
  return ETHToolbox(toolboxParams);
29
29
  default:
30
- throw new Error(`Chain ${chain} is not supported`);
30
+ throw new SwapKitError("toolbox_evm_not_supported", { chain });
31
31
  }
32
32
  }
33
33
 
@@ -1,5 +1,18 @@
1
- import { BaseDecimal, Chain, ChainId, ChainToExplorerUrl, SKConfig } from "@swapkit/helpers";
2
- import type { BrowserProvider, JsonRpcProvider, Provider, TransactionRequest } from "ethers";
1
+ import {
2
+ BaseDecimal,
3
+ Chain,
4
+ ChainId,
5
+ ChainToExplorerUrl,
6
+ SKConfig,
7
+ SwapKitError,
8
+ } from "@swapkit/helpers";
9
+ import type {
10
+ Authorization,
11
+ BrowserProvider,
12
+ JsonRpcProvider,
13
+ Provider,
14
+ TransactionRequest,
15
+ } from "ethers";
3
16
  import { Contract, HDNodeWallet } from "ethers";
4
17
 
5
18
  import { P } from "ts-pattern";
@@ -32,14 +45,15 @@ function serializeTx<P extends JsonRpcProvider | BrowserProvider>(provider: P) {
32
45
  return async function serializeTx({ from, to, nonce, ...tx }: TransactionRequest) {
33
46
  const { Transaction } = await import("ethers");
34
47
 
35
- if (!to) throw new Error("Missing to address");
36
- const txParams = {
48
+ if (!to)
49
+ throw new SwapKitError("toolbox_evm_invalid_transaction", { error: "Missing to address" });
50
+
51
+ return Transaction.from({
37
52
  ...tx,
53
+ authorizationList: tx.authorizationList as Authorization[],
38
54
  to: to as string,
39
55
  nonce: nonce ? nonce : from ? await provider.getTransactionCount(from) : 0,
40
- };
41
-
42
- return Transaction.from(txParams).serialized;
56
+ }).serialized;
43
57
  };
44
58
  }
45
59
 
package/src/index.ts CHANGED
@@ -6,15 +6,18 @@ import {
6
6
  FeeOption,
7
7
  type GenericCreateTransactionParams,
8
8
  type SubstrateChain,
9
+ SwapKitError,
9
10
  type UTXOChain,
10
11
  } from "@swapkit/helpers";
11
12
  import type { getCosmosToolbox } from "@swapkit/toolboxes/cosmos";
12
13
 
13
14
  import type { ETHToolbox, EVMCreateTransactionParams, getEvmToolbox } from "@swapkit/toolboxes/evm";
15
+ import type { getNearToolbox } from "@swapkit/toolboxes/near";
14
16
  import type { RadixToolbox } from "@swapkit/toolboxes/radix";
15
17
  import type { getRippleToolbox } from "@swapkit/toolboxes/ripple";
16
18
  import type { SolanaCreateTransactionParams, getSolanaToolbox } from "@swapkit/toolboxes/solana";
17
19
  import type { getSubstrateToolbox } from "@swapkit/toolboxes/substrate";
20
+ import type { createTronToolbox } from "@swapkit/toolboxes/tron";
18
21
  import type { getUtxoToolbox } from "@swapkit/toolboxes/utxo";
19
22
 
20
23
  export async function getAddressValidator() {
@@ -24,11 +27,14 @@ export async function getAddressValidator() {
24
27
  const { substrateValidateAddress } = await import("@swapkit/toolboxes/substrate");
25
28
  const { getUTXOAddressValidator } = await import("@swapkit/toolboxes/utxo");
26
29
  const { getSolanaAddressValidator } = await import("@swapkit/toolboxes/solana");
30
+ const { validateNearAddress } = await import("@swapkit/toolboxes/near");
27
31
  const { rippleValidateAddress } = await import("@swapkit/toolboxes/ripple");
28
32
  const { radixValidateAddress } = await import("@swapkit/toolboxes/radix");
33
+ const { getTronAddressValidator } = await import("@swapkit/toolboxes/tron");
29
34
 
30
35
  const solanaValidateAddress = await getSolanaAddressValidator();
31
36
  const utxoValidateAddress = await getUTXOAddressValidator();
37
+ const tronValidateAddress = await getTronAddressValidator();
32
38
 
33
39
  return function validateAddress({ address, chain }: { address: string; chain: Chain }) {
34
40
  const isValid = match(chain)
@@ -52,68 +58,62 @@ export async function getAddressValidator() {
52
58
  substrateValidateAddress({ address, chain: chain as SubstrateChain }),
53
59
  )
54
60
  .with(Chain.Radix, () => radixValidateAddress(address))
61
+ .with(Chain.Near, () => validateNearAddress(address))
55
62
  .with(Chain.Ripple, () => rippleValidateAddress(address))
56
63
  .with(Chain.Solana, () => solanaValidateAddress(address))
64
+ .with(Chain.Tron, () => tronValidateAddress(address))
57
65
  .otherwise(() => false);
58
66
 
59
67
  return isValid;
60
68
  };
61
69
  }
62
70
 
63
- export async function getFeeEstimator<T extends keyof CreateTransactionParams>(chain: T) {
64
- const toolbox = await getToolbox(chain);
65
-
71
+ export function getFeeEstimator<T extends keyof CreateTransactionParams>(chain: T) {
66
72
  return async function estimateFee(params: CreateTransactionParams[T]) {
67
- switch (chain) {
68
- case Chain.Arbitrum:
69
- case Chain.Avalanche:
70
- case Chain.Optimism:
71
- case Chain.BinanceSmartChain:
72
- case Chain.Base:
73
- case Chain.Polygon:
74
- case Chain.Ethereum: {
75
- const txObject = await (
76
- toolbox as Awaited<ReturnType<typeof ETHToolbox>>
77
- ).createTransaction(params as EVMCreateTransactionParams);
78
- return (toolbox as Awaited<ReturnType<typeof ETHToolbox>>).estimateTransactionFee({
79
- ...txObject,
80
- feeOption: params.feeOptionKey || FeeOption.Fast,
81
- chain,
82
- });
83
- }
84
- case Chain.Bitcoin:
85
- case Chain.BitcoinCash:
86
- case Chain.Dogecoin:
87
- case Chain.Dash:
88
- case Chain.Litecoin: {
89
- return (toolbox as Awaited<ReturnType<typeof getUtxoToolbox>>).estimateTransactionFee(
90
- params as CreateTransactionParams[Chain.Bitcoin],
91
- );
92
- }
73
+ const { match } = await import("ts-pattern");
93
74
 
94
- case Chain.THORChain:
95
- case Chain.Maya:
96
- case Chain.Kujira:
97
- case Chain.Cosmos: {
75
+ return match(chain as Chain)
76
+ .returnType<Promise<AssetValue>>()
77
+ .with(
78
+ Chain.Arbitrum,
79
+ Chain.Avalanche,
80
+ Chain.Optimism,
81
+ Chain.BinanceSmartChain,
82
+ Chain.Base,
83
+ Chain.Polygon,
84
+ Chain.Ethereum,
85
+ async (chain) => {
86
+ const toolbox = await getToolbox(chain);
87
+ const txObject = await toolbox.createTransaction(params);
88
+
89
+ return (toolbox as Awaited<ReturnType<typeof ETHToolbox>>).estimateTransactionFee({
90
+ ...txObject,
91
+ feeOption: params.feeOptionKey || FeeOption.Fast,
92
+ chain,
93
+ });
94
+ },
95
+ )
96
+ .with(
97
+ Chain.Bitcoin,
98
+ Chain.BitcoinCash,
99
+ Chain.Dogecoin,
100
+ Chain.Dash,
101
+ Chain.Litecoin,
102
+ Chain.Polkadot,
103
+ Chain.Solana,
104
+ Chain.Ripple,
105
+ Chain.Tron,
106
+ Chain.Near,
107
+ async (chain) => {
108
+ const toolbox = await getToolbox(chain);
109
+ return toolbox.estimateTransactionFee(params) as Promise<AssetValue>;
110
+ },
111
+ )
112
+ .with(Chain.THORChain, Chain.Maya, Chain.Kujira, Chain.Cosmos, async () => {
98
113
  const { estimateTransactionFee } = await import("@swapkit/toolboxes/cosmos");
99
114
  return estimateTransactionFee(params);
100
- }
101
-
102
- case Chain.Polkadot: {
103
- return (
104
- toolbox as Awaited<ReturnType<typeof getSubstrateToolbox<Chain.Polkadot>>>
105
- ).estimateTransactionFee(params);
106
- }
107
-
108
- case Chain.Solana: {
109
- return (toolbox as Awaited<ReturnType<typeof getSolanaToolbox>>).estimateTransactionFee(
110
- params as CreateTransactionParams[Chain.Solana],
111
- );
112
- }
113
-
114
- default:
115
- return AssetValue.from({ chain });
116
- }
115
+ })
116
+ .otherwise(async () => AssetValue.from({ chain }));
117
117
  };
118
118
  }
119
119
 
@@ -127,8 +127,10 @@ type Toolboxes = {
127
127
  [key in SubstrateChain]: Awaited<ReturnType<typeof getSubstrateToolbox>>;
128
128
  } & {
129
129
  [Chain.Radix]: Awaited<ReturnType<typeof RadixToolbox>>;
130
+ [Chain.Near]: Awaited<ReturnType<typeof getNearToolbox>>;
130
131
  [Chain.Ripple]: Awaited<ReturnType<typeof getRippleToolbox>>;
131
132
  [Chain.Solana]: Awaited<ReturnType<typeof getSolanaToolbox>>;
133
+ [Chain.Tron]: Awaited<ReturnType<typeof createTronToolbox>>;
132
134
  };
133
135
 
134
136
  type ToolboxParams = { [key in EVMChain]: Parameters<typeof getEvmToolbox>[1] } & {
@@ -139,8 +141,10 @@ type ToolboxParams = { [key in EVMChain]: Parameters<typeof getEvmToolbox>[1] }
139
141
  [key in SubstrateChain]: Parameters<typeof getSubstrateToolbox>[1];
140
142
  } & {
141
143
  [Chain.Radix]: Parameters<typeof RadixToolbox>[0];
144
+ [Chain.Near]: Parameters<typeof getNearToolbox>[0];
142
145
  [Chain.Ripple]: Parameters<typeof getRippleToolbox>[0];
143
146
  [Chain.Solana]: Parameters<typeof getSolanaToolbox>[0];
147
+ [Chain.Tron]: Parameters<typeof createTronToolbox>[0];
144
148
  };
145
149
 
146
150
  type CreateTransactionParams = { [key in EVMChain]: EVMCreateTransactionParams } & {
@@ -151,85 +155,99 @@ type CreateTransactionParams = { [key in EVMChain]: EVMCreateTransactionParams }
151
155
  [key in SubstrateChain]: GenericCreateTransactionParams;
152
156
  } & {
153
157
  [Chain.Radix]: GenericCreateTransactionParams;
158
+ [Chain.Ripple]: GenericCreateTransactionParams;
159
+ [Chain.Near]: GenericCreateTransactionParams;
154
160
  [Chain.Solana]: SolanaCreateTransactionParams;
161
+ [Chain.Tron]: GenericCreateTransactionParams;
155
162
  };
156
163
 
157
164
  export async function getToolbox<T extends keyof Toolboxes>(
158
165
  chain: T,
159
166
  params?: ToolboxParams[T],
160
167
  ): Promise<Toolboxes[T]> {
161
- switch (chain) {
162
- case Chain.Arbitrum:
163
- case Chain.Avalanche:
164
- case Chain.Optimism:
165
- case Chain.BinanceSmartChain:
166
- case Chain.Base:
167
- case Chain.Polygon:
168
- case Chain.Ethereum: {
169
- const { getEvmToolbox } = await import("@swapkit/toolboxes/evm");
170
- const evmToolbox = await getEvmToolbox(chain, params as Parameters<typeof getEvmToolbox>[1]);
171
- return evmToolbox as Toolboxes[T];
172
- }
173
-
174
- case Chain.Litecoin:
175
- case Chain.Dash:
176
- case Chain.Dogecoin:
177
- case Chain.BitcoinCash:
178
- case Chain.Bitcoin: {
179
- const { getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
180
- const utxoToolbox = await getUtxoToolbox(
181
- chain,
182
- params as Parameters<typeof getUtxoToolbox>[1],
183
- );
184
- return utxoToolbox as Toolboxes[T];
185
- }
168
+ const { match } = await import("ts-pattern");
186
169
 
187
- case Chain.Cosmos:
188
- case Chain.Kujira:
189
- case Chain.Maya:
190
- case Chain.THORChain: {
170
+ return match(chain as Chain)
171
+ .returnType<Promise<Toolboxes[T]>>()
172
+ .with(
173
+ Chain.Arbitrum,
174
+ Chain.Avalanche,
175
+ Chain.Optimism,
176
+ Chain.BinanceSmartChain,
177
+ Chain.Base,
178
+ Chain.Polygon,
179
+ Chain.Ethereum,
180
+ async () => {
181
+ const { getEvmToolbox } = await import("@swapkit/toolboxes/evm");
182
+ const evmToolbox = await getEvmToolbox(
183
+ chain as EVMChain,
184
+ params as Parameters<typeof getEvmToolbox>[1],
185
+ );
186
+ return evmToolbox as Toolboxes[T];
187
+ },
188
+ )
189
+ .with(
190
+ Chain.Litecoin,
191
+ Chain.Dash,
192
+ Chain.Dogecoin,
193
+ Chain.BitcoinCash,
194
+ Chain.Bitcoin,
195
+ Chain.Zcash,
196
+ async () => {
197
+ const { getUtxoToolbox } = await import("@swapkit/toolboxes/utxo");
198
+ const utxoToolbox = await getUtxoToolbox(
199
+ chain as UTXOChain,
200
+ params as Parameters<typeof getUtxoToolbox>[1],
201
+ );
202
+ return utxoToolbox as Toolboxes[T];
203
+ },
204
+ )
205
+ .with(Chain.Cosmos, Chain.Kujira, Chain.Maya, Chain.THORChain, async () => {
191
206
  const { getCosmosToolbox } = await import("@swapkit/toolboxes/cosmos");
192
207
  const cosmosToolbox = await getCosmosToolbox(
193
- chain,
208
+ chain as CosmosChain,
194
209
  params as Parameters<typeof getCosmosToolbox>[1],
195
210
  );
196
-
197
211
  return cosmosToolbox as Toolboxes[T];
198
- }
199
-
200
- case Chain.Chainflip:
201
- case Chain.Polkadot: {
212
+ })
213
+ .with(Chain.Chainflip, Chain.Polkadot, async () => {
202
214
  const { getSubstrateToolbox } = await import("@swapkit/toolboxes/substrate");
203
215
  const substrateToolbox = await getSubstrateToolbox(
204
- chain,
216
+ chain as SubstrateChain,
205
217
  params as Parameters<typeof getSubstrateToolbox>[1],
206
218
  );
207
219
  return substrateToolbox as Toolboxes[T];
208
- }
209
-
210
- case Chain.Radix: {
220
+ })
221
+ .with(Chain.Radix, async () => {
211
222
  const { RadixToolbox } = await import("@swapkit/toolboxes/radix");
212
223
  const radixToolbox = await RadixToolbox(params as Parameters<typeof RadixToolbox>[0]);
213
224
  return radixToolbox as Toolboxes[T];
214
- }
215
-
216
- case Chain.Ripple: {
225
+ })
226
+ .with(Chain.Ripple, async () => {
217
227
  const { getRippleToolbox } = await import("@swapkit/toolboxes/ripple");
218
228
  const rippleToolbox = await getRippleToolbox(
219
229
  params as Parameters<typeof getRippleToolbox>[0],
220
230
  );
221
231
  return rippleToolbox as Toolboxes[T];
222
- }
223
-
224
- case Chain.Solana: {
232
+ })
233
+ .with(Chain.Solana, async () => {
225
234
  const { getSolanaToolbox } = await import("@swapkit/toolboxes/solana");
226
235
  const solanaToolbox = await getSolanaToolbox(
227
236
  params as Parameters<typeof getSolanaToolbox>[0],
228
237
  );
229
238
  return solanaToolbox as Toolboxes[T];
230
- }
231
-
232
- default:
233
- throw new Error(`Chain ${chain} is not supported`);
234
- }
239
+ })
240
+ .with(Chain.Tron, async () => {
241
+ const { createTronToolbox } = await import("@swapkit/toolboxes/tron");
242
+ const tronToolbox = await createTronToolbox(params);
243
+ return tronToolbox as Toolboxes[T];
244
+ })
245
+ .with(Chain.Near, async () => {
246
+ const { getNearToolbox } = await import("@swapkit/toolboxes/near");
247
+ const nearToolbox = await getNearToolbox(params as Parameters<typeof getNearToolbox>[0]);
248
+ return nearToolbox as Toolboxes[T];
249
+ })
250
+ .otherwise(() => {
251
+ throw new SwapKitError("toolbox_not_supported", { chain });
252
+ });
235
253
  }
@@ -0,0 +1,80 @@
1
+ import { beforeAll, beforeEach, describe, expect, test } from "bun:test";
2
+ import { AssetValue, Chain, SKConfig } from "@swapkit/helpers";
3
+ import { providers } from "near-api-js";
4
+ import { getFullAccessPublicKey } from "../helpers/core";
5
+ import { getNearToolbox } from "../toolbox";
6
+
7
+ const accountId = "ea03292d08136cca439513a33c76af083e5204eceb4ce720320fff84071a447f";
8
+
9
+ const context: {
10
+ provider: providers.JsonRpcProvider;
11
+ toolbox: Awaited<ReturnType<typeof getNearToolbox>>;
12
+ } = {} as any;
13
+
14
+ beforeAll(() => {
15
+ context.provider = new providers.JsonRpcProvider({
16
+ url: SKConfig.get("rpcUrls")[Chain.Near],
17
+ });
18
+ });
19
+
20
+ beforeEach(async () => {
21
+ context.toolbox = await getNearToolbox();
22
+ });
23
+
24
+ describe("NEAR createTransaction", () => {
25
+ test("should retrieve full access public key for valid account", async () => {
26
+ const toolbox = context.toolbox;
27
+
28
+ const transaction = await toolbox.createTransaction({
29
+ recipient: accountId, // Self transfer
30
+ assetValue: AssetValue.from({
31
+ chain: Chain.Near,
32
+ value: "0.001", // Small amount
33
+ }),
34
+ sender: accountId,
35
+ feeRate: 300000000000000, // 300 TGas
36
+ });
37
+
38
+ expect(transaction).toBeDefined();
39
+ expect(transaction.publicKey).toBeDefined();
40
+ expect(transaction.serialized).toBeDefined();
41
+ }, 30000);
42
+
43
+ test("should throw error for invalid account", async () => {
44
+ const provider = context.provider;
45
+ const invalidAccountId = "non-existent-account-12345.testnet";
46
+
47
+ await expect(async () => {
48
+ await getFullAccessPublicKey(provider, invalidAccountId);
49
+ }).toThrow();
50
+ }, 10000);
51
+
52
+ test("should handle network errors gracefully", async () => {
53
+ const invalidProvider = new providers.JsonRpcProvider({ url: "https://invalid-rpc-url.test" });
54
+
55
+ await expect(async () => {
56
+ await getFullAccessPublicKey(invalidProvider, "any-account.testnet");
57
+ }).toThrow();
58
+ }, 10000);
59
+
60
+ test("should work with contract function call transaction", async () => {
61
+ const toolbox = context.toolbox;
62
+ const provider = context.provider;
63
+
64
+ const contractTransaction = await toolbox.createContractFunctionCall({
65
+ sender: accountId,
66
+ contractId: "wrap.testnet", // Known testnet contract
67
+ methodName: "storage_deposit",
68
+ args: { account_id: accountId },
69
+ gas: "300000000000000", // 300 TGas
70
+ attachedDeposit: "1250000000000000000000", // 0.00125 NEAR for storage
71
+ });
72
+
73
+ expect(contractTransaction).toBeDefined();
74
+ expect(contractTransaction.publicKey).toBeDefined();
75
+
76
+ // Verify the public key can be retrieved directly
77
+ const { publicKey } = await getFullAccessPublicKey(provider, accountId);
78
+ expect(publicKey.toString()).toBe(contractTransaction.publicKey);
79
+ }, 30000);
80
+ });
@@ -0,0 +1,22 @@
1
+ import type { Account, Contract } from "near-api-js";
2
+
3
+ // Create a Near contract instance
4
+ export async function createNearContract<T extends Contract>({
5
+ account,
6
+ contractId,
7
+ viewMethods,
8
+ changeMethods,
9
+ }: {
10
+ account: Account;
11
+ contractId: string;
12
+ viewMethods: string[];
13
+ changeMethods: string[];
14
+ }): Promise<T> {
15
+ const { Contract } = await import("near-api-js");
16
+
17
+ return new Contract(account, contractId, {
18
+ viewMethods,
19
+ changeMethods,
20
+ useLocalViewExecution: true, // Enable local view execution for efficiency
21
+ }) as T;
22
+ }