@swapkit/toolboxes 4.0.0-beta.50 → 4.0.0-beta.52

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