Skip to content

Commit ca93926

Browse files
authored
Merge pull request #18 from dynamic-labs/fix/hardhat
fix: add back hardhat
2 parents 32bee92 + 4f7d191 commit ca93926

File tree

6 files changed

+70
-24
lines changed

6 files changed

+70
-24
lines changed

packages/nextjs/app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const Home: NextPage = () => {
3131
try {
3232
const isTestnet = await primaryWallet?.connector?.isTestnet();
3333
if (!isTestnet) {
34-
alert("You might want to switch to testnet to send transactions");
34+
alert("You're not on a testnet, proceed with caution.");
3535
}
3636
const hash = await sendTransaction(connectedAddress, "0.0001", primaryWallet, networkConfigurations);
3737
setTransactionSignature(hash);

packages/nextjs/app/safe/page.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
getPimlicoSmartAccountClient,
2323
transferERC20,
2424
} from "~~/lib/permissionless";
25+
import { notification } from "~~/utils/scaffold-eth";
2526

2627
const SafePage = () => {
2728
const { address, chain, isConnected } = useAccount();
@@ -61,6 +62,12 @@ const SafePage = () => {
6162
setNetwork(network);
6263
};
6364

65+
useEffect(() => {
66+
if (!process.env.NEXT_PUBLIC_PIMLICO_API_KEY) {
67+
notification.error("Please set NEXT_PUBLIC_PIMLICO_API_KEY in .env file.");
68+
}
69+
}, []);
70+
6471
useEffect(() => {
6572
fetchNetwork();
6673
}, [primaryWallet]);
@@ -72,6 +79,11 @@ const SafePage = () => {
7279
const userAddress = address as `0x${string}`;
7380
if (!primaryWallet || !chain) return;
7481

82+
if (!process.env.NEXT_PUBLIC_PIMLICO_API_KEY) {
83+
notification.error("Please set NEXT_PUBLIC_PIMLICO_API_KEY in .env file and restart");
84+
return;
85+
}
86+
7587
const walletClient = await createWalletClientFromWallet(primaryWallet);
7688
const { account } = await getPimlicoSmartAccountClient(userAddress, chain, walletClient);
7789
setSafeAddress(account.address);
@@ -150,8 +162,13 @@ const SafePage = () => {
150162
const transactionDetail = await getTransactionOnBaseSepoliaByHash(txHash);
151163
setTransactionDetails([...transactionDetails, transactionDetail]);
152164
} catch (err) {
153-
setError("Failed to transfer tokens.");
154-
console.error(err);
165+
if (err instanceof Error) {
166+
notification.error(err.message);
167+
console.error(err.message);
168+
} else {
169+
setError("Failed to transfer tokens.");
170+
console.error(err);
171+
}
155172
} finally {
156173
setLoading(false);
157174
}

packages/nextjs/components/ScaffoldEthAppWithProviders.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ export const queryClient = new QueryClient({
3838
},
3939
});
4040

41+
const evmNetworks = [
42+
...scaffoldConfig.targetNetworks.map(chain => ({
43+
blockExplorerUrls: chain.blockExplorers
44+
? Object.values(chain.blockExplorers as any).map(({ url }: any) => url)
45+
: [],
46+
chainId: chain.id,
47+
name: chain.name,
48+
rpcUrls: Object.values(chain.rpcUrls).map(({ http }) => http[0]),
49+
iconUrls: [
50+
chain.name === "Hardhat"
51+
? "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRz4i1wWF516fnkizp1WSDG5rnG8GfkQAVoVQ&s"
52+
: "",
53+
],
54+
nativeCurrency: chain.nativeCurrency,
55+
networkId: chain.id,
56+
})),
57+
...customEvmNetworks,
58+
];
59+
4160
export const ScaffoldEthAppWithProviders = ({ children }: { children: React.ReactNode }) => {
4261
const { resolvedTheme } = useTheme();
4362

@@ -48,7 +67,7 @@ export const ScaffoldEthAppWithProviders = ({ children }: { children: React.Reac
4867
environmentId: scaffoldConfig.dynamicEnvId,
4968
walletConnectors: [EthereumWalletConnectors],
5069
overrides: {
51-
evmNetworks: networks => mergeNetworks(customEvmNetworks, networks),
70+
evmNetworks: networks => mergeNetworks(evmNetworks, networks),
5271
},
5372
}}
5473
>

packages/nextjs/lib/dynamic.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Wallet } from "@dynamic-labs/sdk-react-core";
22
import { NetworkConfigurationMap } from "@dynamic-labs/types";
33
import { getOrMapViemChain } from "@dynamic-labs/viem-utils";
44
import { Account, Chain, Hex, Transport, WalletClient, parseEther } from "viem";
5+
import { notification } from "~~/utils/scaffold-eth";
56

67
export const signMessage = async (message: string, wallet: any): Promise<string> => {
78
const connector = wallet?.connector;
@@ -15,24 +16,32 @@ export const sendTransaction = async (
1516
wallet: Wallet,
1617
networkConfigurations: NetworkConfigurationMap,
1718
): Promise<string> => {
18-
const walletClient = wallet.connector.getWalletClient<WalletClient<Transport, Chain, Account>>();
19-
20-
const chainID = await wallet.connector.getNetwork();
21-
const currentNetwork = networkConfigurations.evm?.find(network => network.chainId === chainID);
22-
23-
if (!currentNetwork) {
24-
throw new Error("Network not found");
19+
try {
20+
const walletClient = wallet.connector.getWalletClient<WalletClient<Transport, Chain, Account>>();
21+
22+
const chainID = await wallet.connector.getNetwork();
23+
const currentNetwork = networkConfigurations.evm?.find(network => network.chainId === chainID);
24+
25+
if (!currentNetwork) {
26+
throw new Error("Network not found");
27+
}
28+
29+
const chain = getOrMapViemChain(currentNetwork);
30+
31+
const transaction = {
32+
account: wallet.address as Hex,
33+
to: address as Hex,
34+
chain,
35+
value: amount ? parseEther(amount) : undefined,
36+
};
37+
38+
const transactionHash = await walletClient.sendTransaction(transaction);
39+
return transactionHash;
40+
} catch (e) {
41+
if (e instanceof Error) {
42+
notification.error(`Error sending transaction: ${e.message}`);
43+
} else {
44+
notification.error("Error sending transaction");
45+
}
2546
}
26-
27-
const chain = getOrMapViemChain(currentNetwork);
28-
29-
const transaction = {
30-
account: wallet.address as Hex,
31-
to: address as Hex,
32-
chain,
33-
value: amount ? parseEther(amount) : undefined,
34-
};
35-
36-
const transactionHash = await walletClient.sendTransaction(transaction);
37-
return transactionHash;
3847
};

packages/nextjs/scaffold.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type ScaffoldConfig = {
1010

1111
const scaffoldConfig = {
1212
// The networks on which your DApp is live
13-
targetNetworks: [chains.baseSepolia],
13+
targetNetworks: [chains.hardhat],
1414

1515
// The interval at which your front-end polls the RPC servers for new data
1616
// it has no effect if you only target the local network (default is 4000)

packages/nextjs/services/web3/wagmiConfig.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const wagmiConfig = createConfig({
3030
scroll,
3131
scrollSepolia,
3232
sepolia,
33+
hardhat,
3334
...customEvmNetworks.map(getOrMapViemChain),
3435
],
3536
ssr: true,

0 commit comments

Comments
 (0)