@swapkit/toolboxes 1.0.0-beta.2 → 1.0.0-beta.21

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 (124) hide show
  1. package/dist/chunk-12xtvbsp.js +4 -0
  2. package/dist/{chunk-0h4xdrwz.js.map → chunk-12xtvbsp.js.map} +2 -2
  3. package/dist/{chunk-fjfxga2v.js → chunk-5yxc1e69.js} +1 -1
  4. package/dist/{chunk-fjfxga2v.js.map → chunk-5yxc1e69.js.map} +1 -1
  5. package/dist/{chunk-0f0249b1.js → chunk-9bqegm61.js} +1 -1
  6. package/dist/chunk-kbnwrc5b.js +4 -0
  7. package/dist/chunk-kbnwrc5b.js.map +10 -0
  8. package/dist/{chunk-p1kdg37m.js → chunk-s47y8512.js} +2 -2
  9. package/dist/{chunk-p1kdg37m.js.map → chunk-s47y8512.js.map} +1 -1
  10. package/dist/chunk-vtd17cje.js +3 -0
  11. package/dist/chunk-vtd17cje.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 +11 -0
  42. package/dist/src/substrate/index.js +3 -0
  43. package/dist/src/substrate/index.js.map +11 -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 +3 -0
  49. package/dist/src/utxo/index.cjs.map +16 -0
  50. package/dist/src/utxo/index.js +3 -0
  51. package/dist/src/utxo/index.js.map +16 -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 +16 -11
  57. package/src/cosmos/toolbox/index.ts +2 -2
  58. package/src/cosmos/toolbox/thorchain.ts +11 -9
  59. package/src/cosmos/util.ts +13 -6
  60. package/src/evm/__tests__/ethereum.test.ts +1 -1
  61. package/src/evm/helpers.ts +4 -3
  62. package/src/evm/toolbox/baseEVMToolbox.ts +37 -25
  63. package/src/evm/toolbox/index.ts +2 -2
  64. package/src/evm/toolbox/op.ts +21 -7
  65. package/src/index.ts +117 -100
  66. package/src/near/helpers/contractFactory.ts +22 -0
  67. package/src/near/helpers/core.ts +86 -0
  68. package/src/near/helpers/gasEstimation.ts +110 -0
  69. package/src/near/helpers/index.ts +5 -0
  70. package/src/near/helpers/nep141.ts +110 -0
  71. package/src/near/index.ts +24 -0
  72. package/src/near/toolbox.ts +497 -0
  73. package/src/near/types/contract.ts +48 -0
  74. package/src/near/types/nep141.ts +66 -0
  75. package/src/near/types.ts +57 -0
  76. package/src/radix/index.ts +8 -2
  77. package/src/ripple/index.ts +15 -26
  78. package/src/solana/toolbox.ts +75 -2
  79. package/src/substrate/substrate.ts +1 -1
  80. package/src/tron/helpers/trc20.abi.ts +40 -0
  81. package/src/tron/index.ts +16 -0
  82. package/src/tron/toolbox.ts +328 -0
  83. package/src/tron/types.ts +22 -0
  84. package/src/utxo/helpers/api.ts +30 -16
  85. package/src/utxo/helpers/bchaddrjs.ts +8 -8
  86. package/src/utxo/helpers/coinselect.ts +2 -2
  87. package/src/utxo/helpers/txSize.ts +4 -3
  88. package/src/utxo/toolbox/bitcoinCash.ts +22 -14
  89. package/src/utxo/toolbox/index.ts +2 -1
  90. package/src/utxo/toolbox/utxo.ts +35 -27
  91. package/src/utxo/types.ts +2 -0
  92. package/dist/chunk-0h4xdrwz.js +0 -4
  93. package/dist/cosmos/index.cjs +0 -3
  94. package/dist/cosmos/index.cjs.map +0 -16
  95. package/dist/cosmos/index.js +0 -3
  96. package/dist/cosmos/index.js.map +0 -16
  97. package/dist/evm/index.cjs +0 -3
  98. package/dist/evm/index.cjs.map +0 -18
  99. package/dist/evm/index.js +0 -3
  100. package/dist/evm/index.js.map +0 -18
  101. package/dist/index.cjs +0 -3
  102. package/dist/index.cjs.map +0 -10
  103. package/dist/index.js +0 -3
  104. package/dist/index.js.map +0 -10
  105. package/dist/radix/index.cjs.map +0 -10
  106. package/dist/radix/index.js +0 -3
  107. package/dist/radix/index.js.map +0 -10
  108. package/dist/ripple/index.cjs +0 -3
  109. package/dist/ripple/index.cjs.map +0 -10
  110. package/dist/ripple/index.js +0 -3
  111. package/dist/ripple/index.js.map +0 -10
  112. package/dist/solana/index.cjs +0 -3
  113. package/dist/solana/index.cjs.map +0 -10
  114. package/dist/solana/index.js +0 -3
  115. package/dist/solana/index.js.map +0 -10
  116. package/dist/substrate/index.cjs +0 -3
  117. package/dist/substrate/index.cjs.map +0 -11
  118. package/dist/substrate/index.js +0 -3
  119. package/dist/substrate/index.js.map +0 -11
  120. package/dist/utxo/index.cjs +0 -3
  121. package/dist/utxo/index.cjs.map +0 -16
  122. package/dist/utxo/index.js +0 -3
  123. package/dist/utxo/index.js.map +0 -16
  124. /package/dist/{chunk-0f0249b1.js.map → chunk-9bqegm61.js.map} +0 -0
