Skip to content

Commit

Permalink
simplify useGetAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
toddkao committed Feb 7, 2025
1 parent 54706cf commit 0bca068
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 95 deletions.
102 changes: 11 additions & 91 deletions packages/widget/src/hooks/useGetAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
svmWalletAtom,
walletsAtom,
connectedAddressesAtom,
WalletState,
} from "@/state/wallets";
import { useAccount as useCosmosAccount, WalletType } from "graz";
import { useAtom, useAtomValue } from "jotai";
Expand All @@ -16,22 +17,12 @@ import { ChainType } from "@skip-go/client";
import { walletConnectLogo } from "@/constants/wagmi";

export const useGetAccount = () => {
const wallet = useAtomValue(walletsAtom);
const [evmWallet, setEvmWallet] = useAtom(evmWalletAtom);
const [cosmosWallet, setCosmosWallet] = useAtom(cosmosWalletAtom);
const [svmWallet, setSvmWallet] = useAtom(svmWalletAtom);
const connectedAddress = useAtomValue(connectedAddressesAtom);
const { data: chains } = useAtomValue(skipChainsAtom);

const { data: cosmosAccounts, walletType } = useCosmosAccount({
multiChain: true,
});

const solanaWallet = solanaWallets.find((w) => w.name === wallet.svm?.walletName);

const evmAccount = useEvmAccount();
const connectors = useConnectors();

const getAccount = useCallback(
// if checkChainType is true, it only check wallet connected no chainId is dependent
(chainId?: string, checkChainType?: boolean) => {
Expand All @@ -41,100 +32,29 @@ export const useGetAccount = () => {
return {
address: connectedAddress[chainId],
chainType: chainType,
wallet: {
name: "injected",
prettyName: "injected",
},
};
walletName: "injected",
walletPrettyName: "injected",
} as WalletState;
}

const getCosmosAccount = () => {
if (!cosmosAccounts || !chainId) return;
return cosmosAccounts[chainId];
};
const cosmosAccount = getCosmosAccount();

switch (chainType) {
case ChainType.Cosmos: {
if (!cosmosAccount) return;
if (!wallet.cosmos) return;
const walletInfo = getCosmosWalletInfo(wallet.cosmos.walletName as WalletType);

return {
address: cosmosAccount.bech32Address,
chainType,
wallet: {
name: wallet.cosmos.walletName,
prettyName: walletInfo.name,
logo: walletInfo.imgSrc,
isLedger: cosmosAccount.isNanoLedger,
},
};
if (!cosmosWallet) return;
return cosmosWallet;
}
case ChainType.EVM: {
if (!wallet.evm) return;
if (evmAccount.chainId !== Number(chainId) && !checkChainType) return;
if (!evmAccount.address) return;
if (!evmAccount.connector) return;

const getLogo = () => {
if (evmAccount?.connector?.id === "walletConnect") {
return walletConnectLogo;
}

if (evmAccount?.connector?.id.includes("keplr")) {
return getCosmosWalletInfo(WalletType.KEPLR).imgSrc;
}
return connectors.find((item) => item.id === evmAccount.connector?.id)?.icon;
};
return {
address: evmAccount.address as string,
currentConnectedEVMChainId: String(evmAccount.chainId),
chainType,
wallet: {
name: evmAccount.connector.id,
prettyName: evmAccount.connector.name,
logo: getLogo(),
},
};
if (!evmWallet) return;
return evmWallet;
}
case ChainType.SVM: {
if (!wallet.svm) return;
if (!solanaWallet?.publicKey) return;
return {
address: solanaWallet.publicKey.toBase58(),
chainType,
wallet: {
name: solanaWallet.name as string,
prettyName: solanaWallet.name as string,
logo: solanaWallet.name === "WalletConnect" ? walletConnectLogo : solanaWallet.icon,
},
};
if (!svmWallet) return;
return svmWallet;
}
default:
return undefined;
}
},
[
chains,
walletType,
cosmosWallet,
solanaWallet,
svmWallet,
evmAccount.connector,
evmAccount.chainId,
evmAccount.address,
evmWallet,
setCosmosWallet,
setSvmWallet,
setEvmWallet,
cosmosAccounts,
wallet.evm,
wallet.cosmos,
wallet.svm,
connectors,
connectedAddress,
],
[chains, connectedAddress, cosmosWallet, evmWallet, svmWallet],
);

return getAccount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ const ConnectEco = ({ chainType, chainID }: { chainType: ChainType; chainID: str
leftContent={
account ? (
<Row align="center" gap={10}>
{account?.wallet.logo && (
{account?.walletInfo?.logo && (
<img
height={35}
width={35}
style={{ objectFit: "cover" }}
src={account?.wallet.logo}
alt={`${account?.wallet.prettyName} logo`}
title={account?.wallet.prettyName}
src={account?.walletInfo?.logo}
alt={`${account?.walletPrettyName} logo`}
title={account?.walletPrettyName}
/>
)}
<Row align="baseline" gap={8}>
Expand Down

0 comments on commit 0bca068

Please sign in to comment.