Skip to content

Commit

Permalink
refactor: clean up some of the light account types (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
moldy530 committed Jul 30, 2024
1 parent bfcf4f7 commit 08048fc
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 615 deletions.
14 changes: 8 additions & 6 deletions account-kit/core/src/actions/createAccount.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type SmartAccountSigner } from "@aa-sdk/core";
import {
createLightAccount,
createMultiOwnerModularAccount,
type CreateLightAccountParams,
type CreateMultiOwnerModularAccountParams,
type GetLightAccountVersion,
type LightAccountVersion,
} from "@account-kit/smart-contracts";
import { type SmartAccountSigner } from "@aa-sdk/core";
import { custom, type Transport } from "viem";
import { ClientOnlyPropertyError } from "../errors.js";
import type {
Expand All @@ -23,8 +23,7 @@ export type AccountConfig<TAccount extends SupportedAccountTypes> =
CreateLightAccountParams<
Transport,
SmartAccountSigner,
// we only support LightAccount version v1
Exclude<GetLightAccountVersion<"LightAccount">, "v2.0.0">
LightAccountVersion<"LightAccount">
>,
"signer" | "transport" | "chain"
>
Expand Down Expand Up @@ -94,8 +93,11 @@ export async function createAccount<TAccount extends SupportedAccountTypes>(
});
case "MultiOwnerModularAccount":
return createMultiOwnerModularAccount({
...params,
...cachedConfig,
...(params as AccountConfig<"MultiOwnerModularAccount">),
...(cachedConfig as Omit<
CreateMultiOwnerModularAccountParams,
"transport" | "chain" | "signer"
>),
signer,
transport: (opts) => transport({ ...opts, retryCount: 0 }),
chain,
Expand Down
2 changes: 1 addition & 1 deletion account-kit/smart-contracts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export {
LightAccountUnsupported1271Impls,
defaultLightAccountVersion,
getDefaultLightAccountFactoryAddress,
getLightAccountVersionDef,
getLightAccountVersionForAccount,
} from "./light-account/utils.js";

//multi-owner-light-account exports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const chain = polygonMumbai;

const versions = Object.keys(
AccountVersionRegistry.LightAccount
) as LightAccountVersion[];
) as LightAccountVersion<"LightAccount">[];

describe("Light Account Tests", () => {
const dummyMnemonic =
Expand Down Expand Up @@ -170,7 +170,7 @@ const givenConnectedProvider = ({
}: {
signer: SmartAccountSigner;
chain: Chain;
version: LightAccountVersion;
version: LightAccountVersion<"LightAccount">;
}) =>
createLightAccountClient({
account: {
Expand All @@ -181,8 +181,12 @@ const givenConnectedProvider = ({
transport: custom({
request: async ({ method }) => {
if (method === "eth_getStorageAt") {
return AccountVersionRegistry.LightAccount[version].address[chain.id]
.impl;
return (
AccountVersionRegistry.LightAccount[version].addresses.overrides?.[
chain.id
].impl ??
AccountVersionRegistry.LightAccount[version].addresses.default.impl
);
}
return;
},
Expand Down
69 changes: 24 additions & 45 deletions account-kit/smart-contracts/src/light-account/accounts/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { LightAccountAbi_v2 } from "../abis/LightAccountAbi_v2.js";
import { LightAccountFactoryAbi_v1 } from "../abis/LightAccountFactoryAbi_v1.js";
import { LightAccountFactoryAbi_v2 } from "../abis/LightAccountFactoryAbi_v2.js";
import type {
GetEntryPointForLightAccountVersion,
GetLightAccountVersion,
LightAccountEntryPointVersion,
LightAccountVersion,
} from "../types.js";
import {
AccountVersionRegistry,
Expand All @@ -35,17 +35,8 @@ import {

export type LightAccount<
TSigner extends SmartAccountSigner = SmartAccountSigner,
TLightAccountVersion extends GetLightAccountVersion<"LightAccount"> = GetLightAccountVersion<"LightAccount">,
TEntryPointVersion extends GetEntryPointForLightAccountVersion<
"LightAccount",
TLightAccountVersion
> = GetEntryPointForLightAccountVersion<"LightAccount", TLightAccountVersion>
> = LightAccountBase<
TSigner,
"LightAccount",
TLightAccountVersion,
TEntryPointVersion
> & {
TLightAccountVersion extends LightAccountVersion<"LightAccount"> = LightAccountVersion<"LightAccount">
> = LightAccountBase<TSigner, "LightAccount", TLightAccountVersion> & {
encodeTransferOwnership: (newOwner: Address) => Hex;
getOwnerAddress: () => Promise<Address>;
};
Expand All @@ -54,55 +45,41 @@ export type LightAccount<
export type CreateLightAccountParams<
TTransport extends Transport = Transport,
TSigner extends SmartAccountSigner = SmartAccountSigner,
TLightAccountVersion extends GetLightAccountVersion<"LightAccount"> = GetLightAccountVersion<"LightAccount">,
TEntryPointVersion extends GetEntryPointForLightAccountVersion<
"LightAccount",
TLightAccountVersion
> = GetEntryPointForLightAccountVersion<"LightAccount", TLightAccountVersion>
TLightAccountVersion extends LightAccountVersion<"LightAccount"> = LightAccountVersion<"LightAccount">
> = Omit<
CreateLightAccountBaseParams<
TTransport,
TSigner,
"LightAccount",
TLightAccountVersion,
TEntryPointVersion
TTransport,
TSigner
>,
"getAccountInitCode" | "entryPoint" | "version" | "abi" | "accountAddress"
| "getAccountInitCode"
| "entryPoint"
| "version"
| "abi"
| "accountAddress"
| "type"
> & {
salt?: bigint;
initCode?: Hex;
accountAddress?: Address;
factoryAddress?: Address;
version?: TLightAccountVersion;
entryPoint?: EntryPointDef<TEntryPointVersion, Chain>;
entryPoint?: EntryPointDef<
LightAccountEntryPointVersion<"LightAccount", TLightAccountVersion>,
Chain
>;
};
// [!endregion CreateLightAccountParams]

export async function createLightAccount<
TTransport extends Transport = Transport,
TSigner extends SmartAccountSigner = SmartAccountSigner,
TLightAccountVersion extends GetLightAccountVersion<"LightAccount"> = "v1.1.0"
TLightAccountVersion extends LightAccountVersion<"LightAccount"> = "v2.0.0"
>(
config: CreateLightAccountParams<TTransport, TSigner, TLightAccountVersion>
): Promise<LightAccount<TSigner, TLightAccountVersion>>;

export async function createLightAccount<
TTransport extends Transport = Transport,
TSigner extends SmartAccountSigner = SmartAccountSigner,
TLightAccountVersion extends GetLightAccountVersion<"LightAccount"> = GetLightAccountVersion<"LightAccount">,
TEntryPointVersion extends GetEntryPointForLightAccountVersion<
"LightAccount",
TLightAccountVersion
> = GetEntryPointForLightAccountVersion<"LightAccount", TLightAccountVersion>
>(
config: CreateLightAccountParams<
TTransport,
TSigner,
TLightAccountVersion,
TEntryPointVersion
>
): Promise<LightAccount<TSigner, TLightAccountVersion, TEntryPointVersion>>;

/**
* Creates a light account based on the provided parameters such as transport, chain, signer, init code, and more. Ensures that an account is configured and returned with various capabilities, such as transferring ownership and retrieving the owner's address.
*
Expand All @@ -128,7 +105,7 @@ export async function createLightAccount({
chain,
signer,
initCode,
version = defaultLightAccountVersion("LightAccount"),
version = defaultLightAccountVersion(),
entryPoint = getEntryPoint(chain, {
version: AccountVersionRegistry["LightAccount"][version]
.entryPointVersion as any,
Expand Down Expand Up @@ -176,15 +153,17 @@ export async function createLightAccount({
});

const account = await createLightAccountBase<
"LightAccount",
LightAccountVersion<"LightAccount">,
Transport,
SmartAccountSigner,
"LightAccount"
SmartAccountSigner
>({
transport,
chain,
signer,
abi: accountAbi,
version: AccountVersionRegistry["LightAccount"][version],
type: "LightAccount",
version,
entryPoint,
accountAddress: address,
getAccountInitCode,
Expand Down
Loading

0 comments on commit 08048fc

Please sign in to comment.