import { Dialog, Transition } from '@headlessui/react' import BigNumber from 'bignumber.js' import Image from 'next/image' import React, { useMemo, useState } from 'react' import { NumericFormat } from 'react-number-format' import { toast } from 'react-toastify' import Slider from 'components/Slider' import useRepayFunds from 'hooks/mutations/useRepayFunds' import useAllBalances from 'hooks/useAllBalances' import useCreditAccountPositions from 'hooks/useCreditAccountPositions' import useTokenPrices from 'hooks/useTokenPrices' import useCreditManagerStore from 'stores/useCreditManagerStore' import { formatCurrency } from 'utils/formatters' import { getTokenDecimals, getTokenSymbol } from 'utils/tokens' import Button from 'components/Button' import CircularProgress from 'components/CircularProgress' import ContainerSecondary from 'components/ContainerSecondary' // 0.001% buffer / slippage to avoid repay action from not fully repaying the debt amount const REPAY_BUFFER = 1.00001 type Props = { show: boolean onClose: () => void tokenDenom: string } const RepayModal = ({ show, onClose, tokenDenom }: Props) => { const [amount, setAmount] = useState(0) const selectedAccount = useCreditManagerStore((s) => s.selectedAccount) const { data: positionsData } = useCreditAccountPositions(selectedAccount ?? '') const tokenSymbol = getTokenSymbol(tokenDenom) const maxRepayAmount = useMemo(() => { const tokenDebtAmount = positionsData?.debts.find((coin) => coin.denom === tokenDenom)?.amount ?? 0 return BigNumber(tokenDebtAmount) .times(REPAY_BUFFER) .decimalPlaces(0) .div(10 ** getTokenDecimals(tokenDenom)) .toNumber() }, [positionsData, tokenDenom]) const { mutate, isLoading } = useRepayFunds( BigNumber(amount) .times(10 ** getTokenDecimals(tokenDenom)) .toNumber(), tokenDenom, { onSuccess: () => { onClose() toast.success(`${amount} ${tokenSymbol} successfully repaid`) }, }, ) const { data: tokenPrices } = useTokenPrices() const { data: balancesData } = useAllBalances() 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 maxValue = walletAmount > maxRepayAmount ? maxRepayAmount : walletAmount const percentageValue = isNaN(amount) ? 0 : (amount * 100) / maxValue const handleValueChange = (value: number) => { if (value > maxValue) { setAmount(maxValue) return } setAmount(value) } return (
{isLoading && (
)}

About

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

mars
Repay {tokenSymbol}

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

Amount

handleValueChange(v.floatValue || 0)} suffix={` ${tokenSymbol}`} decimalScale={getTokenDecimals(tokenDenom)} />
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)} />
) } export default RepayModal