Skip to content

Commit 6027ff2

Browse files
committed
fixup! feat(sdk): makes chain optional in smart wallet config
1 parent cadb36f commit 6027ff2

File tree

3 files changed

+880
-816
lines changed

3 files changed

+880
-816
lines changed
Lines changed: 123 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { getCachedChain } from "../../../chains/utils.js";
12
import type { Chain } from "../../../chains/types.js";
23
import type { ThirdwebClient } from "../../../client/client.js";
34
import {
4-
type ThirdwebContract,
5-
getContract,
5+
type ThirdwebContract,
6+
getContract,
67
} from "../../../contract/contract.js";
78
import { prepareContractCall } from "../../../transaction/prepare-contract-call.js";
89
import type { PreparedTransaction } from "../../../transaction/prepare-transaction.js";
@@ -28,21 +29,21 @@ import { DEFAULT_ACCOUNT_FACTORY_V0_6 } from "./constants.js";
2829
* @walletUtils
2930
*/
3031
export async function predictSmartAccountAddress(args: {
31-
client: ThirdwebClient;
32-
chain: Chain;
33-
adminAddress: string;
34-
factoryAddress?: string;
35-
accountSalt?: string;
32+
client: ThirdwebClient;
33+
chain?: Chain;
34+
adminAddress: string;
35+
factoryAddress?: string;
36+
accountSalt?: string;
3637
}): 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-
});
38+
return predictAddress({
39+
adminAddress: args.adminAddress,
40+
accountSalt: args.accountSalt,
41+
factoryContract: getContract({
42+
address: args.factoryAddress ?? DEFAULT_ACCOUNT_FACTORY_V0_6,
43+
chain: args.chain ?? getCachedChain(1),
44+
client: args.client,
45+
}),
46+
});
4647
}
4748

4849
/**
@@ -63,132 +64,132 @@ export async function predictSmartAccountAddress(args: {
6364
* @deprecated Use `predictSmartAccountAddress` instead.
6465
*/
6566
export async function predictAddress(args: {
66-
factoryContract: ThirdwebContract;
67-
predictAddressOverride?: (
68-
factoryContract: ThirdwebContract,
69-
admin: string,
70-
) => Promise<string>;
71-
adminAddress: string;
72-
accountSalt?: string;
73-
accountAddress?: string;
67+
factoryContract: ThirdwebContract;
68+
predictAddressOverride?: (
69+
factoryContract: ThirdwebContract,
70+
admin: string,
71+
) => Promise<string>;
72+
adminAddress: string;
73+
accountSalt?: string;
74+
accountAddress?: string;
7475
}): Promise<string> {
75-
const {
76-
factoryContract,
77-
predictAddressOverride: predictAddress,
78-
adminAddress,
79-
accountSalt,
80-
accountAddress,
81-
} = args;
82-
if (predictAddress) {
83-
return predictAddress(factoryContract, adminAddress);
84-
}
85-
if (accountAddress) {
86-
return accountAddress;
87-
}
88-
if (!adminAddress) {
89-
throw new Error(
90-
"Account address is required to predict the smart wallet address.",
91-
);
92-
}
93-
const saltHex =
94-
accountSalt && isHex(accountSalt)
95-
? accountSalt
96-
: stringToHex(accountSalt ?? "");
97-
return readContract({
98-
contract: factoryContract,
99-
method: "function getAddress(address, bytes) returns (address)",
100-
params: [adminAddress, saltHex],
101-
});
76+
const {
77+
factoryContract,
78+
predictAddressOverride: predictAddress,
79+
adminAddress,
80+
accountSalt,
81+
accountAddress,
82+
} = args;
83+
if (predictAddress) {
84+
return predictAddress(factoryContract, adminAddress);
85+
}
86+
if (accountAddress) {
87+
return accountAddress;
88+
}
89+
if (!adminAddress) {
90+
throw new Error(
91+
"Account address is required to predict the smart wallet address.",
92+
);
93+
}
94+
const saltHex =
95+
accountSalt && isHex(accountSalt)
96+
? accountSalt
97+
: stringToHex(accountSalt ?? "");
98+
return readContract({
99+
contract: factoryContract,
100+
method: "function getAddress(address, bytes) returns (address)",
101+
params: [adminAddress, saltHex],
102+
});
102103
}
103104

