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

refactor: combine core and client stores #775

Merged
merged 2 commits into from
Jul 17, 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
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 @@ -59,8 +59,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 @@ -79,7 +79,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 Down Expand Up @@ -108,7 +108,7 @@ export async function createAccount<TAccount extends SupportedAccountTypes>(
})();

if (cachedAccount.status !== "RECONNECTING") {
clientStore.setState(() => ({
store.setState(() => ({
accounts: {
...accounts,
[chain.id]: {
Expand All @@ -125,7 +125,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 @@ -149,7 +149,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 {Chain} 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 @@ -18,7 +18,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 @@ -20,5 +20,5 @@ import type { AlchemyAccountsConfig } from "../types.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;
};
6 changes: 4 additions & 2 deletions account-kit/core/src/actions/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export type GetUserResult = (User & { type: "eoa" | "sca" }) | null;
* @param {AlchemyAccountsConfig} config the account config containing app state
* @returns {GetUserResult} the user if the signer or an EOA are connected
*/
export const getUser = (config: AlchemyAccountsConfig): GetUserResult => {
const user = config.clientStore.getState().user;
export const getUser = (
config: AlchemyAccountsConfig
): (User & { type: "eoa" | "sca" }) | null => {
const user = config.store.getState().user;
if (user == null) return user ?? null;

// @ts-ignore
Expand Down
16 changes: 8 additions & 8 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 @@ -18,13 +18,13 @@ import { getChain } from "./getChain.js";
* @param {AlchemyAccountsConfig} 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 = clientStore.getState().signer ?? createSigner(signerConfig);
if (!clientStore.getState().signer) {
clientStore.setState({
const signer = store.getState().signer ?? createSigner(signerConfig);
if (!store.getState().signer) {
store.setState({
signer,
});
}
Expand Down Expand Up @@ -56,7 +56,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 @@ -21,14 +21,14 @@ import type { AlchemyAccountsConfig } from "../types.js";
* @param {Chain} 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 { ConnectionConfigSchema } from "@aa-sdk/core";
import { DEFAULT_SESSION_MS } from "@account-kit/signer";
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 @@ -64,14 +63,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 @@ -84,15 +78,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 @@ -105,8 +97,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 type { Address } from "@aa-sdk/core";
import { AlchemySignerStatus } from "@account-kit/signer";
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";

export type HydrateResult = {
onMount: () => Promise<void>;
Expand Down Expand Up @@ -40,12 +40,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 @@ -74,8 +74,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 @@ -95,13 +94,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
Loading