transferOwnership
transferOwnership
is an action exported by @alchemy/aa-accounts
which sends a UO that transfers ownership of the account to a new owner, and returns either the UO hash or transaction hash.
Usage
ts
import { smartAccountClient } from "./lightAccountClient";
const accountAddress = smartAccountClient.getAddress();
// transfer ownership
const newOwner = LocalAccountSigner.mnemonicToAccountSigner(NEW_OWNER_MNEMONIC);
const hash = await smartAccountClient.transferOwnership({
newOwner,
waitForTxn: true,
});
// after transaction is mined on the network,
// create a new light account client for the transferred Light Account
const transferredClient = await createLightAccountClient({
transport: custom(smartAccountClient),
chain: smartAccountClient.chain,
signer: newOwner,
accountAddress, // NOTE: You MUST to specify the original smart account address to connect using the new owner/signer
});
ts
import {
createLightAccount,
lightAccountClientActions,
} from "@alchemy/aa-accounts";
import {
LocalAccountSigner,
SmartAccountSigner,
createSmartAccountClient,
polygonMumbai,
} from "@alchemy/aa-core";
import { http } from "viem";
export const chain = polygonMumbai;
export const signer: SmartAccountSigner =
LocalAccountSigner.mnemonicToAccountSigner("YOUR_OWNER_MNEMONIC");
export const rpcTransport = http(
"https://polygon-mumbai.g.alchemy.com/v2/demo"
);
export const smartAccountClient = createSmartAccountClient({
transport: rpcTransport,
chain,
account: await createLightAccount({
transport: rpcTransport,
chain,
signer,
}),
}).extend(lightAccountClientActions);
Returns
Promise<0x${string}>
A Promise
that resolves to the user operation hash (transaction hash if waitForTx
is true) which transferred ownership of the smart account's owner
Parameters
client: SmartAccountClient
-- the client to use to send the transactionoptions: TransferLightAccountOwnershipParams
-- the options to use to transfer ownershipnewOwner: TSigner extends SmartAccountSigner = SmartAccountSigner
-- the new on-chain owner of the accountwaitForTxn?: boolean
-- optionally, wait for the transaction to be mined with the UOaccount?: LightAccount
-- optionally, pass the account if your client is not connected to it