Skip to content

Commit 686d0c3

Browse files
feat: respect custom bundler URL for gas fees (#5231)
1 parent 9425e9e commit 686d0c3

File tree

8 files changed

+91
-3
lines changed

8 files changed

+91
-3
lines changed

.changeset/early-shoes-applaud.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+
Respect custom bundler URL for getting gas fees + better DX for `predictSmartAccountAddress()`

apps/portal/src/app/typescript/v5/sidebar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export const sidebar: SideBar = {
139139
},
140140
...[
141141
"smartWallet",
142-
"predictAddress",
142+
"predictSmartAccountAddress",
143143
"createAndSignUserOp",
144144
"createUnsignedUserOp",
145145
"signUserOp",

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export {
1515
estimateUserOpGas,
1616
} from "../../wallets/smart/lib/bundler.js";
1717

18-
export { predictAddress } from "../../wallets/smart/lib/calls.js";
18+
export {
19+
predictAddress,
20+
predictSmartAccountAddress,
21+
} from "../../wallets/smart/lib/calls.js";
1922

2023
export { getPaymasterAndData } from "../../wallets/smart/lib/paymaster.js";
2124

packages/thirdweb/src/transaction/prepare-contract-call.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ export type PrepareContractCallOptions<
9292
* });
9393
* ```
9494
*
95+
* ### Usage with ERC20 value:
96+
*
97+
* For transactions that transfer ERC20 tokens, you can specify the value as the amount of tokens to transfer.
98+
*
99+
* You can use this in conjuction with the [`getApprovalForTransaction`](https://portal.thirdweb.com/references/typescript/v5/getApprovalForTransaction) function to easily create approval transactions for ERC20 tokens.
100+
*
101+
* This value will also be read by the react hooks and UI components to present to total cost to the user.
102+
*
103+
* ```ts
104+
* import { prepareContractCall } from "thirdweb";
105+
* import { toWei } from "thirdweb/utils";
106+
*
107+
* const transaction = prepareContractCall({
108+
* contract,
109+
* method: "function payWithCoin()",
110+
* params: [],
111+
* erc20Value: {
112+
* tokenAddress: "0x...", // the address of the ERC20 token
113+
* amountWei: toWei("0.1"), // the amount of tokens to transfer in wei
114+
* },
115+
* });
116+
* ```
117+
*
95118
* ### Usage with a JSON ABI function object:
96119
*
97120
* ```ts

packages/thirdweb/src/wallets/smart/lib/calls.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
1-
import type { ThirdwebContract } from "../../../contract/contract.js";
1+
import type { Chain } from "../../../chains/types.js";
2+
import type { ThirdwebClient } from "../../../client/client.js";
3+
import {
4+
type ThirdwebContract,
5+
getContract,
6+
} from "../../../contract/contract.js";
27
import { prepareContractCall } from "../../../transaction/prepare-contract-call.js";
38
import type { PreparedTransaction } from "../../../transaction/prepare-transaction.js";
49
import { readContract } from "../../../transaction/read-contract.js";
510
import { isHex, stringToHex } from "../../../utils/encoding/hex.js";
611
import type { SendTransactionOption } from "../../interfaces/wallet.js";
12+
import { DEFAULT_ACCOUNT_FACTORY_V0_6 } from "./constants.js";
13+
14+
/**
15+
* Predict the address of a smart account.
16+
* @param args - The options for predicting the address of a smart account.
17+
* @returns The predicted address of the smart account.
18+
* @example
19+
* ```ts
20+
* import { predictSmartAccountAddress } from "thirdweb/wallets/smart";
21+
*
22+
* const predictedAddress = await predictSmartAccountAddress({
23+
* client,
24+
* chain,
25+
* adminAddress,
26+
* });
27+
* ```
28+
* @walletUtils
29+
*/
30+
export async function predictSmartAccountAddress(args: {
31+
client: ThirdwebClient;
32+
chain: Chain;
33+
adminAddress: string;
34+
factoryAddress?: string;
35+
accountSalt?: string;
36+
}): Promise<string> {
37+
return predictAddress({
38+
adminAddress: args.adminAddress,
39+
accountSalt: args.accountSalt,
40+
factoryContract: getContract({
41+
address: args.factoryAddress ?? DEFAULT_ACCOUNT_FACTORY_V0_6,
42+
chain: args.chain,
43+
client: args.client,
44+
}),
45+
});
46+
}
747

848
/**
949
* Predict the address of a smart account.
@@ -20,6 +60,7 @@ import type { SendTransactionOption } from "../../interfaces/wallet.js";
2060
* });
2161
* ```
2262
* @walletUtils
63+
* @deprecated Use `predictSmartAccountAddress` instead.
2364
*/
2465
export async function predictAddress(args: {
2566
factoryContract: ThirdwebContract;

packages/thirdweb/src/wallets/smart/lib/userop.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export async function createUnsignedUserOp(args: {
149149
const bundlerOptions = {
150150
client,
151151
chain,
152+
bundlerUrl: overrides?.bundlerUrl,
152153
entrypointAddress: overrides?.entrypointAddress,
153154
};
154155

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +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 { predictSmartAccountAddress } from "./lib/calls.js";
2627
import { DEFAULT_ACCOUNT_FACTORY_V0_7 } from "./lib/constants.js";
2728
import { smartWallet } from "./smart-wallet.js";
2829

@@ -70,6 +71,13 @@ describe.runIf(process.env.TW_SECRET_KEY).sequential(
7071

7172
it("can connect", async () => {
7273
expect(smartWalletAddress).toHaveLength(42);
74+
const predictedAddress = await predictSmartAccountAddress({
75+
client,
76+
chain,
77+
adminAddress: personalAccount.address,
78+
factoryAddress: DEFAULT_ACCOUNT_FACTORY_V0_7,
79+
});
80+
expect(predictedAddress).toEqual(smartWalletAddress);
7381
});
7482

7583
it("should revert on unsuccessful transactions", async () => {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +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 { predictSmartAccountAddress } from "./lib/calls.js";
2627
import { smartWallet } from "./smart-wallet.js";
2728

2829
let wallet: Wallet;
@@ -69,6 +70,12 @@ describe.runIf(process.env.TW_SECRET_KEY).sequential(
6970

7071
it("can connect", async () => {
7172
expect(smartWalletAddress).toHaveLength(42);
73+
const predictedAddress = await predictSmartAccountAddress({
74+
client,
75+
chain,
76+
adminAddress: personalAccount.address,
77+
});
78+
expect(predictedAddress).toEqual(smartWalletAddress);
7279
});
7380

7481
it("should revert on unsuccessful transactions", async () => {

0 commit comments

Comments
 (0)