Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-bats-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Auto resolve entrypoint address from factory when available
42 changes: 40 additions & 2 deletions packages/thirdweb/src/wallets/smart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
} from "viem";
import type { Chain } from "../../chains/types.js";
import { getCachedChain } from "../../chains/utils.js";
import type { ThirdwebClient } from "../../client/client.js";
import { type ThirdwebContract, getContract } from "../../contract/contract.js";
import { allowance } from "../../extensions/erc20/__generated__/IERC20/read/allowance.js";
import { approve } from "../../extensions/erc20/write/approve.js";
Expand All @@ -18,6 +19,7 @@
signEip712Transaction,
} from "../../transaction/actions/zksync/send-eip712-transaction.js";
import type { PreparedTransaction } from "../../transaction/prepare-transaction.js";
import { readContract } from "../../transaction/read-contract.js";
import { getAddress } from "../../utils/address.js";
import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js";
import { concatHex } from "../../utils/encoding/helpers/concat-hex.js";
Expand Down Expand Up @@ -97,10 +99,26 @@
}

const options = creationOptions;
const chain = connectChain ?? options.chain;

// if factory is passed, but no entrypoint, try to resolve entrypoint from factory
if (options.factoryAddress && !options.overrides?.entrypointAddress) {
const entrypointAddress = await getEntrypointFromFactory(
options.factoryAddress,
client,
chain,
);
if (entrypointAddress) {
options.overrides = {
...options.overrides,
entrypointAddress,
};
}
}

const factoryAddress =
options.factoryAddress ??
getDefaultAccountFactory(creationOptions.overrides?.entrypointAddress);
const chain = connectChain ?? options.chain;
getDefaultAccountFactory(options.overrides?.entrypointAddress);
const sponsorGas =
"gasless" in options ? options.gasless : options.sponsorGas;

Expand Down Expand Up @@ -638,3 +656,23 @@
isDeployed = await isContractDeployed(accountContract);
}
}
async function getEntrypointFromFactory(
factoryAddress: string,
client: ThirdwebClient,
chain: Chain,
) {
const factoryContract = getContract({
address: factoryAddress,
client,
chain,
});
try {
const entrypointAddress = await readContract({
contract: factoryContract,
method: "function entrypoint() public view returns (address)",
});
return entrypointAddress;
} catch {
return undefined;
}

Check warning on line 677 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L676-L677

Added lines #L676 - L677 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { isContractDeployed } from "../../utils/bytecode/is-contract-deployed.js
import { sleep } from "../../utils/sleep.js";
import type { Account, Wallet } from "../interfaces/wallet.js";
import { generateAccount } from "../utils/generateAccount.js";
import { ENTRYPOINT_ADDRESS_v0_7 } from "./lib/constants.js";
import { DEFAULT_ACCOUNT_FACTORY_V0_7 } from "./lib/constants.js";
import { smartWallet } from "./smart-wallet.js";

let wallet: Wallet;
Expand Down Expand Up @@ -54,9 +54,7 @@ describe.runIf(process.env.TW_SECRET_KEY).sequential(
wallet = smartWallet({
chain,
gasless: true,
overrides: {
entrypointAddress: ENTRYPOINT_ADDRESS_v0_7,
},
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_7,
});
smartAccount = await wallet.connect({
client: TEST_CLIENT,
Expand Down
9 changes: 3 additions & 6 deletions packages/thirdweb/src/wallets/smart/smart-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,18 @@ import { getDefaultAccountFactory } from "./lib/constants.js";
*
* ## Using v0.7 Entrypoint
*
* 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.
* 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, ENTRYPOINT_ADDRESS_v0_7 } from "thirdweb/wallets/smart";
* 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,
* overrides: {
* entrypointAddress: ENTRYPOINT_ADDRESS_v0_7
* }
* factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_7, // 0.7 factory address
* });
* ```
*
Expand Down
Loading