@tetherto/wdk-wallet-evm-erc-4337 1.0.0-beta.1 → 1.0.0-beta.2

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.
package/bare.js CHANGED
@@ -13,8 +13,8 @@
13
13
  // limitations under the License.
14
14
  'use strict'
15
15
 
16
- import 'bare-wdk-runtime'
16
+ import 'bare-node-runtime/global'
17
17
 
18
- export * from './index.js' with { imports: 'bare-wdk-runtime/package' }
18
+ export * from './index.js' with { imports: 'bare-node-runtime/imports' }
19
19
 
20
- export { default } from './index.js' with { imports: 'bare-wdk-runtime/package' }
20
+ export { default } from './index.js' with { imports: 'bare-node-runtime/imports' }
package/index.js CHANGED
@@ -22,6 +22,7 @@
22
22
  /** @typedef {import('@tetherto/wdk-wallet-evm').TransferOptions} TransferOptions */
23
23
  /** @typedef {import('@tetherto/wdk-wallet-evm').TransferResult} TransferResult */
24
24
  /** @typedef {import('@tetherto/wdk-wallet-evm').EvmTransactionReceipt} EvmTransactionReceipt */
25
+ /** @typedef {import('@tetherto/wdk-wallet-evm').ApproveOptions} ApproveOptions */
25
26
 
26
27
  /** @typedef {import('./src/wallet-manager-evm-erc-4337.js').EvmErc4337WalletConfig} EvmErc4337WalletConfig */
27
28
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tetherto/wdk-wallet-evm-erc-4337",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.2",
4
4
  "description": "A simple package to manage BIP-32 wallets for evm blockchains, which implement the erc-4337 standard and its account abstraction features",
5
5
  "keywords": [
6
6
  "wdk",
@@ -25,15 +25,21 @@
25
25
  },
26
26
  "dependencies": {
27
27
  "@tetherto/wdk-wallet": "^1.0.0-beta.1",
28
- "@tetherto/wdk-wallet-evm": "^1.0.0-beta.2",
28
+ "@tetherto/wdk-wallet-evm": "^1.0.0-beta.3",
29
29
  "@wdk-safe-global/relay-kit": "4.1.3",
30
- "bare-wdk-runtime": "2.0.0",
30
+ "bare-node-runtime": "1.0.3",
31
31
  "ethers": "6.14.4"
32
32
  },
33
33
  "devDependencies": {
34
34
  "standard": "17.1.2",
35
35
  "typescript": "5.8.3"
36
36
  },
37
+ "overrides": {
38
+ "axios": "1.12.2",
39
+ "cookie": "1.0.2",
40
+ "tmp": "0.2.5",
41
+ "ws": "8.18.3"
42
+ },
37
43
  "exports": {
38
44
  ".": {
39
45
  "types": "./types/index.d.ts",
@@ -45,7 +51,7 @@
45
51
  }
46
52
  },
47
53
  "publishConfig": {
48
- "access": "restricted",
54
+ "access": "public",
49
55
  "registry": "https://registry.npmjs.org/"
50
56
  },
