@swapkit/toolboxes 1.0.0-beta.0 → 1.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 (103) hide show
  1. package/dist/chunk-0f0249b1.js +3 -0
  2. package/dist/chunk-0f0249b1.js.map +10 -0
  3. package/dist/chunk-0h4xdrwz.js +4 -0
  4. package/dist/chunk-0h4xdrwz.js.map +10 -0
  5. package/dist/chunk-4yap1fvd.js +3 -0
  6. package/dist/chunk-4yap1fvd.js.map +10 -0
  7. package/dist/chunk-fjfxga2v.js +3 -0
  8. package/dist/chunk-fjfxga2v.js.map +10 -0
  9. package/dist/{chunk-tvrdndbw.js → chunk-p1kdg37m.js} +2 -2
  10. package/dist/{chunk-tvrdndbw.js.map → chunk-p1kdg37m.js.map} +1 -1
  11. package/dist/cosmos/index.cjs +2 -2
  12. package/dist/cosmos/index.cjs.map +10 -13
  13. package/dist/cosmos/index.js +2 -2
  14. package/dist/cosmos/index.js.map +10 -13
  15. package/dist/evm/index.cjs +2 -2
  16. package/dist/evm/index.cjs.map +10 -16
  17. package/dist/evm/index.js +2 -2
  18. package/dist/evm/index.js.map +10 -16
  19. package/dist/index.cjs +2 -2
  20. package/dist/index.cjs.map +4 -3
  21. package/dist/index.js +2 -2
  22. package/dist/index.js.map +4 -3
  23. package/dist/radix/index.cjs +2 -2
  24. package/dist/radix/index.cjs.map +3 -3
  25. package/dist/radix/index.js +2 -2
  26. package/dist/radix/index.js.map +3 -3
  27. package/dist/ripple/index.cjs +3 -0
  28. package/dist/ripple/index.cjs.map +10 -0
  29. package/dist/ripple/index.js +3 -0
  30. package/dist/ripple/index.js.map +10 -0
  31. package/dist/solana/index.cjs +2 -2
  32. package/dist/solana/index.cjs.map +3 -3
  33. package/dist/solana/index.js +2 -2
  34. package/dist/solana/index.js.map +3 -3
  35. package/dist/substrate/index.cjs +2 -2
  36. package/dist/substrate/index.cjs.map +5 -6
  37. package/dist/substrate/index.js +2 -2
  38. package/dist/substrate/index.js.map +5 -6
  39. package/dist/utxo/index.cjs +2 -2
  40. package/dist/utxo/index.cjs.map +9 -11
  41. package/dist/utxo/index.js +2 -2
  42. package/dist/utxo/index.js.map +9 -11
  43. package/package.json +30 -24
  44. package/src/cosmos/index.ts +2 -9
  45. package/src/cosmos/thorchainUtils/addressFormat.ts +1 -2
  46. package/src/cosmos/thorchainUtils/index.ts +1 -1
  47. package/src/cosmos/thorchainUtils/messages.ts +74 -56
  48. package/src/cosmos/thorchainUtils/registry.ts +16 -23
  49. package/src/cosmos/thorchainUtils/types/{proto/MsgCompiled.ts → MsgCompiled.ts} +1 -3
  50. package/src/cosmos/thorchainUtils/types/client-types.ts +16 -23
  51. package/src/cosmos/toolbox/cosmos.ts +334 -0
  52. package/src/cosmos/toolbox/index.ts +33 -0
  53. package/src/cosmos/toolbox/thorchain.ts +118 -131
  54. package/src/cosmos/types.ts +37 -18
  55. package/src/cosmos/util.ts +21 -71
  56. package/src/evm/__tests__/ethereum.test.ts +110 -116
  57. package/src/evm/api.ts +11 -147
  58. package/src/evm/helpers.ts +111 -83
  59. package/src/evm/index.ts +1 -17
  60. package/src/evm/toolbox/baseEVMToolbox.ts +742 -0
  61. package/src/evm/toolbox/evm.ts +69 -0
  62. package/src/evm/toolbox/index.ts +36 -0
  63. package/src/evm/toolbox/op.ts +97 -143
  64. package/src/evm/types.ts +50 -28
  65. package/src/index.ts +235 -0
  66. package/src/radix/index.ts +18 -19
  67. package/src/ripple/index.ts +203 -0
  68. package/src/solana/index.ts +11 -5
  69. package/src/solana/toolbox.ts +223 -133
  70. package/src/substrate/index.ts +2 -3
  71. package/src/substrate/{toolbox/baseSubstrateToolbox.ts → substrate.ts} +104 -72
  72. package/src/substrate/types.ts +120 -0
  73. package/src/utils.ts +27 -0
  74. package/src/utxo/helpers/api.ts +27 -23
  75. package/src/utxo/helpers/bchaddrjs.ts +21 -21
  76. package/src/utxo/helpers/index.ts +0 -1
  77. package/src/utxo/helpers/txSize.ts +3 -4
  78. package/src/utxo/index.ts +3 -7
  79. package/src/utxo/toolbox/bitcoinCash.ts +164 -154
  80. package/src/utxo/toolbox/index.ts +63 -24
  81. package/src/utxo/toolbox/utxo.ts +376 -229
  82. package/src/utxo/types.ts +24 -39
  83. package/src/cosmos/thorchainUtils/types/proto/MsgCompiled.js +0 -2806
  84. package/src/cosmos/thorchainUtils/util.ts +0 -46
  85. package/src/cosmos/toolbox/BaseCosmosToolbox.ts +0 -254
  86. package/src/cosmos/toolbox/gaia.ts +0 -39
  87. package/src/cosmos/toolbox/getToolboxByChain.ts +0 -29
  88. package/src/cosmos/toolbox/kujira.ts +0 -61
  89. package/src/evm/provider.ts +0 -6
  90. package/src/evm/toolbox/EVMToolbox.ts +0 -662
  91. package/src/evm/toolbox/arb.ts +0 -61
  92. package/src/evm/toolbox/avax.ts +0 -36
  93. package/src/evm/toolbox/base.ts +0 -42
  94. package/src/evm/toolbox/bsc.ts +0 -34
  95. package/src/evm/toolbox/eth.ts +0 -44
  96. package/src/evm/toolbox/getToolboxByChain.ts +0 -42
  97. package/src/evm/toolbox/matic.ts +0 -42
  98. package/src/radix/toolbox.ts +0 -693
  99. package/src/substrate/toolbox/index.ts +0 -40
  100. package/src/substrate/types/index.ts +0 -2
  101. package/src/substrate/types/network.ts +0 -42
  102. package/src/substrate/types/wallet.ts +0 -78
  103. package/src/utxo/helpers/utils.ts +0 -45
