Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix MAv2 React Hook client creation for 7702 #1329

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions aa-sdk/core/src/errors/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ export class EntityIdOverrideError extends BaseError {
}

/**
* Error class denoting that the provided ma v2 account type is invalid.
* Error class denoting that the provided ma v2 account mode is invalid.
*/
export class InvalidModularAccountV2Type extends BaseError {
override name = "InvalidModularAccountV2Type";
export class InvalidModularAccountV2Mode extends BaseError {
override name = "InvalidModularAccountV2Mode";

/**
* Initializes a new instance of the error message with a default message indicating that the provided ma v2 account type is invalid.
* Initializes a new instance of the error message with a default message indicating that the provided ma v2 account mode is invalid.
*/
constructor() {
super(`The provided account type is invalid for ModularAccount V2`);
super(`The provided account mode is invalid for ModularAccount V2`);
}
}
2 changes: 1 addition & 1 deletion aa-sdk/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export {
InvalidEntityIdError,
InvalidNonceKeyError,
EntityIdOverrideError,
InvalidModularAccountV2Type,
InvalidModularAccountV2Mode,
} from "./errors/client.js";
export {
EntryPointNotFoundError,
Expand Down
2 changes: 1 addition & 1 deletion aa-sdk/core/src/middleware/defaults/7702gasEstimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { defaultGasEstimator } from "./gasEstimator.js";
* async function createSMA7702AccountClient(
* config: CreateModularAccountV2ClientParams
* ): Promise<SmartAccountClient> {
* const sma7702Account = await createModularAccountV2({ type: "7702", ...config});
* const sma7702Account = await createModularAccountV2({ ...config, mode: "7702" });
*
* return createSmartAccountClient({
* account: sma7702Account,
Expand Down
2 changes: 1 addition & 1 deletion aa-sdk/core/src/middleware/defaults/7702signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { defaultUserOpSigner } from "./userOpSigner.js";
* async function createSMA7702AccountClient(
* config: CreateModularAccountV2ClientParams
* ): Promise<SmartAccountClient> {
* const sma7702Account = await createModularAccountV2({ type: "7702", ...config});
* const sma7702Account = await createModularAccountV2({ ...config, mode: "7702" });
*
* return createSmartAccountClient({
* account: sma7702Account,
Expand Down
12 changes: 9 additions & 3 deletions account-kit/core/src/actions/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ export type AccountConfig<TAccount extends SupportedAccountTypes> =
? OmitSignerTransportChain<
CreateMultiOwnerModularAccountParams<Transport, AlchemyWebSigner>
>
: OmitSignerTransportChain<
: TAccount extends "ModularAccountV2"
? OmitSignerTransportChain<
CreateModularAccountV2Params<Transport, AlchemyWebSigner>
>;
>
: never;

export type CreateAccountParams<TAccount extends SupportedAccountTypes> = {
type: TAccount;
Expand Down Expand Up @@ -154,8 +156,12 @@ export async function createAccount<TAccount extends SupportedAccountTypes>(
return account;
});
case "ModularAccountV2":
const maV2Params = {
mode: "default",
...params,
};
return createModularAccountV2({
...(params as AccountConfig<"ModularAccountV2">),
...(maV2Params as AccountConfig<"ModularAccountV2">),
...(cachedConfig as OmitSignerTransportChain<CreateModularAccountV2Params>),
signer,
transport: (opts) => transport({ ...opts, retryCount: 0 }),
Expand Down
15 changes: 15 additions & 0 deletions account-kit/core/src/actions/getSmartAccountClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { getAccount, type GetAccountParams } from "./getAccount.js";
import { getAlchemyTransport } from "./getAlchemyTransport.js";
import { getConnection } from "./getConnection.js";
import { getSignerStatus } from "./getSignerStatus.js";
import { default7702GasEstimator, default7702UserOpSigner } from "@aa-sdk/core";

export type GetSmartAccountClientParams<
TChain extends Chain | undefined = Chain | undefined,
Expand Down Expand Up @@ -218,12 +219,26 @@ export function getSmartAccountClient(
isLoadingClient: false,
};
case "ModularAccountV2":
const is7702 =
params.accountParams &&
"mode" in params.accountParams &&
params.accountParams.mode === "7702";
return {
client: createAlchemySmartAccountClient({
transport,
chain: connection.chain,
account: account,
policyId: connection.policyId,
...(is7702
? {
gasEstimator: default7702GasEstimator(
clientParams.gasEstimator
),
signUserOperation: default7702UserOpSigner(
clientParams.signUserOperation
),
}
: {}),
...clientParams,
}),
address: account.address,
Expand Down
10 changes: 10 additions & 0 deletions account-kit/core/src/actions/reconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ export async function reconnect(config: AlchemyAccountsConfig) {
);
}

if (accountConfigs[chain.id]?.["ModularAccountV2"]) {
await createAccount(
{
type: "ModularAccountV2",
accountParams: accountConfigs[chain.id]["ModularAccountV2"],
},
config
);
}

setTimeout(() => unsubConnected(), 1);
});

Expand Down
2 changes: 1 addition & 1 deletion account-kit/react/src/hooks/useAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type UseAccountProps<TAccount extends SupportedAccountTypes> =

/**
* Hook to subscribe to account state and interactions, including creation, connection, and status monitoring. It synchronizes with external store updates and provides status-dependent results.
* The supported account types are: LightAccount, MultiOwnerLightAccount, and MultiOwnerModularAccount.
* The supported account types are: LightAccount, MultiOwnerLightAccount, MultiOwnerModularAccount, and ModularAccountV2.
*
* @example
* ```ts
Expand Down
20 changes: 10 additions & 10 deletions account-kit/smart-contracts/src/ma-v2/account/modularAccountV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getEntryPoint,
getAccountAddress,
EntityIdOverrideError,
InvalidModularAccountV2Type,
InvalidModularAccountV2Mode,
} from "@aa-sdk/core";
import {
concatHex,
Expand Down Expand Up @@ -40,13 +40,13 @@ export type CreateModularAccountV2Params<
}) &
(
| {
type: "default";
mode: "default";
salt?: bigint;
factoryAddress?: Address;
initCode?: Hex;
}
| {
type: "7702";
mode: "7702";
}
);

Expand All @@ -58,9 +58,9 @@ export async function createModularAccountV2<
): Promise<ModularAccountV2<TSigner>>;

/**
* Creates a ModularAccount V2 account, with the type depending on the provided "type" field.
* Possible types include: "default", which is SMA Bytecode, and "7702", which is SMA 7702.
* Handles nonce generation, transaction encoding, and type variant-specific behavior like initcode construction.
* Creates a ModularAccount V2 account, with the mode depending on the provided "mode" field.
* Possible modes include: "default", which is SMA Bytecode, and "7702", which is SMA 7702.
* Handles nonce generation, transaction encoding, and mode variant-specific behavior like initcode construction.
*
* @example
* ```ts twoslash
Expand All @@ -79,7 +79,7 @@ export async function createModularAccountV2<
*
*
* const modularAccountV2 = await createModularAccountV2({
* type: "default", // or "7702"
* mode: "default", // or "7702"
* chain,
* signer,
* transport,
Expand Down Expand Up @@ -111,7 +111,7 @@ export async function createModularAccountV2(
});

const accountFunctions = await (async () => {
switch (config.type) {
switch (config.mode) {
case "7702": {
const getAccountInitCode = async (): Promise<Hex> => {
return "0x";
Expand Down Expand Up @@ -186,7 +186,7 @@ export async function createModularAccountV2(
});
}

// If we add more valid types, the switch case branch's type will no longer be `never`, which will cause a compile time error here and ensure we handle the new type.
// If we add more valid modes, the switch case branch's mode will no longer be `never`, which will cause a compile time error here and ensure we handle the new type.
function assertNever(_valid: never): never {
throw new InvalidModularAccountV2Type();
throw new InvalidModularAccountV2Mode();
}
6 changes: 3 additions & 3 deletions account-kit/smart-contracts/src/ma-v2/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type CreateModularAccountV2ClientParams<
TTransport extends Transport = Transport,
TChain extends Chain = Chain,
TSigner extends SmartAccountSigner = SmartAccountSigner
> = OptionalFields<CreateModularAccountV2Params<TTransport, TSigner>, "type"> &
> = OptionalFields<CreateModularAccountV2Params<TTransport, TSigner>, "mode"> &
Omit<
SmartAccountClientConfig<TTransport, TChain>,
"transport" | "account" | "chain"
Expand Down Expand Up @@ -112,16 +112,16 @@ export async function createModularAccountV2Client(
| CreateModularAccountV2AlchemyClientParams
): Promise<SmartAccountClient | AlchemySmartAccountClient> {
const config_ = {
type: config.type ?? "default",
...config,
mode: config.mode ?? "default",
};

const { transport, chain } = config_;

const account = await createModularAccountV2(config_);

const middlewareToAppend =
config.type === "7702"
config.mode === "7702"
? {
gasEstimator: default7702GasEstimator(config.gasEstimator),
signUserOperation: default7702UserOpSigner(config.signUserOperation),
Expand Down
3 changes: 2 additions & 1 deletion examples/ui-demo/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"extends": "next/core-web-vitals",
"rules": {
"import/extensions": "off"
}
},
"ignorePatterns": ["contracts"]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions site/sidebar/reference/aa-sdk/core.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.