Skip to content

Commit

Permalink
show erc20 balances
Browse files Browse the repository at this point in the history
  • Loading branch information
steezeburger committed Oct 24, 2024
1 parent a6b0878 commit 33c0c64
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 137 deletions.
91 changes: 14 additions & 77 deletions web/src/components/WithdrawCard/WithdrawCard.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
import type React from "react";
import { useContext, useEffect, useMemo, useRef, useState } from "react";
import { useContext, useEffect, useMemo, useState } from "react";

import AnimatedArrowSpacer from "components/AnimatedDownArrowSpacer/AnimatedDownArrowSpacer";
import Dropdown, { type DropdownOption } from "components/Dropdown/Dropdown";
import type { EvmChainInfo, IbcChainInfo } from "config/chainConfigs";
import { useConfig } from "config/hooks/useConfig";
import { useIbcChainSelection } from "features/IbcChainSelector";
import {
EthWalletConnector,
getAstriaWithdrawerService,
} from "features/EthWallet";
import { getAstriaWithdrawerService } from "features/EthWallet";
import { useEthWallet } from "features/EthWallet/hooks/useEthWallet";
import { useEvmChainSelection } from "features/EthWallet/hooks/useEvmChainSelection";
import { NotificationType } from "features/Notifications/components/Notification/types";
import { NotificationsContext } from "features/Notifications/contexts/NotificationsContext";

export default function WithdrawCard(): React.ReactElement {
const { addNotification } = useContext(NotificationsContext);
const { userAccount: evmUserAccount, selectedWallet } = useEthWallet();
const { selectedWallet } = useEthWallet();
const { evmChains, ibcChains } = useConfig();

const {
evmAccountAddress: fromAddress,
selectEvmChain,
evmChainsOptions,
selectedEvmChain,
selectEvmCurrency,
evmCurrencyOptions,
selectedEvmCurrency,
evmBalance,
isLoadingEvmBalance,
connectEVMWallet,
} = useEvmChainSelection(evmChains);
const defaultEvmChainOption = useMemo(() => {
return evmChainsOptions[0] || null;
}, [evmChainsOptions]);
const defaultEvmCurrencyOption = useMemo(() => {
return evmCurrencyOptions[0] || null;
}, [evmCurrencyOptions]);
Expand Down Expand Up @@ -92,9 +90,6 @@ export default function WithdrawCard(): React.ReactElement {
};
}, [selectedEvmCurrency, selectedIbcChain, defaultIbcCurrencyOption]);

const [fromAddress, setFromAddress] = useState<string>("");
const [balance, setBalance] = useState<string>("0");
const [isLoadingBalance, setIsLoadingBalance] = useState<boolean>(false);
const [amount, setAmount] = useState<string>("");
const [isAmountValid, setIsAmountValid] = useState<boolean>(false);

Expand All @@ -104,45 +99,19 @@ export default function WithdrawCard(): React.ReactElement {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [isAnimating, setIsAnimating] = useState<boolean>(false);

// create refs to hold the latest state values
const latestState = useRef({
evmUserAccount,
selectedWallet,
recipientAddress,
selectedEvmChain,
});

// update the ref whenever the state changes
useEffect(() => {
latestState.current = {
evmUserAccount,
selectedWallet,
recipientAddress,
selectedEvmChain,
};
}, [evmUserAccount, selectedWallet, recipientAddress, selectedEvmChain]);

useEffect(() => {
if (evmUserAccount?.address) {
setFromAddress(evmUserAccount.address);
}
// TODO - get balance for selected currency
if (evmUserAccount?.balance) {
setBalance(`${evmUserAccount.balance} ${selectedEvmCurrency?.coinDenom}`);
}
}, [evmUserAccount, selectedEvmCurrency]);

useEffect(() => {
if (amount || recipientAddress) {
setHasTouchedForm(true);
}
checkIsFormValid(amount, recipientAddress);
}, [amount, recipientAddress]);

/* biome-ignore lint/correctness/useExhaustiveDependencies: */
useEffect(() => {
if (!selectedEvmChain) {
return;
}
console.log("connecting EVM wallet");
connectEVMWallet().then((_) => {});
}, [selectedEvmChain]);

Expand All @@ -165,45 +134,13 @@ export default function WithdrawCard(): React.ReactElement {
setIsRecipientAddressValid(isRecipientAddressValid);
};

const connectEVMWallet = async () => {
if (!selectedEvmChain) {
// select default chain if none selected, then return. effect handles retriggering.
selectEvmChain(defaultEvmChainOption.value);
return;
}

addNotification({
modalOpts: {
modalType: NotificationType.INFO,
title: "Connect EVM Wallet",
component: <EthWalletConnector />,
onCancel: () => {
const currentState = latestState.current;
setFromAddress("");
selectEvmChain(null);
if (currentState.selectedWallet) {
currentState.selectedWallet = undefined;
}
},
onConfirm: () => {
const currentState = latestState.current;
if (!currentState.evmUserAccount) {
setFromAddress("");
selectEvmChain(null);
} else {
setFromAddress(currentState.evmUserAccount.address);
}
},
},
});
};

const handleWithdraw = async () => {
if (
!selectedWallet ||
!selectedEvmCurrency ||
!isAmountValid ||
!recipientAddress
!recipientAddress ||
!fromAddress
) {
console.error(
"Withdrawal cannot proceed: missing required fields or fields are invalid",
Expand Down Expand Up @@ -329,12 +266,12 @@ export default function WithdrawCard(): React.ReactElement {
Address: {fromAddress}
</p>
)}
{fromAddress && !isLoadingBalance && (
{fromAddress && !isLoadingEvmBalance && (
<p className="mt-2 has-text-grey-lighter has-text-weight-semibold">
Balance: {balance}
Balance: {evmBalance}
</p>
)}
{fromAddress && isLoadingBalance && (
{fromAddress && isLoadingEvmBalance && (
<p className="mt-2 has-text-grey-lighter has-text-weight-semibold">
Balance: <i className="fas fa-spinner fa-pulse" />
</p>
Expand Down
9 changes: 9 additions & 0 deletions web/src/config/chainConfigs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ export type EvmCurrency = {
iconClass?: string;
};

export function evmCurrencyBelongsToChain(
currency: EvmCurrency,
chain: EvmChainInfo,
): boolean {
// FIXME - what if two chains have currencies with the same coinDenom?
// e.g. USDC on Noble and USDC on Celestia
return chain.currencies?.includes(currency);
}

// EvmChains type maps labels to EvmChainInfo objects
export type EvmChains = {
[label: string]: EvmChainInfo;
Expand Down
59 changes: 0 additions & 59 deletions web/src/features/EthWallet/hooks/useEvmChainSelection.ts

This file was deleted.

Loading

0 comments on commit 33c0c64

Please sign in to comment.