import { Switch } from '@headlessui/react' import BigNumber from 'bignumber.js' import { useEffect, useMemo, useState } from 'react' import { toast } from 'react-toastify' import useLocalStorageState from 'use-local-storage-state' import Button from 'components/Button' import CircularProgress from 'components/CircularProgress' import MarsProtocolLogo from 'components/Icons/mars-protocol.svg' import Modal from 'components/Modal' import Slider from 'components/Slider' import Text from 'components/Text' import useDepositCreditAccount from 'hooks/mutations/useDepositCreditAccount' import useAllBalances from 'hooks/useAllBalances' import useAllowedCoins from 'hooks/useAllowedCoins' import useCreditManagerStore from 'stores/useCreditManagerStore' import { getTokenDecimals, getTokenSymbol } from 'utils/tokens' interface Props { open: boolean setOpen: (open: boolean) => void } const FundAccountModal = ({ open, setOpen }: Props) => { const [amount, setAmount] = useState(0) const [selectedToken, setSelectedToken] = useState('') const selectedAccount = useCreditManagerStore((s) => s.selectedAccount) const [lendAssets, setLendAssets] = useLocalStorageState(`lendAssets_${selectedAccount}`, { defaultValue: false, }) const { data: balancesData } = useAllBalances() const { data: allowedCoinsData, isLoading: isLoadingAllowedCoins } = useAllowedCoins() const { mutate, isLoading } = useDepositCreditAccount( selectedAccount || '', selectedToken, BigNumber(amount) .times(10 ** getTokenDecimals(selectedToken)) .toNumber(), { onSuccess: () => { setAmount(0) toast.success(`${amount} ${getTokenSymbol(selectedToken)} successfully Deposited`) setOpen(false) }, }, ) useEffect(() => { if (allowedCoinsData && allowedCoinsData.length > 0) { // initialize selected token when allowedCoins fetch data is available setSelectedToken(allowedCoinsData[0]) } }, [allowedCoinsData]) const walletAmount = useMemo(() => { if (!selectedToken) return 0 return BigNumber(balancesData?.find((balance) => balance.denom === selectedToken)?.amount ?? 0) .div(10 ** getTokenDecimals(selectedToken)) .toNumber() }, [balancesData, selectedToken]) const handleValueChange = (value: number) => { if (value > walletAmount) { setAmount(walletAmount) return } setAmount(value) } const maxValue = walletAmount const percentageValue = isNaN(amount) ? 0 : (amount * 100) / maxValue return (
{isLoading && (
)}
About Bringing the next generation of video creation to the Metaverse.
Powered by deep-learning.
Account {selectedAccount}
Fund Account Transfer assets from your injective wallet to your Mars credit account. If you don’t have any assets in your injective wallet use the injective bridge to transfer funds to your injective wallet. {isLoadingAllowedCoins ? (

Loading...

) : ( <>
Asset:
Amount: handleValueChange(e.target.valueAsNumber)} onBlur={(e) => { if (e.target.value === '') setAmount(0) }} />
{`In wallet: ${walletAmount.toLocaleString()} ${getTokenSymbol(selectedToken)}`} { const decimal = value[0] / 100 const tokenDecimals = getTokenDecimals(selectedToken) // limit decimal precision based on token contract decimals const newAmount = Number((decimal * maxValue).toFixed(tokenDecimals)) setAmount(newAmount) }} onMaxClick={() => setAmount(maxValue)} /> )}
Lending Assets Lend assets from account to earn yield.
) } export default FundAccountModal