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

Use wagmi context for Balances and UniversalKitProvider components #14

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions src/components/Balances/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { roundNumber } from "@/lib/utils";
import { useBitcoinWallet } from "@/index";
import { useAccount, useWalletClient } from "wagmi";
import { useEthersSigner } from "@/hooks/useEthersSigner";
import { useZetaChainClient } from "@/providers/UniversalKitProvider";
import { useZetaChainClient, wagmiContextValue } from "@/providers/UniversalKitProvider";

interface Token {
id: string;
Expand All @@ -22,15 +22,18 @@ interface Token {
interface BalancesProps {
config?: any;
balances?: any;
wagmiContextValue?: wagmiContextValue;
onBalanceClick?: (balance: Token) => void;
}

export const Balances = ({
config,
balances: balancesProp,
wagmiContextValue,
onBalanceClick = () => {},
}: BalancesProps) => {
const { address, status } = useAccount();
const useWagmiAccount = wagmiContextValue?.useAccount || useAccount;
const { address, status } = useWagmiAccount();

const { address: bitcoin } = useBitcoinWallet();
const client = useZetaChainClient();
Expand Down
55 changes: 28 additions & 27 deletions src/providers/UniversalKitProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,32 @@ const ZetaChainClientContext = createContext<any>(null);

export const useZetaChainClient = () => useContext(ZetaChainClientContext);

export interface wagmiContextValue {
useAccount: typeof useAccount;
useChainId: typeof useChainId;
useWalletClient: typeof useWalletClient;
}

interface ZetaChainClientProviderProps {
children: ReactNode;
zetaChainConfig?: any;
wagmiContextValue?: wagmiContextValue;
}

const ZetaChainClientProvider = ({
children,
zetaChainConfig,
}: {
children: ReactNode;
zetaChainConfig?: any;
}) => {
const { status } = useAccount();
const chainId = useChainId();
const { data: walletClient } = useWalletClient({ chainId });
wagmiContextValue
}: ZetaChainClientProviderProps ) => {
const useWagmiAccount = wagmiContextValue?.useAccount || useAccount;
const useWagmiChainId = wagmiContextValue?.useChainId || useChainId;
const useWagmiWalletClient = wagmiContextValue?.useWalletClient || useWalletClient;

const { status } = useWagmiAccount();
const chainId = useWagmiChainId();
const { data: walletClient } = useWagmiWalletClient({ chainId });
const signer = useEthersSigner({ walletClient });

const [zetaChainClient, setZetaChainClient] = useState<any>(null);

useEffect(() => {
Expand Down Expand Up @@ -57,26 +72,12 @@ const ZetaChainClientProvider = ({
);
};

export const UniversalKitProvider = ({
children,
config,
client,
zetaChainConfig,
}: {
children: ReactNode;
config?: any;
client: any;
zetaChainConfig?: any;
}) => {
export const UniversalKitProvider = ({ children, zetaChainConfig, wagmiContextValue }: ZetaChainClientProviderProps) => {
return (
<WagmiProvider config={config || { autoConnect: true }}>
<QueryClientProvider client={client}>
<BitcoinWalletProvider>
<ZetaChainClientProvider zetaChainConfig={zetaChainConfig}>
{children}
</ZetaChainClientProvider>
</BitcoinWalletProvider>
</QueryClientProvider>
</WagmiProvider>
<BitcoinWalletProvider>
<ZetaChainClientProvider zetaChainConfig={zetaChainConfig} wagmiContextValue={wagmiContextValue}>
{children}
</ZetaChainClientProvider>
</BitcoinWalletProvider>
);
};
Loading