@@ -0,0 +1,742 @@
1
+ import {
2
+ type Asset,
3
+ type AssetValue,
4
+ Chain,
5
+ type ChainSigner,
6
+ ContractAddress,
7
+ type EVMChain,
8
+ FeeOption,
9
+ SwapKitError,
10
+ SwapKitNumber,
11
+ isGasAsset,
12
+ } from "@swapkit/helpers";
13
+ import { erc20ABI } from "@swapkit/helpers/contracts";
14
+ import {
15
+ BrowserProvider,
16
+ Contract,
17
+ type ContractTransaction,
18
+ type Fragment,
19
+ type HDNodeWallet,
20
+ Interface,
21
+ type JsonFragment,
22
+ type JsonRpcSigner,
23
+ type Provider,
24
+ type Signer,
25
+ getAddress,
26
+ } from "ethers";
27
+
28
+ import { getL1GasPriceFetcher, toHexString } from "../index";
29
+ import type {
30
+ ApproveParams,
31
+ CallParams,
32
+ EIP1559TxParams,
33
+ EVMCreateTransactionParams,
34
+ EVMTransferParams,
35
+ EVMTxParams,
36
+ EstimateCallParams,
37
+ IsApprovedParams,
38
+ LegacyEVMTxParams,
39
+ } from "../types";
40
+
41
+ type ToolboxWrapParams<P = Provider | BrowserProvider, T = {}> = T & {
42
+ isEIP1559Compatible?: boolean;
43
+ provider: P;
44
+ signer?: Signer;
45
+ chain: EVMChain;
46
+ };
47
+
48
+ export const MAX_APPROVAL = BigInt(
49
+ "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
50
+ );
51
+
52
+ export function BaseEVMToolbox<
53
+ P extends Provider | BrowserProvider,
54
+ S extends
55
+ | (ChainSigner<EVMTransferParams, string> & Signer)
56
+ | JsonRpcSigner
57
+ | HDNodeWallet
58
+ | undefined,
59
+ >({
60
+ chain = Chain.Ethereum,
61
+ provider,
62
+ signer,
63
+ isEIP1559Compatible = true,
64
+ }: { signer: S; provider: P; isEIP1559Compatible?: boolean; chain?: EVMChain }) {
65
+ return {
66
+ getAddress: () => {
67
+ return signer ? signer.getAddress() : undefined;
68
+ },
69
+ call: getCall({ provider, signer, isEIP1559Compatible, chain }),
70
+ estimateCall: getEstimateCall({ provider, signer }),
71
+ EIP1193SendTransaction: getEIP1193SendTransaction(provider),
72
+ approve: getApprove({ provider, signer, isEIP1559Compatible, chain }),
73
+ approvedAmount: getApprovedAmount({ provider, chain }),
74
+ broadcastTransaction: provider.broadcastTransaction,
75
+ createApprovalTx: getCreateApprovalTx({ provider, signer, chain }),
76
+ createContract: getCreateContract({ provider, chain }),
77
+ createContractTxObject: getCreateContractTxObject({ provider, chain }),
78
+ createTransferTx: getCreateTransferTx({ provider, signer, chain }),
79
+ createTransaction: getCreateTransferTx({ provider, signer, chain }),
80
+ estimateGasLimit: getEstimateGasLimit({ provider, signer, chain }),
81
+ estimateGasPrices: getEstimateGasPrices({ chain, provider, isEIP1559Compatible }),
82
+ isApproved: getIsApproved({ provider, chain }),
83
+ sendTransaction: getSendTransaction({ provider, signer, isEIP1559Compatible, chain }),
84
+ signMessage: signer?.signMessage,
85
+ transfer: getTransfer({ provider, signer, isEIP1559Compatible, chain }),
86
+ validateAddress: (address: string) => evmValidateAddress({ address }),
87
+ };
88
+ }
89
+
90
+ export function evmValidateAddress({ address }: { address: string }) {
91
+ try {
92
+ getAddress(address);
93
+ return true;
94
+ } catch (_error) {
95
+ return false;
96
+ }
97
+ }
98
+
99
+ export function isBrowserProvider(provider: any) {
100
+ return provider instanceof BrowserProvider;
101
+ }
102
+
103
+ export function createContract(
104
+ address: string,
105
+ abi: readonly (JsonFragment | Fragment)[],
106
+ provider: Provider,
107
+ ) {
108
+ return new Contract(address, Interface.from(abi), provider);
109
+ }
110
+
111
+ export function getCreateContract({ provider }: ToolboxWrapParams) {
112
+ return function createContract(address: string, abi: readonly (JsonFragment | Fragment)[]) {
113
+ return new Contract(address, Interface.from(abi), provider);
114
+ };
115
+ }
116
+
117
+ const stateMutable = ["payable", "nonpayable"];
118
+ // const nonStateMutable = ['view', 'pure'];
119
+ export function isStateChangingCall({
120
+ abi,
121
+ funcName,
122
+ }: { abi: readonly JsonFragment[]; funcName: string }) {
123
+ const abiFragment = abi.find((fragment: any) => fragment.name === funcName) as any;
124
+ if (!abiFragment) throw new SwapKitError("toolbox_evm_no_abi_fragment", { funcName });
125
+ return abiFragment.stateMutability && stateMutable.includes(abiFragment.stateMutability);
126
+ }
127
+
128
+ export function toChecksumAddress(address: string) {
129
+ return getAddress(address);
130
+ }
131
+
132
+ export function getEIP1193SendTransaction(provider: Provider | BrowserProvider) {
133
+ return function EIP1193SendTransaction({
134
+ value,
135
+ ...params
136
+ }: EVMTxParams | ContractTransaction): Promise<string> {
137
+ if (!isBrowserProvider(provider)) {
138
+ throw new SwapKitError("toolbox_evm_provider_not_eip1193_compatible");
139
+ }
140
+
141
+ return (provider as BrowserProvider).send("eth_sendTransaction", [
142
+ { value: toHexString(BigInt(value || 0)), ...params } as any,
143
+ ]);
144
+ };
145
+ }
146
+
147
+ export function getChecksumAddressFromAsset(asset: Asset, chain: EVMChain) {
148
+ const assetAddress = getTokenAddress(asset, chain);
149
+
150
+ if (assetAddress) {
151
+ return getAddress(assetAddress.toLowerCase());
152
+ }
153
+
154
+ throw new SwapKitError("toolbox_evm_invalid_gas_asset_address");
155
+ }
156
+
157
+ const baseAssetAddress: Record<EVMChain, string> = {
158
+ [Chain.Arbitrum]: ContractAddress.ARB,
159
+ [Chain.Avalanche]: ContractAddress.AVAX,
160
+ [Chain.Base]: ContractAddress.BASE,
161
+ [Chain.BinanceSmartChain]: ContractAddress.BSC,
162
+ [Chain.Ethereum]: ContractAddress.ETH,
163
+ [Chain.Optimism]: ContractAddress.OP,
164
+ [Chain.Polygon]: ContractAddress.MATIC,
165
+ };
166
+ export function getTokenAddress({ chain, symbol, ticker }: Asset, baseAssetChain: EVMChain) {
167
+ try {
168
+ const isBSCBNB = chain === Chain.BinanceSmartChain && symbol === "BNB" && ticker === "BNB";
169
+ const isBaseAsset =
170
+ chain === baseAssetChain && symbol === baseAssetChain && ticker === baseAssetChain;
171
+ const isEVMAsset =
172
+ [Chain.Arbitrum, Chain.Base].includes(chain) && symbol === "ETH" && ticker === "ETH";
173
+
174
+ if (isBaseAsset || isBSCBNB || isEVMAsset) {
175
+ return baseAssetAddress[baseAssetChain];
176
+ }
177
+
178
+ // strip 0X only - 0x is still valid
179
+ return getAddress(symbol.slice(ticker.length + 1).replace(/^0X/, ""));
180
+ } catch (_error) {
181
+ return null;
182
+ }
183
+ }
184
+
185
+ export function getCreateContractTxObject({ provider }: ToolboxWrapParams) {
186
+ return async ({ contractAddress, abi, funcName, funcParams = [], txOverrides }: CallParams) =>
187
+ createContract(contractAddress, abi, provider)
188
+ .getFunction(funcName)
189
+ .populateTransaction(
190
+ ...funcParams.concat(txOverrides).filter((p) => typeof p !== "undefined"),
191
+ );
192
+ }
193
+
194
+ export function getEstimateGasPrices({
195
+ chain,
196
+ provider,
197
+ isEIP1559Compatible = true,
198
+ }: { provider: Provider; isEIP1559Compatible?: boolean; chain: EVMChain }): () => Promise<{
199
+ [key in FeeOption]: {
200
+ l1GasPrice?: bigint;
201
+ gasPrice?: bigint;
202
+ maxFeePerGas?: bigint;
203
+ maxPriorityFeePerGas?: bigint;
204
+ };
205
+ }> {
206
+ if (chain === Chain.Arbitrum) {
207
+ return async function estimateGasPrices() {
208
+ try {
209
+ const { gasPrice } = await provider.getFeeData();
210
+
211
+ if (!gasPrice) throw new Error("No fee data available");
212
+
213
+ return {
214
+ [FeeOption.Average]: { gasPrice },
215
+ [FeeOption.Fast]: { gasPrice },
216
+ [FeeOption.Fastest]: { gasPrice },
217
+ };
218
+ } catch (error) {
219
+ throw new Error(
220
+ `Failed to estimate gas price: ${(error as any).msg ?? (error as any).toString()}`,
221
+ );
222
+ }
223
+ };
224
+ }
225
+
226
+ if (chain === Chain.Optimism) {
227
+ return async function estimateGasPrices() {
228
+ try {
229
+ const { maxFeePerGas, maxPriorityFeePerGas, gasPrice } = await provider.getFeeData();
230
+ const l1GasPrice = getL1GasPriceFetcher(provider)();
231
+ const price = gasPrice as bigint;
232
+
233
+ if (!(maxFeePerGas && maxPriorityFeePerGas)) {
234
+ throw new Error("No fee data available");
235
+ }
236
+
237
+ return {
238
+ [FeeOption.Average]: {
239
+ l1GasPrice,
240
+ gasPrice: price,
241
+ maxFeePerGas,
242
+ maxPriorityFeePerGas,
243
+ },
244
+ [FeeOption.Fast]: {
245
+ l1GasPrice: ((l1GasPrice || 0n) * 15n) / 10n,
246
+ gasPrice: (price * 15n) / 10n,
247
+ maxFeePerGas,
248
+ maxPriorityFeePerGas: (maxPriorityFeePerGas * 15n) / 10n,
249
+ },
250
+ [FeeOption.Fastest]: {
251
+ l1GasPrice: (l1GasPrice || 0n) * 2n,
252
+ gasPrice: price * 2n,
253
+ maxFeePerGas,
254
+ maxPriorityFeePerGas: maxPriorityFeePerGas * 2n,
255
+ },
256
+ };
257
+ } catch (error) {
258
+ throw new Error(
259
+ `Failed to estimate gas price: ${(error as any).msg ?? (error as any).toString()}`,
260
+ );
261
+ }
262
+ };
263
+ }
264
+
265
+ return async function estimateGasPrices() {
266
+ try {
267
+ const { maxFeePerGas, maxPriorityFeePerGas, gasPrice } = await provider.getFeeData();
268
+
269
+ if (isEIP1559Compatible) {
270
+ if (maxFeePerGas === null || maxPriorityFeePerGas === null)
271
+ throw new SwapKitError("toolbox_evm_no_fee_data");
272
+
273
+ return {
274
+ [FeeOption.Average]: { maxFeePerGas, maxPriorityFeePerGas },
275
+ [FeeOption.Fast]: {
276
+ maxFeePerGas: (maxFeePerGas * 15n) / 10n,
277
+ maxPriorityFeePerGas: (maxPriorityFeePerGas * 15n) / 10n,
278
+ },
279
+ [FeeOption.Fastest]: {
280
+ maxFeePerGas: maxFeePerGas * 2n,
281
+ maxPriorityFeePerGas: maxPriorityFeePerGas * 2n,
282
+ },
283
+ };
284
+ }
285
+ if (!gasPrice) throw new SwapKitError("toolbox_evm_no_gas_price");
286
+
287
+ return {
288
+ [FeeOption.Average]: { gasPrice },
289
+ [FeeOption.Fast]: { gasPrice: (gasPrice * 15n) / 10n },
290
+ [FeeOption.Fastest]: { gasPrice: gasPrice * 2n },
291
+ };
292
+ } catch (error) {
293
+ throw new Error(
294
+ `Failed to estimate gas price: ${(error as any).msg ?? (error as any).toString()}`,
295
+ );
296
+ }
297
+ };
298
+ }
299
+
300
+ function getCall({ provider, isEIP1559Compatible, signer, chain }: ToolboxWrapParams) {
301
+ /**
302
+ * @info call contract function
303
+ * When using this method to make a non state changing call to the blockchain, like a isApproved call,
304
+ * the signer needs to be set to undefined
305
+ */
306
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO reduce complexity
307
+ return async function call<T>({
308
+ callProvider,
309
+ contractAddress,
310
+ abi,
311
+ funcName,
312
+ funcParams = [],
313
+ txOverrides = {},
314
+ feeOption = FeeOption.Fast,
315
+ }: CallParams): Promise<T> {
316
+ const contractProvider = callProvider || provider;
317
+ if (!contractAddress) throw new Error("contractAddress must be provided");
318
+
319
+ const isStateChanging = isStateChangingCall({ abi, funcName });
320
+
321
+ if (isStateChanging && isBrowserProvider(contractProvider) && signer) {
322
+ const createTx = getCreateContractTxObject({ provider: contractProvider, chain });
323
+ const from = txOverrides?.from || (await signer?.getAddress());
324
+ const txObject = await createTx({
325
+ contractAddress,
326
+ abi,
327
+ funcName,
328
+ funcParams,
329
+ txOverrides: { ...txOverrides, from },
330
+ });
331
+ const sendTx = getEIP1193SendTransaction(contractProvider);
332
+
333
+ return sendTx(txObject) as Promise<T>;
334
+ }
335
+ const contract = createContract(contractAddress, abi, contractProvider);
336
+
337
+ // only use signer if the contract function is state changing
338
+ if (isStateChanging) {
339
+ if (!signer) throw new SwapKitError("toolbox_evm_no_signer");
340
+
341
+ const from = txOverrides?.from || (await signer.getAddress());
342
+ if (!from) throw new SwapKitError("toolbox_evm_no_signer_address");
343
+
344
+ const connectedContract = contract.connect(signer);
345
+ const estimateGasPrices = getEstimateGasPrices({
346
+ provider,
347
+ isEIP1559Compatible,
348
+ chain: chain as Chain.Ethereum,
349
+ });
350
+ const { maxFeePerGas, maxPriorityFeePerGas, gasPrice } = (await estimateGasPrices())[
351
+ feeOption
352
+ ];
353
+
354
+ const gasLimit = await contract.getFunction(funcName).estimateGas(...funcParams, txOverrides);
355
+
356
+ // @ts-expect-error
357
+ const result = await connectedContract[funcName](...funcParams, {
358
+ ...txOverrides,
359
+ gasLimit,
360
+ maxFeePerGas,
361
+ maxPriorityFeePerGas,
362
+ gasPrice,
363
+ /**
364
+ * nonce must be set due to a possible bug with ethers.js,
365
+ * expecting a synchronous nonce while the JsonRpcProvider delivers Promise
366
+ */
367
+ nonce: txOverrides?.nonce || (await contractProvider.getTransactionCount(from)),
368
+ });
369
+
370
+ return typeof result?.hash === "string" ? result?.hash : result;
371
+ }
372
+
373
+ const result = await contract[funcName]?.(...funcParams);
374
+
375
+ return typeof result?.hash === "string" ? result?.hash : result;
376
+ };
377
+ }
378
+
379
+ function getApprovedAmount({ provider, chain }: ToolboxWrapParams) {
380
+ return function approveAmount({ assetAddress, spenderAddress, from }: IsApprovedParams) {
381
+ const call = getCall({ provider, isEIP1559Compatible: true, chain });
382
+
383
+ return call<bigint>({
384
+ contractAddress: assetAddress,
385
+ abi: erc20ABI,
386
+ funcName: "allowance",
387
+ funcParams: [from, spenderAddress],
388
+ });
389
+ };
390
+ }
391
+
392
+ function getIsApproved({ provider, chain }: ToolboxWrapParams) {
393
+ return async function isApproved({
394
+ assetAddress,
395
+ spenderAddress,
396
+ from,
397
+ amount = MAX_APPROVAL,
398
+ }: IsApprovedParams) {
399
+ const approvedAmount = await getApprovedAmount({ provider, chain })({
400
+ assetAddress,
401
+ spenderAddress,
402
+ from,
403
+ });
404
+
405
+ return SwapKitNumber.fromBigInt(approvedAmount).gte(SwapKitNumber.fromBigInt(BigInt(amount)));
406
+ };
407
+ }
408
+
409
+ function getApprove({ signer, isEIP1559Compatible = true, provider, chain }: ToolboxWrapParams) {
410
+ return async function approve({
411
+ assetAddress,
412
+ spenderAddress,
413
+ feeOptionKey = FeeOption.Fast,
414
+ amount,
415
+ gasLimitFallback,
416
+ from: fromParam,
417
+ nonce,
418
+ }: ApproveParams) {
419
+ const funcParams = [spenderAddress, BigInt(amount || MAX_APPROVAL)];
420
+ const from = (await signer?.getAddress()) || fromParam;
421
+
422
+ const functionCallParams = {
423
+ contractAddress: assetAddress,
424
+ abi: erc20ABI,
425
+ funcName: "approve",
426
+ funcParams,
427
+ signer,
428
+ txOverrides: { from },
429
+ };
430
+
431
+ if (isBrowserProvider(provider)) {
432
+ const createTx = getCreateContractTxObject({ provider, chain });
433
+ const sendTx = getEIP1193SendTransaction(provider);
434
+ const txObject = await createTx(functionCallParams);
435
+
436
+ return sendTx(txObject);
437
+ }
438
+
439
+ const call = getCall({ provider, isEIP1559Compatible, signer, chain });
440
+
441
+ return call<string>({
442
+ ...functionCallParams,
443
+ funcParams,
444
+ txOverrides: {
445
+ from,
446
+ nonce,
447
+ gasLimit: gasLimitFallback ? BigInt(gasLimitFallback.toString()) : undefined,
448
+ },
449
+ feeOption: feeOptionKey,
450
+ });
451
+ };
452
+ }
453
+
454
+ function getTransfer({ signer, isEIP1559Compatible = true, provider }: ToolboxWrapParams) {
455
+ return async function transfer({
456
+ assetValue,
457
+ memo,
458
+ recipient,
459
+ feeOptionKey = FeeOption.Fast,
460
+ sender,
461
+ // data,
462
+ // from: fromOverride,
463
+ // maxFeePerGas,
464
+ // maxPriorityFeePerGas,
465
+ // gasPrice,
466
+ ...tx
467
+ }: EVMTransferParams) {
468
+ const { hexlify, toUtf8Bytes } = await import("ethers");
469
+ const txAmount = assetValue.getBaseValue("bigint");
470
+ const chain = assetValue.chain as EVMChain;
471
+ const from = sender || (await signer?.getAddress());
472
+ const sendTx = getSendTransaction({ provider, signer, isEIP1559Compatible, chain });
473
+
474
+ if (!from) throw new SwapKitError("toolbox_evm_no_from_address");
475
+
476
+ if (assetValue.isGasAsset) {
477
+ const transaction = {
478
+ ...tx,
479
+ from,
480
+ to: recipient,
481
+ value: txAmount,
482
+ data: hexlify(toUtf8Bytes(memo || "")),
483
+ feeOptionKey,
484
+ };
485
+
486
+ return sendTx(transaction);
487
+ }
488
+
489
+ // const call = getCall({ signer, provider, isEIP1559Compatible });
490
+ const contractAddress = getTokenAddress(assetValue, chain);
491
+ if (!contractAddress) throw new SwapKitError("toolbox_evm_no_contract_address");
492
+
493
+ const { maxFeePerGas, maxPriorityFeePerGas, gasPrice } = (
494
+ await getEstimateGasPrices({
495
+ provider,
496
+ isEIP1559Compatible,
497
+ chain,
498
+ })()
499
+ )[feeOptionKey];
500
+
501
+ const transaction = await getCreateTransferTx({ provider, signer, chain })({
502
+ assetValue,
503
+ memo,
504
+ recipient,
505
+ data: hexlify(toUtf8Bytes(memo || "")),
506
+ sender: from,
507
+ maxFeePerGas,
508
+ maxPriorityFeePerGas,
509
+ gasPrice,
510
+ });
511
+
512
+ return sendTx(transaction);
513
+ };
514
+ }
515
+
516
+ function getEstimateCall({ provider, signer }: { signer?: Signer; provider: Provider }) {
517
+ return function estimateCall({
518
+ contractAddress,
519
+ abi,
520
+ funcName,
521
+ funcParams = [],
522
+ txOverrides,
523
+ }: EstimateCallParams) {
524
+ if (!contractAddress) throw new SwapKitError("toolbox_evm_no_contract_address");
525
+
526
+ const contract = createContract(contractAddress, abi, provider);
527
+ return signer
528
+ ? contract
529
+ .connect(signer)
530
+ .getFunction(funcName)
531
+ .estimateGas(...funcParams, txOverrides)
532
+ : contract.getFunction(funcName).estimateGas(...funcParams, txOverrides);
533
+ };
534
+ }
535
+
536
+ function getEstimateGasLimit({ provider, signer }: ToolboxWrapParams) {
537
+ return async function estimateGasLimit({
538
+ assetValue,
539
+ recipient,
540
+ memo,
541
+ sender,
542
+ funcName,
543
+ funcParams,
544
+ txOverrides,
545
+ }: EVMTransferParams & {
546
+ assetValue: AssetValue;
547
+ funcName?: string;
548
+ funcParams?: unknown[];
549
+ txOverrides?: EVMTxParams;
550
+ }) {
551
+ // const value = assetValue.getBaseValue("bigint");
552
+ const value = assetValue.bigIntValue;
553
+
554
+ const assetAddress = assetValue.isGasAsset
555
+ ? null
556
+ : getTokenAddress(assetValue, assetValue.chain as EVMChain);
557
+
558
+ if (assetAddress && funcName) {
559
+ const estimateCall = getEstimateCall({ provider, signer });
560
+ // ERC20 gas estimate
561
+ return estimateCall({
562
+ contractAddress: assetAddress,
563
+ abi: erc20ABI,
564
+ funcName,
565
+ funcParams,
566
+ txOverrides,
567
+ });
568
+ }
569
+
570
+ const { hexlify, toUtf8Bytes } = await import("ethers");
571
+
572
+ return provider.estimateGas({
573
+ from: sender,
574
+ to: recipient,
575
+ value,
576
+ data: memo ? hexlify(toUtf8Bytes(memo)) : undefined,
577
+ });
578
+ };
579
+ }
580
+
581
+ const isEIP1559Transaction = (tx: EVMTxParams) =>
582
+ (tx as EIP1559TxParams).type === 2 ||
583
+ !!(tx as EIP1559TxParams).maxFeePerGas ||
584
+ !!(tx as EIP1559TxParams).maxPriorityFeePerGas;
585
+
586
+ function getSendTransaction({
587
+ provider,
588
+ signer,
589
+ isEIP1559Compatible = true,
590
+ chain,
591
+ }: ToolboxWrapParams) {
592
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO reduce complexity
593
+ return async function sendTransaction({
594
+ feeOptionKey = FeeOption.Fast,
595
+ ...tx
596
+ }: EVMTxParams & { feeOptionKey?: FeeOption }) {
597
+ const { from, to, data, value, ...transaction } = tx;
598
+
599
+ if (!signer) throw new SwapKitError("toolbox_evm_no_signer");
600
+ if (!to) throw new SwapKitError("toolbox_evm_no_to_address");
601
+
602
+ const parsedTxObject = {
603
+ ...transaction,
604
+ data: data || "0x",
605
+ to,
606
+ from,
607
+ value: BigInt(value || 0),
608
+ };
609
+
610
+ // early return to skip gas estimation if provider is EIP-1193
611
+ if (isBrowserProvider(provider)) {
612
+ const sendTx = getEIP1193SendTransaction(provider);
613
+ return sendTx(parsedTxObject);
614
+ }
615
+
616
+ const address = from || (await signer.getAddress());
617
+ const nonce = tx.nonce || (await provider.getTransactionCount(address));
618
+ const chainId = (await provider.getNetwork()).chainId;
619
+
620
+ const isEIP1559 = isEIP1559Transaction(parsedTxObject) || isEIP1559Compatible;
621
+ const estimateGasPrices = getEstimateGasPrices({ provider, isEIP1559Compatible, chain });
622
+
623
+ const feeData =
624
+ (isEIP1559 &&
625
+ !(
626
+ (parsedTxObject as EIP1559TxParams).maxFeePerGas &&
627
+ (parsedTxObject as EIP1559TxParams).maxPriorityFeePerGas
628
+ )) ||
629
+ !(parsedTxObject as LegacyEVMTxParams).gasPrice
630
+ ? Object.entries((await estimateGasPrices())[feeOptionKey]).reduce(
631
+ // biome-ignore lint/performance/noAccumulatingSpread: this is a small object
632
+ (acc, [k, v]) => ({ ...acc, [k]: toHexString(BigInt(v)) }),
633
+ {} as {
634
+ maxFeePerGas?: string;
635
+ maxPriorityFeePerGas?: string;
636
+ gasPrice?: string;
637
+ },
638
+ )
639
+ : {};
640
+ let gasLimit: string;
641
+ try {
642
+ gasLimit = toHexString(
643
+ parsedTxObject.gasLimit || ((await provider.estimateGas(parsedTxObject)) * 11n) / 10n,
644
+ );
645
+ } catch (error) {
646
+ throw new SwapKitError("toolbox_evm_error_estimating_gas_limit", { error });
647
+ }
648
+
649
+ try {
650
+ const txObject = {
651
+ ...parsedTxObject,
652
+ chainId,
653
+ type: isEIP1559 ? 2 : 0,
654
+ gasLimit,
655
+ nonce,
656
+ ...feeData,
657
+ };
658
+
659
+ try {
660
+ const response = await signer.sendTransaction(txObject);
661
+ return response.hash;
662
+ } catch (_error) {
663
+ const txHex = await signer.signTransaction({
664
+ ...txObject,
665
+ from: address,
666
+ });
667
+ const response = await provider.broadcastTransaction(txHex);
668
+ return response.hash;
669
+ }
670
+ } catch (error) {
671
+ throw new SwapKitError("toolbox_evm_error_sending_transaction", { error });
672
+ }
673
+ };
674
+ }
675
+
676
+ function getCreateTransferTx({ provider, signer }: ToolboxWrapParams) {
677
+ return async function createTransferTx({
678
+ assetValue,
679
+ memo,
680
+ recipient,
681
+ data,
682
+ sender: fromOverride,
683
+ maxFeePerGas,
684
+ maxPriorityFeePerGas,
685
+ gasPrice,
686
+ ...tx
687
+ }: EVMCreateTransactionParams) {
688
+ const txAmount = assetValue.getBaseValue("bigint");
689
+ const chain = assetValue.chain as EVMChain;
690
+ const from = fromOverride || (await signer?.getAddress());
691
+
692
+ if (!from) throw new SwapKitError("toolbox_evm_no_from_address");
693
+
694
+ if (isGasAsset(assetValue)) {
695
+ const { hexlify, toUtf8Bytes } = await import("ethers");
696
+
697
+ return {
698
+ ...tx,
699
+ from,
700
+ to: recipient,
701
+ value: txAmount,
702
+ data: data || hexlify(toUtf8Bytes(memo || "")),
703
+ };
704
+ }
705
+
706
+ const contractAddress = getTokenAddress(assetValue, chain);
707
+ if (!contractAddress) throw new SwapKitError("toolbox_evm_no_contract_address");
708
+ const createTx = getCreateContractTxObject({ provider, chain: assetValue.chain as EVMChain });
709
+
710
+ return createTx({
711
+ contractAddress,
712
+ abi: erc20ABI,
713
+ funcName: "transfer",
714
+ funcParams: [recipient, txAmount],
715
+ txOverrides: { from, maxFeePerGas, maxPriorityFeePerGas, gasPrice },
716
+ });
717
+ };
718
+ }
719
+
720
+ function getCreateApprovalTx({ provider, signer, chain }: ToolboxWrapParams) {
721
+ return async function createApprovalTx({
722
+ assetAddress,
723
+ spenderAddress,
724
+ amount,
725
+ from: fromParam,
726
+ }: ApproveParams) {
727
+ const from = (await signer?.getAddress()) || fromParam;
728
+
729
+ const createTx = getCreateContractTxObject({ provider, chain });
730
+ const funcParams = [spenderAddress, BigInt(amount || MAX_APPROVAL)];
731
+
732
+ const txObject = await createTx({
733
+ contractAddress: assetAddress,
734
+ abi: erc20ABI,
735
+ funcName: "approve",
736
+ funcParams,
737
+ txOverrides: { from },
738
+ });
739
+
740
+ return txObject;
741
+ };
742
+ }