Skip to content

Commit

Permalink
fix: check and change network for safe
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew1809 committed Jul 9, 2024
1 parent 2bbb10b commit fd1c0e4
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions packages/nextjs/app/safe/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useState } from "react";
import { ExternalLinkIcon, useDynamicContext } from "@dynamic-labs/sdk-react-core";
import { ExternalLinkIcon, getNetwork, useDynamicContext, useSwitchNetwork } from "@dynamic-labs/sdk-react-core";
import { createWalletClientFromWallet } from "@dynamic-labs/viem-utils";
import { formatUnits } from "viem";
import { baseSepolia } from "viem/chains";
Expand All @@ -26,6 +26,7 @@ import {
const SafePage = () => {
const { address, chain, isConnected } = useAccount();
const { primaryWallet, isAuthenticated } = useDynamicContext();
const switchNetwork = useSwitchNetwork();

const [safeDeployed, setSafeDeployed] = useState(false);
const [safeAddress, setSafeAddress] = useState<string | null>("");
Expand Down Expand Up @@ -57,8 +58,16 @@ const SafePage = () => {
setLoading(true);
setError(null);
try {
if (!primaryWallet) return;
const network = await getNetwork(primaryWallet.connector);

if (network !== baseSepolia.id) {
switchNetwork({ wallet: primaryWallet, network: baseSepolia.id });
}
const userAddress = address as `0x${string}`;
if (!primaryWallet || !chain) return;
console.log(chain);
if (!chain) return;

const walletClient = await createWalletClientFromWallet(primaryWallet);
const { account } = await getPimlicoSmartAccountClient(userAddress, chain, walletClient);
setSafeAddress(account.address);
Expand All @@ -77,8 +86,16 @@ const SafePage = () => {
setLoading(true);
setError(null);
try {
if (!primaryWallet) return;
const network = await getNetwork(primaryWallet.connector);

if (network !== baseSepolia.id) {
switchNetwork({ wallet: primaryWallet, network: baseSepolia.id });
}

const userAddress = address as `0x${string}`;
if (!primaryWallet || !chain) return;
if (!chain) return;

const walletClient = await createWalletClientFromWallet(primaryWallet);
const smartAccountClient = await getPimlicoSmartAccountClient(userAddress, chain, walletClient);
const txHash = await transferERC20(
Expand Down Expand Up @@ -114,8 +131,15 @@ const SafePage = () => {
setLoading(true);
setError(null);
try {
if (!primaryWallet) return;
const network = await getNetwork(primaryWallet.connector);

if (network !== baseSepolia.id) {
switchNetwork({ wallet: primaryWallet, network: baseSepolia.id });
}

const userAddress = address as `0x${string}`;
if (!primaryWallet || !chain) return;
if (!chain) return;
const walletClient = await createWalletClientFromWallet(primaryWallet);
const smartAccountClient = await getPimlicoSmartAccountClient(userAddress, chain, walletClient);
const approveHash = await approveERC20(
Expand Down

0 comments on commit fd1c0e4

Please sign in to comment.