Skip to content

Commit

Permalink
Merge pull request #7 from Moonsong-Labs/feat/check-if-right-rpc-for-…
Browse files Browse the repository at this point in the history
…address

checking if wallet and rpc are connected to same address
  • Loading branch information
calvogenerico authored Dec 23, 2024
2 parents c815168 + 97e0ec7 commit befd188
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
12 changes: 8 additions & 4 deletions web/components/HiddenContent.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { LockIcon } from "lucide-react";
import { useAccount } from "wagmi";
import { useConnectionStatus } from "~~/hooks/use-connection-status";
import { cn } from "~~/utils/cn";

export default function HiddenContent({ children, className }: { children: React.ReactNode; className?: string }) {
const { isConnected } = useAccount();
const { isConnected, walletAndRpcMatch } = useConnectionStatus();

if (isConnected) {
if (isConnected && walletAndRpcMatch) {
return children;
}

const message = !isConnected
? "Connect your wallet to start"
: "Please ensure that your rpc is the right one for your address";

return (
<div className="relative">
<div className="opacity-50">{children}</div>
<div className={cn("absolute -inset-4 flex items-center justify-center backdrop-blur-sm z-10", className)}>
<div className="rounded-lg bg-black px-6 py-4 text-lg font-medium text-neutral-50 shadow-lg flex items-center gap-x-2">
<LockIcon className="w-5 h-5" />
Connect your wallet to start
{message}
</div>
</div>
</div>
Expand Down
30 changes: 30 additions & 0 deletions web/hooks/use-connection-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect } from "react";
import { useAccount, useReadContract } from "wagmi";
import { DAI_TOKEN, ERC20_ABI } from "~~/contracts/tokens";

export function useConnectionStatus() {
const coso = useAccount();
const { isConnected, address: walletAddress } = coso;

const { isSuccess, refetch } = useReadContract({
address: DAI_TOKEN.address,
abi: ERC20_ABI,
functionName: "balanceOf",
args: [walletAddress ?? ""],
query: {
retry: false,
},
});

useEffect(() => {
const interval = setInterval(() => void refetch().catch(() => null), 3000);
return () => {
clearInterval(interval);
};
}, [refetch]);

return {
isConnected,
walletAndRpcMatch: isSuccess,
};
}

0 comments on commit befd188

Please sign in to comment.