@@ -1,4 +1,5 @@
1
1
  import { base64, bech32 } from "@scure/base";
2
+ import { SwapKitError } from "@swapkit/helpers";
2
3
  import { fromByteArray, toByteArray } from "base64-js";
3
4
 
4
5
  export function bech32ToBase64(address: string) {
@@ -17,7 +18,9 @@ export function toBase64(data: Uint8Array) {
17
18
 
18
19
  export function fromBase64(base64String: string) {
19
20
  if (!base64String.match(/^[a-zA-Z0-9+/]*={0,2}$/)) {
20
- throw new Error("Invalid base64 string format");
21
+ throw new SwapKitError("toolbox_cosmos_invalid_params", {
22
+ error: "Invalid base64 string format",
23
+ });
21
24
  }
22
25
  return toByteArray(base64String);
23
26
  }
@@ -1,5 +1,5 @@
1
1
  import type { TxBodyEncodeObject } from "@cosmjs/proto-signing";
2
- import { AssetValue, Chain, ChainToChainId } from "@swapkit/helpers";
2
+ import { AssetValue, Chain, ChainToChainId, SwapKitError } from "@swapkit/helpers";
3
3
 
4
4
  import {
5
5
  createStargateClient,
@@ -104,7 +104,7 @@ const getAccount = async ({ rpcUrl, sender }: { sender: string; rpcUrl: string }
104
104
  const account = await client.getAccount(sender);
105
105
 
106
106
  if (!account) {
107
- throw new Error("Account does not exist");
107
+ throw new SwapKitError("toolbox_cosmos_account_not_found", { sender });
108
108
  }
109
109
 
110
110
  return account;
@@ -3,8 +3,8 @@ import { base64ToBech32, bech32ToBase64 } from "./addressFormat";
3
3
 
4
4
  export async function createDefaultRegistry() {
5
5
  const { $root } = await import("./types/MsgCompiled");
6
- const { Registry } = await import("@cosmjs/proto-signing");
7
- const { defaultRegistryTypes } = await import("@cosmjs/stargate");
6
+ const { Registry } = (await import("@cosmjs/proto-signing")).default;
7
+ const { defaultRegistryTypes } = (await import("@cosmjs/stargate")).default;
8
8
 
9
9
  return new Registry([
10
10
  ...defaultRegistryTypes,
@@ -14,7 +14,7 @@ export async function createDefaultRegistry() {
14
14
  }
15
15
 
16
16
  export async function createDefaultAminoTypes(chain: Chain.THORChain | Chain.Maya) {
17
- const { AminoTypes } = await import("@cosmjs/stargate");
17
+ const { AminoTypes } = (await import("@cosmjs/stargate")).default;
18
18
  const aminoTypePrefix = chain === Chain.THORChain ? "thorchain" : "mayachain";
19
19
 
20
20
  return new AminoTypes({
@@ -17,6 +17,7 @@ import {
17
17
  SKConfig,
18
18
  SwapKitError,
19
19
  SwapKitNumber,
20
+ applyFeeMultiplier,
20
21
  derivationPathToString,
21
22
  updateDerivationPath,
22
23
  } from "@swapkit/helpers";
@@ -38,8 +39,7 @@ export async function fetchFeeRateFromSwapKit(chainId: ChainId, safeDefault: num
38
39
  const responseGasRate = response.find((gas) => gas.chainId === chainId)?.value;
39
40
 
40
41
  return responseGasRate ? Number.parseFloat(responseGasRate) : safeDefault;
41
- } catch (e) {
42
- console.error(e);
42
+ } catch (_e) {
43
43
  return safeDefault;
44
44
  }
45
45
  }
@@ -52,8 +52,8 @@ export async function getSignerFromPhrase({
52
52
  | { chain: Chain; index?: number }
53
53
  | { derivationPath: string }
54
54
  )) {
55
- const { DirectSecp256k1HdWallet } = await import("@cosmjs/proto-signing");
56
- const { stringToPath } = await import("@cosmjs/crypto");
55
+ const { DirectSecp256k1HdWallet } = (await import("@cosmjs/proto-signing")).default;
56
+ const { stringToPath } = (await import("@cosmjs/crypto")).default;
57
57
 
58
58
  const derivationPath =
59
59
  "derivationPath" in derivationParams
@@ -73,7 +73,7 @@ export async function getSignerFromPrivateKey({
73
73
  privateKey: Uint8Array;
74
74
  prefix: string;
75
75
  }) {
76
- const { DirectSecp256k1Wallet } = await import("@cosmjs/proto-signing");
76
+ const { DirectSecp256k1Wallet } = (await import("@cosmjs/proto-signing")).default;
77
77
 
78
78
  return DirectSecp256k1Wallet.fromKey(privateKey, prefix);
79
79
  }
@@ -97,7 +97,7 @@ export function verifySignature(getAccount: (address: string) => Promise<Account
97
97
  }) {
98
98
  const account = await getAccount(address);
99
99
  if (!account?.pubkey) throw new SwapKitError("toolbox_cosmos_verify_signature_no_pubkey");
100
- const { Secp256k1Signature, Secp256k1 } = await import("@cosmjs/crypto");
100
+ const { Secp256k1Signature, Secp256k1 } = (await import("@cosmjs/crypto")).default;
101
101
 
102
102
  const secpSignature = Secp256k1Signature.fromFixedLength(base64.decode(signature));
103
103
  return Secp256k1.verifySignature(secpSignature, base64.decode(message), account.pubkey.value);
@@ -197,7 +197,7 @@ export async function createCosmosToolbox({ chain, ...toolboxParams }: CosmosToo
197
197
  index,
198
198
  }),
199
199
  getSignerFromPrivateKey: async (privateKey: Uint8Array) => {
200
- const { DirectSecp256k1Wallet } = await import("@cosmjs/proto-signing");
200
+ const { DirectSecp256k1Wallet } = (await import("@cosmjs/proto-signing")).default;
201
201
  return DirectSecp256k1Wallet.fromKey(privateKey, chainPrefix);
202
202
  },
203
203
  createPrivateKeyFromPhrase: createPrivateKeyFromPhrase(derivationPath),
@@ -217,8 +217,7 @@ export async function getFeeRateFromThorswap(chainId: ChainId, safeDefault: numb
217
217
  const responseGasRate = response.find((gas) => gas.chainId === chainId)?.value;
218
218
 
219
219
  return responseGasRate ? Number.parseFloat(responseGasRate) : safeDefault;
220
- } catch (e) {
221
- console.error(e);
220
+ } catch (_e) {
222
221
  return safeDefault;
223
222
  }
224
223
  }
@@ -262,8 +261,14 @@ async function getFees(chain: Chain, safeDefault: number) {
262
261
  const baseFee = await fetchFeeRateFromSwapKit(ChainToChainId[chain], safeDefault);
263
262
  return {
264
263
  average: SwapKitNumber.fromBigInt(BigInt(baseFee), BaseDecimal[chain]),
265
- fast: SwapKitNumber.fromBigInt(BigInt(Math.floor(baseFee * 1.5)), BaseDecimal[chain]),
266
- fastest: SwapKitNumber.fromBigInt(BigInt(Math.floor(baseFee * 2)), BaseDecimal[chain]),
264
+ fast: SwapKitNumber.fromBigInt(
265
+ BigInt(applyFeeMultiplier(baseFee, FeeOption.Fast, true)),
266
+ BaseDecimal[chain],
267
+ ),
268
+ fastest: SwapKitNumber.fromBigInt(
269
+ BigInt(applyFeeMultiplier(baseFee, FeeOption.Fastest, true)),
270
+ BaseDecimal[chain],
271
+ ),
267
272
  } as { [key in FeeOption]: SwapKitNumber };
268
273
  }
269
274
 
@@ -1,4 +1,4 @@
1
- import { Chain, type CosmosChain } from "@swapkit/helpers";
1
+ import { Chain, type CosmosChain, SwapKitError } from "@swapkit/helpers";
2
2
 
3
3
  import type { CosmosToolboxParams } from "../types";
4
4
  import { createCosmosToolbox } from "./cosmos";
@@ -25,7 +25,7 @@ export const getCosmosToolbox = <T extends CosmosChain>(
25
25
  return createThorchainToolbox({ chain, ...params }) as CosmosToolboxes[T];
26
26
 
27
27
  default:
28
- throw new Error(`Chain ${chain} is not supported`);
28
+ throw new SwapKitError("toolbox_cosmos_not_supported", { chain });
29
29
  }
30
30
  };
31
31
 
@@ -9,6 +9,7 @@ import {
9
9
  NetworkDerivationPath,
10
10
  RequestClient,
11
11
  SKConfig,
12
+ SwapKitError,
12
13
  SwapKitNumber,
13
14
  derivationPathToString,
14
15
  updateDerivationPath,
@@ -43,8 +44,8 @@ function secp256k1HdWalletFromMnemonic({
43
44
  derivationPath?: string;
44
45
  }) {
45
46
  return async function secp256k1HdWalletFromMnemonic(mnemonic: string, index = 0) {
46
- const { Secp256k1HdWallet } = await import("@cosmjs/amino");
47
- const { stringToPath } = await import("@cosmjs/crypto");
47
+ const { Secp256k1HdWallet } = (await import("@cosmjs/amino")).default;
48
+ const { stringToPath } = (await import("@cosmjs/crypto")).default;
48
49
 
49
50
  return Secp256k1HdWallet.fromMnemonic(mnemonic, {
50
51
  hdPaths: [stringToPath(`${derivationPath}/${index}`)],
@@ -114,8 +115,8 @@ function broadcastMultisigTx({
114
115
  threshold: number,
115
116
  bodyBytes: Uint8Array,
116
117
  ) {
117
- const { encodeSecp256k1Pubkey, pubkeyToAddress } = await import("@cosmjs/amino");
118
- const { makeMultisignedTxBytes } = await import("@cosmjs/stargate");
118
+ const { encodeSecp256k1Pubkey, pubkeyToAddress } = (await import("@cosmjs/amino")).default;
119
+ const { makeMultisignedTxBytes } = (await import("@cosmjs/stargate")).default;
119
120
 
120
121
  const { sequence, fee } = JSON.parse(tx);
121
122
  const multisigPubkey = await createMultisig(membersPubKeys, threshold);
@@ -142,7 +143,8 @@ function broadcastMultisigTx({
142
143
  }
143
144
 
144
145
  async function createMultisig(pubKeys: string[], threshold: number, noSortPubKeys = true) {
145
- const { createMultisigThresholdPubkey, encodeSecp256k1Pubkey } = await import("@cosmjs/amino");
146
+ const { createMultisigThresholdPubkey, encodeSecp256k1Pubkey } = (await import("@cosmjs/amino"))
147
+ .default;
146
148
  return createMultisigThresholdPubkey(
147
149
  pubKeys.map((pubKey) => encodeSecp256k1Pubkey(base64.decode(pubKey))),
148
150
  threshold,
@@ -161,7 +163,7 @@ async function signWithPrivateKey({
161
163
  privateKey: Uint8Array;
162
164
  message: string;
163
165
  }) {
164
- const { Secp256k1 } = await import("@cosmjs/crypto");
166
+ const { Secp256k1 } = (await import("@cosmjs/crypto")).default;
165
167
 
166
168
  const signature = await Secp256k1.createSignature(base64.decode(message), privateKey);
167
169
  return base64.encode(Buffer.concat([signature.r(32), signature.s(32)]));
@@ -209,7 +211,7 @@ export async function createThorchainToolbox({
209
211
  } = await RequestClient.get<ThorchainConstantsResponse>(constantsUrl);
210
212
 
211
213
  if (!nativeFee || Number.isNaN(nativeFee) || nativeFee < 0) {
212
- throw new Error(`Invalid nativeFee: ${nativeFee.toString()}`);
214
+ throw new SwapKitError("toolbox_cosmos_invalid_fee", { nativeFee: nativeFee.toString() });
213
215
  }
214
216
 
215
217
  fee = new SwapKitNumber(nativeFee);
@@ -230,7 +232,7 @@ export async function createThorchainToolbox({
230
232
  }: Omit<GenericTransferParams, "recipient"> & { recipient?: string }) {
231
233
  const { TxRaw } = await import("cosmjs-types/cosmos/tx/v1beta1/tx");
232
234
  const sender = (await signer?.getAccounts())?.[0]?.address;
233
- if (!(sender && signer)) throw new Error("Signer not defined");
235
+ if (!(sender && signer)) throw new SwapKitError("toolbox_cosmos_no_signer");
234
236
 
235
237
  const isAminoSigner = "signAmino" in signer;
236
238
  const registry = await createDefaultRegistry();
@@ -301,7 +303,7 @@ export async function createThorchainToolbox({
301
303
  signWithPrivateKey,
302
304
  transfer,
303
305
  pubkeyToAddress: async (pubkey: Pubkey) => {
304
- const { pubkeyToAddress } = await import("@cosmjs/amino");
306
+ const { pubkeyToAddress } = (await import("@cosmjs/amino")).default;
305
307
  return pubkeyToAddress(pubkey, chainPrefix);
306
308
  },
307
309
  };
@@ -1,6 +1,13 @@
1
1
  import type { OfflineSigner } from "@cosmjs/proto-signing";
2
2
  import type { SigningStargateClientOptions } from "@cosmjs/stargate";
3
- import { AssetValue, Chain, ChainId, type CosmosChain, SKConfig } from "@swapkit/helpers";
3
+ import {
4
+ AssetValue,
5
+ Chain,
6
+ ChainId,
7
+ type CosmosChain,
8
+ SKConfig,
9
+ SwapKitError,
10
+ } from "@swapkit/helpers";
4
11
 
5
12
  import type { CosmosCreateTransactionParams } from "./thorchainUtils";
6
13
 
@@ -69,7 +76,7 @@ export const getDenomWithChain = ({ symbol, chain }: AssetValue) => {
69
76
  };
70
77
 
71
78
  export async function createStargateClient(url: string) {
72
- const { StargateClient } = await import("@cosmjs/stargate");
79
+ const { StargateClient } = (await import("@cosmjs/stargate")).default;
73
80
 
74
81
  return StargateClient.connect(url);
75
82
  }
@@ -79,7 +86,7 @@ export async function createSigningStargateClient(
79
86
  signer: any,
80
87
  optionsOrBaseGas: string | SigningStargateClientOptions = {},
81
88
  ) {
82
- const { SigningStargateClient, GasPrice } = await import("@cosmjs/stargate");
89
+ const { SigningStargateClient, GasPrice } = (await import("@cosmjs/stargate")).default;
83
90
  const gasPrice = typeof optionsOrBaseGas === "string" ? optionsOrBaseGas : "0.0003uatom";
84
91
  const options = typeof optionsOrBaseGas === "string" ? {} : optionsOrBaseGas;
85
92
 
@@ -93,7 +100,7 @@ export async function createOfflineStargateClient(
93
100
  wallet: OfflineSigner,
94
101
  registry?: SigningStargateClientOptions,
95
102
  ) {
96
- const { SigningStargateClient } = await import("@cosmjs/stargate");
103
+ const { SigningStargateClient } = (await import("@cosmjs/stargate")).default;
97
104
 
98
105
  return SigningStargateClient.offline(wallet, registry);
99
106
  }
@@ -126,7 +133,7 @@ const getTransferMsgTypeByChain = (chain: CosmosChain) => {
126
133
  case Chain.Kujira:
127
134
  return "/cosmos.bank.v1beta1.MsgSend";
128
135
  default:
129
- throw new Error("Unsupported chain");
136
+ throw new SwapKitError("toolbox_cosmos_not_supported", { chain });
130
137
  }
131
138
  };
132
139
 
@@ -149,7 +156,7 @@ export const cosmosCreateTransaction = async ({
149
156
  const accountOnChain = await client.getAccount(sender);
150
157
 
151
158
  if (!accountOnChain) {
152
- throw new Error("Account does not exist");
159
+ throw new SwapKitError("toolbox_cosmos_account_not_found", { sender });
153
160
  }
154
161
 
155
162
  const gasAsset = AssetValue.from({ chain });
@@ -35,7 +35,7 @@ beforeEach(async () => {
35
35
  },
36
36
  });
37
37
  context.provider = provider;
38
- context.toolbox = await getEvmToolbox(Chain.Ethereum, { provider, signer });
38
+ context.toolbox = await getEvmToolbox(Chain.Ethereum, { provider, signer: signer as any });
39
39
  });
40
40
 
41
41
  afterEach(async () => {
@@ -8,6 +8,7 @@ import {
8
8
  FeeOption,
9
9
  type NetworkParams,
10
10
  SKConfig,
11
+ SwapKitError,
11
12
  SwapKitNumber,
12
13
  } from "@swapkit/helpers";
13
14
  import type { BrowserProvider, Provider } from "ethers";
@@ -71,7 +72,7 @@ export const estimateMaxSendableAmount = async ({
71
72
  const isFeeEVMLegacyCompatible = "gasPrice" in gasRate && gasRate.gasPrice !== undefined;
72
73
 
73
74
  if (!(gasRate && (isFeeEVMLegacyCompatible || isFeeEIP1559Compatible))) {
74
- throw new Error("Could not fetch fee data");
75
+ throw new SwapKitError("toolbox_evm_no_fee_data");
75
76
  }
76
77
 
77
78
  const gasPrice = isFeeEIP1559Compatible
@@ -116,7 +117,7 @@ export function getEstimateTransactionFee({
116
117
  return assetValue.set(SwapKitNumber.fromBigInt(fee, assetValue.decimal));
117
118
  }
118
119
 
119
- throw new Error("No gas price found");
120
+ throw new SwapKitError("toolbox_evm_no_gas_price");
120
121
  };
121
122
  }
122
123
 
@@ -168,6 +169,6 @@ function getNetworkInfo<C extends EVMChain>({ chain }: { chain: C }) {
168
169
  nativeCurrency: { name: "Polygon", symbol: Chain.Polygon, decimals },
169
170
  };
170
171
  default:
171
- throw new Error(`Chain ${chain} is not supported`);
172
+ throw new SwapKitError("toolbox_evm_not_supported", { chain });
172
173
  }
173
174
  }
@@ -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