Skip to content

Commit

Permalink
refactor: combine core and client stores
Browse files Browse the repository at this point in the history
  • Loading branch information
dphilipson committed Jun 24, 2024
1 parent 2795e69 commit aab1656
Show file tree
Hide file tree
Showing 25 changed files with 173 additions and 252 deletions.
12 changes: 6 additions & 6 deletions account-kit/core/src/actions/createAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export async function createAccount<TAccount extends SupportedAccountTypes>(
{ type, accountParams: params }: CreateAccountParams<TAccount>,
config: AlchemyAccountsConfig
): Promise<SupportedAccounts> {
const clientStore = config.clientStore;
const accounts = clientStore.getState().accounts;
const store = config.store;
const accounts = store.getState().accounts;
if (!accounts) {
throw new ClientOnlyPropertyError("account");
}
Expand All @@ -80,7 +80,7 @@ export async function createAccount<TAccount extends SupportedAccountTypes>(
if (cachedAccount.status !== "RECONNECTING" && cachedAccount.account) {
return cachedAccount.account;
}
const cachedConfig = clientStore.getState().accountConfigs[chain.id]?.[type];
const cachedConfig = store.getState().accountConfigs[chain.id]?.[type];

const accountPromise = (() => {
switch (type) {
Expand All @@ -106,7 +106,7 @@ export async function createAccount<TAccount extends SupportedAccountTypes>(
})();

if (cachedAccount.status !== "RECONNECTING") {
clientStore.setState(() => ({
store.setState(() => ({
accounts: {
...accounts,
[chain.id]: {
Expand All @@ -123,7 +123,7 @@ export async function createAccount<TAccount extends SupportedAccountTypes>(
try {
const account = await accountPromise;
const initCode = await account.getInitCode();
clientStore.setState((state) => ({
store.setState((state) => ({
accounts: {
...accounts,
[chain.id]: {
Expand All @@ -147,7 +147,7 @@ export async function createAccount<TAccount extends SupportedAccountTypes>(
},
}));
} catch (error) {
clientStore.setState(() => ({
store.setState(() => ({
accounts: {
...accounts,
[chain.id]: {
Expand Down
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/getAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const getAccount = <TAccount extends SupportedAccountTypes>(
{ type }: GetAccountParams<TAccount>,
config: AlchemyAccountsConfig
): GetAccountResult<TAccount> => {
const accounts = config.clientStore.getState().accounts;
const accounts = config.store.getState().accounts;
const chain = getChain(config);
const account = accounts?.[chain.id]?.[type];
if (!account) {
Expand Down
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/getBundlerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ import type { AlchemyAccountsConfig } from "../types";
export const getBundlerClient = (
config: AlchemyAccountsConfig
): ClientWithAlchemyMethods => {
return config.coreStore.getState().bundlerClient;
return config.store.getState().bundlerClient;
};
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/getChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import type { AlchemyAccountsConfig } from "../types";
* @returns the currently active chain
*/
export function getChain(config: AlchemyAccountsConfig): Chain {
return config.coreStore.getState().chain;
return config.store.getState().chain;
}
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/getConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getChain } from "./getChain.js";
*/
export function getConnection(config: AlchemyAccountsConfig): Connection {
const chain = getChain(config);
const connection = config.coreStore.getState().connections.get(chain.id);
const connection = config.store.getState().connections.get(chain.id);
if (!connection) {
throw new ChainNotFoundError(chain);
}
Expand Down
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/getSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ import type { hydrate } from "../hydrate.js";
export const getSigner = (
config: AlchemyAccountsConfig
): AlchemyWebSigner | null => {
return config.clientStore.getState().signer ?? null;
return config.store.getState().signer ?? null;
};
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/getSignerStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ import type { AlchemyAccountsConfig } from "../types";
* @returns {SignerStatus} The current signer status from the client store
*/
export const getSignerStatus = (config: AlchemyAccountsConfig) => {
return config.clientStore.getState().signerStatus;
return config.store.getState().signerStatus;
};
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { AlchemyAccountsConfig } from "../types";
export const getUser = (
config: AlchemyAccountsConfig
): (User & { type: "eoa" | "sca" }) | null => {
const user = config.clientStore.getState().user;
const user = config.store.getState().user;
if (user == null) return user ?? null;

// @ts-ignore
Expand Down
12 changes: 6 additions & 6 deletions account-kit/core/src/actions/reconnect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSigner } from "../store/client.js";
import { createSigner } from "../store/store.js";
import type { AlchemyAccountsConfig } from "../types.js";
import { createAccount } from "./createAccount.js";
import { getChain } from "./getChain.js";
Expand All @@ -10,13 +10,13 @@ import { getChain } from "./getChain.js";
* @param config the account config which contains the client store
*/
export async function reconnect(config: AlchemyAccountsConfig) {
const { clientStore } = config;
const signerConfig = clientStore.getState().config;
const accountConfigs = clientStore.getState().accountConfigs;
const { store } = config;
const signerConfig = store.getState().config;
const accountConfigs = store.getState().accountConfigs;

const signer = createSigner(signerConfig);
const chain = getChain(config);
clientStore.setState({
store.setState({
signer,
});

Expand Down Expand Up @@ -45,7 +45,7 @@ export async function reconnect(config: AlchemyAccountsConfig) {
});

const unsubDisconnected = signer.on("disconnected", () => {
clientStore.setState({
store.setState({
accountConfigs: {},
});
unsubDisconnected();
Expand Down
4 changes: 2 additions & 2 deletions account-kit/core/src/actions/setChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import type { AlchemyAccountsConfig } from "../types.js";
* @param chain the chain to change to. It must be present in the connections config object
*/
export async function setChain(config: AlchemyAccountsConfig, chain: Chain) {
const connection = config.coreStore.getState().connections.get(chain.id);
const connection = config.store.getState().connections.get(chain.id);
if (connection == null) {
throw new ChainNotFoundError(chain);
}

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

config.coreStore.setState(() => ({
config.store.setState(() => ({
chain,
bundlerClient: createAlchemyPublicRpcClient({
chain,
Expand Down
4 changes: 2 additions & 2 deletions account-kit/core/src/actions/watchAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ export const watchAccount =
config: AlchemyAccountsConfig
) =>
(onChange: (account: GetAccountResult<TAccount>) => void) => {
const accounts = config.clientStore.getState().accounts;
const accounts = config.store.getState().accounts;
if (!accounts) {
throw new ClientOnlyPropertyError("account");
}

const chain = getChain(config);
return config.clientStore.subscribe(
return config.store.subscribe(
// this should be available on the client now because of the check above
({ accounts }) => accounts![chain.id][type],
onChange,
Expand Down
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/watchBundlerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { AlchemyAccountsConfig } from "../types";
export const watchBundlerClient =
(config: AlchemyAccountsConfig) =>
(onChange: (bundlerClient: ClientWithAlchemyMethods) => void) => {
return config.coreStore.subscribe(
return config.store.subscribe(
({ bundlerClient }) => bundlerClient,
onChange
);
Expand Down
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/watchChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ import type { AlchemyAccountsConfig } from "../types";
*/
export function watchChain(config: AlchemyAccountsConfig) {
return (onChange: (chain: Chain) => void) => {
return config.coreStore.subscribe(({ chain }) => chain, onChange);
return config.store.subscribe(({ chain }) => chain, onChange);
};
}
6 changes: 2 additions & 4 deletions account-kit/core/src/actions/watchConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import type { AlchemyAccountsConfig, Connection } from "../types";
*/
export function watchConnection(config: AlchemyAccountsConfig) {
return (onChange: (connection: Connection) => void) => {
return config.coreStore.subscribe(
return config.store.subscribe(
({ chain }) => chain,
(chain) => {
const connection = config.coreStore
.getState()
.connections.get(chain.id);
const connection = config.store.getState().connections.get(chain.id);

if (connection) {
onChange(connection);
Expand Down
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/watchSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import type { AlchemyAccountsConfig } from "../types";
export const watchSigner =
(config: AlchemyAccountsConfig) =>
(onChange: (signer?: AlchemyWebSigner) => void) => {
return config.clientStore.subscribe(({ signer }) => signer, onChange);
return config.store.subscribe(({ signer }) => signer, onChange);
};
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/watchSignerStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { AlchemyAccountsConfig } from "../types.js";
export const watchSignerStatus =
(config: AlchemyAccountsConfig) =>
(onChange: (status: SignerStatus) => void) => {
return config.clientStore.subscribe(
return config.store.subscribe(
({ signerStatus }) => signerStatus,
onChange,
{ equalityFn: (a, b) => a.status === b.status }
Expand Down
2 changes: 1 addition & 1 deletion account-kit/core/src/actions/watchUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { AlchemyAccountsConfig } from "../types";
*/
export const watchUser =
(config: AlchemyAccountsConfig) => (onChange: (user?: User) => void) => {
return config.clientStore.subscribe(({ user }) => user, onChange, {
return config.store.subscribe(({ user }) => user, onChange, {
equalityFn: (a, b) => a?.userId === b?.userId,
});
};
17 changes: 4 additions & 13 deletions account-kit/core/src/createConfig.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { DEFAULT_SESSION_MS } from "@account-kit/signer";
import { ConnectionConfigSchema } from "@aa-sdk/core";
import { createStorage, createConfig as createWagmiConfig } from "@wagmi/core";
import { createClientStore } from "./store/client.js";
import { createCoreStore } from "./store/core.js";
import { DEFAULT_STORAGE_KEY } from "./store/types.js";
import type {
AlchemyAccountsConfig,
Connection,
CreateConfigProps,
} from "./types.js";
import { createAccountKitStore } from "./store/store.js";

export const DEFAULT_IFRAME_CONTAINER_ID = "alchemy-signer-iframe-container";

Expand Down Expand Up @@ -53,14 +52,9 @@ export const createConfig = (
});
}

const coreStore = createCoreStore({
const store = createAccountKitStore({
connections,
chain,
storage: storage?.(),
ssr,
});

const clientStore = createClientStore({
client: {
connection: signerConnection ?? connections[0],
iframeConfig,
Expand All @@ -73,15 +67,13 @@ export const createConfig = (
? { sessionLength: sessionConfig.expirationTimeMs }
: undefined
),
// TODO: this is duplicated from the core store
chains: connections.map((x) => x.chain),
ssr,
});

const wagmiConfig = createWagmiConfig({
connectors,
chains: [chain, ...connections.map((c) => c.chain)],
client: () => config.coreStore.getState().bundlerClient,
client: () => config.store.getState().bundlerClient,
storage: createStorage({
key: `${DEFAULT_STORAGE_KEY}:wagmi`,
storage: storage
Expand All @@ -94,8 +86,7 @@ export const createConfig = (
});

const config: AlchemyAccountsConfig = {
coreStore,
clientStore,
store: store,
_internal: {
ssr,
wagmiConfig,
Expand Down
23 changes: 11 additions & 12 deletions account-kit/core/src/hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { AlchemySignerStatus } from "@account-kit/signer";
import type { Address } from "@aa-sdk/core";
import { hydrate as wagmi_hydrate } from "@wagmi/core";
import { reconnect } from "./actions/reconnect.js";
import type { AccountState, StoreState, StoredState } from "./store/types.js";
import type { AlchemyAccountsConfig, SupportedAccountTypes } from "./types.js";
import {
convertSignerStatusToState,
createDefaultAccountState,
defaultAccountState,
} from "./store/client.js";
import type { AccountState, ClientState, StoredState } from "./store/types.js";
import type { AlchemyAccountsConfig, SupportedAccountTypes } from "./types.js";
} from "./store/store.js";

/**
* Will hydrate the client store with the provided initial state if one is provided.
Expand All @@ -26,12 +26,12 @@ export function hydrate(
? initialState.alchemy
: initialState;

if (initialAlchemyState && !config.clientStore.persist.hasHydrated()) {
if (initialAlchemyState && !config.store.persist.hasHydrated()) {
const { accountConfigs, signerStatus, ...rest } = initialAlchemyState;
const shouldReconnectAccounts =
signerStatus.isConnected || signerStatus.isAuthenticating;

config.clientStore.setState({
config.store.setState({
...rest,
accountConfigs,
signerStatus: convertSignerStatusToState(
Expand Down Expand Up @@ -60,8 +60,7 @@ export function hydrate(
return {
async onMount() {
if (config._internal.ssr) {
await config.clientStore.persist.rehydrate();
await config.coreStore.persist.rehydrate();
await config.store.persist.rehydrate();
}

await wagmi_onMount();
Expand All @@ -81,13 +80,13 @@ const reconnectingState = <T extends SupportedAccountTypes>(
});

const hydrateAccountState = (
accountConfigs: ClientState["accountConfigs"],
accountConfigs: StoreState["accountConfigs"],
shouldReconnectAccounts: boolean,
config: AlchemyAccountsConfig
): ClientState["accounts"] => {
const chains = Array.from(
config.coreStore.getState().connections.entries()
).map(([, cnx]) => cnx.chain);
): StoreState["accounts"] => {
const chains = Array.from(config.store.getState().connections.entries()).map(
([, cnx]) => cnx.chain
);
const initialState = createDefaultAccountState(chains);

return Object.entries(accountConfigs).reduce((acc, [chainKey, config]) => {
Expand Down
2 changes: 1 addition & 1 deletion account-kit/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export type * from "./createConfig.js";
export { DEFAULT_IFRAME_CONTAINER_ID, createConfig } from "./createConfig.js";
export { ClientOnlyPropertyError } from "./errors.js";
export { hydrate } from "./hydrate.js";
export { defaultAccountState } from "./store/client.js";
export { defaultAccountState } from "./store/store.js";
export type { SignerStatus } from "./store/types.js";
export type * from "./types.js";
export { cookieStorage, cookieToInitialState } from "./utils/cookies.js";
Expand Down
Loading

0 comments on commit aab1656

Please sign in to comment.