Skip to content

Commit fe0ce86

Browse files
feat: auto-resolve entrypoint address from factory
1 parent 45fcfb1 commit fe0ce86

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

.changeset/late-bats-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Auto resolve entrypoint address from factory when available

packages/thirdweb/src/wallets/smart/index.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "viem";
99
import type { Chain } from "../../chains/types.js";
1010
import { getCachedChain } from "../../chains/utils.js";
11+
import type { ThirdwebClient } from "../../client/client.js";
1112
import { type ThirdwebContract, getContract } from "../../contract/contract.js";
1213
import { allowance } from "../../extensions/erc20/__generated__/IERC20/read/allowance.js";
1314
import { approve } from "../../extensions/erc20/write/approve.js";
@@ -18,6 +19,7 @@ import {
1819
signEip712Transaction,
1920
} from "../../transaction/actions/zksync/send-eip712-transaction.js";
2021
import type { PreparedTransaction } from "../../transaction/prepare-transaction.js";
22+
import { readContract } from "../../transaction/read-contract.js";
2123
import { getAddress } from "../../utils/address.js";
2224
import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js";
2325
import { concatHex } from "../../utils/encoding/helpers/concat-hex.js";
@@ -97,10 +99,26 @@ export async function connectSmartWallet(
9799
}
98100

99101
const options = creationOptions;
102+
const chain = connectChain ?? options.chain;
103+
104+
// if factory is passed, but no entrypoint, try to resolve entrypoint from factory
105+
if (options.factoryAddress && !options.overrides?.entrypointAddress) {
106+
const entrypointAddress = await getEntrypointFromFactory(
107+
options.factoryAddress,
108+
client,
109+
chain,
110+
);
111+
if (entrypointAddress) {
112+
options.overrides = {
113+
...options.overrides,
114+
entrypointAddress,
115+
};
116+
}
117+
}
118+
100119
const factoryAddress =
101120
options.factoryAddress ??
102-
getDefaultAccountFactory(creationOptions.overrides?.entrypointAddress);
103-
const chain = connectChain ?? options.chain;
121+
getDefaultAccountFactory(options.overrides?.entrypointAddress);
104122
const sponsorGas =
105123
"gasless" in options ? options.gasless : options.sponsorGas;
106124

@@ -638,3 +656,23 @@ async function confirmContractDeployment(args: {
638656
isDeployed = await isContractDeployed(accountContract);
639657
}
640658
}
659+
async function getEntrypointFromFactory(
660+
factoryAddress: string,
661+
client: ThirdwebClient,
662+
chain: Chain,
663+
) {
664+
const factoryContract = getContract({
665+
address: factoryAddress,
666+
client,
667+
chain,
668+
});
669+
try {
670+
const entrypointAddress = await readContract({
671+
contract: factoryContract,
672+
method: "function entrypoint() public view returns (address)",
673+
});
674+
return entrypointAddress;
675+
} catch {
676+
return undefined;
677+
}
678+
}

packages/thirdweb/src/wallets/smart/smart-wallet-integration-v07.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { isContractDeployed } from "../../utils/bytecode/is-contract-deployed.js
2323
import { sleep } from "../../utils/sleep.js";
2424
import type { Account, Wallet } from "../interfaces/wallet.js";
2525
import { generateAccount } from "../utils/generateAccount.js";
26-
import { ENTRYPOINT_ADDRESS_v0_7 } from "./lib/constants.js";
26+
import { DEFAULT_ACCOUNT_FACTORY_V0_7 } from "./lib/constants.js";
2727
import { smartWallet } from "./smart-wallet.js";
2828

2929
let wallet: Wallet;
@@ -54,9 +54,7 @@ describe.runIf(process.env.TW_SECRET_KEY).sequential(
5454
wallet = smartWallet({
5555
chain,
5656
gasless: true,
57-
overrides: {
58-
entrypointAddress: ENTRYPOINT_ADDRESS_v0_7,
59-
},
57+
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_7,
6058
});
6159
smartAccount = await wallet.connect({
6260
client: TEST_CLIENT,

packages/thirdweb/src/wallets/smart/smart-wallet.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,18 @@ import { getDefaultAccountFactory } from "./lib/constants.js";
8585
*
8686
* ## Using v0.7 Entrypoint
8787
*
88-
* Both v0.6 (default) and v0.7 ERC4337 Entrypoints are supported. To use the v0.7 Entrypoint, you can pass the `entrypointAddress` option to the `smartWallet` function, as well as a compatible account factory.
88+
* Both v0.6 (default) and v0.7 ERC4337 Entrypoints are supported. To use the v0.7 Entrypoint, simply pass in a compatible account factory.
8989
*
9090
* 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).
9191
*
9292
* ```ts
93-
* import { smartWallet, DEFAULT_ACCOUNT_FACTORY_V0_7, ENTRYPOINT_ADDRESS_v0_7 } from "thirdweb/wallets/smart";
93+
* import { smartWallet, DEFAULT_ACCOUNT_FACTORY_V0_7 } from "thirdweb/wallets/smart";
9494
* import { sepolia } from "thirdweb/chains";
9595
*
9696
* const wallet = smartWallet({
9797
* chain: sepolia,
9898
* sponsorGas: true, // enable sponsored transactions
99-
* factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_7,
100-
* overrides: {
101-
* entrypointAddress: ENTRYPOINT_ADDRESS_v0_7
102-
* }
99+
* factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_7, // 0.7 factory address
103100
* });
104101
* ```
105102
*

0 commit comments

Comments
 (0)