Skip to content

Commit 3c803e8

Browse files
committed
fix(sdk): use transaction-specified chain in smart account
1 parent 84d3dc4 commit 3c803e8

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

.changeset/fair-planes-doubt.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+
SDK: Fix chain switching in smart account transactions

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,30 @@ async function createSmartAccount(
240240
} else {
241241
paymasterOverride = options.overrides?.paymaster;
242242
}
243+
244+
const accountContractForTransaction = (() => {
245+
// If this transaction is for a different chain than the initial one, get the account contract for that chain
246+
if (transaction.chainId !== accountContract.chain.id) {
247+
return getContract({
248+
address: account.address,
249+
chain: getCachedChain(transaction.chainId),
250+
client: options.client,
251+
});
252+
}
253+
// Default to the existing account contract
254+
return accountContract;
255+
})();
256+
243257
const executeTx = prepareExecute({
244-
accountContract,
258+
accountContract: accountContractForTransaction,
245259
transaction,
246260
executeOverride: options.overrides?.execute,
247261
});
248262
return _sendUserOp({
249263
executeTx,
250264
options: {
251265
...options,
266+
chain: getCachedChain(transaction.chainId),
252267
overrides: {
253268
...options.overrides,
254269
paymaster: paymasterOverride,

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import type { Account, Wallet } from "../interfaces/wallet.js";
2626
import { generateAccount } from "../utils/generateAccount.js";
2727
import { predictSmartAccountAddress } from "./lib/calls.js";
2828
import { smartWallet } from "./smart-wallet.js";
29+
import { TEST_WALLET_A } from "~test/addresses.js";
2930

3031
let wallet: Wallet;
3132
let smartAccount: Account;
@@ -309,6 +310,19 @@ describe.runIf(process.env.TW_SECRET_KEY).sequential(
309310
expect(wallet.getChain()?.id).toEqual(baseSepolia.id);
310311
});
311312

313+
it("can send a transaction on another chain", async () => {
314+
const tx = await sendAndConfirmTransaction({
315+
transaction: prepareTransaction({
316+
to: TEST_WALLET_A,
317+
client: TEST_CLIENT,
318+
chain: baseSepolia,
319+
value: 0n,
320+
}),
321+
account: wallet.getAccount()!,
322+
});
323+
expect(tx.transactionHash).toHaveLength(66);
324+
});
325+
312326
it("can execute 2 tx in parallel", async () => {
313327
const newSmartWallet = smartWallet({
314328
chain,

0 commit comments

Comments
 (0)