-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
uses a placeholder signer for now <!-- start pr-codex --> --- ## PR-Codex overview This PR introduces a new smart contract account type called `SMAV2Account`, along with its creation function `createSMAV2Account`. It includes types, parameters, and logic for initializing accounts using a bundler client, handling initial owners, and generating account addresses. ### Detailed summary - Added type `SMAV2Account` for smart contract accounts. - Introduced type `CreateSMAV2AccountParams` for account creation parameters. - Implemented `createSMAV2Account` function to create a new account. - Added logic to handle account initialization and owner address retrieval. - Utilized `createBundlerClient` for client creation. - Integrated `standardExecutor` and `multiOwnerMessageSigner` for account functionality. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
- Loading branch information
Showing
1 changed file
with
127 additions
and
0 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
account-kit/smart-contracts/src/ma-v2/account/account.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import type { | ||
EntryPointDef, | ||
SmartAccountSigner, | ||
SmartContractAccountWithSigner, | ||
ToSmartContractAccountParams, | ||
} from "@aa-sdk/core"; | ||
import { | ||
createBundlerClient, | ||
getAccountAddress, | ||
getEntryPoint, | ||
toSmartContractAccount, | ||
} from "@aa-sdk/core"; | ||
import { | ||
concatHex, | ||
encodeFunctionData, | ||
type Address, | ||
type Chain, | ||
type Hex, | ||
type Transport, | ||
} from "viem"; | ||
import { accountFactoryAbi } from "../abis/accountFactoryAbi.js"; | ||
import { addresses } from "../utils.js"; | ||
import { standardExecutor } from "../../msca/account/standardExecutor.js"; | ||
import { multiOwnerMessageSigner } from "../../msca/plugins/multi-owner/signer.js"; // TODO: swap for MA v2 signer | ||
|
||
export const DEFAULT_OWNER_ENTITY_ID = 0; | ||
|
||
export type SMAV2Account< | ||
TSigner extends SmartAccountSigner = SmartAccountSigner | ||
> = SmartContractAccountWithSigner<"SMAV2Account", TSigner, "0.7.0">; | ||
|
||
export type CreateSMAV2AccountParams< | ||
TTransport extends Transport = Transport, | ||
TSigner extends SmartAccountSigner = SmartAccountSigner, | ||
TEntryPointVersion extends "0.7.0" = "0.7.0" | ||
> = Pick< | ||
ToSmartContractAccountParams< | ||
"SMAV2Account", | ||
TTransport, | ||
Chain, | ||
TEntryPointVersion | ||
>, | ||
"transport" | "chain" | ||
> & { | ||
signer: TSigner; | ||
salt?: bigint; | ||
factoryAddress?: Address; | ||
initCode?: Hex; | ||
initialOwner?: Address; | ||
accountAddress?: Address; | ||
entryPoint?: EntryPointDef<TEntryPointVersion, Chain>; | ||
}; | ||
|
||
export async function createSMAV2Account< | ||
TTransport extends Transport = Transport, | ||
TSigner extends SmartAccountSigner = SmartAccountSigner | ||
>( | ||
config: CreateSMAV2AccountParams<TTransport, TSigner> | ||
): Promise<SMAV2Account<TSigner>>; | ||
|
||
export async function createSMAV2Account( | ||
config: CreateSMAV2AccountParams | ||
): Promise<SMAV2Account> { | ||
const { | ||
transport, | ||
chain, | ||
signer, | ||
salt = 0n, | ||
factoryAddress = addresses.accountFactory, | ||
initCode, | ||
initialOwner, | ||
accountAddress, | ||
entryPoint = getEntryPoint(chain, { version: "0.7.0" }), | ||
} = config; | ||
|
||
const client = createBundlerClient({ | ||
transport, | ||
chain, | ||
}); | ||
|
||
const getAccountInitCode = async () => { | ||
if (initCode) { | ||
return initCode; | ||
} | ||
|
||
// If an initial owner is not provided, use the signer's address | ||
const ownerAddress = initialOwner || (await signer.getAddress()); | ||
|
||
return concatHex([ | ||
factoryAddress, | ||
encodeFunctionData({ | ||
abi: accountFactoryAbi, | ||
functionName: "createAccount", | ||
args: [ownerAddress, salt, DEFAULT_OWNER_ENTITY_ID], | ||
}), | ||
]); | ||
}; | ||
|
||
const _accountAddress = await getAccountAddress({ | ||
client, | ||
entryPoint, | ||
accountAddress, | ||
getAccountInitCode, | ||
}); | ||
|
||
const baseAccount = await toSmartContractAccount({ | ||
transport, | ||
chain, | ||
entryPoint, | ||
accountAddress: _accountAddress, | ||
source: `SMAV2Account`, | ||
getAccountInitCode, | ||
...standardExecutor, | ||
...multiOwnerMessageSigner( | ||
// TODO: temp | ||
client, | ||
addresses.accountFactory, | ||
() => signer, | ||
addresses.accountFactory | ||
), | ||
}); | ||
|
||
return { | ||
...baseAccount, | ||
getSigner: () => signer, | ||
}; | ||
} |