assembly/components/sidebar/context/liquity/SidebarLiquityTrovePayback.vue

227 lines
7.1 KiB
Vue
Raw Normal View History

2021-08-21 12:36:15 +00:00
<template>
<SidebarContextRootContainer>
2021-08-21 12:59:26 +00:00
<template #title>Payback {{ debtToken.symbol }}</template>
2021-08-21 13:10:16 +00:00
<div class="flex justify-around items-center w-full">
2021-08-21 12:59:26 +00:00
<SidebarSectionValueWithIcon class="" label="Debt" center>
2021-08-21 12:36:15 +00:00
<template #icon
2021-08-21 13:10:16 +00:00
><IconCurrency :currency="debtToken.key" class="w-16 h-16" noHeight
2021-08-21 12:36:15 +00:00
/></template>
2021-08-23 19:57:11 +00:00
<template #value
>{{ formatDecimal(changedDebt) }} {{ debtToken.symbol }}</template
>
2021-08-21 12:36:15 +00:00
</SidebarSectionValueWithIcon>
<SidebarSectionValueWithIcon class="" label="Token Balance" center>
<template #icon
2021-08-21 13:10:16 +00:00
><IconCurrency :currency="debtToken.key" class="w-16 h-16" noHeight
2021-08-21 12:36:15 +00:00
/></template>
2021-08-23 19:57:11 +00:00
<template #value
>{{ formatDecimal(changedBalance) }} {{ debtToken.symbol }}</template
>
2021-08-21 12:36:15 +00:00
</SidebarSectionValueWithIcon>
</div>
<div class="bg-[#C5CCE1] bg-opacity-[0.15] mt-10 p-8">
<h3 class="text-primary-gray text-xs font-semibold mb-2.5">
2021-08-21 12:59:26 +00:00
Amount to payback
2021-08-21 12:36:15 +00:00
</h3>
<input-numeric
v-model="amount"
2021-08-21 12:59:26 +00:00
placeholder="Amount to payback"
2021-08-21 12:36:15 +00:00
:error="errors.amount.message"
>
<template v-if="!isMaxAmount" #suffix>
<div class="absolute mt-2 top-0 right-0 mr-4">
<button
type="button"
class="text-primary-blue-dark font-semibold text-sm hover:text-primary-blue-hover"
@click="toggle"
>
Max
</button>
</div>
</template>
</input-numeric>
<SidebarContextHeading class="mt-5">
Projected Debt Position
</SidebarContextHeading>
<SidebarSectionStatus
class="mt-8"
2021-08-21 12:59:26 +00:00
:liquidation="liquidation"
2021-08-21 12:36:15 +00:00
:status="status"
/>
2021-08-21 12:59:26 +00:00
<SidebarSectionValueWithIcon
class="mt-8"
:label="`Liquidation Price (${collateralToken.symbol})`"
>
2021-08-21 12:36:15 +00:00
<template #value>
{{ formatUsdMax(liquidationPrice, liquidationMaxPrice) }}
<span class="text-primary-gray"
>/ {{ formatUsd(liquidationMaxPrice) }}</span
>
</template>
</SidebarSectionValueWithIcon>
<div class="flex flex-shrink-0 mt-10">
<ButtonCTA
class="w-full"
:disabled="!isValid || pending"
:loading="pending"
@click="cast"
>
Payback
</ButtonCTA>
</div>
<ValidationErrors :error-messages="errorMessages" class="mt-6" />
</div>
</SidebarContextRootContainer>
</template>
<script>
import { computed, defineComponent, ref } from '@nuxtjs/composition-api'
import InputNumeric from '~/components/common/input/InputNumeric.vue'
import { useBalances } from '~/composables/useBalances'
import { useBigNumber } from '~/composables/useBigNumber'
import { useFormatting } from '~/composables/useFormatting'
import { useValidators } from '~/composables/useValidators'
import { useValidation } from '~/composables/useValidation'
import { useToken } from '~/composables/useToken'
import { useParsing } from '~/composables/useParsing'
import { useMaxAmountActive } from '~/composables/useMaxAmountActive'
import { useWeb3 } from '~/composables/useWeb3'
import ToggleButton from '~/components/common/input/ToggleButton.vue'
import { useDSA } from '~/composables/useDSA'
import ButtonCTA from '~/components/common/input/ButtonCTA.vue'
import { useNotification } from '~/composables/useNotification'
import Button from '~/components/Button.vue'
import { useSidebar } from '~/composables/useSidebar'
2021-08-21 12:59:26 +00:00
import { useLiquityPosition } from '~/composables/protocols/useLiquityPosition'
2021-08-21 12:36:15 +00:00
export default defineComponent({
components: { InputNumeric, ToggleButton, ButtonCTA, Button },
2021-08-21 12:59:26 +00:00
setup() {
2021-08-21 12:36:15 +00:00
const { close } = useSidebar()
const { networkName, account } = useWeb3()
const { dsa } = useDSA()
2021-08-21 12:59:26 +00:00
const { valInt } = useToken()
2021-08-23 19:57:11 +00:00
const { getBalanceByKey, fetchBalances } = useBalances()
2021-08-21 12:59:26 +00:00
const { formatDecimal, formatUsdMax, formatUsd } = useFormatting()
const { isZero, gte, plus, max, minus, min } = useBigNumber()
2021-08-21 12:36:15 +00:00
const { parseSafeFloat } = useParsing()
2021-08-23 19:57:11 +00:00
const { showPendingTransaction, showConfirmedTransaction, showWarning } = useNotification()
2021-08-21 12:36:15 +00:00
const amount = ref('')
const amountParsed = computed(() => parseSafeFloat(amount.value))
2021-08-21 12:59:26 +00:00
const {
debt,
collateral,
collateralInWei,
liquidation,
liquidationMaxPrice,
debtToken,
collateralToken,
getTrovePositionHints,
2021-08-23 19:57:11 +00:00
fetchPosition,
2021-08-21 12:59:26 +00:00
} = useLiquityPosition()
2021-08-21 12:36:15 +00:00
2021-08-21 12:59:26 +00:00
const balance = computed(() => getBalanceByKey(debtToken.value.key))
2021-08-21 12:36:15 +00:00
2021-08-21 12:59:26 +00:00
const changedDebt = computed(() => max(minus(debt.value, amountParsed.value), '0').toFixed())
const changedBalance = computed(() => max(minus(balance.value, amountParsed.value), '0').toFixed())
2021-08-21 12:36:15 +00:00
2021-08-21 12:59:26 +00:00
const { liquidationPrice, status } = useLiquityPosition(collateral, changedDebt)
const { validateAmount, validateIsLoggedIn, validateLiquityDebt, validateLiquityTroveExists } = useValidators()
2021-08-21 12:36:15 +00:00
2021-08-21 12:59:26 +00:00
const maxBalance = computed(() => min(balance.value, debt.value).toFixed())
const { toggle, isMaxAmount } = useMaxAmountActive(amount, maxBalance)
2021-08-21 12:36:15 +00:00
const errors = computed(() => {
const hasAmountValue = !isZero(amount.value)
return {
2021-08-21 12:59:26 +00:00
troveExists: { message: validateLiquityTroveExists(), show: true },
amount: { message: validateAmount(amountParsed.value, maxBalance.value), show: hasAmountValue },
minDebt: { message: validateLiquityDebt(changedDebt.value), show: hasAmountValue },
2021-08-21 12:36:15 +00:00
auth: { message: validateIsLoggedIn(!!account.value), show: true },
}
})
const { errorMessages, isValid } = useValidation(errors)
const pending = ref(false)
async function cast() {
pending.value = true
2021-08-21 13:10:16 +00:00
const inputAmountInWei = valInt(amountParsed.value, debtToken.value.decimals)
2021-08-21 12:59:26 +00:00
const totalDebtAmountInWei = valInt(changedDebt.value, debtToken.value.decimals)
const { upperHint, lowerHint } = await getTrovePositionHints(collateralInWei.value, totalDebtAmountInWei)
2021-08-21 12:36:15 +00:00
2021-08-21 12:59:26 +00:00
const getId = 0
const setId = 0
2021-08-21 12:36:15 +00:00
2021-08-21 12:59:26 +00:00
const spells = dsa.value.Spell()
2021-08-21 12:36:15 +00:00
spells.add({
2021-08-21 12:59:26 +00:00
connector: 'LIQUITY-A',
method: 'repay',
args: [inputAmountInWei, upperHint, lowerHint, getId, setId],
2021-08-21 12:36:15 +00:00
})
2021-08-21 12:59:26 +00:00
2021-08-21 12:36:15 +00:00
try {
const txHash = await dsa.value.cast({
spells,
from: account.value,
2021-08-23 19:57:11 +00:00
onReceipt: async receipt => {
showConfirmedTransaction(receipt.transactionHash);
await fetchBalances(true);
await fetchPosition();
}
2021-08-21 12:36:15 +00:00
})
showPendingTransaction(txHash)
} catch (error) {
showWarning(error.message)
}
pending.value = false
close()
}
return {
2021-08-21 12:59:26 +00:00
debt,
balance,
2021-08-21 12:36:15 +00:00
amount,
status,
2021-08-21 12:59:26 +00:00
liquidation,
2021-08-21 12:36:15 +00:00
liquidationPrice,
liquidationMaxPrice,
2021-08-21 12:59:26 +00:00
formatUsd,
formatUsdMax,
formatDecimal,
errors,
2021-08-21 12:36:15 +00:00
errorMessages,
2021-08-21 12:59:26 +00:00
isMaxAmount,
2021-08-21 12:36:15 +00:00
isValid,
2021-08-21 12:59:26 +00:00
cast,
pending,
toggle,
debtToken,
collateralToken,
changedDebt,
changedBalance,
2021-08-21 12:36:15 +00:00
}
},
})
</script>