@toruslabs/ethereum-controllers 8.17.0 → 9.0.0

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 (67) hide show
  1. package/dist/lib.cjs/Account/AccountTrackerController.js +15 -7
  2. package/dist/lib.cjs/AccountAbstraction/AccountAbstractionController.js +4 -6
  3. package/dist/lib.cjs/Eip5792/walletGetCallsStatus.js +2 -2
  4. package/dist/lib.cjs/Eip5792/walletSendCalls.js +8 -9
  5. package/dist/lib.cjs/Eip7702/eip7702Utils.js +3 -3
  6. package/dist/lib.cjs/Gas/GasFeeController.js +3 -2
  7. package/dist/lib.cjs/Keyring/KeyringController.js +69 -40
  8. package/dist/lib.cjs/Message/utils.js +15 -14
  9. package/dist/lib.cjs/Nfts/NftHandler.js +54 -12
  10. package/dist/lib.cjs/Nfts/NftsController.js +1 -4
  11. package/dist/lib.cjs/Preferences/PreferencesController.js +2 -2
  12. package/dist/lib.cjs/Tokens/TokenHandler.js +39 -7
  13. package/dist/lib.cjs/Tokens/TokensController.js +15 -7
  14. package/dist/lib.cjs/Transaction/TransactionController.js +17 -20
  15. package/dist/lib.cjs/Transaction/TransactionGasUtil.js +11 -12
  16. package/dist/lib.cjs/Transaction/TransactionUtils.js +44 -39
  17. package/dist/lib.cjs/index.js +0 -1
  18. package/dist/lib.cjs/types/Account/AccountTrackerController.d.ts +1 -1
  19. package/dist/lib.cjs/types/AccountAbstraction/AccountAbstractionController.d.ts +1 -3
  20. package/dist/lib.cjs/types/Keyring/KeyringController.d.ts +4 -4
  21. package/dist/lib.cjs/types/Nfts/NftHandler.d.ts +4 -3
  22. package/dist/lib.cjs/types/Nfts/NftsController.d.ts +0 -1
  23. package/dist/lib.cjs/types/Tokens/TokenHandler.d.ts +4 -3
  24. package/dist/lib.cjs/types/Tokens/TokensController.d.ts +1 -1
  25. package/dist/lib.cjs/types/Transaction/TransactionUtils.d.ts +11 -3
  26. package/dist/lib.cjs/types/utils/abis.d.ts +544 -99
  27. package/dist/lib.cjs/types/utils/eip5792Types.d.ts +0 -1
  28. package/dist/lib.cjs/types/utils/hex.d.ts +9 -0
  29. package/dist/lib.cjs/types/utils/interfaces.d.ts +205 -2
  30. package/dist/lib.cjs/types/utils/transaction.d.ts +1 -1
  31. package/dist/lib.cjs/types/utils/viem.d.ts +8 -0
  32. package/dist/lib.cjs/utils/abis.js +12 -0
  33. package/dist/lib.cjs/utils/conversionUtils.js +3 -4
  34. package/dist/lib.cjs/utils/eip5792Types.js +0 -2
  35. package/dist/lib.cjs/utils/helpers.js +9 -8
  36. package/dist/lib.cjs/utils/hex.js +23 -0
  37. package/dist/lib.cjs/utils/transaction.js +7 -11
  38. package/dist/lib.cjs/utils/viem.js +214 -0
  39. package/dist/lib.esm/Account/AccountTrackerController.js +15 -7
  40. package/dist/lib.esm/AccountAbstraction/AccountAbstractionController.js +5 -7
  41. package/dist/lib.esm/Eip5792/walletGetCallsStatus.js +1 -1
  42. package/dist/lib.esm/Eip5792/walletSendCalls.js +9 -10
  43. package/dist/lib.esm/Eip7702/eip7702Utils.js +1 -1
  44. package/dist/lib.esm/Gas/GasFeeController.js +3 -2
  45. package/dist/lib.esm/Keyring/KeyringController.js +68 -40
  46. package/dist/lib.esm/Message/utils.js +13 -12
  47. package/dist/lib.esm/Network/createEthereumMiddleware.js +2 -2
  48. package/dist/lib.esm/Nfts/NftHandler.js +54 -12
  49. package/dist/lib.esm/Nfts/NftsController.js +1 -4
  50. package/dist/lib.esm/Preferences/PreferencesController.js +1 -1
  51. package/dist/lib.esm/Tokens/TokenHandler.js +39 -7
  52. package/dist/lib.esm/Tokens/TokensController.js +15 -7
  53. package/dist/lib.esm/Transaction/TransactionController.js +6 -9
  54. package/dist/lib.esm/Transaction/TransactionGasUtil.js +11 -12
  55. package/dist/lib.esm/Transaction/TransactionUtils.js +30 -26
  56. package/dist/lib.esm/index.js +2 -2
  57. package/dist/lib.esm/utils/abis.js +12 -0
  58. package/dist/lib.esm/utils/conversionUtils.js +2 -3
  59. package/dist/lib.esm/utils/eip5792Types.js +1 -2
  60. package/dist/lib.esm/utils/helpers.js +8 -7
  61. package/dist/lib.esm/utils/hex.js +21 -0
  62. package/dist/lib.esm/utils/interfaces.js +64 -0
  63. package/dist/lib.esm/utils/transaction.js +7 -11
  64. package/dist/lib.esm/utils/viem.js +214 -0
  65. package/package.json +13 -18
  66. package/dist/ethereumControllers.umd.min.js +0 -2
  67. package/dist/ethereumControllers.umd.min.js.LICENSE.txt +0 -50
