@tetherto/wdk-wallet-evm-erc-4337 1.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +11 -0
- package/README.md +740 -0
- package/bare.js +20 -0
- package/index.js +32 -0
- package/package.json +57 -0
- package/src/wallet-account-evm-erc-4337.js +238 -0
- package/src/wallet-account-read-only-evm-erc-4337.js +269 -0
- package/src/wallet-manager-evm-erc-4337.js +113 -0
- package/types/index.d.ts +11 -0
- package/types/src/wallet-account-evm-erc-4337.d.ts +90 -0
- package/types/src/wallet-account-read-only-evm-erc-4337.d.ts +133 -0
- package/types/src/wallet-manager-evm-erc-4337.d.ts +54 -0
package/bare.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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 'bare-wdk-runtime'
|
|
17
|
+
|
|
18
|
+
export * from './index.js' with { imports: 'bare-wdk-runtime/package' }
|
|
19
|
+
|
|
20
|
+
export { default } from './index.js' with { imports: 'bare-wdk-runtime/package' }
|
package/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
|
|
15
|
+
'use strict'
|
|
16
|
+
|
|
17
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').FeeRates} FeeRates */
|
|
18
|
+
|
|
19
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').KeyPair} KeyPair */
|
|
20
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').EvmTransaction} EvmTransaction */
|
|
21
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').TransactionResult} TransactionResult */
|
|
22
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').TransferOptions} TransferOptions */
|
|
23
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').TransferResult} TransferResult */
|
|
24
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').EvmTransactionReceipt} EvmTransactionReceipt */
|
|
25
|
+
|
|
26
|
+
/** @typedef {import('./src/wallet-manager-evm-erc-4337.js').EvmErc4337WalletConfig} EvmErc4337WalletConfig */
|
|
27
|
+
|
|
28
|
+
export { default } from './src/wallet-manager-evm-erc-4337.js'
|
|
29
|
+
|
|
30
|
+
export { default as WalletAccountReadOnlyEvmErc4337 } from './src/wallet-account-read-only-evm-erc-4337.js'
|
|
31
|
+
|
|
32
|
+
export { default as WalletAccountEvmErc4337 } from './src/wallet-account-evm-erc-4337.js'
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tetherto/wdk-wallet-evm-erc-4337",
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
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
|
+
"keywords": [
|
|
6
|
+
"wdk",
|
|
7
|
+
"wallet",
|
|
8
|
+
"bip-32",
|
|
9
|
+
"ethereum",
|
|
10
|
+
"erc-4337"
|
|
11
|
+
],
|
|
12
|
+
"author": "Tether",
|
|
13
|
+
"license": "Apache-2.0",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git://github.com/tetherto/wdk-wallet-evm-erc-4337.git"
|
|
17
|
+
},
|
|
18
|
+
"main": "index.js",
|
|
19
|
+
"type": "module",
|
|
20
|
+
"types": "./types",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build:types": "tsc",
|
|
23
|
+
"lint": "standard",
|
|
24
|
+
"lint:fix": "standard --fix"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@tetherto/wdk-wallet": "^1.0.0-beta.1",
|
|
28
|
+
"@tetherto/wdk-wallet-evm": "^1.0.0-beta.2",
|
|
29
|
+
"@wdk-safe-global/relay-kit": "4.1.3",
|
|
30
|
+
"bare-wdk-runtime": "2.0.0",
|
|
31
|
+
"ethers": "6.14.4"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"standard": "17.1.2",
|
|
35
|
+
"typescript": "5.8.3"
|
|
36
|
+
},
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"types": "./types/index.d.ts",
|
|
40
|
+
"bare": "./bare.js",
|
|
41
|
+
"default": "./index.js"
|
|
42
|
+
},
|
|
43
|
+
"./package": {
|
|
44
|
+
"default": "./package.json"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "restricted",
|
|
49
|
+
"registry": "https://registry.npmjs.org/"
|
|
50
|
+
},
|
|
51
|
+
"standard": {
|
|
52
|
+
"ignore": [
|
|
53
|
+
"bare.js",
|
|
54
|
+
"tests/**/*.js"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
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
|
+
|
|
15
|
+
'use strict'
|
|
16
|
+
|
|
17
|
+
import { WalletAccountEvm } from '@tetherto/wdk-wallet-evm'
|
|
18
|
+
|
|
19
|
+
import { Safe4337Pack } from '@wdk-safe-global/relay-kit'
|
|
20
|
+
|
|
21
|
+
import WalletAccountReadOnlyEvmErc4337, { SALT_NONCE } from './wallet-account-read-only-evm-erc-4337.js'
|
|
22
|
+
|
|
23
|
+
/** @typedef {import('ethers').Eip1193Provider} Eip1193Provider */
|
|
24
|
+
|
|
25
|
+
/** @typedef {import('@tetherto/wdk-wallet').IWalletAccount} IWalletAccount */
|
|
26
|
+
|
|
27
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').KeyPair} KeyPair */
|
|
28
|
+
|
|
29
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').EvmTransaction} EvmTransaction */
|
|
30
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').TransactionResult} TransactionResult */
|
|
31
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').TransferOptions} TransferOptions */
|
|
32
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').TransferResult} TransferResult */
|
|
33
|
+
|
|
34
|
+
/** @typedef {import('./wallet-account-read-only-evm-erc-4337.js').EvmErc4337WalletConfig} EvmErc4337WalletConfig */
|
|
35
|
+
|
|
36
|
+
const FEE_TOLERANCE_COEFFICIENT = 120n
|
|
37
|
+
|
|
38
|
+
/** @implements {IWalletAccount} */
|
|
39
|
+
export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc4337 {
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new evm [erc-4337](https://www.erc4337.io/docs) wallet account.
|
|
42
|
+
*
|
|
43
|
+
* @param {string | Uint8Array} seed - The wallet's [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) seed phrase.
|
|
44
|
+
* @param {string} path - The BIP-44 derivation path (e.g. "0'/0/0").
|
|
45
|
+
* @param {EvmErc4337WalletConfig} config - The configuration object.
|
|
46
|
+
*/
|
|
47
|
+
constructor (seed, path, config) {
|
|
48
|
+
const ownerAccount = new WalletAccountEvm(seed, path, config)
|
|
49
|
+
|
|
50
|
+
super(ownerAccount._address, config)
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The evm erc-4337 wallet account configuration.
|
|
54
|
+
*
|
|
55
|
+
* @protected
|
|
56
|
+
* @type {EvmErc4337WalletConfig}
|
|
57
|
+
*/
|
|
58
|
+
this._config = config
|
|
59
|
+
|
|
60
|
+
/** @private */
|
|
61
|
+
this._ownerAccount = ownerAccount
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The derivation path's index of this account.
|
|
66
|
+
*
|
|
67
|
+
* @type {number}
|
|
68
|
+
*/
|
|
69
|
+
get index () {
|
|
70
|
+
return this._ownerAccount.index
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
|
|
75
|
+
*
|
|
76
|
+
* @type {string}
|
|
77
|
+
*/
|
|
78
|
+
get path () {
|
|
79
|
+
return this._ownerAccount.path
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The account's key pair.
|
|
84
|
+
*
|
|
85
|
+
* @type {KeyPair}
|
|
86
|
+
*/
|
|
87
|
+
get keyPair () {
|
|
88
|
+
return this._ownerAccount.keyPair
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Signs a message.
|
|
93
|
+
*
|
|
94
|
+
* @param {string} message - The message to sign.
|
|
95
|
+
* @returns {Promise<string>} The message's signature.
|
|
96
|
+
*/
|
|
97
|
+
async sign (message) {
|
|
98
|
+
return await this._ownerAccount.sign(message)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Verifies a message's signature.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} message - The original message.
|
|
105
|
+
* @param {string} signature - The signature to verify.
|
|
106
|
+
* @returns {Promise<boolean>} True if the signature is valid.
|
|
107
|
+
*/
|
|
108
|
+
async verify (message, signature) {
|
|
109
|
+
return await this._ownerAccount.verify(message, signature)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Sends a transaction.
|
|
114
|
+
*
|
|
115
|
+
* @param {EvmTransaction | EvmTransaction[]} tx - The transaction, or an array of multiple transactions to send in batch.
|
|
116
|
+
* @param {Pick<EvmErc4337WalletConfig, 'paymasterToken'>} [config] - If set, overrides the 'paymasterToken' option defined in the wallet account configuration.
|
|
117
|
+
* @returns {Promise<TransactionResult>} The transaction's result.
|
|
118
|
+
*/
|
|
119
|
+
async sendTransaction (tx, config) {
|
|
120
|
+
const { paymasterToken } = config ?? this._config
|
|
121
|
+
|
|
122
|
+
const { fee } = await this.quoteSendTransaction(tx, config)
|
|
123
|
+
|
|
124
|
+
const hash = await this._sendUserOperation([tx].flat(), {
|
|
125
|
+
paymasterTokenAddress: paymasterToken.address,
|
|
126
|
+
amountToApprove: BigInt(fee * FEE_TOLERANCE_COEFFICIENT / 100n)
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
return { hash, fee }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Transfers a token to another address.
|
|
134
|
+
*
|
|
135
|
+
* @param {TransferOptions} options - The transfer's options.
|
|
136
|
+
* @param {Pick<EvmErc4337WalletConfig, 'paymasterToken' | 'transferMaxFee'>} [config] - If set, overrides the 'paymasterToken' and 'transferMaxFee' options defined in the wallet account configuration.
|
|
137
|
+
* @returns {Promise<TransferResult>} The transfer's result.
|
|
138
|
+
*/
|
|
139
|
+
async transfer (options, config) {
|
|
140
|
+
const { paymasterToken, transferMaxFee } = config ?? this._config
|
|
141
|
+
|
|
142
|
+
const tx = await WalletAccountEvm._getTransferTransaction(options)
|
|
143
|
+
|
|
144
|
+
const { fee } = await this.quoteSendTransaction(tx, config)
|
|
145
|
+
|
|
146
|
+
if (transferMaxFee !== undefined && fee >= transferMaxFee) {
|
|
147
|
+
throw new Error('Exceeded maximum fee cost for transfer operation.')
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const hash = await this._sendUserOperation([tx], {
|
|
151
|
+
paymasterTokenAddress: paymasterToken.address,
|
|
152
|
+
amountToApprove: BigInt(fee * FEE_TOLERANCE_COEFFICIENT / 100n)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
return { hash, fee }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Returns a read-only copy of the account.
|
|
160
|
+
*
|
|
161
|
+
* @returns {Promise<WalletAccountReadOnlyEvmErc4337>} The read-only account.
|
|
162
|
+
*/
|
|
163
|
+
async toReadOnlyAccount () {
|
|
164
|
+
const address = await this._ownerAccount.getAddress()
|
|
165
|
+
|
|
166
|
+
const readOnlyAccount = new WalletAccountReadOnlyEvmErc4337(address, this._config)
|
|
167
|
+
|
|
168
|
+
return readOnlyAccount
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Disposes the wallet account, erasing the private key from the memory.
|
|
173
|
+
*/
|
|
174
|
+
dispose () {
|
|
175
|
+
this._ownerAccount.dispose()
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async _getSafe4337Pack () {
|
|
179
|
+
if (!this._safe4337Pack) {
|
|
180
|
+
const owner = await this._ownerAccount.getAddress()
|
|
181
|
+
|
|
182
|
+
this._safe4337Pack = await Safe4337Pack.init({
|
|
183
|
+
provider: this._config.provider,
|
|
184
|
+
signer: this._ownerAccount._account,
|
|
185
|
+
bundlerUrl: this._config.bundlerUrl,
|
|
186
|
+
safeModulesVersion: this._config.safeModulesVersion,
|
|
187
|
+
options: {
|
|
188
|
+
owners: [owner],
|
|
189
|
+
threshold: 1,
|
|
190
|
+
saltNonce: SALT_NONCE
|
|
191
|
+
},
|
|
192
|
+
paymasterOptions: {
|
|
193
|
+
paymasterUrl: this._config.paymasterUrl,
|
|
194
|
+
paymasterAddress: this._config.paymasterAddress,
|
|
195
|
+
paymasterTokenAddress: this._config.paymasterToken.address,
|
|
196
|
+
skipApproveTransaction: true
|
|
197
|
+
},
|
|
198
|
+
customContracts: {
|
|
199
|
+
entryPointAddress: this._config.entryPointAddress
|
|
200
|
+
}
|
|
201
|
+
})
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return this._safe4337Pack
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** @private */
|
|
208
|
+
async _sendUserOperation (txs, options) {
|
|
209
|
+
const safe4337Pack = await this._getSafe4337Pack()
|
|
210
|
+
|
|
211
|
+
const address = await this.getAddress()
|
|
212
|
+
|
|
213
|
+
const twoMinutesFromNow = Math.floor(Date.now() / 1_000) + 2 * 60
|
|
214
|
+
|
|
215
|
+
try {
|
|
216
|
+
const safeOperation = await safe4337Pack.createTransaction({
|
|
217
|
+
transactions: txs.map(tx => ({ from: address, ...tx })),
|
|
218
|
+
options: {
|
|
219
|
+
validUntil: twoMinutesFromNow,
|
|
220
|
+
feeEstimator: this._feeEstimator,
|
|
221
|
+
...options
|
|
222
|
+
}
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
const signedSafeOperation = await safe4337Pack.signSafeOperation(safeOperation)
|
|
226
|
+
|
|
227
|
+
return await safe4337Pack.executeTransaction({
|
|
228
|
+
executable: signedSafeOperation
|
|
229
|
+
})
|
|
230
|
+
} catch (err) {
|
|
231
|
+
if (err.message.includes('AA50')) {
|
|
232
|
+
throw new Error('Not enough funds on the safe account to repay the paymaster.')
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
throw err
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
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
|
+
|
|
15
|
+
'use strict'
|
|
16
|
+
|
|
17
|
+
import { WalletAccountReadOnly } from '@tetherto/wdk-wallet'
|
|
18
|
+
|
|
19
|
+
import { WalletAccountReadOnlyEvm } from '@tetherto/wdk-wallet-evm'
|
|
20
|
+
|
|
21
|
+
import { Safe4337Pack, GenericFeeEstimator } from '@wdk-safe-global/relay-kit'
|
|
22
|
+
|
|
23
|
+
/** @typedef {import('ethers').Eip1193Provider} Eip1193Provider */
|
|
24
|
+
|
|
25
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').EvmTransaction} EvmTransaction */
|
|
26
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').TransactionResult} TransactionResult */
|
|
27
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').TransferOptions} TransferOptions */
|
|
28
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').TransferResult} TransferResult */
|
|
29
|
+
|
|
30
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').EvmTransactionReceipt} EvmTransactionReceipt */
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {Object} EvmErc4337WalletConfig
|
|
34
|
+
* @property {number} chainId - The blockchain's id (e.g., 1 for ethereum).
|
|
35
|
+
* @property {string | Eip1193Provider} provider - The url of the rpc provider, or an instance of a class that implements eip-1193.
|
|
36
|
+
* @property {string} bundlerUrl - The url of the bundler service.
|
|
37
|
+
* @property {string} paymasterUrl - The url of the paymaster service.
|
|
38
|
+
* @property {string} paymasterAddress - The address of the paymaster smart contract.
|
|
39
|
+
* @property {string} entryPointAddress - The address of the entry point smart contract.
|
|
40
|
+
* @property {string} safeModulesVersion - The safe modules version.
|
|
41
|
+
* @property {Object} paymasterToken - The paymaster token configuration.
|
|
42
|
+
* @property {string} paymasterToken.address - The address of the paymaster token.
|
|
43
|
+
* @property {number | bigint} [transferMaxFee] - The maximum fee amount for transfer operations.
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
export const SALT_NONCE = '0x69b348339eea4ed93f9d11931c3b894c8f9d8c7663a053024b11cb7eb4e5a1f6'
|
|
47
|
+
|
|
48
|
+
export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOnly {
|
|
49
|
+
/**
|
|
50
|
+
* Creates a new read-only evm [erc-4337](https://www.erc4337.io/docs) wallet account.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} address - The evm account's address.
|
|
53
|
+
* @param {Omit<EvmErc4337WalletConfig, 'transferMaxFee'>} config - The configuration object.
|
|
54
|
+
*/
|
|
55
|
+
constructor (address, config) {
|
|
56
|
+
super(undefined)
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* The read-only evm erc-4337 wallet account configuration.
|
|
60
|
+
*
|
|
61
|
+
* @protected
|
|
62
|
+
* @type {Omit<EvmErc4337WalletConfig, 'transferMaxFee'>}
|
|
63
|
+
*/
|
|
64
|
+
this._config = config
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The safe's implementation of the erc-4337 standard.
|
|
68
|
+
*
|
|
69
|
+
* @protected
|
|
70
|
+
* @type {Safe4337Pack | undefined}
|
|
71
|
+
*/
|
|
72
|
+
this._safe4337Pack = undefined
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* The safe's fee estimator.
|
|
76
|
+
*
|
|
77
|
+
* @protected
|
|
78
|
+
* @type {GenericFeeEstimator}
|
|
79
|
+
*/
|
|
80
|
+
this._feeEstimator = new GenericFeeEstimator(
|
|
81
|
+
config.provider,
|
|
82
|
+
`0x${config.chainId.toString(16)}`
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
/** @private */
|
|
86
|
+
this._ownerAccountAddress = address
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns the account's address.
|
|
91
|
+
*
|
|
92
|
+
* @returns {Promise<string>} The account's address.
|
|
93
|
+
*/
|
|
94
|
+
async getAddress () {
|
|
95
|
+
const safe4337pack = await this._getSafe4337Pack()
|
|
96
|
+
|
|
97
|
+
const address = await safe4337pack.protocolKit.getAddress()
|
|
98
|
+
|
|
99
|
+
return address
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Returns the account's eth balance.
|
|
104
|
+
*
|
|
105
|
+
* @returns {Promise<bigint>} The eth balance (in weis).
|
|
106
|
+
*/
|
|
107
|
+
async getBalance () {
|
|
108
|
+
const evmReadOnlyAccount = await this._getEvmReadOnlyAccount()
|
|
109
|
+
|
|
110
|
+
return await evmReadOnlyAccount.getBalance()
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Returns the account balance for a specific token.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} tokenAddress - The smart contract address of the token.
|
|
117
|
+
* @returns {Promise<bigint>} The token balance (in base unit).
|
|
118
|
+
*/
|
|
119
|
+
async getTokenBalance (tokenAddress) {
|
|
120
|
+
const evmReadOnlyAccount = await this._getEvmReadOnlyAccount()
|
|
121
|
+
|
|
122
|
+
return await evmReadOnlyAccount.getTokenBalance(tokenAddress)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns the account's balance for the paymaster token provided in the wallet account configuration.
|
|
127
|
+
*
|
|
128
|
+
* @returns {Promise<bigint>} The paymaster token balance (in base unit).
|
|
129
|
+
*/
|
|
130
|
+
async getPaymasterTokenBalance () {
|
|
131
|
+
const { paymasterToken } = this._config
|
|
132
|
+
|
|
133
|
+
return await this.getTokenBalance(paymasterToken.address)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Quotes the costs of a send transaction operation.
|
|
138
|
+
*
|
|
139
|
+
* @param {EvmTransaction | EvmTransaction[]} tx - The transaction, or an array of multiple transactions to send in batch.
|
|
140
|
+
* @param {Pick<EvmErc4337WalletConfig, 'paymasterToken'>} [config] - If set, overrides the 'paymasterToken' option defined in the wallet account configuration.
|
|
141
|
+
* @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
|
|
142
|
+
*/
|
|
143
|
+
async quoteSendTransaction (tx, config) {
|
|
144
|
+
const { paymasterToken } = config ?? this._config
|
|
145
|
+
|
|
146
|
+
const fee = await this._getUserOperationGasCost([tx].flat(), {
|
|
147
|
+
paymasterTokenAddress: paymasterToken.address,
|
|
148
|
+
amountToApprove: BigInt(Number.MAX_SAFE_INTEGER)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
return { fee: BigInt(fee) }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Quotes the costs of a transfer operation.
|
|
156
|
+
*
|
|
157
|
+
* @param {TransferOptions} options - The transfer's options.
|
|
158
|
+
* @param {Pick<EvmErc4337WalletConfig, 'paymasterToken'>} [config] - If set, overrides the 'paymasterToken' option defined in the wallet account configuration.
|
|
159
|
+
* @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
|
|
160
|
+
*/
|
|
161
|
+
async quoteTransfer (options, config) {
|
|
162
|
+
const tx = await WalletAccountReadOnlyEvm._getTransferTransaction(options)
|
|
163
|
+
|
|
164
|
+
const result = await this.quoteSendTransaction(tx, config)
|
|
165
|
+
|
|
166
|
+
return result
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Returns a transaction's receipt.
|
|
171
|
+
*
|
|
172
|
+
* @param {string} hash - The user operation hash.
|
|
173
|
+
* @returns {Promise<EvmTransactionReceipt | null>} – The receipt, or null if the transaction has not been included in a block yet.
|
|
174
|
+
*/
|
|
175
|
+
async getTransactionReceipt (hash) {
|
|
176
|
+
const safe4337Pack = await this._getSafe4337Pack()
|
|
177
|
+
|
|
178
|
+
const evmReadOnlyAccount = await this._getEvmReadOnlyAccount()
|
|
179
|
+
|
|
180
|
+
const { transactionHash } = await safe4337Pack.getUserOperationByHash(hash)
|
|
181
|
+
|
|
182
|
+
if (!transactionHash) {
|
|
183
|
+
return null
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return await evmReadOnlyAccount.getTransactionReceipt(transactionHash)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Returns the safe's erc-4337 pack of the account.
|
|
191
|
+
*
|
|
192
|
+
* @protected
|
|
193
|
+
* @returns {Promise<Safe4337Pack>} The safe's erc-4337 pack.
|
|
194
|
+
*/
|
|
195
|
+
async _getSafe4337Pack () {
|
|
196
|
+
if (!this._safe4337Pack) {
|
|
197
|
+
this._safe4337Pack = await Safe4337Pack.init({
|
|
198
|
+
provider: this._config.provider,
|
|
199
|
+
bundlerUrl: this._config.bundlerUrl,
|
|
200
|
+
safeModulesVersion: this._config.safeModulesVersion,
|
|
201
|
+
options: {
|
|
202
|
+
owners: [this._ownerAccountAddress],
|
|
203
|
+
threshold: 1,
|
|
204
|
+
saltNonce: SALT_NONCE
|
|
205
|
+
},
|
|
206
|
+
paymasterOptions: {
|
|
207
|
+
paymasterUrl: this._config.paymasterUrl,
|
|
208
|
+
paymasterAddress: this._config.paymasterAddress,
|
|
209
|
+
paymasterTokenAddress: this._config.paymasterToken.address,
|
|
210
|
+
skipApproveTransaction: true
|
|
211
|
+
},
|
|
212
|
+
customContracts: {
|
|
213
|
+
entryPointAddress: this._config.entryPointAddress
|
|
214
|
+
}
|
|
215
|
+
})
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return this._safe4337Pack
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** @private */
|
|
222
|
+
async _getEvmReadOnlyAccount () {
|
|
223
|
+
const address = await this.getAddress()
|
|
224
|
+
|
|
225
|
+
const evmReadOnlyAccount = new WalletAccountReadOnlyEvm(address, this._config)
|
|
226
|
+
|
|
227
|
+
return evmReadOnlyAccount
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** @private */
|
|
231
|
+
async _getUserOperationGasCost (txs, options) {
|
|
232
|
+
const safe4337Pack = await this._getSafe4337Pack()
|
|
233
|
+
|
|
234
|
+
const address = await this.getAddress()
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
const safeOperation = await safe4337Pack.createTransaction({
|
|
238
|
+
transactions: txs.map(tx => ({ from: address, ...tx })),
|
|
239
|
+
options: {
|
|
240
|
+
feeEstimator: this._feeEstimator,
|
|
241
|
+
...options
|
|
242
|
+
}
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
const {
|
|
246
|
+
callGasLimit,
|
|
247
|
+
verificationGasLimit,
|
|
248
|
+
preVerificationGas,
|
|
249
|
+
paymasterVerificationGasLimit,
|
|
250
|
+
paymasterPostOpGasLimit,
|
|
251
|
+
maxFeePerGas
|
|
252
|
+
} = safeOperation.userOperation
|
|
253
|
+
|
|
254
|
+
const gasCost = Number((callGasLimit + verificationGasLimit + preVerificationGas + paymasterVerificationGasLimit + paymasterPostOpGasLimit) * maxFeePerGas)
|
|
255
|
+
|
|
256
|
+
const exchangeRate = await safe4337Pack.getTokenExchangeRate(options.paymasterTokenAddress)
|
|
257
|
+
|
|
258
|
+
const gasCostInPaymasterToken = Math.ceil(gasCost * exchangeRate / 10 ** 18)
|
|
259
|
+
|
|
260
|
+
return gasCostInPaymasterToken
|
|
261
|
+
} catch (error) {
|
|
262
|
+
if (error.message.includes('AA50')) {
|
|
263
|
+
throw new Error('Simulation failed: not enough funds in the safe account to repay the paymaster.')
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
throw error
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|