Skip to content

Commit

Permalink
refactor: use alchemy transport in createConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
moldy530 committed Oct 1, 2024
1 parent d3c59fb commit 4afe428
Show file tree
Hide file tree
Showing 25 changed files with 431 additions and 175 deletions.
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/*
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();
});
10 changes: 8 additions & 2 deletions account-kit/core/src/actions/getAlchemyTransport.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type { AlchemyTransport } from "@account-kit/infra";
import { alchemy, type AlchemyTransport } from "@account-kit/infra";
import { ChainNotFoundError } from "../errors.js";
import type { AlchemyAccountsConfig } from "../types";

export function getAlchemyTransport(
config: AlchemyAccountsConfig
): AlchemyTransport {
return config.store.getState().transport;
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;
};
5 changes: 2 additions & 3 deletions account-kit/core/src/actions/setChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ export async function setChain(config: AlchemyAccountsConfig, chain: Chain) {
}

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

config.store.setState(() => ({
chain,
transport,
bundlerClient: createAlchemyPublicRpcClient({
transport,
chain,
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;
},
}
);
};
6 changes: 2 additions & 4 deletions account-kit/core/src/actions/watchSmartAccountClient.test.ts
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 All @@ -10,8 +10,6 @@ import { setChain } from "./setChain.js";
import { watchSmartAccountClient } from "./watchSmartAccountClient.js";

describe("watchSmartAccountClient", () => {
beforeEach(() => localStorage.clear());

it("should fire the on subscribe callback if signer status changes", () => {
const config = givenConfig();
const onChange = vi.fn();
Expand Down Expand Up @@ -101,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
6 changes: 3 additions & 3 deletions account-kit/core/src/actions/watchSmartAccountClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ import {
}

return config.store.subscribe(
({ signerStatus, accounts, bundlerClient, chain }) => ({
({ signerStatus, accounts, chain }) => ({
signerStatus,
account: accounts![chain.id][params.type],
bundlerClient,
chain,
}),
() => {
onChange(getSmartAccountClient(params, config));
Expand All @@ -61,7 +61,7 @@ import {
return (
a.signerStatus === b.signerStatus &&
a.account === b.account &&
a.bundlerClient === b.bundlerClient
a.chain === b.chain
);
},
}
Expand Down
38 changes: 17 additions & 21 deletions account-kit/core/src/createConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConnectionConfigSchema } from "@aa-sdk/core";
import { DEFAULT_SESSION_MS } from "@account-kit/signer";
import { createStorage, createConfig as createWagmiConfig } from "@wagmi/core";
import { getBundlerClient } from "./actions/getBundlerClient.js";
import { createAccountKitStore } from "./store/store.js";
import { DEFAULT_STORAGE_KEY } from "./store/types.js";
import type {
Expand All @@ -25,7 +25,7 @@ export const DEFAULT_IFRAME_CONTAINER_ID = "alchemy-signer-iframe-container";
*
* const config = createConfig({
* chain: sepolia,
* apiKey: "your-api-key",
* transport: alchemy({ apiKey: "your-api-key" }),
* });
* ```
*
Expand All @@ -51,35 +51,31 @@ export const createConfig = (
} = params;

const connections: Connection[] = [];
if (connectionConfig.connections != null) {
connectionConfig.connections.forEach(({ chain, ...config }) => {
connections.push({
...ConnectionConfigSchema.parse(config),
policyId: config.policyId,
chain,
});
if (connectionConfig.chains == null) {
connections.push({
transport: connectionConfig.transport.config,
policyId: connectionConfig.policyId,
chain,
});
} else if (connectionConfig.chains != null) {
connectionConfig.chains.forEach(({ chain, ...config }) => {
} else {
connectionConfig.chains.forEach(({ chain, policyId, transport }) => {
connections.push({
apiKey: connectionConfig.apiKey,
policyId: config.policyId,
transport: transport?.config ?? connectionConfig.transport!.config,
chain,
policyId,
});
});
} else {
connections.push({
...ConnectionConfigSchema.parse(connectionConfig),
policyId: connectionConfig.policyId,
chain,
});
}

const defaultConnection = connections[0].transport;
const store = createAccountKitStore({
connections,
chain,
client: {
connection: signerConnection ?? connections[0],
connection:
signerConnection ??
defaultConnection.alchemyConnection ??
defaultConnection,
iframeConfig,
rootOrgId,
rpId,
Expand All @@ -98,7 +94,7 @@ export const createConfig = (
const wagmiConfig = createWagmiConfig({
connectors,
chains: [chain, ...connections.map((c) => c.chain)],
client: () => config.store.getState().bundlerClient,
client: () => getBundlerClient(config),
storage: createStorage({
key: `${DEFAULT_STORAGE_KEY}:wagmi`,
storage: storage
Expand Down
Loading

0 comments on commit 4afe428

Please sign in to comment.