@tetherto/wdk-wallet-evm 1.0.0-beta.2 → 1.0.0-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +50 -0
- package/README.md +171 -218
- package/bare.js +3 -3
- package/{hardhat.config.cjs → hardhat.config.js} +4 -3
- package/index.js +24 -1
- package/package.json +31 -14
- package/src/memory-safe/hd-node-wallet.js +5 -4
- package/src/memory-safe/signing-key.js +4 -3
- package/src/multicall.js +75 -0
- package/src/signers/bare.js +20 -0
- package/src/signers/index.js +18 -0
- package/src/signers/private-key-signer-evm.js +166 -0
- package/src/signers/seed-signer-evm.js +185 -0
- package/src/signers/signer-evm.js +69 -0
- package/src/wallet-account-evm.js +307 -68
- package/src/wallet-account-read-only-evm.js +509 -33
- package/src/wallet-manager-evm.js +142 -40
- package/types/index.d.ts +20 -1
- package/types/src/signers/index.d.ts +4 -0
- package/types/src/signers/private-key-signer-evm.d.ts +106 -0
- package/types/src/signers/seed-signer-evm.d.ts +126 -0
- package/types/src/signers/signer-evm.d.ts +41 -0
- package/types/src/wallet-account-evm.d.ts +166 -32
- package/types/src/wallet-account-read-only-evm.d.ts +260 -14
- package/types/src/wallet-manager-evm.d.ts +79 -17
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Copyright 2024 Tether Operations Limited
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
'use strict'
|
|
15
|
+
|
|
16
|
+
import { ISigner, NotImplementedError } from '@tetherto/wdk-wallet'
|
|
17
|
+
|
|
18
|
+
/** @typedef {import('ethers').TransactionLike} TransactionLike */
|
|
19
|
+
/** @typedef {import('ethers').AuthorizationRequest} AuthorizationRequest */
|
|
20
|
+
/** @typedef {import('ethers').Authorization} Authorization */
|
|
21
|
+
/** @typedef {import('../wallet-account-read-only-evm.js').TypedData} TypedData */
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Interface for EVM signers, extending the base `ISigner` from `@tetherto/wdk-wallet`.
|
|
25
|
+
*
|
|
26
|
+
* @interface
|
|
27
|
+
*/
|
|
28
|
+
export class ISignerEvm extends ISigner {
|
|
29
|
+
/**
|
|
30
|
+
* The account's address, if available.
|
|
31
|
+
*
|
|
32
|
+
* @deprecated Use {@link getAddress} instead. This property will be removed in an upcoming
|
|
33
|
+
* release: not all signers (e.g. hardware signers) can expose the address synchronously.
|
|
34
|
+
* @type {string | undefined}
|
|
35
|
+
*/
|
|
36
|
+
get address () {
|
|
37
|
+
throw new NotImplementedError('address')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Signs a transaction.
|
|
42
|
+
*
|
|
43
|
+
* @param {TransactionLike} tx - The transaction to sign.
|
|
44
|
+
* @returns {Promise<string>} The signed transaction as a hex string.
|
|
45
|
+
*/
|
|
46
|
+
async signTransaction (tx) {
|
|
47
|
+
throw new NotImplementedError('signTransaction(tx)')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Signs typed data according to EIP-712.
|
|
52
|
+
*
|
|
53
|
+
* @param {TypedData} typedData - The typed data to sign.
|
|
54
|
+
* @returns {Promise<string>} The typed data signature.
|
|
55
|
+
*/
|
|
56
|
+
async signTypedData (typedData) {
|
|
57
|
+
throw new NotImplementedError('signTypedData(typedData)')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Signs an ERC-7702 authorization tuple.
|
|
62
|
+
*
|
|
63
|
+
* @param {AuthorizationRequest} auth - The authorization request.
|
|
64
|
+
* @returns {Promise<Authorization>} The signed authorization.
|
|
65
|
+
*/
|
|
66
|
+
async signAuthorization (auth) {
|
|
67
|
+
throw new NotImplementedError('signAuthorization(auth)')
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -14,102 +14,163 @@
|
|
|
14
14
|
|
|
15
15
|
'use strict'
|
|
16
16
|
|
|
17
|
-
import {
|
|
17
|
+
import { Contract, VoidSigner, Transaction, ZeroAddress } from 'ethers'
|
|
18
18
|
|
|
19
|
-
import
|
|
19
|
+
import { MaximumFeeExceededError, ProviderRequiredError, ValueError } from '@tetherto/wdk-wallet'
|
|
20
20
|
|
|
21
21
|
import WalletAccountReadOnlyEvm from './wallet-account-read-only-evm.js'
|
|
22
22
|
|
|
23
|
-
import
|
|
23
|
+
import SeedSignerEvm, { BIP_44_ETH_DERIVATION_PATH_PREFIX } from './signers/seed-signer-evm.js'
|
|
24
|
+
import PrivateKeySignerEvm from './signers/private-key-signer-evm.js'
|
|
24
25
|
|
|
26
|
+
/** @typedef {import('./signers/signer-evm.js').ISignerEvm} ISignerEvm */
|
|
25
27
|
/** @typedef {import('ethers').HDNodeWallet} HDNodeWallet */
|
|
28
|
+
/** @typedef {import('ethers').AuthorizationRequest} AuthorizationRequest */
|
|
29
|
+
/** @typedef {import('ethers').Authorization} Authorization */
|
|
30
|
+
/** @typedef {import('ethers').AuthorizationLike} AuthorizationLike */
|
|
26
31
|
|
|
27
32
|
/** @typedef {import('@tetherto/wdk-wallet').IWalletAccount} IWalletAccount */
|
|
28
33
|
|
|
29
34
|
/** @typedef {import('@tetherto/wdk-wallet').KeyPair} KeyPair */
|
|
30
35
|
/** @typedef {import('@tetherto/wdk-wallet').TransactionResult} TransactionResult */
|
|
31
|
-
/** @typedef {import('@tetherto/wdk-wallet').TransferOptions} TransferOptions */
|
|
32
36
|
/** @typedef {import('@tetherto/wdk-wallet').TransferResult} TransferResult */
|
|
33
37
|
|
|
38
|
+
/** @typedef {import('./wallet-account-read-only-evm.js').TypedData} TypedData */
|
|
34
39
|
/** @typedef {import('./wallet-account-read-only-evm.js').EvmTransaction} EvmTransaction */
|
|
40
|
+
/** @typedef {import('./wallet-account-read-only-evm.js').EvmTransferOptions} EvmTransferOptions */
|
|
41
|
+
/** @typedef {import('./wallet-account-read-only-evm.js').EvmGasOverrides} EvmGasOverrides */
|
|
35
42
|
/** @typedef {import('./wallet-account-read-only-evm.js').EvmWalletConfig} EvmWalletConfig */
|
|
36
43
|
|
|
37
|
-
|
|
44
|
+
/**
|
|
45
|
+
* @typedef {Object} ApproveOptions
|
|
46
|
+
* @property {string} token - The address of the token to approve.
|
|
47
|
+
* @property {string} spender - The spender's address.
|
|
48
|
+
* @property {number | bigint} amount - The amount of tokens to approve to the spender.
|
|
49
|
+
*/
|
|
38
50
|
|
|
39
|
-
/**
|
|
51
|
+
/**
|
|
52
|
+
* The options of a token approval, extended with the optional gas overrides of an evm transaction.
|
|
53
|
+
*
|
|
54
|
+
* @typedef {ApproveOptions & EvmGasOverrides} EvmApproveOptions
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @typedef {Object} SignerOptions
|
|
59
|
+
* @property {boolean} [shouldWipeSignerOnDisposal] - If true, wipes the signer given at construction on calls to the 'dispose' method.
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
// Default BIP-44 account path, relative to BIP_44_ETH_DERIVATION_PATH_PREFIX, used by the
|
|
63
|
+
// seed overload when no path is given.
|
|
64
|
+
const DEFAULT_ACCOUNT_PATH = "0'/0/0"
|
|
65
|
+
|
|
66
|
+
const USDT_MAINNET_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7'
|
|
67
|
+
|
|
68
|
+
const DELEGATION_TX_GAS_LIMIT = 100_000
|
|
69
|
+
|
|
70
|
+
/** @implements {IWalletAccount<string>} */
|
|
40
71
|
export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
41
72
|
/**
|
|
42
|
-
* Creates a new evm wallet account
|
|
73
|
+
* Creates a new evm wallet account from a BIP-39 seed, deriving the account's key at the
|
|
74
|
+
* given BIP-44 path.
|
|
43
75
|
*
|
|
44
|
-
* @
|
|
45
|
-
* @param {string}
|
|
76
|
+
* @overload
|
|
77
|
+
* @param {string | Uint8Array} seed - A BIP-39 mnemonic seed phrase, or a raw BIP-32 master seed (16-64 bytes).
|
|
78
|
+
* @param {string} path - The BIP-44 account path, relative to "m/44'/60'" (e.g. "0'/0/0").
|
|
46
79
|
* @param {EvmWalletConfig} [config] - The configuration object.
|
|
80
|
+
* @throws {ValueError} If the given seed phrase is invalid.
|
|
47
81
|
*/
|
|
48
|
-
constructor (seed, path, config = {}) {
|
|
49
|
-
if (typeof seed === 'string') {
|
|
50
|
-
if (!bip39.validateMnemonic(seed)) {
|
|
51
|
-
throw new Error('The seed phrase is invalid.')
|
|
52
|
-
}
|
|
53
82
|
|
|
54
|
-
|
|
55
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Creates a new evm wallet account from a BIP-39 seed at path m/44'/60'/0'/0/0.
|
|
85
|
+
*
|
|
86
|
+
* @overload
|
|
87
|
+
* @param {string | Uint8Array} seed - A BIP-39 mnemonic seed phrase, or a raw BIP-32 master seed (16-64 bytes).
|
|
88
|
+
* @param {EvmWalletConfig} [config] - The configuration object.
|
|
89
|
+
* @throws {ValueError} If the given seed phrase is invalid.
|
|
90
|
+
*/
|
|
56
91
|
|
|
57
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Creates a new evm wallet account using a signer.
|
|
94
|
+
*
|
|
95
|
+
* @overload
|
|
96
|
+
* @param {ISignerEvm} signer - A signer implementing the EVM signer interface.
|
|
97
|
+
* @param {EvmWalletConfig & SignerOptions} [config] - The configuration object.
|
|
98
|
+
*/
|
|
58
99
|
|
|
59
|
-
|
|
60
|
-
|
|
100
|
+
constructor (seedOrSigner, pathOrConfig = {}, config = {}) {
|
|
101
|
+
const isSeed = typeof seedOrSigner === 'string' || seedOrSigner instanceof Uint8Array
|
|
102
|
+
const hasPath = typeof pathOrConfig === 'string'
|
|
103
|
+
const [signer, configuration] = isSeed
|
|
104
|
+
? [
|
|
105
|
+
new SeedSignerEvm(seedOrSigner, `${BIP_44_ETH_DERIVATION_PATH_PREFIX}/${hasPath ? pathOrConfig : DEFAULT_ACCOUNT_PATH}`),
|
|
106
|
+
hasPath ? config : pathOrConfig
|
|
107
|
+
]
|
|
108
|
+
: [seedOrSigner, pathOrConfig]
|
|
61
109
|
|
|
62
|
-
super(
|
|
110
|
+
super(signer.address, configuration)
|
|
63
111
|
|
|
64
112
|
/**
|
|
65
|
-
*
|
|
113
|
+
* If true, disposes the signer on calls to the 'dispose' method.
|
|
66
114
|
*
|
|
67
115
|
* @protected
|
|
68
|
-
* @type {
|
|
116
|
+
* @type {boolean}
|
|
69
117
|
*/
|
|
70
|
-
this.
|
|
118
|
+
this._shouldWipeSignerOnDisposal = isSeed || Boolean(configuration.shouldWipeSignerOnDisposal)
|
|
71
119
|
|
|
72
120
|
/**
|
|
73
|
-
* The account.
|
|
121
|
+
* The wallet account configuration.
|
|
74
122
|
*
|
|
75
123
|
* @protected
|
|
76
|
-
* @type {
|
|
124
|
+
* @type {EvmWalletConfig}
|
|
77
125
|
*/
|
|
78
|
-
this.
|
|
126
|
+
this._config = configuration
|
|
79
127
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
128
|
+
/** @private */
|
|
129
|
+
this._signer = signer
|
|
83
130
|
}
|
|
84
131
|
|
|
85
132
|
/**
|
|
86
|
-
* The derivation path
|
|
133
|
+
* The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)),
|
|
134
|
+
* or null if the account's signer is not bound to a BIP-44 position (e.g. private-key signers).
|
|
87
135
|
*
|
|
88
|
-
* @type {
|
|
136
|
+
* @type {string | null}
|
|
89
137
|
*/
|
|
90
|
-
get
|
|
91
|
-
return this.
|
|
138
|
+
get path () {
|
|
139
|
+
return this._signer.path
|
|
92
140
|
}
|
|
93
141
|
|
|
94
142
|
/**
|
|
95
|
-
* The
|
|
143
|
+
* The account's key pair.
|
|
96
144
|
*
|
|
97
|
-
*
|
|
145
|
+
* The uint8 arrays are bound to the wallet account, so any external change will reflect to the internal representation. For this reason,
|
|
146
|
+
* it's strongly recommended to treat the key pair as a read-only view of the keys. While it's still technically possible to alter their
|
|
147
|
+
* content, client code should never do so.
|
|
148
|
+
*
|
|
149
|
+
* @type {KeyPair | null}
|
|
98
150
|
*/
|
|
99
|
-
get
|
|
100
|
-
return this.
|
|
151
|
+
get keyPair () {
|
|
152
|
+
return this._signer.keyPair
|
|
101
153
|
}
|
|
102
154
|
|
|
103
155
|
/**
|
|
104
|
-
*
|
|
156
|
+
* Creates a new evm wallet account from a raw private key.
|
|
105
157
|
*
|
|
106
|
-
* @
|
|
158
|
+
* @param {string | Uint8Array} privateKey - The raw private key (hex string with or without 0x, or 32 bytes).
|
|
159
|
+
* @param {EvmWalletConfig} [config] - The configuration object.
|
|
160
|
+
* @returns {WalletAccountEvm} The wallet account.
|
|
107
161
|
*/
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
162
|
+
static fromPrivateKey (privateKey, config = {}) {
|
|
163
|
+
const signer = new PrivateKeySignerEvm(privateKey)
|
|
164
|
+
return new WalletAccountEvm(signer, { ...config, shouldWipeSignerOnDisposal: true })
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Returns the account's address.
|
|
169
|
+
*
|
|
170
|
+
* @returns {Promise<string>} The account's address.
|
|
171
|
+
*/
|
|
172
|
+
async getAddress () {
|
|
173
|
+
return await this._signer.getAddress()
|
|
113
174
|
}
|
|
114
175
|
|
|
115
176
|
/**
|
|
@@ -119,82 +180,260 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
119
180
|
* @returns {Promise<string>} The message's signature.
|
|
120
181
|
*/
|
|
121
182
|
async sign (message) {
|
|
122
|
-
return await this.
|
|
183
|
+
return await this._signer.sign(message)
|
|
123
184
|
}
|
|
124
185
|
|
|
125
186
|
/**
|
|
126
|
-
*
|
|
187
|
+
* Signs typed data according to EIP-712.
|
|
127
188
|
*
|
|
128
|
-
* @param {
|
|
129
|
-
* @
|
|
130
|
-
* @returns {Promise<boolean>} True if the signature is valid.
|
|
189
|
+
* @param {TypedData} typedData - The typed data to sign.
|
|
190
|
+
* @returns {Promise<string>} The typed data signature.
|
|
131
191
|
*/
|
|
132
|
-
async
|
|
133
|
-
|
|
192
|
+
async signTypedData ({ domain, types, message }) {
|
|
193
|
+
return await this._signer.signTypedData({ domain, types, message })
|
|
194
|
+
}
|
|
134
195
|
|
|
135
|
-
|
|
196
|
+
/**
|
|
197
|
+
* Signs a transaction.
|
|
198
|
+
*
|
|
199
|
+
* If a provider is set, it also estimates the transaction's costs and checks them against the transaction max. fee option.
|
|
200
|
+
*
|
|
201
|
+
* @param {EvmTransaction} tx - The transaction to sign.
|
|
202
|
+
* @returns {Promise<string>} The signed transaction as a hex string.
|
|
203
|
+
* @throws {MaximumFeeExceededError} If a provider is set, and the transaction's cost surpasses the transaction max. fee option.
|
|
204
|
+
*/
|
|
205
|
+
async signTransaction (tx) {
|
|
206
|
+
if (this._provider && this._config.transactionMaxFee !== undefined) {
|
|
207
|
+
const { fee } = await this.quoteSendTransaction(tx)
|
|
208
|
+
|
|
209
|
+
if (fee > this._config.transactionMaxFee) {
|
|
210
|
+
throw new MaximumFeeExceededError('Exceeded maximum fee cost for transaction operation.')
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return await this._signer.signTransaction({
|
|
214
|
+
from: await this.getAddress(),
|
|
215
|
+
...tx
|
|
216
|
+
})
|
|
136
217
|
}
|
|
137
218
|
|
|
138
219
|
/**
|
|
139
220
|
* Sends a transaction.
|
|
140
221
|
*
|
|
141
|
-
* @param {EvmTransaction} tx - The transaction.
|
|
222
|
+
* @param {EvmTransaction | string} tx - The transaction, or a signed raw transaction as a hex string.
|
|
142
223
|
* @returns {Promise<TransactionResult>} The transaction's result.
|
|
224
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
225
|
+
* @throws {MaximumFeeExceededError} If the transaction's cost exceeds the maximum transaction fee option.
|
|
226
|
+
* @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or a type 3 transaction omits `maxFeePerBlobGas`.
|
|
143
227
|
*/
|
|
144
228
|
async sendTransaction (tx) {
|
|
145
|
-
if (!this.
|
|
146
|
-
throw new
|
|
229
|
+
if (!this._provider) {
|
|
230
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to send transactions.')
|
|
147
231
|
}
|
|
148
232
|
|
|
149
233
|
const { fee } = await this.quoteSendTransaction(tx)
|
|
234
|
+
if (this._config.transactionMaxFee !== undefined && fee > this._config.transactionMaxFee) {
|
|
235
|
+
throw new MaximumFeeExceededError('Exceeded maximum fee cost for transaction operation.')
|
|
236
|
+
}
|
|
150
237
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
238
|
+
if (typeof tx === 'string') {
|
|
239
|
+
const hash = await this._provider.send('eth_sendRawTransaction', [tx])
|
|
240
|
+
return { hash, fee }
|
|
241
|
+
}
|
|
155
242
|
|
|
243
|
+
// Build, sign and broadcast raw transaction using the signer
|
|
244
|
+
const address = await this.getAddress()
|
|
245
|
+
const voidSigner = new VoidSigner(address, this._provider)
|
|
246
|
+
const populated = await voidSigner.populateTransaction(tx)
|
|
247
|
+
const signed = await this._signer.signTransaction(populated)
|
|
248
|
+
const hash = await this._provider.send('eth_sendRawTransaction', [signed])
|
|
156
249
|
return { hash, fee }
|
|
157
250
|
}
|
|
158
251
|
|
|
252
|
+
/**
|
|
253
|
+
* Quotes the costs of a send transaction operation.
|
|
254
|
+
*
|
|
255
|
+
* The transaction is always simulated through gas estimation, so one that would revert is rejected here instead of
|
|
256
|
+
* reaching the signer. A `gasLimit` set on the transaction replaces the estimated gas in the quote, and a `maxFeePerGas`
|
|
257
|
+
* (or `gasPrice`) set on it replaces the fee rate fetched from the provider, so the quote is the transaction's maximum
|
|
258
|
+
* cost as it will be sent.
|
|
259
|
+
* A signed raw transaction is simulated the same way and quoted from its own gas limit and fee cap.
|
|
260
|
+
*
|
|
261
|
+
* @param {EvmTransaction | string} tx - The transaction, or a signed raw transaction as a hex string.
|
|
262
|
+
* @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
|
|
263
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
264
|
+
* @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or a type 3 transaction omits `maxFeePerBlobGas`.
|
|
265
|
+
* @throws {Error} If the simulation of the transaction reverts, as an ethers error with code `CALL_EXCEPTION`.
|
|
266
|
+
*/
|
|
267
|
+
async quoteSendTransaction (tx) {
|
|
268
|
+
if (typeof tx === 'string') {
|
|
269
|
+
if (!this._provider) {
|
|
270
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to quote send transaction operations.')
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const { from, to, value, data, gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas, type, nonce, chainId, authorizationList } = Transaction.from(tx)
|
|
274
|
+
|
|
275
|
+
await this._estimateGas({ from, to, value, data, gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas, type, nonce, chainId, authorizationList })
|
|
276
|
+
|
|
277
|
+
return { fee: gasLimit * (maxFeePerGas ?? gasPrice) }
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return await super.quoteSendTransaction(tx)
|
|
281
|
+
}
|
|
282
|
+
|
|
159
283
|
/**
|
|
160
284
|
* Transfers a token to another address.
|
|
161
285
|
*
|
|
162
|
-
* @param {
|
|
286
|
+
* @param {EvmTransferOptions} options - The transfer's options, including any gas overrides to carry onto the transaction.
|
|
163
287
|
* @returns {Promise<TransferResult>} The transfer's result.
|
|
288
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
289
|
+
* @throws {MaximumFeeExceededError} If the transfer's cost exceeds the maximum transfer fee option.
|
|
164
290
|
*/
|
|
165
291
|
async transfer (options) {
|
|
166
|
-
if (!this.
|
|
167
|
-
throw new
|
|
292
|
+
if (!this._provider) {
|
|
293
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to transfer tokens.')
|
|
168
294
|
}
|
|
169
295
|
|
|
170
296
|
const tx = await WalletAccountEvm._getTransferTransaction(options)
|
|
171
297
|
|
|
172
298
|
const { fee } = await this.quoteSendTransaction(tx)
|
|
173
299
|
|
|
174
|
-
if (this._config.transferMaxFee !== undefined && fee
|
|
175
|
-
throw new
|
|
300
|
+
if (this._config.transferMaxFee !== undefined && fee > this._config.transferMaxFee) {
|
|
301
|
+
throw new MaximumFeeExceededError('Exceeded maximum fee cost for transfer operation.')
|
|
176
302
|
}
|
|
177
303
|
|
|
178
|
-
const { hash } = await this.
|
|
304
|
+
const { hash } = await this.sendTransaction(tx)
|
|
179
305
|
|
|
180
306
|
return { hash, fee }
|
|
181
307
|
}
|
|
182
308
|
|
|
309
|
+
/**
|
|
310
|
+
* Approves a specific amount of tokens to a spender.
|
|
311
|
+
*
|
|
312
|
+
* @param {EvmApproveOptions} options - The approve options, including any gas overrides to carry onto the transaction.
|
|
313
|
+
* @returns {Promise<TransactionResult>} The transaction's result.
|
|
314
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
315
|
+
* @throws {ValueError} If trying to approve usdts on ethereum with allowance not equal to zero (due to the usdt allowance reset requirement).
|
|
316
|
+
*/
|
|
317
|
+
async approve (options) {
|
|
318
|
+
if (!this._provider) {
|
|
319
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to approve funds.')
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const { token, spender, amount } = options
|
|
323
|
+
const { chainId } = await this._provider.getNetwork()
|
|
324
|
+
|
|
325
|
+
if (chainId === 1n && token.toLowerCase() === USDT_MAINNET_ADDRESS.toLowerCase()) {
|
|
326
|
+
const currentAllowance = await this.getAllowance(token, spender)
|
|
327
|
+
if (currentAllowance > 0n && BigInt(amount) > 0n) {
|
|
328
|
+
throw new ValueError(
|
|
329
|
+
'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.'
|
|
330
|
+
)
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const abi = ['function approve(address spender, uint256 amount) returns (bool)']
|
|
335
|
+
const contract = new Contract(token, abi, this._provider)
|
|
336
|
+
|
|
337
|
+
const tx = {
|
|
338
|
+
to: token,
|
|
339
|
+
value: 0,
|
|
340
|
+
data: contract.interface.encodeFunctionData('approve', [spender, amount]),
|
|
341
|
+
...WalletAccountReadOnlyEvm._getGasOverrides(options)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return await this.sendTransaction(tx)
|
|
345
|
+
}
|
|
346
|
+
|
|
183
347
|
/**
|
|
184
348
|
* Returns a read-only copy of the account.
|
|
185
349
|
*
|
|
186
350
|
* @returns {Promise<WalletAccountReadOnlyEvm>} The read-only account.
|
|
187
351
|
*/
|
|
188
352
|
async toReadOnlyAccount () {
|
|
189
|
-
|
|
353
|
+
if (!this._evmReadOnlyAccount) {
|
|
354
|
+
this._evmReadOnlyAccount = new WalletAccountReadOnlyEvm(await this.getAddress(), this._config)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return this._evmReadOnlyAccount
|
|
358
|
+
}
|
|
190
359
|
|
|
191
|
-
|
|
360
|
+
/**
|
|
361
|
+
* Signs an ERC-7702 authorization tuple.
|
|
362
|
+
*
|
|
363
|
+
* The chainId and nonce are populated from the provider when not explicitly provided.
|
|
364
|
+
*
|
|
365
|
+
* @param {AuthorizationRequest} auth - The authorization request.
|
|
366
|
+
* @returns {Promise<Authorization>} The signed authorization.
|
|
367
|
+
* @throws {ProviderRequiredError} If the chainId or nonce are not provided and the wallet is not connected to a provider.
|
|
368
|
+
*/
|
|
369
|
+
async signAuthorization (auth) {
|
|
370
|
+
const populated = { ...auth }
|
|
371
|
+
if (populated.chainId === undefined || populated.nonce === undefined) {
|
|
372
|
+
if (!this._provider) {
|
|
373
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to populate the authorization chainId and nonce. Provide them explicitly to sign offline.')
|
|
374
|
+
}
|
|
375
|
+
if (populated.chainId == null) {
|
|
376
|
+
const { chainId } = await this._provider.getNetwork()
|
|
377
|
+
populated.chainId = chainId
|
|
378
|
+
}
|
|
379
|
+
if (populated.nonce == null) {
|
|
380
|
+
const address = await this.getAddress()
|
|
381
|
+
populated.nonce = await this._provider.getTransactionCount(address)
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return await this._signer.signAuthorization(populated)
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Delegates this EOA to a smart contract via an ERC-7702 type 4 transaction.
|
|
389
|
+
*
|
|
390
|
+
* The transaction is sent to the EOA itself with zero value and no data.
|
|
391
|
+
* A fixed gas limit is used because `eth_estimateGas` may revert when
|
|
392
|
+
* the delegate contract lacks a `receive`/`fallback` function.
|
|
393
|
+
*
|
|
394
|
+
* @param {string} delegateAddress - The address of the contract to delegate to.
|
|
395
|
+
* @returns {Promise<TransactionResult>} The transaction result.
|
|
396
|
+
* @throws {ProviderRequiredError} If the wallet is not connected to a provider.
|
|
397
|
+
*/
|
|
398
|
+
async delegate (delegateAddress) {
|
|
399
|
+
if (!this._provider) {
|
|
400
|
+
throw new ProviderRequiredError('The wallet must be connected to a provider to delegate.')
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const address = await this.getAddress()
|
|
404
|
+
const nonceHex = await this._provider.send('eth_getTransactionCount', [address, 'latest'])
|
|
405
|
+
const nonce = Number(nonceHex)
|
|
406
|
+
|
|
407
|
+
const auth = await this.signAuthorization({
|
|
408
|
+
address: delegateAddress,
|
|
409
|
+
nonce: nonce + 1
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
return await this.sendTransaction({
|
|
413
|
+
type: 4,
|
|
414
|
+
nonce,
|
|
415
|
+
to: address,
|
|
416
|
+
value: 0,
|
|
417
|
+
gasLimit: DELEGATION_TX_GAS_LIMIT,
|
|
418
|
+
authorizationList: [auth]
|
|
419
|
+
})
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Revokes any active ERC-7702 delegation by delegating to the zero address.
|
|
424
|
+
*
|
|
425
|
+
* @returns {Promise<TransactionResult>} The transaction result.
|
|
426
|
+
*/
|
|
427
|
+
async revokeDelegation () {
|
|
428
|
+
return await this.delegate(ZeroAddress)
|
|
192
429
|
}
|
|
193
430
|
|
|
194
431
|
/**
|
|
195
432
|
* Disposes the wallet account, erasing the private key from the memory.
|
|
196
433
|
*/
|
|
197
434
|
dispose () {
|
|
198
|
-
this.
|
|
435
|
+
if (this._shouldWipeSignerOnDisposal) {
|
|
436
|
+
this._signer.dispose()
|
|
437
|
+
}
|
|
199
438
|
}
|
|
200
439
|
}
|