@@ -1,8 +1,8 @@
1
1
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
2
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
3
  import { get } from '@toruslabs/http-helpers';
4
- import { Contract } from 'ethers';
5
4
  import log from 'loglevel';
5
+ import { createPublicClient, custom } from 'viem';
6
6
  import { erc721Abi, erc1155Abi } from '../utils/abis.js';
7
7
  import { CONTRACT_TYPE_ERC721, CONTRACT_TYPE_ERC1155, OLD_ERC721_LIST, ERC721_INTERFACE_ID, ERC1155_INTERFACE_ID } from '../utils/constants.js';
8
8
  import { sanitizeNftMetdataUrl } from '../utils/helpers.js';
@@ -31,6 +31,7 @@ class NftHandler {
31
31
  _defineProperty(this, "chainId", void 0);
32
32
  _defineProperty(this, "provider", void 0);
33
33
  _defineProperty(this, "isSpecial", void 0);
34
+ _defineProperty(this, "publicClient", void 0);
34
35
  this.chainId = chainId;
35
36
  this.contractAddress = contractAddress;
36
37
  this.contractImage = contractImage;
@@ -38,6 +39,9 @@ class NftHandler {
38
39
  this.contractSymbol = contractSymbol;
39
40
  this.nftStandard = nftStandard;
40
41
  this.provider = provider;
42
+ this.publicClient = createPublicClient({
43
+ transport: custom(this.provider)
44
+ });
41
45
  this.contractDescription = contractDescription;
42
46
  this.contractFallbackLogo = contractFallbackLogo;
43
47
  this.contractSupply = contractSupply;
@@ -101,8 +105,17 @@ class NftHandler {
101
105
  }
102
106
  if (!this.contractName || !this.contractSymbol || !this.contractDescription) {
103
107
  const abi = this.nftStandard === CONTRACT_TYPE_ERC721 ? erc721Abi : erc1155Abi;
104
- const contract = new Contract(this.contractAddress, abi, this.provider);
105
- const [name, symbol] = await Promise.all([contract.name(), contract.symbol()]);
108
+ const [name, symbol] = await Promise.all([this.publicClient.readContract({
109
+ address: this.contractAddress,
110
+ abi,
111
+ functionName: "name",
112
+ authorizationList: undefined
113
+ }), this.publicClient.readContract({
114
+ address: this.contractAddress,
115
+ abi,
116
+ functionName: "symbol",
117
+ authorizationList: undefined
118
+ })]);
106
119
  returnNft.contractName = name;
107
120
  returnNft.contractSymbol = symbol;
108
121
  if (!this.contractName) this.contractName = name;
@@ -115,14 +128,25 @@ class NftHandler {
115
128
  standard
116
129
  } = await this.checkNftStandard();
117
130
  const abi = standard === CONTRACT_TYPE_ERC721 ? erc721Abi : erc1155Abi;
118
- const contract = new Contract(this.contractAddress, abi, this.provider);
119
131
  if (standard === CONTRACT_TYPE_ERC1155) {
120
- const balance = await contract.balanceOf(userAddress, tokenId);
121
- return balance;
132
+ const balance = await this.publicClient.readContract({
133
+ address: this.contractAddress,
134
+ abi,
135
+ functionName: "balanceOf",
136
+ args: [userAddress, BigInt(tokenId)],
137
+ authorizationList: undefined
138
+ });
139
+ return String(balance);
122
140
  }
123
141
  let owner = "";
124
142
  try {
125
- owner = await contract.ownerOf(tokenId);
143
+ owner = await this.publicClient.readContract({
144
+ address: this.contractAddress,
145
+ abi,
146
+ functionName: "ownerOf",
147
+ args: [BigInt(tokenId)],
148
+ authorizationList: undefined
149
+ });
126
150
  } catch {
127
151
  throw new Error("Token id doesn't exists");
128
152
  }
@@ -164,14 +188,32 @@ class NftHandler {
164
188
  }
165
189
  async contractSupportsInterface(standard, interfaceId) {
166
190
  const abi = standard === CONTRACT_TYPE_ERC721 ? erc721Abi : erc1155Abi;
167
- const contract = new Contract(this.contractAddress, abi, this.provider);
168
- return contract.supportsInterface(interfaceId);
191
+ return await this.publicClient.readContract({
192
+ address: this.contractAddress,
193
+ abi,
194
+ functionName: "supportsInterface",
195
+ args: [interfaceId],
196
+ authorizationList: undefined
197
+ });
169
198
  }
170
199
  async getCollectibleTokenURI(tokenId, standard = CONTRACT_TYPE_ERC721) {
171
- const method = standard === CONTRACT_TYPE_ERC721 ? "tokenURI" : "uri";
172
200
  const abi = standard === CONTRACT_TYPE_ERC721 ? erc721Abi : erc1155Abi;
173
- const contract = new Contract(this.contractAddress, abi, this.provider);
174
- return contract[method](tokenId);
201
+ if (standard === CONTRACT_TYPE_ERC721) {
202
+ return await this.publicClient.readContract({
203
+ address: this.contractAddress,
204
+ abi,
205
+ functionName: "tokenURI",
206
+ args: [BigInt(tokenId)],
207
+ authorizationList: undefined
208
+ });
209
+ }
210
+ return await this.publicClient.readContract({
211
+ address: this.contractAddress,
212
+ abi,
213
+ functionName: "uri",
214
+ args: [BigInt(tokenId)],
215
+ authorizationList: undefined
216
+ });
175
217
  }
176
218
  }
177
219
 
@@ -2,7 +2,6 @@ import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
2
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
3
  import { BaseController, PollingManager } from '@toruslabs/base-controllers';
4
4
  import deepmerge from 'deepmerge';
5
- import { BrowserProvider } from 'ethers';
6
5
  import log from 'loglevel';
7
6
  import { MM_NFT_API_SUPPORTED_CHAINS } from '../utils/constants.js';
8
7
  import { NftHandler } from './NftHandler.js';
@@ -25,12 +24,10 @@ class NftsController extends BaseController {
25
24
  });
26
25
  _defineProperty(this, "name", "NftsController");
27
26
  _defineProperty(this, "provider", void 0);
28
- _defineProperty(this, "ethersProvider", void 0);
29
27
  _defineProperty(this, "getCustomNfts", void 0);
30
28
  _defineProperty(this, "getSimpleHashNfts", void 0);
31
29
  _defineProperty(this, "pollingManager", void 0);
32
30
  this.provider = provider;
33
- this.ethersProvider = new BrowserProvider(this.provider, "any");
34
31
  this.getCustomNfts = getCustomNfts;
35
32
  this.getSimpleHashNfts = getSimpleHashNfts;
36
33
  this.defaultConfig = {
@@ -174,7 +171,7 @@ class NftsController extends BaseController {
174
171
  const promiseSettledResult = await Promise.allSettled(currentNetworkNfts.map(async x => {
175
172
  try {
176
173
  const tokenInstance = new NftHandler(_objectSpread(_objectSpread({}, x), {}, {
177
- provider: this.ethersProvider
174
+ provider: this.provider
178
175
  }));
179
176
  const contractData = await tokenInstance.getContractMetadata();
180
177
  const assetData = await Promise.allSettled(x.assets.map(y => tokenInstance.getNftMetadata(userAddress, y)));
@@ -1,10 +1,10 @@
1
1
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
- import { stripHexPrefix } from '@ethereumjs/util';
3
2
  import { BasePreferencesController, CHAIN_NAMESPACES, isUnauthorizedError, TransactionStatus } from '@toruslabs/base-controllers';
4
3
  import { Mutex } from 'async-mutex';
5
4
  import log from 'loglevel';
6
5
  import { MM_TOKEN_API_SUPPORTED_CHAINS, SUPPORTED_NETWORKS } from '../utils/constants.js';
7
6
  import { formatPastTx, addEtherscanTransactions, getEthTxStatus, formatTime, formatDate } from '../utils/helpers.js';
7
+ import { stripHexPrefix } from '../utils/hex.js';
8
8
 
9
9
  class PreferencesController extends BasePreferencesController {
10
10
  constructor({
@@ -1,6 +1,6 @@
1
1
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
2
- import { Contract } from 'ethers';
3
2
  import log from 'loglevel';
3
+ import { createPublicClient, custom } from 'viem';
4
4
  import { erc20Abi } from '../utils/abis.js';
5
5
 
6
6
  class TokenHandler {
@@ -15,20 +15,38 @@ class TokenHandler {
15
15
  _defineProperty(this, "symbol", void 0);
16
16
  _defineProperty(this, "decimals", void 0);
17
17
  _defineProperty(this, "name", void 0);
18
- _defineProperty(this, "contract", void 0);
18
+ _defineProperty(this, "provider", void 0);
19
+ _defineProperty(this, "publicClient", void 0);
19
20
  this.address = address;
20
- this.contract = new Contract(address, erc20Abi, provider);
21
+ this.provider = provider;
22
+ this.publicClient = createPublicClient({
23
+ transport: custom(this.provider)
24
+ });
21
25
  this.symbol = symbol;
22
26
  this.decimals = decimals;
23
27
  this.name = name;
24
28
  }
25
29
  async getSymbol() {
26
- if (!this.symbol || this.symbol === "ERC20") this.symbol = await this.contract.symbol();
30
+ if (!this.symbol || this.symbol === "ERC20") {
31
+ this.symbol = await this.publicClient.readContract({
32
+ address: this.address,
33
+ abi: erc20Abi,
34
+ functionName: "symbol",
35
+ authorizationList: undefined
36
+ });
37
+ }
27
38
  return this.symbol;
28
39
  }
29
40
  async getDecimals() {
30
41
  try {
31
- if (!this.decimals) this.decimals = await this.contract.decimals();
42
+ if (!this.decimals) {
43
+ this.decimals = Number(await this.publicClient.readContract({
44
+ address: this.address,
45
+ abi: erc20Abi,
46
+ functionName: "decimals",
47
+ authorizationList: undefined
48
+ }));
49
+ }
32
50
  return this.decimals;
33
51
  } catch (error) {
34
52
  log.warn(`Could not get decimals for token ${this.address}`, error);
@@ -36,12 +54,26 @@ class TokenHandler {
36
54
  }
37
55
  }
38
56
  async getName() {
39
- if (!this.name) this.name = await this.contract.name();
57
+ if (!this.name) {
58
+ const name = await this.publicClient.readContract({
59
+ address: this.address,
60
+ abi: erc20Abi,
61
+ functionName: "name",
62
+ authorizationList: undefined
63
+ });
64
+ this.name = name;
65
+ }
40
66
  return this.name;
41
67
  }
42
68
  async getUserBalance(userAddress) {
43
69
  if (!this.decimals) await this.getDecimals();
44
- const balance = await this.contract.balanceOf(userAddress);
70
+ const balance = await this.publicClient.readContract({
71
+ address: this.address,
72
+ abi: erc20Abi,
73
+ functionName: "balanceOf",
74
+ args: [userAddress],
75
+ authorizationList: undefined
76
+ });
45
77
  return balance.toString(16);
46
78
  }
47
79
  }
@@ -1,8 +1,8 @@
1
1
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
2
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
3
  import { BaseController, PollingManager } from '@toruslabs/base-controllers';
4
- import { BrowserProvider, Contract, toQuantity } from 'ethers';
5
4
  import log from 'loglevel';
5
+ import { createPublicClient, custom, toHex } from 'viem';
6
6
  import { singleBalanceCheckerAbi } from '../utils/abis.js';
7
7
  import { MM_TOKEN_API_SUPPORTED_CHAINS } from '../utils/constants.js';
8
8
  import { SINGLE_CALL_BALANCES_ADDRESSES } from '../utils/contractAddresses.js';
@@ -27,13 +27,15 @@ class TokensController extends BaseController {
27
27
  });
28
28
  _defineProperty(this, "name", "TokensController");
29
29
  _defineProperty(this, "provider", void 0);
30
- _defineProperty(this, "ethersProvider", void 0);
30
+ _defineProperty(this, "publicClient", void 0);
31
31
  _defineProperty(this, "pollingManager", void 0);
32
32
  _defineProperty(this, "getProviderConfig", void 0);
33
33
  _defineProperty(this, "getCustomTokens", void 0);
34
34
  _defineProperty(this, "getEtherScanTokens", void 0);
35
35
  this.provider = provider;
36
- this.ethersProvider = new BrowserProvider(this.provider, "any");
36
+ this.publicClient = createPublicClient({
37
+ transport: custom(this.provider)
38
+ });
37
39
  this.getCustomTokens = getCustomTokens;
38
40
  this.getEtherScanTokens = getEtherScanTokens;
39
41
  this.getProviderConfig = getProviderConfig;
@@ -167,10 +169,16 @@ class TokensController extends BaseController {
167
169
  const remainingTokensAddresses = remainingTokens.map(x => x.tokenAddress);
168
170
  const currentSingleCallAddress = SINGLE_CALL_BALANCES_ADDRESSES[currentChainId];
169
171
  if (currentSingleCallAddress) {
170
- const ethContract = new Contract(currentSingleCallAddress, singleBalanceCheckerAbi, this.ethersProvider);
171
- const result = await ethContract.balances([userAddress], remainingTokensAddresses);
172
+ const result = await this.publicClient.readContract({
173
+ address: currentSingleCallAddress,
174
+ abi: singleBalanceCheckerAbi,
175
+ functionName: "balances",
176
+ args: [[userAddress], remainingTokensAddresses],
177
+ authorizationList: undefined
178
+ });
172
179
  remainingTokensAddresses.forEach((_, index) => {
173
- const balance = toQuantity(result[index]);
180
+ var _result$index;
181
+ const balance = toHex((_result$index = result[index]) !== null && _result$index !== void 0 ? _result$index : 0n);
174
182
  if (balance && balance !== "0x0") {
175
183
  nonZeroTokens.push(_objectSpread(_objectSpread({}, remainingTokens[index]), {}, {
176
184
  balance,
@@ -208,7 +216,7 @@ class TokensController extends BaseController {
208
216
  decimals: Number.parseInt(x.decimals),
209
217
  name: x.name,
210
218
  symbol: x.symbol,
211
- provider: this.ethersProvider
219
+ provider: this.provider
212
220
  });
213
221
  const balance = await tokenInstance.getUserBalance(this.userSelectedAddress);
214
222
  return {
@@ -1,16 +1,15 @@
1
1
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
2
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
- import { addHexPrefix, stripHexPrefix } from '@ethereumjs/util';
4
3
  import { TRANSACTION_TYPES, TX_EVENTS, TransactionStatus } from '@toruslabs/base-controllers';
5
4
  import { rpcErrors, providerErrors } from '@web3auth/auth';
6
5
  import { BigNumber } from 'bignumber.js';
7
- import { Signature, Transaction, keccak256 } from 'ethers';
8
6
  import log from 'loglevel';
9
- import { toHex } from 'viem';
7
+ import { toHex, keccak256 } from 'viem';
10
8
  import { generateEIP7702BatchTransaction } from '../Eip7702/eip7702Utils.js';
11
9
  import { METHOD_TYPES, TRANSACTION_ENVELOPE_TYPES, CONTRACT_TYPE_ERC7821, GAS_ESTIMATE_TYPES, CHAIN_ID_TO_GAS_LIMIT_BUFFER_MAP } from '../utils/constants.js';
12
10
  import { decGWEIToHexWEI } from '../utils/conversionUtils.js';
13
11
  import { bnLessThan, getChainType, GAS_LIMITS } from '../utils/helpers.js';
12
+ import { addHexPrefix, stripHexPrefix } from '../utils/hex.js';
14
13
  import { TRANSACTION_CATEGORY_EIP7702 } from '../utils/interfaces.js';
15
14
  import { NonceTracker } from './NonceTracker.js';
16
15
  import { PendingTransactionTracker } from './PendingTransactionTracker.js';
@@ -244,20 +243,19 @@ class TransactionController extends TransactionStateManager {
244
243
  address: signedAuthorization.address,
245
244
  chainId: BigInt(signedAuthorization.chainId),
246
245
  nonce: BigInt(signedAuthorization.nonce),
247
- signature: Signature.from({
246
+ signature: {
248
247
  r: signedAuthorization.r,
249
248
  s: signedAuthorization.s,
250
249
  yParity: parseInt(signedAuthorization.yParity, 16)
251
- })
250
+ }
252
251
  });
253
252
  }
254
253
  txMeta.transaction.authorizationList = signedAuthorizations;
255
254
  }
256
- const tx = Transaction.from(txParams);
257
- const signedEthTx = await this.signEthTx(tx, fromAddress);
255
+ const signedEthTx = await this.signEthTx(txParams, fromAddress);
258
256
  txMeta.r = signedEthTx.signature.r;
259
257
  txMeta.s = signedEthTx.signature.s;
260
- txMeta.v = addHexPrefix(signedEthTx.signature.v.toString(16));
258
+ txMeta.v = signedEthTx.signature.v != null ? toHex(signedEthTx.signature.v) : undefined;
261
259
  const rawTx = signedEthTx.serialized;
262
260
  txMeta.rawTransaction = rawTx;
263
261
  this.updateTransactionInState(txMeta, "transactions#signTransaction: add r, s, v values and rawTx");
@@ -276,7 +274,6 @@ class TransactionController extends TransactionStateManager {
276
274
  } catch (error) {
277
275
  if (error.message.toLowerCase().includes("known transaction")) {
278
276
  txHash = keccak256(addHexPrefix(rawTx));
279
- txHash = addHexPrefix(txHash);
280
277
  } else {
281
278
  throw error;
282
279
  }
@@ -1,10 +1,9 @@
1
1
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
2
  import _defineProperty from '@babel/runtime/helpers/defineProperty';
3
- import { stripHexPrefix, addHexPrefix } from '@ethereumjs/util';
4
3
  import { cloneDeep } from '@toruslabs/base-controllers';
5
- import { BN } from 'bn.js';
6
4
  import log from 'loglevel';
7
5
  import { DUMMY_AUTHORIZATION_SIGNATURE } from '../utils/eip7702Types.js';
6
+ import { addHexPrefix } from '../utils/hex.js';
8
7
  import { isEip7702SetCodeTx } from './TransactionUtils.js';
9
8
 
10
9
  class TransactionGasUtil {
@@ -17,9 +16,9 @@ class TransactionGasUtil {
17
16
  async analyzeGasUsage(txMeta) {
18
17
  const block = await this.blockTracker.getLatestBlock();
19
18
  // fallback to block gasLimit
20
- const blockGasLimitBN = new BN(stripHexPrefix(block.gasLimit), 16);
21
- const saferGasLimitBN = blockGasLimitBN.mul(new BN(19)).div(new BN(20));
22
- let estimatedGasHex = addHexPrefix(saferGasLimitBN.toString("hex"));
19
+ const blockGasLimit = BigInt(addHexPrefix(block.gasLimit));
20
+ const saferGasLimit = blockGasLimit * 19n / 20n;
21
+ let estimatedGasHex = addHexPrefix(saferGasLimit.toString(16));
23
22
  let simulationFails;
24
23
  try {
25
24
  estimatedGasHex = await this.estimateTxGas(txMeta);
@@ -45,17 +44,17 @@ class TransactionGasUtil {
45
44
  Adds a gas buffer with out exceeding the block gas limit
46
45
  */
47
46
  addGasBuffer(initialGasLimitHex, blockGasLimitHex, multiplier = 1.5) {
48
- const initialGasLimitBn = new BN(stripHexPrefix(initialGasLimitHex), 16);
49
- const blockGasLimitBn = new BN(stripHexPrefix(blockGasLimitHex), 16);
50
- const upperGasLimitBn = blockGasLimitBn.muln(0.9);
51
- const bufferedGasLimitBn = initialGasLimitBn.muln(multiplier);
47
+ const initialGasLimit = BigInt(addHexPrefix(initialGasLimitHex));
48
+ const blockGasLimit = BigInt(addHexPrefix(blockGasLimitHex));
49
+ const upperGasLimit = blockGasLimit * 9n / 10n;
50
+ const bufferedGasLimit = initialGasLimit * BigInt(Math.round(multiplier * 10)) / 10n;
52
51
 
53
52
  // if initialGasLimit is above blockGasLimit, dont modify it
54
- if (initialGasLimitBn.gt(upperGasLimitBn)) return addHexPrefix(initialGasLimitBn.toString("hex"));
53
+ if (initialGasLimit > upperGasLimit) return addHexPrefix(initialGasLimit.toString(16));
55
54
  // if bufferedGasLimit is below blockGasLimit, use bufferedGasLimit
56
- if (bufferedGasLimitBn.lt(upperGasLimitBn)) return addHexPrefix(bufferedGasLimitBn.toString("hex"));
55
+ if (bufferedGasLimit < upperGasLimit) return addHexPrefix(bufferedGasLimit.toString(16));
57
56
  // otherwise use blockGasLimit
58
- return addHexPrefix(upperGasLimitBn.toString("hex"));
57
+ return addHexPrefix(upperGasLimit.toString(16));
59
58
  }
60
59
 
61
60
  /**
@@ -1,15 +1,11 @@
1
1
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
2
- import { addHexPrefix, isHexString, isValidAddress } from '@ethereumjs/util';
3
- import { randomId, TransactionStatus, TRANSACTION_TYPES } from '@toruslabs/base-controllers';
2
+ import { TRANSACTION_TYPES, TransactionStatus, randomId } from '@toruslabs/base-controllers';
4
3
  import { rpcErrors } from '@web3auth/auth';
5
- import { Interface } from 'ethers';
6
4
  import log from 'loglevel';
5
+ import { isHex, decodeFunctionData, isAddress } from 'viem';
7
6
  import { erc20Abi, erc721Abi, erc1155Abi } from '../utils/abis.js';
8
- import { CONTRACT_TYPE_ERC20, CONTRACT_TYPE_ERC721, CONTRACT_TYPE_ERC1155, METHOD_TYPES, CONTRACT_TYPE_ETH, TRANSACTION_ENVELOPE_TYPES } from '../utils/constants.js';
9
-
10
- const erc20Interface = new Interface(erc20Abi);
11
- const erc721Interface = new Interface(erc721Abi);
12
- const erc1155Interface = new Interface(erc1155Abi);
7
+ import { CONTRACT_TYPE_ETH, TRANSACTION_ENVELOPE_TYPES, CONTRACT_TYPE_ERC20, CONTRACT_TYPE_ERC721, CONTRACT_TYPE_ERC1155, METHOD_TYPES } from '../utils/constants.js';
8
+ import { addHexPrefix } from '../utils/hex.js';
13
9
 
14
10
  // functions that handle normalizing of that key in txParams
15
11
 
@@ -65,7 +61,9 @@ function transactionMatchesNetwork(transaction, chainId) {
65
61
  */
66
62
  function isEIP1559Transaction(transaction) {
67
63
  var _transaction$transact, _transaction$transact2;
68
- return isHexString(addHexPrefix(transaction === null || transaction === void 0 || (_transaction$transact = transaction.transaction) === null || _transaction$transact === void 0 ? void 0 : _transaction$transact.maxFeePerGas)) && isHexString(addHexPrefix(transaction === null || transaction === void 0 || (_transaction$transact2 = transaction.transaction) === null || _transaction$transact2 === void 0 ? void 0 : _transaction$transact2.maxPriorityFeePerGas));
64
+ const maxFeePerGas = transaction === null || transaction === void 0 || (_transaction$transact = transaction.transaction) === null || _transaction$transact === void 0 ? void 0 : _transaction$transact.maxFeePerGas;
65
+ const maxPriorityFeePerGas = transaction === null || transaction === void 0 || (_transaction$transact2 = transaction.transaction) === null || _transaction$transact2 === void 0 ? void 0 : _transaction$transact2.maxPriorityFeePerGas;
66
+ return typeof maxFeePerGas === "string" && typeof maxPriorityFeePerGas === "string" && isHex(addHexPrefix(maxFeePerGas)) && isHex(addHexPrefix(maxPriorityFeePerGas));
69
67
  }
70
68
 
71
69
  /**
@@ -78,7 +76,7 @@ function isEIP1559Transaction(transaction) {
78
76
  * EIP1559 fields
79
77
  */
80
78
  function isLegacyTransaction(transaction) {
81
- return typeof transaction.transaction.maxFeePerGas === "undefined" && typeof transaction.transaction.maxPriorityFeePerGas === "undefined" && (typeof transaction.transaction.gasPrice === "undefined" || isHexString(addHexPrefix(transaction.transaction.gasPrice)));
79
+ return typeof transaction.transaction.maxFeePerGas === "undefined" && typeof transaction.transaction.maxPriorityFeePerGas === "undefined" && (typeof transaction.transaction.gasPrice === "undefined" || isHex(addHexPrefix(transaction.transaction.gasPrice)));
82
80
  }
83
81
 
84
82
  /**
@@ -129,7 +127,7 @@ function validateFrom(txParams) {
129
127
  if (!(typeof txParams.from === "string")) {
130
128
  throw rpcErrors.invalidParams(`Invalid "from" address "${txParams.from}": not a string.`);
131
129
  }
132
- if (!isValidAddress(txParams.from)) {
130
+ if (!isAddress(txParams.from)) {
133
131
  throw rpcErrors.invalidParams('Invalid "from" address.');
134
132
  }
135
133
  }
@@ -144,7 +142,7 @@ function validateRecipient(txParameters) {
144
142
  } else {
145
143
  throw rpcErrors.invalidParams('Invalid "to" address.');
146
144
  }
147
- } else if (txParameters.to !== undefined && !isValidAddress(txParameters.to)) {
145
+ } else if (txParameters.to !== undefined && !isAddress(txParameters.to)) {
148
146
  throw rpcErrors.invalidParams('Invalid "to" address.');
149
147
  }
150
148
  return txParameters;
@@ -213,10 +211,10 @@ function validateTxParameters(txParams, eip1559Compatibility = true) {
213
211
  if (typeof authorization !== "object" || authorization === null) {
214
212
  throw rpcErrors.invalidParams(`Invalid transaction params: ${key} is not an object. got: (${authorization})`);
215
213
  }
216
- if (typeof authorization.address !== "string" || !isValidAddress(authorization.address)) {
214
+ if (typeof authorization.address !== "string" || !isAddress(authorization.address)) {
217
215
  throw rpcErrors.invalidParams(`Invalid transaction params: authorization.address is not a valid address. got: (${authorization.address})`);
218
216
  }
219
- if (typeof authorization.chainId !== "string" || !isHexString(addHexPrefix(authorization.chainId))) {
217
+ if (typeof authorization.chainId !== "string" || !isHex(addHexPrefix(authorization.chainId))) {
220
218
  throw rpcErrors.invalidParams(`Invalid transaction params: authorization.chainId is not a valid hex string. got: (${authorization.chainId})`);
221
219
  }
222
220
  });
@@ -247,36 +245,42 @@ function getFinalStates() {
247
245
  }
248
246
  function parseStandardTokenTransactionData(data) {
249
247
  try {
250
- const txDesc = erc20Interface.parseTransaction({
251
- data
248
+ var _txDesc$args;
249
+ const txDesc = decodeFunctionData({
250
+ abi: erc20Abi,
251
+ data: data
252
252
  });
253
253
  if (txDesc) return {
254
- name: txDesc.name,
255
- methodParams: txDesc.args.toArray(),
254
+ name: txDesc.functionName,
255
+ methodParams: [...((_txDesc$args = txDesc.args) !== null && _txDesc$args !== void 0 ? _txDesc$args : [])],
256
256
  type: CONTRACT_TYPE_ERC20
257
257
  };
258
258
  } catch {
259
259
  // ignore and next try to parse with erc721 ABI
260
260
  }
261
261
  try {
262
- const txDesc = erc721Interface.parseTransaction({
263
- data
262
+ var _txDesc$args2;
263
+ const txDesc = decodeFunctionData({
264
+ abi: erc721Abi,
265
+ data: data
264
266
  });
265
267
  if (txDesc) return {
266
- name: txDesc.name,
267
- methodParams: txDesc.args.toArray(),
268
+ name: txDesc.functionName,
269
+ methodParams: [...((_txDesc$args2 = txDesc.args) !== null && _txDesc$args2 !== void 0 ? _txDesc$args2 : [])],
268
270
  type: CONTRACT_TYPE_ERC721
269
271
  };
270
272
  } catch {
271
273
  // ignore and next try to parse with erc1155 ABI
272
274
  }
273
275
  try {
274
- const txDesc = erc1155Interface.parseTransaction({
275
- data
276
+ var _txDesc$args3;
277
+ const txDesc = decodeFunctionData({
278
+ abi: erc1155Abi,
279
+ data: data
276
280
  });
277
281
  if (txDesc) return {
278
- name: txDesc.name,
279
- methodParams: txDesc.args.toArray(),
282
+ name: txDesc.functionName,
283
+ methodParams: [...((_txDesc$args3 = txDesc.args) !== null && _txDesc$args3 !== void 0 ? _txDesc$args3 : [])],
280
284
  type: CONTRACT_TYPE_ERC1155
281
285
  };
282
286
  } catch {
@@ -30,7 +30,7 @@ export { createNestedTransactionMeta, determineTransactionType, ensureFieldIsStr
30
30
  export { erc1155Abi, erc20Abi, erc721Abi, erc7821Abi, singleBalanceCheckerAbi } from './utils/abis.js';
31
31
  export { ARBITRUM_MAINNET_CHAIN_ID, ARBITRUM_TESTNET_CHAIN_ID, AVALANCHE_MAINNET_CHAIN_ID, AVALANCHE_TESTNET_CHAIN_ID, BASE_CHAIN_ID, BASE_TESTNET_CHAIN_ID, BSC_MAINNET_CHAIN_ID, BSC_TESTNET_CHAIN_ID, BUNDLER_METHOD_TYPES, CELO_MAINNET_CHAIN_ID, CHAIN_ID_TO_GAS_LIMIT_BUFFER_MAP, COINGECKO_PLATFORMS_CHAIN_CODE_MAP, COINGECKO_SUPPORTED_CURRENCIES, CONTRACT_TYPE_ERC1155, CONTRACT_TYPE_ERC20, CONTRACT_TYPE_ERC721, CONTRACT_TYPE_ERC7821, CONTRACT_TYPE_ETH, ERC1155_INTERFACE_ID, ERC721_ENUMERABLE_INTERFACE_ID, ERC721_INTERFACE_ID, ERC721_METADATA_INTERFACE_ID, GAS_ESTIMATE_TYPES, LINEA_CHAIN_ID, LINEA_SEPOLIA_CHAIN_ID, LOCALHOST, MAINNET_CHAIN_ID, METHOD_TYPES, MM_NFT_API_SUPPORTED_CHAINS, MM_TOKEN_API_SUPPORTED_CHAINS, OLD_ERC721_LIST, OPTIMISM_MAINNET_CHAIN_ID, OPTIMISM_TESTNET_CHAIN_ID, PAYMASTER_METHOD_TYPES, POLYGON_AMOY_CHAIN_ID, POLYGON_CHAIN_ID, SEPOLIA_CHAIN_ID, SMART_ACCOUNT, SUPPORTED_NETWORKS, TEST_CHAINS, TRANSACTION_ENVELOPE_TYPES, XDAI_CHAIN_ID } from './utils/constants.js';
32
32
  export { addCurrencies, conversionGTE, conversionGreaterThan, conversionLTE, conversionLessThan, conversionMax, conversionUtil, decGWEIToHexWEI, getBigNumber, hexWEIToDecGWEI, multiplyCurrencies, subtractCurrencies, toNegative } from './utils/conversionUtils.js';
33
- export { EIP5792ErrorCode, EIP_5792_METHODS, Eip5792AtomicStatus, GetCallsStatusCode, SUPPORTED_EIP_5792_VERSIONS } from './utils/eip5792Types.js';
33
+ export { EIP5792ErrorCode, EIP_5792_METHODS, Eip5792AtomicStatus, GetCallsStatusCode } from './utils/eip5792Types.js';
34
34
  export { DUMMY_AUTHORIZATION_SIGNATURE, EIP_7702_METHODS, EIP_7702_PREFIX, EIP_7702_REVOKE_ADDRESS } from './utils/eip7702Types.js';
35
35
  export { BNToHex, GAS_LIMITS, addEtherscanTransactions, bnLessThan, formatDate, formatPastTx, formatTime, getChainType, getEthTxStatus, getEtherScanHashLink, getIpfsEndpoint, hexToBn, isAddressByChainId, sanitizeNftMetdataUrl, toChecksumAddressByChainId } from './utils/helpers.js';
36
36
  export { TRANSACTION_CATEGORY_EIP7702 } from './utils/interfaces.js';
@@ -40,6 +40,6 @@ export { MetamaskSmartAccount } from './AccountAbstraction/smartAccounts/Metamas
40
40
  export { NexusSmartAccount } from './AccountAbstraction/smartAccounts/NexusSmartAccount.js';
41
41
  export { SafeSmartAccount } from './AccountAbstraction/smartAccounts/SafeSmartAccount.js';
42
42
  export { TrustSmartAccount } from './AccountAbstraction/smartAccounts/TrustSmartAccount.js';
43
+ export { generateBatchId, processMultipleTransactions, processSingleTransaction, validateSendCallsParams, walletSendCalls } from './Eip5792/walletSendCalls.js';
43
44
  export { mapTransactionStatusToEip5792Status, walletGetCallsStatus } from './Eip5792/walletGetCallsStatus.js';
44
45
  export { walletGetCapabilities } from './Eip5792/walletGetCapabilities.js';
45
- export { generateBatchId, processMultipleTransactions, processSingleTransaction, validateSendCallsParams, walletSendCalls } from './Eip5792/walletSendCalls.js';
@@ -7,6 +7,7 @@ const erc20Abi = [{
7
7
  type: "string"
8
8
  }],
9
9
  payable: false,
10
+ stateMutability: "view",
10
11
  type: "function"
11
12
  }, {
12
13
  constant: false,
@@ -23,6 +24,7 @@ const erc20Abi = [{
23
24
  type: "bool"
24
25
  }],
25
26
  payable: false,
27
+ stateMutability: "nonpayable",
26
28
  type: "function"
27
29
  }, {
28
30
  constant: true,
@@ -33,6 +35,7 @@ const erc20Abi = [{
33
35
  type: "uint256"
34
36
  }],
35
37
  payable: false,
38
+ stateMutability: "view",
36
39
  type: "function"
37
40
  }, {
38
41
  constant: false,
@@ -52,6 +55,7 @@ const erc20Abi = [{
52
55
  type: "bool"
53
56
  }],
54
57
  payable: false,
58
+ stateMutability: "nonpayable",
55
59
  type: "function"
56
60
  }, {
57
61
  constant: true,
@@ -62,6 +66,7 @@ const erc20Abi = [{
62
66
  type: "uint256"
63
67
  }],
64
68
  payable: false,
69
+ stateMutability: "view",
65
70
  type: "function"
66
71
  }, {
67
72
  constant: true,
@@ -75,6 +80,7 @@ const erc20Abi = [{
75
80
  type: "uint256"
76
81
  }],
77
82
  payable: false,
83
+ stateMutability: "view",
78
84
  type: "function"
79
85
  }, {
80
86
  constant: true,
@@ -85,6 +91,7 @@ const erc20Abi = [{
85
91
  type: "string"
86
92
  }],
87
93
  payable: false,
94
+ stateMutability: "view",
88
95
  type: "function"
89
96
  }, {
90
97
  constant: false,
@@ -101,6 +108,7 @@ const erc20Abi = [{
101
108
  type: "bool"
102
109
  }],
103
110
  payable: false,
111
+ stateMutability: "nonpayable",
104
112
  type: "function"
105
113
  }, {
106
114
  constant: false,
@@ -120,6 +128,7 @@ const erc20Abi = [{
120
128
  type: "bool"
121
129
  }],
122
130
  payable: false,
131
+ stateMutability: "nonpayable",
123
132
  type: "function"
124
133
  }, {
125
134
  constant: true,
@@ -136,6 +145,7 @@ const erc20Abi = [{
136
145
  type: "uint256"
137
146
  }],
138
147
  payable: false,
148
+ stateMutability: "view",
139
149
  type: "function"
140
150
  }, {
141
151
  inputs: [{
@@ -151,9 +161,11 @@ const erc20Abi = [{
151
161
  name: "_tokenSymbol",
152
162
  type: "string"
153
163
  }],
164
+ stateMutability: "nonpayable",
154
165
  type: "constructor"
155
166
  }, {
156
167
  payable: false,
168
+ stateMutability: "nonpayable",
157
169
  type: "fallback"
158
170
  }];
159
171
  const erc721Abi = [{
@@ -1,9 +1,8 @@
1
1
  import _objectDestructuringEmpty from '@babel/runtime/helpers/objectDestructuringEmpty';
2
2
  import _objectSpread from '@babel/runtime/helpers/objectSpread2';
3
3
  import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
4
- import { stripHexPrefix } from '@ethereumjs/util';
5
4
  import { BigNumber } from 'bignumber.js';
6
- import BN from 'bn.js';
5
+ import { stripHexPrefix } from './hex.js';
7
6
 
8
7
  const _excluded = ["aBase", "bBase"],
9
8
  _excluded2 = ["aBase", "bBase"],
@@ -32,7 +31,7 @@ const toSpecifiedDenomination = {
32
31
  const baseChange = {
33
32
  hex: n => n.toString(16),
34
33
  dec: n => new BigNumber(n).toString(10),
35
- BN: n => new BN(n.toString(16))
34
+ BN: n => new BigNumber(n.toString(16))
36
35
  };
37
36
 
38
37
  // Utility function for checking base types