import React, { useMemo, useState } from 'react' import Image from 'next/image' import { Transition, Dialog, Switch } from '@headlessui/react' import BigNumber from 'bignumber.js' import { toast } from 'react-toastify' import { NumericFormat } from 'react-number-format' import { getTokenDecimals, getTokenSymbol } from 'utils/tokens' import ContainerSecondary from './ContainerSecondary' import Button from './Button' import Spinner from './Spinner' import useAllBalances from 'hooks/useAllBalances' import Slider from 'components/Slider' import useBorrowFunds from 'hooks/mutations/useBorrowFunds' import useTokenPrices from 'hooks/useTokenPrices' import useMarkets from 'hooks/useMarkets' import useCalculateMaxBorrowAmount from 'hooks/useCalculateMaxBorrowAmount' import Tooltip from './Tooltip' import { formatCurrency } from 'utils/formatters' type Props = { show: boolean onClose: () => void tokenDenom: string } const BorrowModal = ({ show, onClose, tokenDenom }: Props) => { const [amount, setAmount] = useState(0) const [isBorrowToCreditAccount, setIsBorrowToCreditAccount] = useState(false) const tokenSymbol = getTokenSymbol(tokenDenom) const { mutate, isLoading } = useBorrowFunds( BigNumber(amount) .times(10 ** getTokenDecimals(tokenDenom)) .toNumber(), tokenDenom, !isBorrowToCreditAccount, { 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, isBorrowToCreditAccount) const percentageValue = useMemo(() => { if (isNaN(amount) || maxValue === 0) return 0 return (amount * 100) / maxValue }, [amount, maxValue]) const handleValueChange = (value: number) => { if (value > maxValue) { setAmount(maxValue) return } setAmount(value) } const handleSliderValueChange = (value: number[]) => { 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) } const handleBorrowTargetChange = () => { setIsBorrowToCreditAccount((c) => !c) // reset amount due to max value calculations changing depending on borrow target setAmount(0) } return (
{isLoading && (
)}

About

Bringing the next generation of video creation to the Metaverse.
Powered by deep-learning.

mars
Borrow {tokenSymbol}

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

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

Amount

handleValueChange(v.floatValue || 0)} suffix={` ${tokenSymbol}`} decimalScale={getTokenDecimals(tokenDenom)} />
1 {tokenSymbol} = {formatCurrency(tokenPrice)}
{formatCurrency(tokenPrice * amount)}
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 BorrowModal