-
Notifications
You must be signed in to change notification settings - Fork 360
/
smart-wallet.ts
239 lines (235 loc) · 7.75 KB
/
smart-wallet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { trackConnect } from "../../analytics/track/connect.js";
import type { Chain } from "../../chains/types.js";
import { getCachedChainIfExists } from "../../chains/utils.js";
import { getContract } from "../../contract/contract.js";
import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js";
import { isContractDeployed } from "../../utils/bytecode/is-contract-deployed.js";
import type { Account, Wallet } from "../interfaces/wallet.js";
import { createWalletEmitter } from "../wallet-emitter.js";
import type {
CreateWalletArgs,
WalletConnectionOption,
} from "../wallet-types.js";
import { getDefaultAccountFactory } from "./lib/constants.js";
/**
* Creates a ERC4337 smart wallet based on a admin account.
*
* Smart wallets are smart contract wallets that enable multiple benefits for users:
*
* - Sponsor gas fees for transactions
* - Multiple owners
* - Session keys
* - Batch transactions
* - Predictable addresses
* - Programmable features
*
* [Learn more about account abstraction](https://portal.thirdweb.com/connect/account-abstraction/how-it-works)
*
* @param createOptions - The options for creating the wallet.
* Refer to [SmartWalletCreationOptions](https://portal.thirdweb.com/references/typescript/v5/SmartWalletCreationOptions) for more details.
* @returns The created smart wallet.
* @example
*
* ## Connect to a smart wallet
*
* To connect to a smart wallet, you need to provide an admin account as the `personalAccount` option.
*
* Any wallet can be used as an admin account, including an in-app wallets.
*
* The `sponsorGas` option is used to enable sponsored gas for transactions automatically.
*
* ```ts
* import { smartWallet, inAppWallet } from "thirdweb/wallets";
* import { sepolia } from "thirdweb/chains";
* import { sendTransaction } from "thirdweb";
*
* const wallet = smartWallet({
* chain: sepolia,
* sponsorGas: true, // enable sponsored transactions
* });
*
* // any wallet can be used as an admin account
* // in this example we use an in-app wallet
* const adminWallet = inAppWallet();
* const personalAccount = await adminWallet.connect({
* client,
* chain: sepolia,
* strategy: "google",
* });
*
* const smartAccount = await wallet.connect({
* client,
* personalAccount, // pass the admin account
* });
*
* // sending sponsored transactions with the smartAccount
* await sendTransaction({
* account: smartAccount,
* transaction,
* });
* ```
*
* ## Using a custom account factory
*
* You can pass a custom account factory to the `smartWallet` function to use a your own account factory.
*
* ```ts
* import { smartWallet } from "thirdweb/wallets";
* import { sepolia } from "thirdweb/chains";
*
* const wallet = smartWallet({
* chain: sepolia,
* sponsorGas: true, // enable sponsored transactions
* factoryAddress: "0x...", // custom factory address
* });
*
* ## Using v0.7 Entrypoint
*
* Both v0.6 (default) and v0.7 ERC4337 Entrypoints are supported. To use the v0.7 Entrypoint, simply pass in a compatible account factory.
*
* You can use the predeployed `DEFAULT_ACCOUNT_FACTORY_V0_7` or deploy your own [AccountFactory v0.7](https://thirdweb.com/thirdweb.eth/AccountFactory_0_7).
*
* ```ts
* import { smartWallet, DEFAULT_ACCOUNT_FACTORY_V0_7 } from "thirdweb/wallets/smart";
* import { sepolia } from "thirdweb/chains";
*
* const wallet = smartWallet({
* chain: sepolia,
* sponsorGas: true, // enable sponsored transactions
* factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_7, // 0.7 factory address
* });
* ```
*
* ## Configuring the smart wallet
*
* You can pass options to the `smartWallet` function to configure the smart wallet.
*
* ```ts
* import { smartWallet } from "thirdweb/wallets";
* import { sepolia } from "thirdweb/chains";
*
* const wallet = smartWallet({
* chain: sepolia,
* sponsorGas: true, // enable sponsored transactions
* factoryAddress: "0x...", // custom factory address
* overrides: {
* accountAddress: "0x...", // override account address
* accountSalt: "0x...", // override account salt
* entrypointAddress: "0x...", // override entrypoint address
* erc20Paymaster: { ... }, // enable erc20 paymaster
* bundlerUrl: "https://...", // override bundler url
* paymaster: (userOp) => { ... }, // override paymaster
* ...
* }
* });
* ```
*
* Refer to [SmartWalletOptions](https://portal.thirdweb.com/references/typescript/v5/SmartWalletOptions) for more details.
*
* @wallet
*/
export function smartWallet(
createOptions: CreateWalletArgs<"smart">[1],
): Wallet<"smart"> {
const emitter = createWalletEmitter<"smart">();
let account: Account | undefined = undefined;
let adminAccount: Account | undefined = undefined;
let chain: Chain | undefined = undefined;
let lastConnectOptions: WalletConnectionOption<"smart"> | undefined;
const _smartWallet: Wallet<"smart"> = {
id: "smart",
subscribe: emitter.subscribe,
getChain() {
if (!chain) {
return undefined;
}
chain = getCachedChainIfExists(chain.id) || chain;
return chain;
},
getConfig: () => createOptions,
getAccount: () => account,
getAdminAccount: () => adminAccount,
autoConnect: async (options) => {
const { connectSmartWallet } = await import("./index.js");
const [connectedAccount, connectedChain] = await connectSmartWallet(
_smartWallet,
options,
createOptions,
);
// set the states
lastConnectOptions = options;
account = connectedAccount;
chain = connectedChain;
trackConnect({
client: options.client,
walletType: "smart",
walletAddress: account.address,
});
// return account
return account;
},
connect: async (options) => {
const { connectSmartWallet } = await import("./index.js");
const [connectedAccount, connectedChain] = await connectSmartWallet(
_smartWallet,
options,
createOptions,
);
// set the states
adminAccount = options.personalAccount;
lastConnectOptions = options;
account = connectedAccount;
chain = connectedChain;
trackConnect({
client: options.client,
walletType: "smart",
walletAddress: account.address,
});
// return account
emitter.emit("accountChanged", account);
return account;
},
disconnect: async () => {
account = undefined;
chain = undefined;
const { disconnectSmartWallet } = await import("./index.js");
await disconnectSmartWallet(_smartWallet);
emitter.emit("disconnect", undefined);
},
switchChain: async (newChain: Chain) => {
if (!lastConnectOptions) {
throw new Error("Cannot switch chain without a previous connection");
}
const isZksyncChain = await isZkSyncChain(newChain);
if (!isZksyncChain) {
// check if factory is deployed
const factory = getContract({
address:
createOptions.factoryAddress ||
getDefaultAccountFactory(
createOptions.overrides?.entrypointAddress,
),
chain: newChain,
client: lastConnectOptions.client,
});
const isDeployed = await isContractDeployed(factory);
if (!isDeployed) {
throw new Error(
`Factory contract not deployed on chain: ${newChain.id}`,
);
}
}
const { connectSmartWallet } = await import("./index.js");
const [connectedAccount, connectedChain] = await connectSmartWallet(
_smartWallet,
{ ...lastConnectOptions, chain: newChain },
createOptions,
);
// set the states
account = connectedAccount;
chain = connectedChain;
emitter.emit("chainChanged", newChain);
},
};
return _smartWallet;
}