51
57
  "standard": {
@@ -14,6 +14,8 @@
14
14
 
15
15
  'use strict'
16
16
 
17
+ import { Contract } from 'ethers'
18
+
17
19
  import { WalletAccountEvm } from '@tetherto/wdk-wallet-evm'
18
20
 
19
21
  import { Safe4337Pack } from '@wdk-safe-global/relay-kit'
@@ -30,11 +32,14 @@ import WalletAccountReadOnlyEvmErc4337, { SALT_NONCE } from './wallet-account-re
30
32
  /** @typedef {import('@tetherto/wdk-wallet-evm').TransactionResult} TransactionResult */
31
33
  /** @typedef {import('@tetherto/wdk-wallet-evm').TransferOptions} TransferOptions */
32
34
  /** @typedef {import('@tetherto/wdk-wallet-evm').TransferResult} TransferResult */
35
+ /** @typedef {import('@tetherto/wdk-wallet-evm').ApproveOptions} ApproveOptions */
33
36
 
34
37
  /** @typedef {import('./wallet-account-read-only-evm-erc-4337.js').EvmErc4337WalletConfig} EvmErc4337WalletConfig */
35
38
 
36
39
  const FEE_TOLERANCE_COEFFICIENT = 120n
37
40
 
41
+ const USDT_MAINNET_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7'
42
+
38
43
  /** @implements {IWalletAccount} */
39
44
  export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc4337 {
40
45
  /**
@@ -109,6 +114,42 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
109
114
  return await this._ownerAccount.verify(message, signature)
110
115
  }
111
116
 
117
+ /**
118
+ * Approves a specific amount of tokens to a spender.
119
+ *
120
+ * @param {ApproveOptions} options - The approve options.
121
+ * @returns {Promise<TransactionResult>} - The transaction’s result.
122
+ * @throws {Error} - If trying to approve usdts on ethereum with allowance not equal to zero (due to the usdt allowance reset requirement).
123
+ */
124
+ async approve (options) {
125
+ if (!this._ownerAccount._provider) {
126
+ throw new Error('The wallet must be connected to a provider to approve funds.')
127
+ }
128
+
129
+ const { token, spender, amount } = options
130
+ const chainId = await this._getChainId()
131
+
132
+ if (chainId === 1n && token.toLowerCase() === USDT_MAINNET_ADDRESS.toLowerCase()) {
133
+ const currentAllowance = await this.getAllowance(token, spender)
134
+ if (currentAllowance > 0n && BigInt(amount) > 0n) {
135
+ throw new Error(
136
+ 'USDT requires the current allowance to be reset to 0 before setting a new non-zero value. Please send an "approve" transaction with an amount of 0 first.'
137
+ )
138
+ }
139
+ }
140
+
141
+ const abi = ['function approve(address spender, uint256 amount) returns (bool)']
142
+ const contract = new Contract(token, abi, this._ownerAccount._provider)
143
+
144
+ const tx = {
145
+ to: token,
146
+ value: 0,
147
+ data: contract.interface.encodeFunctionData('approve', [spender, amount])
148
+ }
149
+
150
+ return await this.sendTransaction(tx)
151
+ }
152
+
112
153
  /**
113
154
  * Sends a transaction.
114
155
  *
@@ -217,7 +258,7 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
217
258
  transactions: txs.map(tx => ({ from: address, ...tx })),
218
259
  options: {
219
260
  validUntil: twoMinutesFromNow,
220
- feeEstimator: this._feeEstimator,
261
+ feeEstimator: await this._getFeeEstimator(),
221
262
  ...options
222
263
  }
223
264
  })
@@ -75,12 +75,17 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
75
75
  * The safe's fee estimator.
76
76
  *
77
77
  * @protected
78
- * @type {GenericFeeEstimator}
78
+ * @type {GenericFeeEstimator | undefined}
79
79
  */
80
- this._feeEstimator = new GenericFeeEstimator(
81
- config.provider,
82
- `0x${config.chainId.toString(16)}`
83
- )
80
+ this._feeEstimator = undefined
81
+
82
+ /**
83
+ * The chain id.
84
+ *
85
+ * @protected
86
+ * @type {bigint | undefined}
87
+ */
88
+ this._chainId = undefined
84
89
 
85
90
  /** @private */
86
91
  this._ownerAccountAddress = address
@@ -186,6 +191,18 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
186
191
  return await evmReadOnlyAccount.getTransactionReceipt(transactionHash)
187
192
  }
188
193
 
194
+ /**
195
+ * Returns the current allowance for the given token and spender.
196
+ * @param {string} token - The token’s address.
197
+ * @param {string} spender - The spender’s address.
198
+ * @returns {Promise<bigint>} - The allowance.
199
+ */
200
+ async getAllowance (token, spender) {
201
+ const readOnlyAccount = await this._getEvmReadOnlyAccount()
202
+
203
+ return await readOnlyAccount.getAllowance(token, spender)
204
+ }
205
+
189
206
  /**
190
207
  * Returns the safe's erc-4337 pack of the account.
191
208
  *
@@ -218,6 +235,24 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
218
235
  return this._safe4337Pack
219
236
  }
220
237
 
238
+ /**
239
+ * Returns the chain id.
240
+ *
241
+ * @protected
242
+ * @returns {Promise<bigint>} - The chain id.
243
+ */
244
+ async _getChainId () {
245
+ if (!this._chainId) {
246
+ const evmReadOnlyAccount = await this._getEvmReadOnlyAccount()
247
+
248
+ const { chainId } = await evmReadOnlyAccount._provider.getNetwork()
249
+
250
+ this._chainId = chainId
251
+ }
252
+
253
+ return this._chainId
254
+ }
255
+
221
256
  /** @private */
222
257
  async _getEvmReadOnlyAccount () {
223
258
  const address = await this.getAddress()
@@ -227,6 +262,20 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
227
262
  return evmReadOnlyAccount
228
263
  }
229
264
 
265
+ /** @private */
266
+ async _getFeeEstimator () {
267
+ if (!this._feeEstimator) {
268
+ const chainId = await this._getChainId()
269
+
270
+ this._feeEstimator = new GenericFeeEstimator(
271
+ this._config.provider,
272
+ `0x${chainId.toString(16)}`
273
+ )
274
+ }
275
+
276
+ return this._feeEstimator
277
+ }
278
+
230
279
  /** @private */
231
280
  async _getUserOperationGasCost (txs, options) {
232
281
  const safe4337Pack = await this._getSafe4337Pack()
@@ -237,7 +286,7 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
237
286
  const safeOperation = await safe4337Pack.createTransaction({
238
287
  transactions: txs.map(tx => ({ from: address, ...tx })),
239
288
  options: {
240
- feeEstimator: this._feeEstimator,
289
+ feeEstimator: await this._getFeeEstimator(),
241
290
  ...options
242
291
  }
243
292
  })
package/types/index.d.ts CHANGED
@@ -8,4 +8,5 @@ export type TransactionResult = import("@tetherto/wdk-wallet-evm").TransactionRe
8
8
  export type TransferOptions = import("@tetherto/wdk-wallet-evm").TransferOptions;
9
9
  export type TransferResult = import("@tetherto/wdk-wallet-evm").TransferResult;
10
10
  export type EvmTransactionReceipt = import("@tetherto/wdk-wallet-evm").EvmTransactionReceipt;
11
+ export type ApproveOptions = import("@tetherto/wdk-wallet-evm").ApproveOptions;
11
12
  export type EvmErc4337WalletConfig = import("./src/wallet-manager-evm-erc-4337.js").EvmErc4337WalletConfig;
@@ -50,10 +50,18 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
50
50
  * @returns {Promise<boolean>} True if the signature is valid.
51
51
  */
52
52
  verify(message: string, signature: string): Promise<boolean>;
53
+ /**
54
+ * Approves a specific amount of tokens to a spender.
55
+ *
56
+ * @param {ApproveOptions} options - The approve options.
57
+ * @returns {Promise<TransactionResult>} - The transaction’s result.
58
+ * @throws {Error} - If trying to approve usdts on ethereum with allowance not equal to zero (due to the usdt allowance reset requirement).
59
+ */
60
+ approve(options: ApproveOptions): Promise<TransactionResult>;
53
61
  /**
54
62
  * Sends a transaction.
55
63
  *
56
- * @param {EvmTransaction | EvmTransaction[]} tx - The transaction, or an array of multiple transactions to send in batch.
64
+ * @param {EvmTransaction | EvmTransaction[]} tx - The transaction, or an array of multiple transactions to send in batch.
57
65
  * @param {Pick<EvmErc4337WalletConfig, 'paymasterToken'>} [config] - If set, overrides the 'paymasterToken' option defined in the wallet account configuration.
58
66
  * @returns {Promise<TransactionResult>} The transaction's result.
59
67
  */
@@ -86,5 +94,6 @@ export type EvmTransaction = import("@tetherto/wdk-wallet-evm").EvmTransaction;
86
94
  export type TransactionResult = import("@tetherto/wdk-wallet-evm").TransactionResult;
87
95
  export type TransferOptions = import("@tetherto/wdk-wallet-evm").TransferOptions;
88
96
  export type TransferResult = import("@tetherto/wdk-wallet-evm").TransferResult;
97
+ export type ApproveOptions = import("@tetherto/wdk-wallet-evm").ApproveOptions;
89
98
  export type EvmErc4337WalletConfig = import("./wallet-account-read-only-evm-erc-4337.js").EvmErc4337WalletConfig;
90
99
  import WalletAccountReadOnlyEvmErc4337 from './wallet-account-read-only-evm-erc-4337.js';
@@ -24,9 +24,16 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
24
24
  * The safe's fee estimator.
25
25
  *
26
26
  * @protected
27
- * @type {GenericFeeEstimator}
27
+ * @type {GenericFeeEstimator | undefined}
28
28
  */
29
- protected _feeEstimator: GenericFeeEstimator;
29
+ protected _feeEstimator: GenericFeeEstimator | undefined;
30
+ /**
31
+ * The chain id.
32
+ *
33
+ * @protected
34
+ * @type {bigint | undefined}
35
+ */
36
+ protected _chainId: bigint | undefined;
30
37
  /** @private */
31
38
  private _ownerAccountAddress;
32
39
  /**
@@ -71,6 +78,13 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
71
78
  * @returns {Promise<EvmTransactionReceipt | null>} – The receipt, or null if the transaction has not been included in a block yet.
72
79
  */
73
80
  getTransactionReceipt(hash: string): Promise<EvmTransactionReceipt | null>;
81
+ /**
82
+ * Returns the current allowance for the given token and spender.
83
+ * @param {string} token - The token’s address.
84
+ * @param {string} spender - The spender’s address.
85
+ * @returns {Promise<bigint>} - The allowance.
86
+ */
87
+ getAllowance(token: string, spender: string): Promise<bigint>;
74
88
  /**
75
89
  * Returns the safe's erc-4337 pack of the account.
76
90
  *
@@ -78,9 +92,18 @@ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOn
78
92
  * @returns {Promise<Safe4337Pack>} The safe's erc-4337 pack.
79
93
  */
80
94
  protected _getSafe4337Pack(): Promise<Safe4337Pack>;
95
+ /**
96
+ * Returns the chain id.
97
+ *
98
+ * @protected
99
+ * @returns {Promise<bigint>} - The chain id.
100
+ */
101
+ protected _getChainId(): Promise<bigint>;
81
102
  /** @private */
82
103
  private _getEvmReadOnlyAccount;
83
104
  /** @private */
105
+ private _getFeeEstimator;
106
+ /** @private */
84
107
  private _getUserOperationGasCost;
85
108
  }
86
109
  export type Eip1193Provider = import("ethers").Eip1193Provider;