mirror of
https://github.com/Instadapp/assembly.git
synced 2024-07-29 22:37:06 +00:00
Liquity Trove Payback
This commit is contained in:
parent
b5d680c043
commit
6ee3b7731f
|
|
@ -1,42 +1,32 @@
|
||||||
<template>
|
<template>
|
||||||
<SidebarContextRootContainer>
|
<SidebarContextRootContainer>
|
||||||
<template #title>Payback {{ symbol }}</template>
|
<template #title>Payback {{ debtToken.symbol }}</template>
|
||||||
|
|
||||||
<SidebarRateTypeSelect
|
<div class="mt-6 flex justify-around items-center w-full">
|
||||||
class="flex flex-col items-center"
|
<SidebarSectionValueWithIcon class="" label="Debt" center>
|
||||||
v-model="rateType"
|
|
||||||
:items="annualPercentageRateTypes"
|
|
||||||
:borrow-stable-rate="borrowStableRate"
|
|
||||||
:stable-borrow-enabled="stableBorrowEnabled"
|
|
||||||
/>
|
|
||||||
|
|
||||||
<div class="mt-6 flex justify-around items-center w-full">
|
|
||||||
<SidebarSectionValueWithIcon class="" label="Borrowed" center>
|
|
||||||
<template #icon
|
<template #icon
|
||||||
><IconCurrency :currency="rootTokenKey" class="w-20 h-20" noHeight
|
><IconCurrency :currency="debtToken.key" class="w-20 h-20" noHeight
|
||||||
/></template>
|
/></template>
|
||||||
<template #value>{{ formatNumber(balance) }} {{ symbol }}</template>
|
<template #value>{{ formatDecimal(changedDebt) }} {{ debtToken.symbol }}</template>
|
||||||
</SidebarSectionValueWithIcon>
|
</SidebarSectionValueWithIcon>
|
||||||
|
|
||||||
<SidebarSectionValueWithIcon class="" label="Token Balance" center>
|
<SidebarSectionValueWithIcon class="" label="Token Balance" center>
|
||||||
<template #icon
|
<template #icon
|
||||||
><IconCurrency :currency="rootTokenKey" class="w-20 h-20" noHeight
|
><IconCurrency :currency="collateralToken.key" class="w-20 h-20" noHeight
|
||||||
/></template>
|
/></template>
|
||||||
|
|
||||||
<template #value
|
<template #value>{{ formatDecimal(changedBalance) }} {{ collateralToken.symbol }}</template>
|
||||||
>{{ formatNumber(tokenMaxBalance) }} {{ symbol }}</template
|
|
||||||
>
|
|
||||||
</SidebarSectionValueWithIcon>
|
</SidebarSectionValueWithIcon>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="bg-[#C5CCE1] bg-opacity-[0.15] mt-10 p-8">
|
<div class="bg-[#C5CCE1] bg-opacity-[0.15] mt-10 p-8">
|
||||||
<h3 class="text-primary-gray text-xs font-semibold mb-2.5">
|
<h3 class="text-primary-gray text-xs font-semibold mb-2.5">
|
||||||
Amount to supply
|
Amount to payback
|
||||||
</h3>
|
</h3>
|
||||||
|
|
||||||
<input-numeric
|
<input-numeric
|
||||||
v-model="amount"
|
v-model="amount"
|
||||||
placeholder="Amount to supply"
|
placeholder="Amount to payback"
|
||||||
:error="errors.amount.message"
|
:error="errors.amount.message"
|
||||||
>
|
>
|
||||||
<template v-if="!isMaxAmount" #suffix>
|
<template v-if="!isMaxAmount" #suffix>
|
||||||
|
|
@ -58,11 +48,14 @@
|
||||||
|
|
||||||
<SidebarSectionStatus
|
<SidebarSectionStatus
|
||||||
class="mt-8"
|
class="mt-8"
|
||||||
:liquidation="maxLiquidation"
|
:liquidation="liquidation"
|
||||||
:status="status"
|
:status="status"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<SidebarSectionValueWithIcon class="mt-8" label="Liquidation Price (ETH)">
|
<SidebarSectionValueWithIcon
|
||||||
|
class="mt-8"
|
||||||
|
:label="`Liquidation Price (${collateralToken.symbol})`"
|
||||||
|
>
|
||||||
<template #value>
|
<template #value>
|
||||||
{{ formatUsdMax(liquidationPrice, liquidationMaxPrice) }}
|
{{ formatUsdMax(liquidationPrice, liquidationMaxPrice) }}
|
||||||
<span class="text-primary-gray"
|
<span class="text-primary-gray"
|
||||||
|
|
@ -90,7 +83,6 @@
|
||||||
<script>
|
<script>
|
||||||
import { computed, defineComponent, ref } from '@nuxtjs/composition-api'
|
import { computed, defineComponent, ref } from '@nuxtjs/composition-api'
|
||||||
import InputNumeric from '~/components/common/input/InputNumeric.vue'
|
import InputNumeric from '~/components/common/input/InputNumeric.vue'
|
||||||
import { useAaveV2Position } from '~/composables/protocols/useAaveV2Position'
|
|
||||||
import { useBalances } from '~/composables/useBalances'
|
import { useBalances } from '~/composables/useBalances'
|
||||||
import { useBigNumber } from '~/composables/useBigNumber'
|
import { useBigNumber } from '~/composables/useBigNumber'
|
||||||
import { useFormatting } from '~/composables/useFormatting'
|
import { useFormatting } from '~/composables/useFormatting'
|
||||||
|
|
@ -100,90 +92,60 @@ import { useToken } from '~/composables/useToken'
|
||||||
import { useParsing } from '~/composables/useParsing'
|
import { useParsing } from '~/composables/useParsing'
|
||||||
import { useMaxAmountActive } from '~/composables/useMaxAmountActive'
|
import { useMaxAmountActive } from '~/composables/useMaxAmountActive'
|
||||||
import { useWeb3 } from '~/composables/useWeb3'
|
import { useWeb3 } from '~/composables/useWeb3'
|
||||||
import atokens from '~/constant/atokens'
|
|
||||||
import ToggleButton from '~/components/common/input/ToggleButton.vue'
|
import ToggleButton from '~/components/common/input/ToggleButton.vue'
|
||||||
import { useDSA } from '~/composables/useDSA'
|
import { useDSA } from '~/composables/useDSA'
|
||||||
import ButtonCTA from '~/components/common/input/ButtonCTA.vue'
|
import ButtonCTA from '~/components/common/input/ButtonCTA.vue'
|
||||||
import { useNotification } from '~/composables/useNotification'
|
import { useNotification } from '~/composables/useNotification'
|
||||||
import Button from '~/components/Button.vue'
|
import Button from '~/components/Button.vue'
|
||||||
import { useSidebar } from '~/composables/useSidebar'
|
import { useSidebar } from '~/composables/useSidebar'
|
||||||
|
import { useLiquityPosition } from '~/composables/protocols/useLiquityPosition'
|
||||||
|
|
||||||
export default defineComponent({
|
export default defineComponent({
|
||||||
components: { InputNumeric, ToggleButton, ButtonCTA, Button },
|
components: { InputNumeric, ToggleButton, ButtonCTA, Button },
|
||||||
props: {
|
setup() {
|
||||||
tokenKey: { type: String, required: true },
|
|
||||||
},
|
|
||||||
setup(props) {
|
|
||||||
const { close } = useSidebar()
|
const { close } = useSidebar()
|
||||||
const { networkName, account } = useWeb3()
|
const { networkName, account } = useWeb3()
|
||||||
const { dsa } = useDSA()
|
const { dsa } = useDSA()
|
||||||
const { getTokenByKey, valInt } = useToken()
|
const { valInt } = useToken()
|
||||||
const { getBalanceByKey, getBalanceRawByKey, fetchBalances } = useBalances()
|
const { getBalanceByKey } = useBalances()
|
||||||
const { formatNumber, formatUsdMax, formatUsd } = useFormatting()
|
const { formatDecimal, formatUsdMax, formatUsd } = useFormatting()
|
||||||
const { isZero, gt, plus, max, minus } = useBigNumber()
|
const { isZero, gte, plus, max, minus, min } = useBigNumber()
|
||||||
const { parseSafeFloat } = useParsing()
|
const { parseSafeFloat } = useParsing()
|
||||||
const { showPendingTransaction, showWarning } = useNotification()
|
const { showPendingTransaction, showWarning } = useNotification()
|
||||||
const { status, displayPositions, liquidation, maxLiquidation, liquidationPrice, liquidationMaxPrice, annualPercentageRateTypes } = useAaveV2Position({
|
|
||||||
overridePosition: (position) => {
|
|
||||||
if (rootTokenKey.value !== position.key) return position
|
|
||||||
|
|
||||||
return {
|
|
||||||
...position,
|
|
||||||
borrow: max(minus(position.borrow, amountParsed.value), '0').toFixed(),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const rateType = ref(null)
|
|
||||||
|
|
||||||
const amount = ref('')
|
const amount = ref('')
|
||||||
const amountParsed = computed(() => parseSafeFloat(amount.value))
|
const amountParsed = computed(() => parseSafeFloat(amount.value))
|
||||||
|
|
||||||
const rootTokenKey = computed(() => atokens[networkName.value].rootTokens.includes(props.tokenKey) ? props.tokenKey : 'eth')
|
const {
|
||||||
|
debt,
|
||||||
|
collateral,
|
||||||
|
collateralInWei,
|
||||||
|
liquidation,
|
||||||
|
liquidationMaxPrice,
|
||||||
|
debtToken,
|
||||||
|
collateralToken,
|
||||||
|
getTrovePositionHints,
|
||||||
|
} = useLiquityPosition()
|
||||||
|
|
||||||
const currentPosition = computed(() =>
|
const balance = computed(() => getBalanceByKey(debtToken.value.key))
|
||||||
displayPositions.value.find((position) => position.key === rootTokenKey.value)
|
|
||||||
)
|
|
||||||
|
|
||||||
const token = computed(() => getTokenByKey(rootTokenKey.value))
|
const changedDebt = computed(() => max(minus(debt.value, amountParsed.value), '0').toFixed())
|
||||||
const symbol = computed(() => token.value?.symbol)
|
const changedBalance = computed(() => max(minus(balance.value, amountParsed.value), '0').toFixed())
|
||||||
const decimals = computed(() => token.value?.decimals)
|
|
||||||
const balance = computed(() => {
|
|
||||||
if (rateType.value?.value === 'stable') {
|
|
||||||
return currentPosition.value?.borrowStable || '0'
|
|
||||||
}
|
|
||||||
return currentPosition.value?.borrow || '0'
|
|
||||||
})
|
|
||||||
|
|
||||||
const tokenMaxBalance = computed(() => getBalanceByKey(rootTokenKey.value))
|
const { liquidationPrice, status } = useLiquityPosition(collateral, changedDebt)
|
||||||
const tokenMaxBalanceRaw = computed(() => getBalanceRawByKey(rootTokenKey.value))
|
const { validateAmount, validateIsLoggedIn, validateLiquityDebt, validateLiquityTroveExists } = useValidators()
|
||||||
|
|
||||||
const availableLiquidity = computed(() => currentPosition.value?.availableLiquidity || '0')
|
const maxBalance = computed(() => min(balance.value, debt.value).toFixed())
|
||||||
const borrowStableRate = computed(() => currentPosition.value?.borrowStableRate || '0')
|
const { toggle, isMaxAmount } = useMaxAmountActive(amount, maxBalance)
|
||||||
const stableBorrowEnabled = computed(
|
|
||||||
() => currentPosition.value?.stableBorrowEnabled && isZero(currentPosition.value?.supply)
|
|
||||||
)
|
|
||||||
|
|
||||||
const address = computed(() => token.value?.address)
|
|
||||||
|
|
||||||
const factor = computed(
|
|
||||||
() => displayPositions.value?.find((position) => rootTokenKey.value === position.key)?.factor
|
|
||||||
)
|
|
||||||
|
|
||||||
const { toggle, isMaxAmount } = useMaxAmountActive(amount, balance)
|
|
||||||
|
|
||||||
const { validateAmount, validateLiquidation, validateLiquidity, validateIsLoggedIn } = useValidators()
|
|
||||||
const errors = computed(() => {
|
const errors = computed(() => {
|
||||||
const hasAmountValue = !isZero(amount.value)
|
const hasAmountValue = !isZero(amount.value)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
amount: { message: validateAmount(amountParsed.value), show: hasAmountValue },
|
troveExists: { message: validateLiquityTroveExists(), show: true },
|
||||||
liquidation: { message: validateLiquidation(status.value, liquidation.value), show: hasAmountValue },
|
amount: { message: validateAmount(amountParsed.value, maxBalance.value), show: hasAmountValue },
|
||||||
|
minDebt: { message: validateLiquityDebt(changedDebt.value), show: hasAmountValue },
|
||||||
auth: { message: validateIsLoggedIn(!!account.value), show: true },
|
auth: { message: validateIsLoggedIn(!!account.value), show: true },
|
||||||
liquidity: {
|
|
||||||
message: validateLiquidity(amountParsed.value, availableLiquidity.value, symbol.value),
|
|
||||||
show: hasAmountValue,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const { errorMessages, isValid } = useValidation(errors)
|
const { errorMessages, isValid } = useValidation(errors)
|
||||||
|
|
@ -193,22 +155,22 @@ export default defineComponent({
|
||||||
async function cast() {
|
async function cast() {
|
||||||
pending.value = true
|
pending.value = true
|
||||||
|
|
||||||
const amount = isMaxAmount.value
|
const inputAmountInWei = valInt(amountParsed.value, collateralToken.value.decimals)
|
||||||
? gte(tokenMaxBalance.value, balance.value)
|
const totalDebtAmountInWei = valInt(changedDebt.value, debtToken.value.decimals)
|
||||||
? $dsa().maxValue
|
const { upperHint, lowerHint } = await getTrovePositionHints(collateralInWei.value, totalDebtAmountInWei)
|
||||||
: tokenMaxBalanceRaw.value
|
|
||||||
: valInt(amountParsed.value, decimals.value)
|
const getId = 0
|
||||||
|
const setId = 0
|
||||||
|
|
||||||
const spells = dsa.value.Spell()
|
const spells = dsa.value.Spell()
|
||||||
|
|
||||||
const rateMode = rateType.value?.rateMode
|
|
||||||
|
|
||||||
spells.add({
|
spells.add({
|
||||||
connector: 'aave_v2',
|
connector: 'LIQUITY-A',
|
||||||
method: 'payback',
|
method: 'repay',
|
||||||
args: [address.value, amount, rateMode, 0, 0],
|
args: [inputAmountInWei, upperHint, lowerHint, getId, setId],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const txHash = await dsa.value.cast({
|
const txHash = await dsa.value.cast({
|
||||||
spells,
|
spells,
|
||||||
|
|
@ -226,31 +188,27 @@ export default defineComponent({
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pending,
|
debt,
|
||||||
cast,
|
balance,
|
||||||
errors,
|
|
||||||
amount,
|
amount,
|
||||||
status,
|
status,
|
||||||
rootTokenKey,
|
liquidation,
|
||||||
token,
|
|
||||||
symbol,
|
|
||||||
balance,
|
|
||||||
formatNumber,
|
|
||||||
formatUsdMax,
|
|
||||||
formatUsd,
|
|
||||||
toggle,
|
|
||||||
isMaxAmount,
|
|
||||||
maxLiquidation,
|
|
||||||
liquidationPrice,
|
liquidationPrice,
|
||||||
liquidationMaxPrice,
|
liquidationMaxPrice,
|
||||||
|
formatUsd,
|
||||||
|
formatUsdMax,
|
||||||
|
formatDecimal,
|
||||||
|
errors,
|
||||||
errorMessages,
|
errorMessages,
|
||||||
|
isMaxAmount,
|
||||||
isValid,
|
isValid,
|
||||||
annualPercentageRateTypes,
|
cast,
|
||||||
availableLiquidity,
|
pending,
|
||||||
borrowStableRate,
|
toggle,
|
||||||
stableBorrowEnabled,
|
debtToken,
|
||||||
rateType,
|
collateralToken,
|
||||||
tokenMaxBalance,
|
changedDebt,
|
||||||
|
changedBalance,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user