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

feat: add alchemyTransport to use in other places #990

Merged
merged 6 commits into from
Oct 1, 2024
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
7 changes: 5 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
**/dist/*
examples/**/*
site/.vitepress/cache/**/*
.nx/*
.github/*
/.nx/cache
/.nx/cache

/examples/*
!/examples/ui-demo
/examples/ui-demo/.next/*
7 changes: 2 additions & 5 deletions aa-sdk/core/src/client/smartAccountClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { custom, type Transaction } from "viem";
import { polygonMumbai } from "viem/chains";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";
import * as receiptActions from "../actions/bundler/getUserOperationReceipt.js";
import type { UserOperationReceipt } from "../types.js";
import {
Expand Down Expand Up @@ -110,10 +110,7 @@ describe("SmartAccountClient Tests", async () => {
const thenExpectRetriesToBe = async (
expectedRetryMsDelays: number[],
expectedMockCalls: number,
getUserOperationReceiptMock: SpyInstance<
any,
Promise<UserOperationReceipt | null>
>
getUserOperationReceiptMock: MockInstance
) => {
expect(retryMsDelays).toEqual(expectedRetryMsDelays);
expect(getUserOperationReceiptMock).toHaveBeenCalledTimes(
Expand Down
18 changes: 9 additions & 9 deletions aa-sdk/core/src/middleware/erc7677middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import {
} from "../utils/index.js";
import type { ClientMiddlewareFn } from "./types";

export type Erc7677RpcSchema = [
export type Erc7677RpcSchema<
TContext extends Record<string, any> = Record<string, any>
> = [
{
Method: "pm_getPaymasterStubData";
Parameters: [UserOperationRequest, Address, Hex, Record<string, any>];
Parameters: [UserOperationRequest, Address, Hex, TContext];
ReturnType: {
sponsor?: { name: string; icon?: string }; // Sponsor info
paymaster?: Address; // Paymaster address (entrypoint v0.7)
Expand All @@ -38,7 +40,7 @@ export type Erc7677RpcSchema = [
},
{
Method: "pm_getPaymasterData";
Parameters: [UserOperationRequest, Address, Hex, Record<string, any>];
Parameters: [UserOperationRequest, Address, Hex, TContext];
ReturnType: {
paymaster?: Address; // Paymaster address (entrypoint v0.7)
paymasterData?: Hex; // Paymaster data (entrypoint v0.7)
Expand All @@ -47,12 +49,10 @@ export type Erc7677RpcSchema = [
}
];

export type Erc7677Client<T extends Transport = Transport> = Client<
T,
Chain,
undefined,
Erc7677RpcSchema
>;
export type Erc7677Client<
T extends Transport = Transport,
TContext extends Record<string, any> = Record<string, any>
> = Client<T, Chain, undefined, Erc7677RpcSchema<TContext>>;

export type Erc7677MiddlewareParams<
TContext extends Record<string, any> | undefined =
Expand Down
3 changes: 3 additions & 0 deletions account-kit/core/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
beforeEach(() => {
localStorage.clear();
});
14 changes: 14 additions & 0 deletions account-kit/core/src/actions/getAlchemyTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { alchemy, type AlchemyTransport } from "@account-kit/infra";
import { ChainNotFoundError } from "../errors.js";
import type { AlchemyAccountsConfig } from "../types";

export function getAlchemyTransport(
config: AlchemyAccountsConfig
): AlchemyTransport {
const { chain, connections } = config.store.getState();
if (!connections.has(chain.id)) {
throw new ChainNotFoundError(chain);
}

return alchemy(connections.get(chain.id)!.transport);
}
6 changes: 4 additions & 2 deletions account-kit/core/src/actions/getBundlerClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ClientWithAlchemyMethods } from "@account-kit/infra";
import { type ClientWithAlchemyMethods } from "@account-kit/infra";
import type { AlchemyAccountsConfig } from "../types";

/**
Expand All @@ -18,5 +18,7 @@ import type { AlchemyAccountsConfig } from "../types";
export const getBundlerClient = (
config: AlchemyAccountsConfig
): ClientWithAlchemyMethods => {
return config.store.getState().bundlerClient;
const { bundlerClient } = config.store.getState();

return bundlerClient;
};
45 changes: 18 additions & 27 deletions account-kit/core/src/actions/getSmartAccountClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
createAlchemySmartAccountClientFromExisting,
createAlchemySmartAccountClient,
type AlchemySmartAccountClient,
type AlchemySmartAccountClientConfig,
} from "@account-kit/infra";
Expand All @@ -19,7 +19,7 @@ import {
type MultiOwnerPluginActions,
type PluginManagerActions,
} from "@account-kit/smart-contracts";
import type { Address, Chain, Transport } from "viem";
import type { Address, Chain } from "viem";
import type {
AlchemyAccountsConfig,
Connection,
Expand All @@ -29,21 +29,16 @@ import type {
} from "../types";
import { createAccount } from "./createAccount.js";
import { getAccount, type GetAccountParams } from "./getAccount.js";
import { getBundlerClient } from "./getBundlerClient.js";
import { getAlchemyTransport } from "./getAlchemyTransport.js";
import { getConnection } from "./getConnection.js";
import { getSignerStatus } from "./getSignerStatus.js";

export type GetSmartAccountClientParams<
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
TAccount extends SupportedAccountTypes = SupportedAccountTypes
> = Omit<
AlchemySmartAccountClientConfig<
TTransport,
TChain,
SupportedAccount<TAccount>
>,
"rpcUrl" | "chain" | "apiKey" | "jwt" | "account"
AlchemySmartAccountClientConfig<TChain, SupportedAccount<TAccount>>,
"transport" | "account" | "chain"
> &
GetAccountParams<TAccount>;

Expand All @@ -60,29 +55,22 @@ export type ClientActions<
: never;

export type GetSmartAccountClientResult<
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
TAccount extends SupportedAccounts = SupportedAccounts
> = {
client?: AlchemySmartAccountClient<
TTransport,
TChain,
TAccount,
ClientActions<TAccount>
>;
client?: AlchemySmartAccountClient<TChain, TAccount, ClientActions<TAccount>>;
address?: Address;
isLoadingClient: boolean;
error?: Error;
};

export function getSmartAccountClient<
TTransport extends Transport = Transport,
TChain extends Chain | undefined = Chain | undefined,
TAccount extends SupportedAccountTypes = SupportedAccountTypes
>(
params: GetSmartAccountClientParams<TTransport, TChain, TAccount>,
params: GetSmartAccountClientParams<TChain, TAccount>,
config: AlchemyAccountsConfig
): GetSmartAccountClientResult<TTransport, TChain, SupportedAccount<TAccount>>;
): GetSmartAccountClientResult<TChain, SupportedAccount<TAccount>>;

/**
* Obtains a smart account client based on the provided parameters and configuration. Supports creating any of the SupportAccountTypes in Account Kit.
Expand Down Expand Up @@ -116,7 +104,7 @@ export function getSmartAccountClient(
config
);
const signerStatus = getSignerStatus(config);
const bundlerClient = getBundlerClient(config);
const transport = getAlchemyTransport(config);
const connection = getConnection(config);
const clientState =
config.store.getState().smartAccountClients[connection.chain.id]?.[type];
Expand Down Expand Up @@ -189,8 +177,9 @@ export function getSmartAccountClient(
switch (account.source) {
case "LightAccount":
return {
client: createAlchemySmartAccountClientFromExisting({
client: bundlerClient,
client: createAlchemySmartAccountClient({
transport,
chain: connection.chain,
account: account,
policyId: connection.policyId,
...clientParams,
Expand All @@ -200,8 +189,9 @@ export function getSmartAccountClient(
};
case "MultiOwnerLightAccount":
return {
client: createAlchemySmartAccountClientFromExisting({
client: bundlerClient,
client: createAlchemySmartAccountClient({
transport,
chain: connection.chain,
account: account,
policyId: connection.policyId,
...clientParams,
Expand All @@ -211,8 +201,9 @@ export function getSmartAccountClient(
};
case "MultiOwnerModularAccount":
return {
client: createAlchemySmartAccountClientFromExisting({
client: bundlerClient,
client: createAlchemySmartAccountClient({
transport,
chain: connection.chain,
account: account,
policyId: connection.policyId,
...clientParams,
Expand Down
5 changes: 3 additions & 2 deletions account-kit/core/src/actions/setChain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAlchemyPublicRpcClient } from "@account-kit/infra";
import { alchemy, createAlchemyPublicRpcClient } from "@account-kit/infra";
import { switchChain } from "@wagmi/core";
import type { Chain } from "viem";
import { ChainNotFoundError } from "../errors.js";
Expand Down Expand Up @@ -27,12 +27,13 @@ export async function setChain(config: AlchemyAccountsConfig, chain: Chain) {
}

await switchChain(config._internal.wagmiConfig, { chainId: chain.id });
const transport = connection.transport;

config.store.setState(() => ({
chain,
bundlerClient: createAlchemyPublicRpcClient({
chain,
connectionConfig: connection,
transport: alchemy(transport),
}),
}));
}
62 changes: 62 additions & 0 deletions account-kit/core/src/actions/watchBundlerClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { alchemy, arbitrumSepolia, sepolia } from "@account-kit/infra";
import { AlchemySignerStatus } from "@account-kit/signer";
import { createConfig } from "../createConfig.js";
import {
convertSignerStatusToState,
createDefaultAccountState,
} from "../store/store.js";
import { setChain } from "./setChain.js";
import { watchBundlerClient } from "./watchBundlerClient.js";

describe("watchBundlerClient", () => {
it("should not fire the callback if transport or chain didn't change", () => {
const config = givenConfig();
const onChange = vi.fn();

watchBundlerClient(config)(onChange);

config.store.setState({
signerStatus: convertSignerStatusToState(
AlchemySignerStatus.AWAITING_EMAIL_AUTH
),
});

expect(onChange).not.toHaveBeenCalled();
});

it("should fire the callback if chain changed", async () => {
const config = givenConfig();
const onChange = vi.fn();

watchBundlerClient(config)(onChange);

await setChain(config, arbitrumSepolia);

expect(onChange).toHaveBeenCalled();
});

it("should not fire if the chain id is the same", async () => {
const config = givenConfig();
const onChange = vi.fn();

watchBundlerClient(config)(onChange);

await setChain(config, sepolia);

expect(onChange).not.toHaveBeenCalled();
});

const givenConfig = () => {
const config = createConfig({
chain: sepolia,
chains: [{ chain: sepolia }, { chain: arbitrumSepolia }],
transport: alchemy({ apiKey: "AN_API_KEY" }),
});

config.store.setState({
accounts: createDefaultAccountState([sepolia, arbitrumSepolia]),
});

return config;
};
});
9 changes: 7 additions & 2 deletions account-kit/core/src/actions/watchBundlerClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ClientWithAlchemyMethods } from "@account-kit/infra";
import { type ClientWithAlchemyMethods } from "@account-kit/infra";
import type { AlchemyAccountsConfig } from "../types";

/**
Expand All @@ -21,6 +21,11 @@ export const watchBundlerClient =
(onChange: (bundlerClient: ClientWithAlchemyMethods) => void) => {
return config.store.subscribe(
({ bundlerClient }) => bundlerClient,
onChange
onChange,
{
equalityFn(a, b) {
return a.chain.id === b.chain.id;
},
}
);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { arbitrumSepolia, sepolia } from "@account-kit/infra";
import { alchemy, arbitrumSepolia, sepolia } from "@account-kit/infra";
import { AlchemySignerStatus } from "@account-kit/signer";
import { createConfig } from "../createConfig.js";
import {
Expand Down Expand Up @@ -99,7 +99,7 @@ describe("watchSmartAccountClient", () => {
const config = createConfig({
chain: sepolia,
chains: [{ chain: sepolia }, { chain: arbitrumSepolia }],
apiKey: "AN_API_KEY",
transport: alchemy({ apiKey: "AN_API_KEY" }),
});

config.store.setState({
Expand Down
Loading
Loading