104105
/**
105106
* @internal
106107
*/
107108
export function prepareCreateAccount(args: {
108-
factoryContract: ThirdwebContract;
109-
adminAddress: string;
110-
accountSalt?: string;
111-
createAccountOverride?: (
112-
factoryContract: ThirdwebContract,
113-
admin: string,
114-
) => PreparedTransaction;
109+
factoryContract: ThirdwebContract;
110+
adminAddress: string;
111+
accountSalt?: string;
112+
createAccountOverride?: (
113+
factoryContract: ThirdwebContract,
114+
admin: string,
115+
) => PreparedTransaction;
115116
}): PreparedTransaction {
116-
const {
117-
adminAddress,
118-
factoryContract,
119-
createAccountOverride: createAccount,
120-
accountSalt,
121-
} = args;
122-
if (createAccount) {
123-
return createAccount(factoryContract, adminAddress);
124-
}
125-
const saltHex =
126-
accountSalt && isHex(accountSalt)
127-
? accountSalt
128-
: stringToHex(accountSalt ?? "");
129-
return prepareContractCall({
130-
contract: factoryContract,
131-
method: "function createAccount(address, bytes) returns (address)",
132-
params: [adminAddress, saltHex],
133-
});
117+
const {
118+
adminAddress,
119+
factoryContract,
120+
createAccountOverride: createAccount,
121+
accountSalt,
122+
} = args;
123+
if (createAccount) {
124+
return createAccount(factoryContract, adminAddress);
125+
}
126+
const saltHex =
127+
accountSalt && isHex(accountSalt)
128+
? accountSalt
129+
: stringToHex(accountSalt ?? "");
130+
return prepareContractCall({
131+
contract: factoryContract,
132+
method: "function createAccount(address, bytes) returns (address)",
133+
params: [adminAddress, saltHex],
134+
});
134135
}
135136

136137
/**
137138
* @internal
138139
*/
139140
export function prepareExecute(args: {
140-
accountContract: ThirdwebContract;
141-
transaction: SendTransactionOption;
142-
executeOverride?: (
143-
accountContract: ThirdwebContract,
144-
transaction: SendTransactionOption,
145-
) => PreparedTransaction;
141+
accountContract: ThirdwebContract;
142+
transaction: SendTransactionOption;
143+
executeOverride?: (
144+
accountContract: ThirdwebContract,
145+
transaction: SendTransactionOption,
146+
) => PreparedTransaction;
146147
}): PreparedTransaction {
147-
const { accountContract, transaction, executeOverride: execute } = args;
148-
if (execute) {
149-
return execute(accountContract, transaction);
150-
}
151-
return prepareContractCall({
152-
contract: accountContract,
153-
method: "function execute(address, uint256, bytes)",
154-
params: [
155-
transaction.to || "",
156-
transaction.value || 0n,
157-
transaction.data || "0x",
158-
],
159-
// if gas is specified for the inner tx, use that and add 21k for the execute call on the account contract
160-
// this avoids another estimateGas call when bundling the userOp
161-
// and also allows for passing custom gas limits for the inner tx
162-
gas: transaction.gas ? transaction.gas + 21000n : undefined,
163-
});
148+
const { accountContract, transaction, executeOverride: execute } = args;
149+
if (execute) {
150+
return execute(accountContract, transaction);
151+
}
152+
return prepareContractCall({
153+
contract: accountContract,
154+
method: "function execute(address, uint256, bytes)",
155+
params: [
156+
transaction.to || "",
157+
transaction.value || 0n,
158+
transaction.data || "0x",
159+
],
160+
// if gas is specified for the inner tx, use that and add 21k for the execute call on the account contract
161+
// this avoids another estimateGas call when bundling the userOp
162+
// and also allows for passing custom gas limits for the inner tx
163+
gas: transaction.gas ? transaction.gas + 21000n : undefined,
164+
});
164165
}
165166

166167
/**
167168
* @internal
168169
*/
169170
export function prepareBatchExecute(args: {
170-
accountContract: ThirdwebContract;
171-
transactions: SendTransactionOption[];
172-
executeBatchOverride?: (
173-
accountContract: ThirdwebContract,
174-
transactions: SendTransactionOption[],
175-
) => PreparedTransaction;
171+
accountContract: ThirdwebContract;
172+
transactions: SendTransactionOption[];
173+
executeBatchOverride?: (
174+
accountContract: ThirdwebContract,
175+
transactions: SendTransactionOption[],
176+
) => PreparedTransaction;
176177
}): PreparedTransaction {
177-
const {
178-
accountContract,
179-
transactions,
180-
executeBatchOverride: executeBatch,
181-
} = args;
182-
if (executeBatch) {
183-
return executeBatch(accountContract, transactions);
184-
}
185-
return prepareContractCall({
186-
contract: accountContract,
187-
method: "function executeBatch(address[], uint256[], bytes[])",
188-
params: [
189-
transactions.map((tx) => tx.to || ""),
190-
transactions.map((tx) => tx.value || 0n),
191-
transactions.map((tx) => tx.data || "0x"),
192-
],
193-
});
178+
const {
179+
accountContract,
180+
transactions,
181+
executeBatchOverride: executeBatch,
182+
} = args;
183+
if (executeBatch) {
184+
return executeBatch(accountContract, transactions);
185+
}
186+
return prepareContractCall({
187+
contract: accountContract,
188+
method: "function executeBatch(address[], uint256[], bytes[])",
189+
params: [
190+
transactions.map((tx) => tx.to || ""),
191+
transactions.map((tx) => tx.value || 0n),
192+
transactions.map((tx) => tx.data || "0x"),
193+
],
194+
});
194195
}

0 commit comments

Comments
 (0)