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: v3 #25

Merged
merged 1 commit into from
Sep 20, 2024
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
32 changes: 19 additions & 13 deletions packages/nextjs/app/safe/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
"use client";

import { useEffect, useState } from "react";
import { ExternalLinkIcon, getNetwork, useDynamicContext, useSwitchNetwork } from "@dynamic-labs/sdk-react-core";
import { createWalletClientFromWallet } from "@dynamic-labs/viem-utils";
import {
ExternalLinkIcon,
getNetwork,
useDynamicContext,
useIsLoggedIn,
useSwitchNetwork,
} from "@dynamic-labs/sdk-react-core";
import { formatUnits } from "viem";
import { baseSepolia } from "viem/chains";
import { useAccount, useBalance, useReadContract } from "wagmi";
Expand Down Expand Up @@ -31,7 +36,8 @@ import { notification } from "~~/utils/scaffold-eth";

const SafePage = () => {
const { address, chain, isConnected } = useAccount();
const { primaryWallet, isAuthenticated } = useDynamicContext();
const { primaryWallet } = useDynamicContext();
const isLoggedIn = useIsLoggedIn();
const switchNetwork = useSwitchNetwork();

const [safeDeployed, setSafeDeployed] = useState(false);
Expand Down Expand Up @@ -85,19 +91,19 @@ const SafePage = () => {
args: [safeAddress],
});

const fetchNetwork = async () => {
if (!primaryWallet) return;
const network = Number(await getNetwork(primaryWallet.connector));
setNetwork(network);
};

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

useEffect(() => {
const fetchNetwork = async () => {
if (!primaryWallet) return;
const network = Number(await getNetwork(primaryWallet.connector));
setNetwork(network);
};

fetchNetwork();
}, [primaryWallet]);

Expand All @@ -113,7 +119,7 @@ const SafePage = () => {
return;
}

const walletClient = await createWalletClientFromWallet(primaryWallet);
const walletClient = await primaryWallet.getWalletClient();
const { account } = await getPimlicoSmartAccountClient(userAddress, chain, walletClient);
setSafeAddress(account.address);
setSafeDeployed(true);
Expand Down Expand Up @@ -463,7 +469,7 @@ const SafePage = () => {
</div>
) : (
<>
{isConnected && isAuthenticated && network !== baseSepolia.id ? (
{isConnected && isLoggedIn && network !== baseSepolia.id ? (
<button
className="btn btn-success"
onClick={() => switchNetwork({ wallet: primaryWallet, network: baseSepolia.id })}
Expand All @@ -474,13 +480,13 @@ const SafePage = () => {
<button
className="btn btn-success"
onClick={handleDeploySafe}
disabled={loading || !isConnected || !isAuthenticated}
disabled={loading || !isConnected || !isLoggedIn}
>
{loading ? (
<>
<span className="loading loading-spinner"></span>Deploying...
</>
) : isConnected && isAuthenticated ? (
) : isConnected && isLoggedIn ? (
"Deploy Safe Account"
) : (
"Connect Wallet first"
Expand Down
16 changes: 12 additions & 4 deletions packages/nextjs/lib/dynamic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { isEthereumWallet } from "@dynamic-labs/ethereum";
import { getOrMapViemChain } from "@dynamic-labs/ethereum-core";
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 { Hex, parseEther } from "viem";
import { notification } from "~~/utils/scaffold-eth";

export const signMessage = async (message: string, wallet: any): Promise<string> => {
Expand All @@ -17,7 +18,11 @@ export const sendTransaction = async (
networkConfigurations: NetworkConfigurationMap,
): Promise<string | undefined> => {
try {
const walletClient = wallet.connector.getWalletClient<WalletClient<Transport, Chain, Account>>();
if (!isEthereumWallet(wallet)) {
throw new Error("Wallet is not an Ethereum wallet");
}

const walletClient = await wallet.getWalletClient();

const chainID = await wallet.connector.getNetwork();
const currentNetwork = networkConfigurations.evm?.find(network => network.chainId === chainID);
Expand All @@ -26,7 +31,10 @@ export const sendTransaction = async (
throw new Error("Network not found");
}

const chain = getOrMapViemChain(currentNetwork);
const chain = getOrMapViemChain({
...currentNetwork,
chainId: Number(currentNetwork.chainId),
});

const transaction = {
account: wallet.address as Hex,
Expand Down
8 changes: 4 additions & 4 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"postinstall": "patch-package"
},
"dependencies": {
"@dynamic-labs/ethereum": "^3.0.0-alpha.13",
"@dynamic-labs/sdk-react-core": "^3.0.0-alpha.13",
"@dynamic-labs/viem-utils": "3.0.0-alpha.13",
"@dynamic-labs/wagmi-connector": "^3.0.0-alpha.13",
"@dynamic-labs/ethereum": "^3.0.0",
"@dynamic-labs/ethereum-core": "3.0.0",
"@dynamic-labs/sdk-react-core": "^3.0.0",
"@dynamic-labs/wagmi-connector": "^3.0.0",
"@heroicons/react": "^2.0.11",
"@lighthouse-web3/sdk": "^0.3.7",
"@safe-global/api-kit": "^2.4.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/services/web3/wagmiConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getOrMapViemChain } from "@dynamic-labs/viem-utils";
import { getOrMapViemChain } from "@dynamic-labs/ethereum-core";
import { Chain, createClient, http } from "viem";
import {
arbitrum,
Expand Down
Loading
Loading