Skip to content

Commit

Permalink
Merge pull request #20 from consolelabs/fix/auto-ask-to-change-network
Browse files Browse the repository at this point in the history
fix: auto ask to change network
  • Loading branch information
leduyhien152 authored Apr 17, 2024
2 parents ca4adc6 + 1338fda commit dbaede5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
16 changes: 12 additions & 4 deletions components/stake/flexible/flexible-stake-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export const FlexibleStakeContent = (props: Props) => {
value: 0,
display: "",
});
const { isConnected, isCorrectNetwork, changeNetwork } = useWalletNetwork({
chain,
});
const { isConnected, isCorrectNetwork, changeNetwork, checkNetwork } =
useWalletNetwork({
chain,
});

const convertedBalance = Number(
formatUnits(balance, stakingToken?.token_decimal)
Expand Down Expand Up @@ -93,7 +94,14 @@ export const FlexibleStakeContent = (props: Props) => {
<DrawerOverlay />
<DrawerContent className="!w-full rounded-xl h-[calc(100%-60px)]">
<div className="flex justify-center items-center h-full">
<LoginWidget raw onchain chain="evm" />
<LoginWidget
raw
onchain
chain="evm"
onWalletConnectSuccess={async () => {
checkNetwork();
}}
/>
</div>
</DrawerContent>
</DrawerPortal>
Expand Down
22 changes: 18 additions & 4 deletions hooks/useWalletNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { tryCatch } from "@/utils/tryCatch";
import { toast } from "@mochi-ui/core";
import { ChainProvider, useLoginWidget } from "@mochi-web3/login-widget";
import { useEffect, useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";

interface Props {
chain?: { id?: number; name?: string; rpc?: string; currency?: string };
Expand All @@ -21,9 +21,8 @@ export const useWalletNetwork = (props: Props) => {
() => ({})
);

const changeNetwork = async () => {
const changeNetwork = useCallback(async () => {
if (!provider) return;
console.log("sdgdfg");
try {
await provider.request({
method: "wallet_switchEthereumChain",
Expand All @@ -37,7 +36,7 @@ export const useWalletNetwork = (props: Props) => {
// This error code indicates that the chain has not been added to MetaMask.
if (switchError.code === 4902) {
try {
await provider?.provider.request({
await provider.request({
method: "wallet_addEthereumChain",
params: [
{
Expand Down Expand Up @@ -73,8 +72,22 @@ export const useWalletNetwork = (props: Props) => {
});
}
}
}, [chain?.currency, chain?.id, chain?.name, chain?.rpc, provider]);

const [shouldChangeNetwork, setShouldChangeNetwork] = useState(false);
const checkNetwork = () => {
if (!isCorrectNetwork) {
setShouldChangeNetwork(true);
}
};

useEffect(() => {
if (shouldChangeNetwork && !!provider) {
changeNetwork();
setShouldChangeNetwork(false);
}
}, [changeNetwork, provider, shouldChangeNetwork]);

useEffect(() => {
setIsCorrectNetwork(chainId === `0x${chain?.id?.toString(16)}`);
}, [chain?.id, chainId]);
Expand All @@ -83,5 +96,6 @@ export const useWalletNetwork = (props: Props) => {
isConnected: !!connectedAddress,
isCorrectNetwork,
changeNetwork,
checkNetwork,
};
};

0 comments on commit dbaede5

Please sign in to comment.