Skip to content

Commit

Permalink
Merge pull request #18 from dynamic-labs/fix/hardhat
Browse files Browse the repository at this point in the history
fix: add back hardhat
  • Loading branch information
matthew1809 authored Jul 10, 2024
2 parents 32bee92 + 4f7d191 commit ca93926
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Home: NextPage = () => {
try {
const isTestnet = await primaryWallet?.connector?.isTestnet();
if (!isTestnet) {
alert("You might want to switch to testnet to send transactions");
alert("You're not on a testnet, proceed with caution.");
}
const hash = await sendTransaction(connectedAddress, "0.0001", primaryWallet, networkConfigurations);
setTransactionSignature(hash);
Expand Down
21 changes: 19 additions & 2 deletions packages/nextjs/app/safe/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getPimlicoSmartAccountClient,
transferERC20,
} from "~~/lib/permissionless";
import { notification } from "~~/utils/scaffold-eth";

const SafePage = () => {
const { address, chain, isConnected } = useAccount();
Expand Down Expand Up @@ -61,6 +62,12 @@ const SafePage = () => {
setNetwork(network);
};

useEffect(() => {
if (!process.env.NEXT_PUBLIC_PIMLICO_API_KEY) {
notification.error("Please set NEXT_PUBLIC_PIMLICO_API_KEY in .env file.");
}
}, []);

useEffect(() => {
fetchNetwork();
}, [primaryWallet]);
Expand All @@ -72,6 +79,11 @@ const SafePage = () => {
const userAddress = address as `0x${string}`;
if (!primaryWallet || !chain) return;

if (!process.env.NEXT_PUBLIC_PIMLICO_API_KEY) {
notification.error("Please set NEXT_PUBLIC_PIMLICO_API_KEY in .env file and restart");
return;
}

const walletClient = await createWalletClientFromWallet(primaryWallet);
const { account } = await getPimlicoSmartAccountClient(userAddress, chain, walletClient);
setSafeAddress(account.address);
Expand Down Expand Up @@ -150,8 +162,13 @@ const SafePage = () => {
const transactionDetail = await getTransactionOnBaseSepoliaByHash(txHash);
setTransactionDetails([...transactionDetails, transactionDetail]);
} catch (err) {
setError("Failed to transfer tokens.");
console.error(err);
if (err instanceof Error) {
notification.error(err.message);
console.error(err.message);
} else {
setError("Failed to transfer tokens.");
console.error(err);
}
} finally {
setLoading(false);
}
Expand Down
21 changes: 20 additions & 1 deletion packages/nextjs/components/ScaffoldEthAppWithProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ export const queryClient = new QueryClient({
},
});

const evmNetworks = [
...scaffoldConfig.targetNetworks.map(chain => ({
blockExplorerUrls: chain.blockExplorers
? Object.values(chain.blockExplorers as any).map(({ url }: any) => url)
: [],
chainId: chain.id,
name: chain.name,
rpcUrls: Object.values(chain.rpcUrls).map(({ http }) => http[0]),
iconUrls: [
chain.name === "Hardhat"
? "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRz4i1wWF516fnkizp1WSDG5rnG8GfkQAVoVQ&s"
: "",
],
nativeCurrency: chain.nativeCurrency,
networkId: chain.id,
})),
...customEvmNetworks,
];

export const ScaffoldEthAppWithProviders = ({ children }: { children: React.ReactNode }) => {
const { resolvedTheme } = useTheme();

Expand All @@ -48,7 +67,7 @@ export const ScaffoldEthAppWithProviders = ({ children }: { children: React.Reac
environmentId: scaffoldConfig.dynamicEnvId,
walletConnectors: [EthereumWalletConnectors],
overrides: {
evmNetworks: networks => mergeNetworks(customEvmNetworks, networks),
evmNetworks: networks => mergeNetworks(evmNetworks, networks),
},
}}
>
Expand Down
47 changes: 28 additions & 19 deletions packages/nextjs/lib/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Wallet } from "@dynamic-labs/sdk-react-core";
import { NetworkConfigurationMap } from "@dynamic-labs/types";
import { getOrMapViemChain } from "@dynamic-labs/viem-utils";
import { Account, Chain, Hex, Transport, WalletClient, parseEther } from "viem";
import { notification } from "~~/utils/scaffold-eth";

export const signMessage = async (message: string, wallet: any): Promise<string> => {
const connector = wallet?.connector;
Expand All @@ -15,24 +16,32 @@ export const sendTransaction = async (
wallet: Wallet,
networkConfigurations: NetworkConfigurationMap,
): Promise<string> => {
const walletClient = wallet.connector.getWalletClient<WalletClient<Transport, Chain, Account>>();

const chainID = await wallet.connector.getNetwork();
const currentNetwork = networkConfigurations.evm?.find(network => network.chainId === chainID);

if (!currentNetwork) {
throw new Error("Network not found");
try {
const walletClient = wallet.connector.getWalletClient<WalletClient<Transport, Chain, Account>>();

const chainID = await wallet.connector.getNetwork();
const currentNetwork = networkConfigurations.evm?.find(network => network.chainId === chainID);

if (!currentNetwork) {
throw new Error("Network not found");
}

const chain = getOrMapViemChain(currentNetwork);

const transaction = {
account: wallet.address as Hex,
to: address as Hex,
chain,
value: amount ? parseEther(amount) : undefined,
};

const transactionHash = await walletClient.sendTransaction(transaction);
return transactionHash;
} catch (e) {
if (e instanceof Error) {
notification.error(`Error sending transaction: ${e.message}`);
} else {
notification.error("Error sending transaction");
}
}

const chain = getOrMapViemChain(currentNetwork);

const transaction = {
account: wallet.address as Hex,
to: address as Hex,
chain,
value: amount ? parseEther(amount) : undefined,
};

const transactionHash = await walletClient.sendTransaction(transaction);
return transactionHash;
};
2 changes: 1 addition & 1 deletion packages/nextjs/scaffold.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type ScaffoldConfig = {

const scaffoldConfig = {
// The networks on which your DApp is live
targetNetworks: [chains.baseSepolia],
targetNetworks: [chains.hardhat],

// The interval at which your front-end polls the RPC servers for new data
// it has no effect if you only target the local network (default is 4000)
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/services/web3/wagmiConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const wagmiConfig = createConfig({
scroll,
scrollSepolia,
sepolia,
hardhat,
...customEvmNetworks.map(getOrMapViemChain),
],
ssr: true,
Expand Down

0 comments on commit ca93926

Please sign in to comment.