Skip to content

Commit

Permalink
fix(core): initial state parsing and disconnect logic (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
moldy530 committed Jul 30, 2024
1 parent 1a4b799 commit 1f1047b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
7 changes: 7 additions & 0 deletions account-kit/core/src/actions/disconnect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { AlchemySignerStatus } from "@account-kit/signer";
import { disconnect as wagmi_disconnect } from "@wagmi/core";
import { convertSignerStatusToState } from "../store/store.js";
import type { AlchemyAccountsConfig } from "../types";
import { getSigner } from "./getSigner.js";

Expand All @@ -25,5 +27,10 @@ export async function disconnect(config: AlchemyAccountsConfig): Promise<void> {
await wagmi_disconnect(config._internal.wagmiConfig);
await signer?.disconnect();
config.store.setState(() => config.store.getInitialState());

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

config.store.persist.clearStorage();
}
7 changes: 4 additions & 3 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/store.js";
import type { AccountState, StoreState, StoredState } from "./store/types.js";
import type { AlchemyAccountsConfig, SupportedAccountTypes } from "./types.js";

export type HydrateResult = {
onMount: () => Promise<void>;
Expand Down Expand Up @@ -41,7 +41,8 @@ export function hydrate(
: initialState;

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

Expand Down
4 changes: 2 additions & 2 deletions account-kit/core/src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const createAccountKitStore = (
},
reviver: (key, value) => {
if (key === "bundlerClient") {
const connection = value as Connection;
const { connection } = value as { connection: Connection };
return createAlchemyPublicRpcClient({
chain: connection.chain,
connectionConfig: connection,
Expand All @@ -65,7 +65,7 @@ export const createAccountKitStore = (
skipHydration: ssr,
partialize: ({ signer, accounts, ...writeableState }) =>
writeableState,
version: 2,
version: 3,
})
: () => createInitialStoreState(params)
)
Expand Down
7 changes: 4 additions & 3 deletions account-kit/core/src/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export type SignerStatus = {
};

export type StoredState = {
alchemy: Omit<StoreState, "signer" | "accounts">;
alchemy: Omit<StoreState, "signer" | "accounts" | "bundlerClient"> & {
bundlerClient: { connection: Connection };
};
wagmi?: WagmiState;
};

Expand All @@ -72,7 +74,6 @@ export type CreateAccountKitStoreParams = ClientStoreConfig & {

export type StoreState = {
// non-serializable
// getting this state should throw an error if not on the client
signer?: AlchemyWebSigner;
accounts?: {
[chain: number]: {
Expand All @@ -82,6 +83,7 @@ export type StoreState = {
// serializable state
// NOTE: in some cases this can be serialized to cookie storage
// be mindful of how big this gets. cookie limit 4KB
bundlerClient: ClientWithAlchemyMethods;
config: ClientStoreConfig;
accountConfigs: {
[chain: number]: Partial<{
Expand All @@ -90,7 +92,6 @@ export type StoreState = {
};
user?: User;
signerStatus: SignerStatus;
bundlerClient: ClientWithAlchemyMethods;
chain: Chain;
connections: Map<number, Connection>;
};
Expand Down
4 changes: 2 additions & 2 deletions account-kit/core/src/utils/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DEFAULT_SESSION_MS } from "@account-kit/signer";
import { cookieToInitialState as wagmiCookieToInitialState } from "@wagmi/core";
import Cookies from "js-cookie";
import type { StoreState, StoredState } from "../store/types.js";
import type { StoredState } from "../store/types.js";
import type { AlchemyAccountsConfig } from "../types.js";
import { deserialize } from "./deserialize.js";

Expand Down Expand Up @@ -69,7 +69,7 @@ export function cookieToInitialState(
if (!state) return;

const alchemyClientState = deserialize<{
state: Omit<StoreState, "signer" | "accounts">;
state: StoredState["alchemy"];
}>(state).state;

const wagmiClientState = wagmiCookieToInitialState(
Expand Down

0 comments on commit 1f1047b

Please sign in to comment.