From 93e725fc597e1646b5f82699414687f2a7d856e0 Mon Sep 17 00:00:00 2001 From: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com> Date: Thu, 7 Sep 2023 10:20:19 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Merge=20FundAccount=20and=20Account?= =?UTF-8?q?Fund=20(#431)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ⚡ Merge FundAccount and AccountFund * fix build * 🐛 fundAccount not showing, small typos/text corrections --- .../Account/AccountDetails.test.tsx | 2 +- .../Account/AccountBalancesTable/index.tsx | 4 +- src/components/Account/AccountCreateFirst.tsx | 6 +- src/components/Account/AccountDetails.tsx | 2 +- .../AccountFundContent.tsx} | 137 +++++++------ .../AccountFund/AccountFundFullPage.tsx | 42 ++++ src/components/Account/AccountList.tsx | 6 +- src/components/Account/AccountMenuContent.tsx | 4 +- src/components/Account/AccountSummary.tsx | 2 +- .../Account/AccountDeleteAlertDialog.tsx | 4 +- .../Modals/Account/AccountDeleteModal.tsx | 4 +- .../Modals/FundWithdraw/FundAccount.tsx | 181 +----------------- .../FundAndWithdrawModalContent.tsx | 3 - src/components/Trade/AccountDetailsCard.tsx | 2 +- .../TradeChart/OsmosisTheGraphDataFeed.ts | 2 +- src/store/slices/broadcast.ts | 5 +- 16 files changed, 143 insertions(+), 263 deletions(-) rename src/components/Account/{AccountFund.tsx => AccountFund/AccountFundContent.tsx} (73%) create mode 100644 src/components/Account/AccountFund/AccountFundFullPage.tsx diff --git a/__tests__/components/Account/AccountDetails.test.tsx b/__tests__/components/Account/AccountDetails.test.tsx index 7fc19549..5dc5e226 100644 --- a/__tests__/components/Account/AccountDetails.test.tsx +++ b/__tests__/components/Account/AccountDetails.test.tsx @@ -41,4 +41,4 @@ describe('', () => { const container = screen.queryByTestId('account-details') expect(container).not.toBeInTheDocument() }) -}) \ No newline at end of file +}) diff --git a/src/components/Account/AccountBalancesTable/index.tsx b/src/components/Account/AccountBalancesTable/index.tsx index 186de65c..0d0daa94 100644 --- a/src/components/Account/AccountBalancesTable/index.tsx +++ b/src/components/Account/AccountBalancesTable/index.tsx @@ -12,7 +12,7 @@ import { useLocation, useNavigate } from 'react-router-dom' import { getAmountChangeColor } from 'components/Account/AccountBalancesTable/functions' import useAccountBalanceData from 'components/Account/AccountBalancesTable/useAccountBalanceData' -import AccountFund from 'components/Account/AccountFund' +import AccountFundFullPage from 'components/Account/AccountFund/AccountFundFullPage' import ActionButton from 'components/Button/ActionButton' import DisplayCurrency from 'components/DisplayCurrency' import { FormattedNumber } from 'components/FormattedNumber' @@ -147,7 +147,7 @@ export default function Index(props: Props) { } useStore.setState({ focusComponent: { - component: , + component: , onClose: () => { useStore.setState({ getStartedModal: true }) }, diff --git a/src/components/Account/AccountCreateFirst.tsx b/src/components/Account/AccountCreateFirst.tsx index 007a750a..189b65e0 100644 --- a/src/components/Account/AccountCreateFirst.tsx +++ b/src/components/Account/AccountCreateFirst.tsx @@ -1,7 +1,7 @@ import { useCallback, useEffect } from 'react' import { useLocation, useNavigate } from 'react-router-dom' -import AccountFund from 'components/Account/AccountFund' +import AccountFundFullPage from 'components/Account/AccountFund/AccountFundFullPage' import FullOverlayContent from 'components/FullOverlayContent' import WalletSelect from 'components/Wallet/WalletSelect' import useToggle from 'hooks/useToggle' @@ -27,7 +27,7 @@ export default function AccountCreateFirst() { navigate(getRoute(getPage(pathname), address, accountId)) useStore.setState({ focusComponent: { - component: , + component: , onClose: () => { useStore.setState({ getStartedModal: true }) }, @@ -51,4 +51,4 @@ export default function AccountCreateFirst() { docs='account' /> ) -} +} \ No newline at end of file diff --git a/src/components/Account/AccountDetails.tsx b/src/components/Account/AccountDetails.tsx index 84626f8a..6f2b2d49 100644 --- a/src/components/Account/AccountDetails.tsx +++ b/src/components/Account/AccountDetails.tsx @@ -154,4 +154,4 @@ function AccountDetails(props: Props) { ) -} \ No newline at end of file +} diff --git a/src/components/Account/AccountFund.tsx b/src/components/Account/AccountFund/AccountFundContent.tsx similarity index 73% rename from src/components/Account/AccountFund.tsx rename to src/components/Account/AccountFund/AccountFundContent.tsx index 3743a3a2..14db63d6 100644 --- a/src/components/Account/AccountFund.tsx +++ b/src/components/Account/AccountFund/AccountFundContent.tsx @@ -1,20 +1,18 @@ +import classNames from 'classnames' import React, { useCallback, useEffect, useMemo, useState } from 'react' -import { useParams } from 'react-router-dom' import Button from 'components/Button' -import Card from 'components/Card' import DepositCapMessage from 'components/DepositCapMessage' -import FullOverlayContent from 'components/FullOverlayContent' -import { Plus } from 'components/Icons' +import { ArrowRight, Plus } from 'components/Icons' import SwitchAutoLend from 'components/Switch/SwitchAutoLend' import Text from 'components/Text' import TokenInputWithSlider from 'components/TokenInput/TokenInputWithSlider' import WalletBridges from 'components/Wallet/WalletBridges' import { BN_ZERO } from 'constants/math' -import useAccounts from 'hooks/useAccounts' -import useCurrentAccount from 'hooks/useCurrentAccount' +import useAutoLend from 'hooks/useAutoLend' import useMarketAssets from 'hooks/useMarketAssets' import useToggle from 'hooks/useToggle' +import { useUpdatedAccount } from 'hooks/useUpdatedAccount' import useWalletBalances from 'hooks/useWalletBalances' import useStore from 'store' import { BNCoin } from 'types/classes/BNCoin' @@ -24,56 +22,78 @@ import { defaultFee } from 'utils/constants' import { getCapLeftWithBuffer } from 'utils/generic' import { BN } from 'utils/helpers' -export default function AccountFund() { - const address = useStore((s) => s.address) +interface Props { + account?: Account + address: string + accountId: string + isFullPage?: boolean +} + +export default function AccountFundContent(props: Props) { const deposit = useStore((s) => s.deposit) const walletAssetModal = useStore((s) => s.walletAssetsModal) - const { accountId } = useParams() - const { data: accounts } = useAccounts(address) - const currentAccount = useCurrentAccount() + const [isFunding, setIsFunding] = useToggle(false) - const [selectedAccountId, setSelectedAccountId] = useState(null) const [fundingAssets, setFundingAssets] = useState([]) - const { data: walletBalances } = useWalletBalances(address) + const { data: marketAssets } = useMarketAssets() + const { data: walletBalances } = useWalletBalances(props.address) + const { autoLendEnabledAccountIds } = useAutoLend() + const [isLending, toggleIsLending] = useToggle(false) + const { simulateDeposits } = useUpdatedAccount(props.account) + const baseAsset = getBaseAsset() + const hasAssetSelected = fundingAssets.length > 0 const hasFundingAssets = fundingAssets.length > 0 && fundingAssets.every((a) => a.toCoin().amount !== '0') - const { data: marketAssets } = useMarketAssets() - const [isLending, toggleIsLending] = useToggle(false) - - const baseBalance = useMemo( - () => walletBalances.find(byDenom(baseAsset.denom))?.amount ?? '0', - [walletBalances, baseAsset], - ) - const balances = walletBalances.map((coin) => new BNCoin(coin)) const selectedDenoms = useMemo(() => { return walletAssetModal?.selectedDenoms ?? [] }, [walletAssetModal?.selectedDenoms]) - const handleClick = useCallback(async () => { - setIsFunding(true) - if (!accountId) return - const result = await deposit({ - accountId, - coins: fundingAssets, - lend: isLending, - }) - setIsFunding(false) - if (result) useStore.setState({ focusComponent: null, walletAssetsModal: null }) - }, [setIsFunding, accountId, deposit, fundingAssets, isLending]) + const baseBalance = useMemo( + () => walletBalances.find(byDenom(baseAsset.denom))?.amount ?? '0', + [walletBalances, baseAsset], + ) const handleSelectAssetsClick = useCallback(() => { useStore.setState({ walletAssetsModal: { isOpen: true, selectedDenoms, + isBorrow: false, }, }) }, [selectedDenoms]) + const handleClick = useCallback(async () => { + setIsFunding(true) + if (!props.accountId) return + const result = await deposit({ + accountId: props.accountId, + coins: fundingAssets, + lend: isLending, + }) + setIsFunding(false) + if (result) + useStore.setState({ + fundAndWithdrawModal: null, + walletAssetsModal: null, + focusComponent: null, + }) + }, [setIsFunding, props.accountId, deposit, fundingAssets, isLending]) + + useEffect(() => { + if (BN(baseBalance).isLessThan(defaultFee.amount[0].amount)) { + useStore.setState({ focusComponent: { component: } }) + } + }, [baseBalance]) + + useEffect(() => { + simulateDeposits(isLending ? 'lend' : 'deposit', fundingAssets) + }, [isLending, fundingAssets, simulateDeposits]) + useEffect(() => { const currentSelectedDenom = fundingAssets.map((asset) => asset.denom) @@ -101,15 +121,8 @@ export default function AccountFund() { }, []) useEffect(() => { - if (BN(baseBalance).isLessThan(defaultFee.amount[0].amount)) { - useStore.setState({ focusComponent: { component: } }) - } - }, [baseBalance]) - - useEffect(() => { - if (accounts && !selectedAccountId && accountId) - setSelectedAccountId(currentAccount?.id ?? accountId) - }, [accounts, selectedAccountId, accountId, currentAccount]) + toggleIsLending(autoLendEnabledAccountIds.includes(props.accountId)) + }, [props.accountId, autoLendEnabledAccountIds, toggleIsLending]) const depositCapReachedCoins = useMemo(() => { const depositCapReachedCoins: BNCoin[] = [] @@ -126,15 +139,9 @@ export default function AccountFund() { return depositCapReachedCoins }, [fundingAssets, marketAssets]) - if (!selectedAccountId) return null - return ( - - + <> +
{!hasAssetSelected && Please select an asset.} {fundingAssets.map((coin) => { const asset = getAssetByDenom(coin.denom) as Asset @@ -143,7 +150,10 @@ export default function AccountFund() { return (
-
+
) -} +} \ No newline at end of file diff --git a/src/components/Account/AccountMenuContent.tsx b/src/components/Account/AccountMenuContent.tsx index f1d78c9f..2c00c0bd 100644 --- a/src/components/Account/AccountMenuContent.tsx +++ b/src/components/Account/AccountMenuContent.tsx @@ -3,7 +3,7 @@ import { useCallback, useEffect } from 'react' import { useLocation, useNavigate, useParams } from 'react-router-dom' import AccountCreateFirst from 'components/Account/AccountCreateFirst' -import AccountFund from 'components/Account/AccountFund' +import AccountFund from 'components/Account/AccountFund/AccountFundFullPage' import AccountList from 'components/Account/AccountList' import Button from 'components/Button' import { CircularProgress } from 'components/CircularProgress' @@ -164,4 +164,4 @@ export default function AccountMenuContent(props: Props) { ) } -export { ACCOUNT_MENU_BUTTON_ID } +export { ACCOUNT_MENU_BUTTON_ID } \ No newline at end of file diff --git a/src/components/Account/AccountSummary.tsx b/src/components/Account/AccountSummary.tsx index 5a5a50f2..51ad9039 100644 --- a/src/components/Account/AccountSummary.tsx +++ b/src/components/Account/AccountSummary.tsx @@ -126,4 +126,4 @@ function Item(props: ItemProps) {
{props.children}
) -} \ No newline at end of file +} diff --git a/src/components/Modals/Account/AccountDeleteAlertDialog.tsx b/src/components/Modals/Account/AccountDeleteAlertDialog.tsx index 7dc791d6..750f68f7 100644 --- a/src/components/Modals/Account/AccountDeleteAlertDialog.tsx +++ b/src/components/Modals/Account/AccountDeleteAlertDialog.tsx @@ -24,7 +24,7 @@ export default function AccoundDeleteAlertDialog(props: Props) { title, description, negativeButton: { - text: 'Close', + text: 'Cancel', icon: , onClick: closeHandler, }, @@ -33,4 +33,4 @@ export default function AccoundDeleteAlertDialog(props: Props) { }, [showAlertDialog, title, description, closeHandler, positiveButton]) return null -} +} \ No newline at end of file diff --git a/src/components/Modals/Account/AccountDeleteModal.tsx b/src/components/Modals/Account/AccountDeleteModal.tsx index 69b95354..65ecd464 100644 --- a/src/components/Modals/Account/AccountDeleteModal.tsx +++ b/src/components/Modals/Account/AccountDeleteModal.tsx @@ -95,7 +95,7 @@ function AccountDeleteModal(props: Props) { description='Deleting your credit account is irreversible.' closeHandler={closeDeleteAccountModal} positiveButton={{ - text: 'Close Positions', + text: 'Delete Account', icon: , isAsync: true, onClick: deleteAccountHandler, @@ -140,4 +140,4 @@ function AccountDeleteModal(props: Props) { ) -} +} \ No newline at end of file diff --git a/src/components/Modals/FundWithdraw/FundAccount.tsx b/src/components/Modals/FundWithdraw/FundAccount.tsx index ef5ab596..81b8da23 100644 --- a/src/components/Modals/FundWithdraw/FundAccount.tsx +++ b/src/components/Modals/FundWithdraw/FundAccount.tsx @@ -1,25 +1,5 @@ -import { useCallback, useEffect, useMemo, useState } from 'react' - -import Button from 'components/Button' -import DepositCapMessage from 'components/DepositCapMessage' -import { ArrowRight, Plus } from 'components/Icons' -import SwitchAutoLend from 'components/Switch/SwitchAutoLend' -import Text from 'components/Text' -import TokenInputWithSlider from 'components/TokenInput/TokenInputWithSlider' -import WalletBridges from 'components/Wallet/WalletBridges' -import { BN_ZERO } from 'constants/math' -import useAutoLend from 'hooks/useAutoLend' -import useMarketAssets from 'hooks/useMarketAssets' -import useToggle from 'hooks/useToggle' -import { useUpdatedAccount } from 'hooks/useUpdatedAccount' -import useWalletBalances from 'hooks/useWalletBalances' +import AccountFundContent from 'components/Account/AccountFund/AccountFundContent' import useStore from 'store' -import { BNCoin } from 'types/classes/BNCoin' -import { byDenom } from 'utils/array' -import { getAssetByDenom, getBaseAsset } from 'utils/assets' -import { defaultFee } from 'utils/constants' -import { getCapLeftWithBuffer } from 'utils/generic' -import { BN } from 'utils/helpers' interface Props { account: Account @@ -27,161 +7,10 @@ interface Props { export default function FundAccount(props: Props) { const { account } = props - const accountId = account.id + const address = useStore((s) => s.address) - const deposit = useStore((s) => s.deposit) - const walletAssetModal = useStore((s) => s.walletAssetsModal) - const [isFunding, setIsFunding] = useToggle(false) - const [fundingAssets, setFundingAssets] = useState([]) - const { data: walletBalances } = useWalletBalances(address) - const baseAsset = getBaseAsset() - const hasAssetSelected = fundingAssets.length > 0 - const hasFundingAssets = - fundingAssets.length > 0 && fundingAssets.every((a) => a.toCoin().amount !== '0') - const { autoLendEnabledAccountIds } = useAutoLend() - const [isLending, toggleIsLending] = useToggle(false) - const { simulateDeposits } = useUpdatedAccount(account) - const { data: marketAssets } = useMarketAssets() - const baseBalance = useMemo( - () => walletBalances.find(byDenom(baseAsset.denom))?.amount ?? '0', - [walletBalances, baseAsset], - ) + const accountId = account.id + if (!address) return null - const balances = walletBalances.map((coin) => new BNCoin(coin)) - - const selectedDenoms = useMemo(() => { - return walletAssetModal?.selectedDenoms ?? [] - }, [walletAssetModal?.selectedDenoms]) - - const handleClick = useCallback(async () => { - setIsFunding(true) - if (!accountId) return - const result = await deposit({ - accountId, - coins: fundingAssets, - lend: isLending, - }) - setIsFunding(false) - if (result) useStore.setState({ fundAndWithdrawModal: null, walletAssetsModal: null }) - }, [setIsFunding, accountId, deposit, fundingAssets, isLending]) - - const handleSelectAssetsClick = useCallback(() => { - useStore.setState({ - walletAssetsModal: { - isOpen: true, - selectedDenoms, - isBorrow: false, - }, - }) - }, [selectedDenoms]) - - useEffect(() => { - toggleIsLending(autoLendEnabledAccountIds.includes(accountId)) - }, [accountId, autoLendEnabledAccountIds, toggleIsLending]) - - useEffect(() => { - const currentSelectedDenom = fundingAssets.map((asset) => asset.denom) - - if ( - selectedDenoms.every((denom) => currentSelectedDenom.includes(denom)) && - selectedDenoms.length === currentSelectedDenom.length - ) - return - - const newFundingAssets = selectedDenoms.map((denom) => - BNCoin.fromDenomAndBigNumber(denom, BN(fundingAssets.find(byDenom(denom))?.amount ?? '0')), - ) - - setFundingAssets(newFundingAssets) - }, [selectedDenoms, fundingAssets]) - - const updateFundingAssets = useCallback((amount: BigNumber, denom: string) => { - setFundingAssets((fundingAssets) => { - const updateIdx = fundingAssets.findIndex(byDenom(denom)) - if (updateIdx === -1) return fundingAssets - - fundingAssets[updateIdx].amount = amount - return [...fundingAssets] - }) - }, []) - - useEffect(() => { - simulateDeposits(isLending ? 'lend' : 'deposit', fundingAssets) - }, [isLending, fundingAssets, simulateDeposits]) - - useEffect(() => { - if (BN(baseBalance).isLessThan(defaultFee.amount[0].amount)) { - useStore.setState({ focusComponent: { component: } }) - } - }, [baseBalance]) - - const depositCapReachedCoins = useMemo(() => { - const depositCapReachedCoins: BNCoin[] = [] - fundingAssets.forEach((asset) => { - const marketAsset = marketAssets.find(byDenom(asset.denom)) - if (!marketAsset) return - - const capLeft = getCapLeftWithBuffer(marketAsset.cap) - - if (asset.amount.isLessThanOrEqualTo(capLeft)) return - - depositCapReachedCoins.push(BNCoin.fromDenomAndBigNumber(asset.denom, capLeft)) - }) - return depositCapReachedCoins - }, [fundingAssets, marketAssets]) - - return ( - <> -
- {!hasAssetSelected && Please select an asset.} - {fundingAssets.map((coin) => { - const asset = getAssetByDenom(coin.denom) as Asset - - const balance = balances.find(byDenom(coin.denom))?.amount ?? BN_ZERO - return ( - updateFundingAssets(amount, asset.denom)} - amount={coin.amount ?? BN_ZERO} - max={balance} - balances={balances} - maxText='Max' - disabled={isFunding} - className='w-full mb-4' - /> - ) - })} -
-