My disconnect button calls deactivateWeb3
.
Here is the code I use to connect (most of it is from the moralis-react-client) with some adjustments to remove the moralis authentication:
Authenticate:
export const AuthenticateModal = ({ isOpen, onClose }: AuthenticateModalProps) => {
const { authenticate, enableWeb3, isWeb3EnableLoading, isWeb3Enabled, isInitialized } = useMoralis();
const {switchNetwork} = useChain()
const [authError, setAuthError] = useState<null | Error>(null);
const [isAuthenticating, setIsAuthenticating] = useState(false);
const handleAuth = async (provider: 'metamask' | 'walletconnect') => {
setAuthError(null);
setIsAuthenticating(true);
await enableWeb3({ throwOnError: true, provider, chainId: 137 });
if (provider == 'metamask') {
switchNetwork("0x89")
}
const { account, chainId } = Moralis;
if (!account) {
throw new Error('Connecting to chain failed, as no connected account was found');
}
if (!chainId) {
throw new Error('Connecting to chain failed, as no connected chain was found');
}
onClose();
setIsAuthenticating(false);
};
Authenticate Button:
export const AuthenticateButton = ({ onClick }: AuthenticateButtonProps) => {
const { chainId, account, isWeb3Enabled, deactivateWeb3, enableWeb3, logout } = useMoralis();
const connectString = useMemo(() => {
let output = "";
if (account) {
output += `${account.substring(0, 5)}...${account.substring(
account.length - 3
)}`;
}
return output;
}, [account, chainId]);
return (
<button
className="py-3 md:flex hover:bg-themeDarkYellow text-white text-center justify-center rounded-full min-w-[18rem] md:min-w-[10rem] px-6 bg-themeYellow"
onClick={!isWeb3Enabled ? onClick : deactivateWeb3}
>
{isWeb3Enabled && account ? connectString + "(connected)!" : "Connect"}
</button>
);
};
Also, this is an example of a function in my components:
const createClone = async function () {
const ethers = Moralis.web3Library;
const web3Provider = await Moralis.enableWeb3({provider: 'walletConnect' || 'metamask'});
const signer = web3Provider.getSigner();
const contract = new ethers.Contract(address, wfABI, signer);
const transaction = await contract.createClone({
value: Moralis.Units.ETH("10"),
gasLimit: 6000000,
});
await transaction.wait(3);
await handleSuccess(transaction);
};