diff --git a/account-kit/smart-contracts/src/light-account/clients/client.test.ts b/account-kit/smart-contracts/src/light-account/clients/client.test.ts index 0c58a7c142..25442a97da 100644 --- a/account-kit/smart-contracts/src/light-account/clients/client.test.ts +++ b/account-kit/smart-contracts/src/light-account/clients/client.test.ts @@ -4,12 +4,21 @@ import { erc7677Middleware, LocalAccountSigner, type BatchUserOperationCallData, + type SmartAccountClient, type SmartAccountSigner, type UserOperationCallData, type UserOperationOverrides, type UserOperationStruct, } from "@aa-sdk/core"; -import { custom, parseEther, type Address, publicActions } from "viem"; +import { + custom, + parseEther, + type Address, + publicActions, + type Chain, + type Client, + type CustomTransport, +} from "viem"; import { setBalance } from "viem/actions"; import { resetBalance } from "~test/accounts.js"; import { accounts } from "~test/constants.js"; @@ -21,6 +30,15 @@ import { getMSCAUpgradeToData } from "../../msca/utils.js"; import type { LightAccountVersion } from "../types.js"; import { AccountVersionRegistry } from "../utils.js"; import { createLightAccountClient } from "./client.js"; +import { + alchemy, + polygonMumbai, + alchemyEnhancedApiActions, + type AlchemyTransport, + type AlchemySmartAccountClient, + type AlchemyEnhancedApis, +} from "@account-kit/infra"; +import { Alchemy, Network } from "alchemy-sdk"; const versions = Object.keys( AccountVersionRegistry.LightAccount @@ -387,4 +405,76 @@ describe("Light Account Tests", () => { chain: instance.chain, ...(usePaymaster ? erc7677Middleware() : {}), }); + + const givenAlchemyConnectedProvider = async ({ + signer, + chain, + }: { + signer: SmartAccountSigner; + chain: Chain; + }) => + createLightAccountClient({ + transport: alchemy({ + jwt: "test", + }), + chain, + signer, + accountAddress: "0x86f3B0211764971Ad0Fc8C8898d31f5d792faD84", + }); + it("Should have some alchemy specific types", async () => { + const alchemy = new Alchemy({ + network: Network.MATIC_MUMBAI, + apiKey: "test", + }); + const chain = polygonMumbai; + + const provider = ( + await givenAlchemyConnectedProvider({ signer, chain }) + ).extend(alchemyEnhancedApiActions(alchemy)); + + assertType>(provider); + assertType(provider); + assertType(provider); + assertType(provider); + assertType( + // @ts-expect-error + await givenAlchemyConnectedProvider({ signer, chain }) + ); + // @ts-expect-error + assertType>(provider); + }); + it("Should have some non-alchemy specific types", async () => { + const chain = polygonMumbai; + + const signer: SmartAccountSigner = new LocalAccountSigner( + accounts.fundedAccountOwner + ); + const provider = await givenConnectedProvider({ + signer, + version: "v1.0.1", + }); + + assertType(provider); + assertType>(provider); + assertType( + // @ts-expect-error + await givenAlchemyConnectedProvider({ signer, chain }) + ); + // @ts-expect-error + assertType>(provider); + // @ts-expect-error + assertType(provider); + // @ts-expect-error + assertType(provider); + + expect(() => { + const alchemy = new Alchemy({ + network: Network.MATIC_MUMBAI, + apiKey: "test", + }); + + // @ts-expect-error + provider.extend(alchemyEnhancedApiActions(alchemy)); + }).not.toBeFalsy(); + }); }); diff --git a/account-kit/smart-contracts/src/light-account/clients/client.ts b/account-kit/smart-contracts/src/light-account/clients/client.ts index ee1bab0ff8..b700030efc 100644 --- a/account-kit/smart-contracts/src/light-account/clients/client.ts +++ b/account-kit/smart-contracts/src/light-account/clients/client.ts @@ -16,9 +16,17 @@ import { lightAccountClientActions, type LightAccountClientActions, } from "../decorators/lightAccount.js"; +import { + type AlchemySmartAccountClient, + type AlchemyTransport, +} from "@account-kit/infra"; +import { + createLightAccountAlchemyClient, + type AlchemyLightAccountClientConfig, +} from "./alchemyClient.js"; export type CreateLightAccountClientParams< - TTransport extends Transport = Transport, + TTransport extends Transport | AlchemyTransport = Transport, TChain extends Chain | undefined = Chain | undefined, TSigner extends SmartAccountSigner = SmartAccountSigner > = { @@ -31,14 +39,28 @@ export type CreateLightAccountClientParams< >; export function createLightAccountClient< - TChain extends Chain | undefined = Chain | undefined, TSigner extends SmartAccountSigner = SmartAccountSigner >( - args: CreateLightAccountClientParams + params: AlchemyLightAccountClientConfig & { + transport: AlchemyTransport; + } +): Promise< + AlchemySmartAccountClient< + Chain | undefined, + LightAccount, + LightAccountClientActions + > +>; +export function createLightAccountClient< + TChain extends Chain | undefined = Chain | undefined, + TSigner extends SmartAccountSigner = SmartAccountSigner, + TTransport extends Transport = Transport +>( + args: CreateLightAccountClientParams ): Promise< SmartAccountClient< CustomTransport, - Chain, + TChain, LightAccount, SmartAccountClientActions & LightAccountClientActions> @@ -48,6 +70,8 @@ export function createLightAccountClient< /** * Creates a light account client using the provided parameters, including account information, transport mechanism, blockchain chain, and additional client configurations. This function first creates a light account and then uses it to create a smart account client, extending it with light account client actions. * + * Also, we modified the return type to be the light account alchemy client if the transport is alchemy. + * * @example * ```ts * import { createLightAccountClient } from "@account-kit/smart-contracts"; @@ -67,9 +91,16 @@ export function createLightAccountClient< */ export async function createLightAccountClient( params: CreateLightAccountClientParams -): Promise { +): Promise { const { transport, chain } = params; + if (isAlchemyTransport(transport, chain)) { + return await createLightAccountAlchemyClient({ + ...params, + transport, + }); + } + const lightAccount = await createLightAccount({ ...params, transport, @@ -83,3 +114,10 @@ export async function createLightAccountClient( account: lightAccount, }).extend(lightAccountClientActions); } + +function isAlchemyTransport( + transport: Transport, + chain: Chain +): transport is AlchemyTransport { + return transport({ chain }).config.type === "alchemy"; +} diff --git a/site/pages/reference/account-kit/infra/functions/isAlchemyTransport.mdx b/site/pages/reference/account-kit/infra/functions/isAlchemyTransport.mdx new file mode 100644 index 0000000000..8e3c9944c1 --- /dev/null +++ b/site/pages/reference/account-kit/infra/functions/isAlchemyTransport.mdx @@ -0,0 +1,36 @@ +--- +# This file is autogenerated +title: isAlchemyTransport +description: Overview of the isAlchemyTransport method +--- + +# isAlchemyTransport + +Checks whether the given transport is an AlchemyTransport. + +## Import + +```ts +import { isAlchemyTransport } from "@account-kit/infra"; +``` + +## Usage + +```ts +import { isAlchemyTransport } from "@account-kit/infra"; +if (isAlchemyTransport(transport)) { + // do things here that are only when transport is an AlchemyTransport +} +``` + +## Parameters + +### x + +`Transport | AlchemyTransport` +The transport to check. + +## Returns + +`boolean` +True if the transport is of type AlchemyTransport, otherwise false. diff --git a/site/pages/reference/account-kit/smart-contracts/functions/createLightAccountClient.mdx b/site/pages/reference/account-kit/smart-contracts/functions/createLightAccountClient.mdx index a412ec27a4..568b40462c 100644 --- a/site/pages/reference/account-kit/smart-contracts/functions/createLightAccountClient.mdx +++ b/site/pages/reference/account-kit/smart-contracts/functions/createLightAccountClient.mdx @@ -9,6 +9,8 @@ description: Overview of the createLightAccountClient method Creates a light account client using the provided parameters, including account information, transport mechanism, blockchain chain, and additional client configurations. This function first creates a light account and then uses it to create a smart account client, extending it with light account client actions. +Also, we modified the return type to be the light account alchemy client if the transport is alchemy. + ## Import ```ts