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

Fix/auto connect #412

Merged
merged 4 commits into from
Jul 7, 2023
Merged
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
5 changes: 4 additions & 1 deletion packages/nextjs/components/scaffold-eth/Faucet.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from "react";
import { useIsMounted } from "usehooks-ts";
import { Address as AddressType, createWalletClient, http, parseEther } from "viem";
import { useNetwork } from "wagmi";
import { hardhat } from "wagmi/chains";
Expand All @@ -20,6 +21,8 @@ export const Faucet = () => {
const [sendValue, setSendValue] = useState("");

const { chain: ConnectedChain } = useNetwork();
const isMounted = useIsMounted();

const localWalletClient = createWalletClient({
chain: hardhat,
transport: http(),
Expand Down Expand Up @@ -74,7 +77,7 @@ export const Faucet = () => {
};

// Render only on local chain
if (!ConnectedChain || ConnectedChain.id !== hardhat.id) {
if (ConnectedChain?.id !== hardhat.id || !isMounted()) {
sverps marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs/components/scaffold-eth/FaucetButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState } from "react";
import { useIsMounted } from "usehooks-ts";
import { createWalletClient, http, parseEther } from "viem";
import { useAccount, useNetwork } from "wagmi";
import { hardhat } from "wagmi/chains";
Expand All @@ -15,7 +16,10 @@ const FAUCET_ADDRESS = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";
export const FaucetButton = () => {
const { address } = useAccount();
const { balance } = useAccountBalance(address);

const { chain: ConnectedChain } = useNetwork();
const isMounted = useIsMounted();

const [loading, setLoading] = useState(false);
const localWalletClient = createWalletClient({
chain: hardhat,
Expand All @@ -40,7 +44,7 @@ export const FaucetButton = () => {
};

// Render only on local chain
if (!ConnectedChain || ConnectedChain.id !== hardhat.id) {
if (ConnectedChain?.id !== hardhat.id || !isMounted()) {
sverps marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ import { ConnectButton } from "@rainbow-me/rainbowkit";
import { useDisconnect, useSwitchNetwork } from "wagmi";
import { ArrowLeftOnRectangleIcon, ArrowsRightLeftIcon, ChevronDownIcon } from "@heroicons/react/24/solid";
import { Balance, BlockieAvatar } from "~~/components/scaffold-eth";
import { useAutoConnect, useNetworkColor } from "~~/hooks/scaffold-eth";
import { useNetworkColor } from "~~/hooks/scaffold-eth";
import { getTargetNetwork } from "~~/utils/scaffold-eth";

/**
* Custom Wagmi Connect Button (watch balance + custom design)
*/
export const RainbowKitCustomConnectButton = () => {
useAutoConnect();

const networkColor = useNetworkColor();
const configuredNetwork = getTargetNetwork();
const { disconnect } = useDisconnect();
Expand Down
1 change: 0 additions & 1 deletion packages/nextjs/hooks/scaffold-eth/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from "./useAccountBalance";
export * from "./useAnimationConfig";
export * from "./useAutoConnect";
export * from "./useBurnerWallet";
export * from "./useDeployedContractInfo";
export * from "./useNativeCurrencyPrice";
Expand Down
73 changes: 0 additions & 73 deletions packages/nextjs/hooks/scaffold-eth/useAutoConnect.ts

This file was deleted.

14 changes: 3 additions & 11 deletions packages/nextjs/scaffold.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ export type ScaffoldConfig = {
pollingInterval: number;
alchemyApiKey: string;
walletConnectProjectId: string;
burnerWallet: {
enabled: boolean;
onlyLocal: boolean;
};
onlyLocalBurnerWallet: boolean;
walletAutoConnect: boolean;
};

Expand All @@ -32,13 +29,8 @@ const scaffoldConfig = {
// .env.local for local testing, and in the Vercel/system env config for live apps.
walletConnectProjectId: process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID || "3a8170812b534d0ff9d794f19a901d64",

// Burner Wallet configuration
burnerWallet: {
// Set it to false to completely remove burner wallet from all networks
enabled: true,
// Only show the Burner Wallet when running on hardhat network
onlyLocal: true,
},
// Only show the Burner Wallet when running on hardhat network
onlyLocalBurnerWallet: true,

/**
* Auto connect:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "~~/services/web3/wagmi-burner/BurnerConnector";
import { getTargetNetwork } from "~~/utils/scaffold-eth";

const burnerConfig = scaffoldConfig.burnerWallet;
const { onlyLocalBurnerWallet } = scaffoldConfig;
const targetNetwork = getTargetNetwork();
export interface BurnerWalletOptions {
chains: Chain[];
Expand All @@ -26,11 +26,7 @@ export const burnerWalletConfig = ({ chains }: BurnerWalletOptions): Wallet => (
iconUrl: "https://avatars.githubusercontent.com/u/56928858?s=200&v=4",
iconBackground: "#0c2f78",
hidden: () => {
if (!burnerConfig.enabled) {
return true;
}

if (burnerConfig.onlyLocal) {
if (onlyLocalBurnerWallet) {
return targetNetwork.id !== hardhat.id;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/nextjs/services/web3/wagmiConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createConfig } from "wagmi";
import scaffoldConfig from "~~/scaffold.config";
import { appChains, wagmiConnectors } from "~~/services/web3/wagmiConnectors";

export const wagmiConfig = createConfig({
autoConnect: false,
autoConnect: scaffoldConfig.walletAutoConnect,
connectors: wagmiConnectors,
publicClient: appChains.publicClient,
});
10 changes: 6 additions & 4 deletions packages/nextjs/services/web3/wagmiConnectors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import { burnerWalletConfig } from "~~/services/web3/wagmi-burner/burnerWalletCo
import { getTargetNetwork } from "~~/utils/scaffold-eth";

const configuredNetwork = getTargetNetwork();
const burnerConfig = scaffoldConfig.burnerWallet;
const { onlyLocalBurnerWallet } = scaffoldConfig;

// We always want to have mainnet enabled (ENS resolution, ETH price, etc). But only once.
const enabledChains =
(configuredNetwork.id as number) === 1 ? [configuredNetwork] : [configuredNetwork, chains.mainnet];
const enabledChains = configuredNetwork.id === 1 ? [configuredNetwork] : [configuredNetwork, chains.mainnet];

/**
* Chains for the app
Expand Down Expand Up @@ -53,6 +52,9 @@ const wallets = [
braveWallet(walletsOptions),
coinbaseWallet({ ...walletsOptions, appName: "scaffold-eth-2" }),
rainbowWallet(walletsOptions),
...(configuredNetwork.id === chains.hardhat.id || !onlyLocalBurnerWallet
? [burnerWalletConfig({ chains: [appChains.chains[0]] })]
: []),
];

/**
Expand All @@ -61,6 +63,6 @@ const wallets = [
export const wagmiConnectors = connectorsForWallets([
{
groupName: "Supported Wallets",
wallets: burnerConfig.enabled ? [...wallets, burnerWalletConfig({ chains: [appChains.chains[0]] })] : wallets,
wallets,
},
]);