import { ArrowDownTrayIcon, ArrowPathIcon, ExclamationCircleIcon, } from '@heroicons/react/20/solid' import { useWallet } from '@solana/wallet-adapter-react' import { useTranslation } from 'next-i18next' import React, { useCallback, useMemo, useState } from 'react' import NumberFormat, { NumberFormatValues } from 'react-number-format' import mangoStore from '@store/mangoStore' import { ACCOUNT_ACTION_MODAL_INNER_HEIGHT, INPUT_TOKEN_DEFAULT, } from './../utils/constants' import { notify } from './../utils/notifications' import { TokenAccount } from './../utils/tokens' import ActionTokenList from './account/ActionTokenList' import ButtonGroup from './forms/ButtonGroup' import Label from './forms/Label' import Button, { IconButton } from './shared/Button' import Loading from './shared/Loading' import { EnterBottomExitBottom, FadeInFadeOut } from './shared/Transitions' import { withValueLimit } from './swap/MarketSwapForm' import MaxAmountButton from '@components/shared/MaxAmountButton' import Tooltip from '@components/shared/Tooltip' import HealthImpactTokenChange from '@components/HealthImpactTokenChange' import SolBalanceWarnings from '@components/shared/SolBalanceWarnings' import useSolBalance from 'hooks/useSolBalance' import FormatNumericValue from './shared/FormatNumericValue' import Decimal from 'decimal.js' import { floorToDecimal } from 'utils/numbers' import BankAmountWithValue from './shared/BankAmountWithValue' import useBanksWithBalances from 'hooks/useBanksWithBalances' import { isMangoError } from 'types' import TokenListButton from './shared/TokenListButton' import { ACCOUNT_ACTIONS_NUMBER_FORMAT_CLASSES, BackButton } from './BorrowForm' import TokenLogo from './shared/TokenLogo' import SecondaryConnectButton from './shared/SecondaryConnectButton' import useMangoAccountAccounts from 'hooks/useMangoAccountAccounts' import InlineNotification from './shared/InlineNotification' import Link from 'next/link' interface DepositFormProps { onSuccess: () => void token?: string } export const walletBalanceForToken = ( walletTokens: TokenAccount[], token: string, ): { maxAmount: number; maxDecimals: number } => { const group = mangoStore.getState().group const bank = group?.banksMapByName.get(token)?.[0] let walletToken if (bank) { const tokenMint = bank?.mint walletToken = tokenMint ? walletTokens.find((t) => t.mint.toString() === tokenMint.toString()) : null } return { maxAmount: walletToken ? walletToken.uiAmount : 0, maxDecimals: bank?.mintDecimals || 6, } } function DepositForm({ onSuccess, token }: DepositFormProps) { const { t } = useTranslation(['common', 'account']) const [inputAmount, setInputAmount] = useState('') const [submitting, setSubmitting] = useState(false) const [selectedToken, setSelectedToken] = useState( token || INPUT_TOKEN_DEFAULT, ) const [showTokenList, setShowTokenList] = useState(false) const [sizePercentage, setSizePercentage] = useState('') const [refreshingWalletTokens, setRefreshingWalletTokens] = useState(false) const { maxSolDeposit } = useSolBalance() const banks = useBanksWithBalances('walletBalance') const { usedTokens, totalTokens } = useMangoAccountAccounts() const bank = useMemo(() => { const group = mangoStore.getState().group return group?.banksMapByName.get(selectedToken)?.[0] }, [selectedToken]) const tokenPositionsFull = useMemo(() => { if (!bank || !usedTokens.length || !totalTokens.length) return false const hasTokenPosition = usedTokens.find( (token) => token.tokenIndex === bank.tokenIndex, ) return hasTokenPosition ? false : usedTokens.length >= totalTokens.length }, [bank, usedTokens, totalTokens]) const { connected, publicKey } = useWallet() const walletTokens = mangoStore((s) => s.wallet.tokens) const tokenMax = useMemo(() => { return walletBalanceForToken(walletTokens, selectedToken) }, [walletTokens, selectedToken]) const setMax = useCallback(() => { const max = floorToDecimal(tokenMax.maxAmount, tokenMax.maxDecimals) setInputAmount(max.toFixed()) setSizePercentage('100') }, [tokenMax]) const handleSizePercentage = useCallback( (percentage: string) => { setSizePercentage(percentage) const amount = floorToDecimal( new Decimal(tokenMax.maxAmount).mul(percentage).div(100), tokenMax.maxDecimals, ) setInputAmount(amount.toFixed()) }, [tokenMax], ) const handleSelectToken = (token: string) => { setSelectedToken(token) setShowTokenList(false) } const handleRefreshWalletBalances = useCallback(async () => { if (!publicKey) return const actions = mangoStore.getState().actions setRefreshingWalletTokens(true) await actions.fetchWalletTokens(publicKey) setRefreshingWalletTokens(false) }, [publicKey]) const handleDeposit = useCallback(async () => { const client = mangoStore.getState().client const group = mangoStore.getState().group const actions = mangoStore.getState().actions const mangoAccount = mangoStore.getState().mangoAccount.current if (!mangoAccount || !group || !bank || !publicKey) return setSubmitting(true) try { const { signature: tx, slot } = await client.tokenDeposit( group, mangoAccount, bank.mint, parseFloat(inputAmount), ) notify({ title: 'Transaction confirmed', type: 'success', txid: tx, }) await actions.reloadMangoAccount(slot) actions.fetchWalletTokens(publicKey) setSubmitting(false) onSuccess() } catch (e) { console.error('Error depositing:', e) setSubmitting(false) if (!isMangoError(e)) return notify({ title: 'Transaction failed', description: e.message, txid: e?.txid, type: 'error', }) } }, [bank, publicKey, inputAmount]) const showInsufficientBalance = tokenMax.maxAmount < Number(inputAmount) || (selectedToken === 'SOL' && maxSolDeposit <= 0) return ( <> setShowTokenList(false)} />

{t('select-deposit-token')}

{t('token')}

{t('deposit-rate')}

{t('wallet-balance')}

} setShowList={setShowTokenList} />
{ setInputAmount( !Number.isNaN(Number(e.value)) ? e.value : '', ) }} isAllowed={withValueLimit} />
handleSizePercentage(p)} values={['10', '25', '50', '75', '100']} unit="%" />
{bank ? (

{t('deposit-amount')}

{t('collateral-value')}

) : null}
{connected ? ( ) : ( )} {tokenPositionsFull ? ( {t('error-token-positions-full')}{' '} onSuccess()} shallow> {t('manage')} } /> ) : null}
) } export default DepositForm