From 9708e77b1add81928341fbb59b3893c43a447157 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Mon, 29 Apr 2024 10:40:21 +0100 Subject: [PATCH] expose user account client, address, chain ID --- packages/account-kit/src/bundle/SyncStore.tsx | 18 ++++++++++- packages/account-kit/src/bundle/store.ts | 31 +++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/account-kit/src/bundle/SyncStore.tsx b/packages/account-kit/src/bundle/SyncStore.tsx index 28e560e662..04ecf38f21 100644 --- a/packages/account-kit/src/bundle/SyncStore.tsx +++ b/packages/account-kit/src/bundle/SyncStore.tsx @@ -2,6 +2,7 @@ import { useAccountModal } from "../useAccountModal"; import { usePreparedAppAccountClient } from "../usePreparedAppAccountClient"; import { useEffect } from "react"; import { Store } from "./store"; +import { useAccount, useConnectorClient } from "wagmi"; export type Props = { store: Store; @@ -10,6 +11,8 @@ export type Props = { export function SyncStore({ store }: Props) { const { accountModalOpen, openAccountModal, closeAccountModal, toggleAccountModal } = useAccountModal(); const appAccountClient = usePreparedAppAccountClient(); + const { data: userAccountClient } = useConnectorClient(); + const { address: userAddress, chainId: userChainId } = useAccount(); useEffect(() => { store.setState({ @@ -18,8 +21,21 @@ export function SyncStore({ store }: Props) { closeAccountModal, toggleAccountModal, appAccountClient, + userAccountClient, + userAddress, + userChainId, }); - }, [store, accountModalOpen, appAccountClient, closeAccountModal, openAccountModal, toggleAccountModal]); + }, [ + store, + accountModalOpen, + appAccountClient, + closeAccountModal, + openAccountModal, + toggleAccountModal, + userAccountClient, + userAddress, + userChainId, + ]); return <>; } diff --git a/packages/account-kit/src/bundle/store.ts b/packages/account-kit/src/bundle/store.ts index 6b14e7d879..41ee12c630 100644 --- a/packages/account-kit/src/bundle/store.ts +++ b/packages/account-kit/src/bundle/store.ts @@ -1,13 +1,37 @@ import { StoreApi, createStore } from "zustand/vanilla"; import { AppAccountClient } from "../common"; +import { Account, Chain, Client, Hex, Transport } from "viem"; export type State = { accountModalOpen: undefined | boolean; openAccountModal: undefined | (() => void); closeAccountModal: undefined | (() => void); toggleAccountModal: undefined | ((open: boolean) => void); - // TODO: downcast this to a plain wallet client to support non-smart account use cases later - appAccountClient: AppAccountClient | undefined; + /** + * Viem client bound to the app account (app signer or smart account) and app chain. + * It is extended with all public and wallet actions and includes some MUD's custom actions for ease of use with account delegation and smart accounts. + * This will only be set when all Account Kit requirements are met (has signing key, account delegation, gas balance). + */ + appAccountClient: undefined | AppAccountClient; + /** + * Viem connector client. + * It does not include any actions, so you'll need to use it with standalone/tree-shakable actions actions or extend it yourself. + * + * Note that this may be briefly `undefined` when a user switches accounts or chains in their wallet. + * It's recommended to use `userAddress` or `userChainId` if you need a stable representation of these values. + * + * Also note that this client may be connected to a different chain than the app chain. + * It's recommended that you check `userChainId` before each write call and `switchChain` as necessary. + */ + userAccountClient: undefined | Client; + /** + * Address of the currently connected user. + */ + userAddress: undefined | Hex; + /** + * Chain ID of the currently connected user. + */ + userChainId: undefined | number; }; export type Store = StoreApi; @@ -18,4 +42,7 @@ export const store = createStore(() => ({ closeAccountModal: undefined, toggleAccountModal: undefined, appAccountClient: undefined, + userAccountClient: undefined, + userAddress: undefined, + userChainId: undefined, }));