import React, { useMemo, useState } from 'react' import { XMarkIcon } from '@heroicons/react/24/solid' import { toast } from 'react-toastify' import Button from 'components/Button' import Container from 'components/Container' import { getTokenDecimals, getTokenSymbol } from 'utils/tokens' import useBorrowFunds from 'hooks/useBorrowFunds' import useTokenPrices from 'hooks/useTokenPrices' import { formatCurrency } from 'utils/formatters' import { Switch } from '@headlessui/react' import BigNumber from 'bignumber.js' import useAllBalances from 'hooks/useAllBalances' import useMarkets from 'hooks/useMarkets' import Tooltip from 'components/Tooltip' import ContainerSecondary from 'components/ContainerSecondary' import Spinner from 'components/Spinner' import useCalculateMaxBorrowAmount from 'hooks/useCalculateMaxBorrowAmount' import Slider from 'components/Slider' const BorrowFunds = ({ tokenDenom, onClose }: any) => { const [amount, setAmount] = useState(0) const [borrowToCreditAccount, setBorrowToCreditAccount] = useState(false) const tokenSymbol = getTokenSymbol(tokenDenom) const { mutate, isLoading } = useBorrowFunds(amount, tokenDenom, !borrowToCreditAccount, { onSuccess: () => { onClose() toast.success(`${amount} ${tokenSymbol} successfully Borrowed`) }, }) const { data: tokenPrices } = useTokenPrices() const { data: balancesData } = useAllBalances() const { data: marketsData } = useMarkets() const handleSubmit = () => { mutate() } const walletAmount = useMemo(() => { return BigNumber(balancesData?.find((balance) => balance.denom === tokenDenom)?.amount ?? 0) .div(10 ** getTokenDecimals(tokenDenom)) .toNumber() }, [balancesData, tokenDenom]) const tokenPrice = tokenPrices?.[tokenDenom] ?? 0 const borrowRate = Number(marketsData?.[tokenDenom].borrow_rate) const maxValue = useCalculateMaxBorrowAmount(tokenDenom, borrowToCreditAccount) const percentageValue = useMemo(() => { if (isNaN(amount) || maxValue === 0) return 0 return (amount * 100) / maxValue }, [amount, maxValue]) const isSubmitDisabled = !amount || amount < 0 const handleValueChange = (value: number) => { if (value > maxValue) { setAmount(maxValue) return } setAmount(value) } const handleBorrowTargetChange = () => { setBorrowToCreditAccount((c) => !c) // reset amount due to max value calculations changing depending on borrow target setAmount(0) } return ( {isLoading && (
)}

Borrow {tokenSymbol}

In wallet:{' '} {walletAmount.toLocaleString()} {tokenSymbol}

Borrow Rate: {(borrowRate * 100).toFixed(2)}%

Amount
handleValueChange(e.target.valueAsNumber)} />
1 {tokenSymbol} ={' '} {formatCurrency(tokenPrice)}
{formatCurrency(tokenPrice * amount)}
{ const decimal = value[0] / 100 const tokenDecimals = getTokenDecimals(tokenDenom) // limit decimal precision based on token contract decimals const newAmount = Number((decimal * maxValue).toFixed(tokenDecimals)) setAmount(newAmount) }} onMaxClick={() => setAmount(maxValue)} />
Borrow to Credit Account{' '}

OFF = Borrow directly into your wallet by using your account Assets as collateral. The borrowed asset will become a liability in your account.

ON = Borrow into your Account. The borrowed asset will be available in the account as an Asset and appear also as a liability in your account.

} />
) } export default BorrowFunds