assembly/components/sidebar/context/aaveV2/SidebarAaveV2Supply.vue

195 lines
6.1 KiB
Vue
Raw Normal View History

2021-07-24 23:40:30 +00:00
<template>
<SidebarContextRootContainer>
2021-07-26 18:33:52 +00:00
<template #title>Supply {{ symbol }}</template>
2021-07-24 23:40:30 +00:00
2021-07-27 18:54:59 +00:00
<SidebarSectionValueWithIcon label="Token Balance" center>
2021-07-26 18:33:52 +00:00
<template #icon
><IconCurrency :currency="rootTokenKey" class="w-20 h-20" noHeight
/></template>
2021-07-26 22:19:20 +00:00
<template #value>{{ formatNumber(balance) }} {{ symbol }}</template>
2021-07-24 23:40:30 +00:00
</SidebarSectionValueWithIcon>
2021-07-27 18:54:59 +00:00
<div class="bg-[#C5CCE1] bg-opacity-[0.15] mt-10 p-8">
<h3 class="text-primary-gray text-xs font-semibold mb-2.5">
Amount to supply
</h3>
2021-07-26 22:19:20 +00:00
<input-numeric
v-model="amount"
placeholder="Amount to supply"
:error="errors.amount.message"
>
2021-07-27 18:54:59 +00:00
<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>
2021-07-26 22:19:20 +00:00
2021-07-27 18:54:59 +00:00
<SidebarContextHeading class="mt-5">
Projected Debt Position
</SidebarContextHeading>
2021-07-26 22:19:20 +00:00
<SidebarSectionStatus
2021-07-27 18:54:59 +00:00
class="mt-8"
2021-07-26 22:19:20 +00:00
:liquidation="maxLiquidation"
:status="status"
/>
2021-07-27 18:54:59 +00:00
<SidebarSectionValueWithIcon class="mt-8" label="Liquidation Price (ETH)">
2021-07-26 22:19:20 +00:00
<template #value>
2021-07-27 19:16:38 +00:00
{{ formatUsdMax(liquidationPrice, liquidationMaxPrice) }} <span class="text-primary-gray">/
2021-07-27 18:54:59 +00:00
{{ formatUsd(liquidationMaxPrice) }}</span>
2021-07-26 22:19:20 +00:00
</template>
</SidebarSectionValueWithIcon>
2021-07-27 18:54:59 +00:00
<div class="flex flex-shrink-0 mt-10">
2021-07-26 22:19:20 +00:00
<ButtonCTA
class="w-full"
:disabled="!isValid || pending"
:loading="pending"
@click="cast"
>
Supply
</ButtonCTA>
</div>
<ValidationErrors :error-messages="errorMessages" class="mt-6" />
2021-07-24 23:40:30 +00:00
</div>
</SidebarContextRootContainer>
</template>
<script>
2021-07-26 18:33:52 +00:00
import { computed, defineComponent, ref } from '@nuxtjs/composition-api'
import InputNumeric from '~/components/common/input/InputNumeric.vue'
2021-07-24 23:40:30 +00:00
import { useAaveV2Position } from '~/composables/useAaveV2Position'
2021-07-26 22:19:20 +00:00
import { useBalances } from '~/composables/useBalances'
import { useBigNumber } from '~/composables/useBigNumber'
2021-07-24 23:40:30 +00:00
import { useFormatting } from '~/composables/useFormatting'
2021-07-26 22:19:20 +00:00
import { useValidators } from '~/composables/useValidators'
import { useValidation } from '~/composables/useValidation'
2021-07-26 18:33:52 +00:00
import { useToken } from '~/composables/useToken'
2021-07-26 22:19:20 +00:00
import { useParsing } from '~/composables/useParsing'
import { useMaxAmountActive } from '~/composables/useMaxAmountActive'
import { useWeb3 } from '~/composables/useWeb3'
import atokens from '~/constant/atokens'
import ToggleButton from '~/components/common/input/ToggleButton.vue'
import { useDSA } from '~/composables/useDSA'
import ButtonCTA from '~/components/common/input/ButtonCTA.vue'
2021-07-27 18:54:59 +00:00
import Button from '~/components/Button.vue'
2021-07-27 19:16:38 +00:00
import { useSidebar } from '~/composables/useSidebar'
2021-07-24 23:40:30 +00:00
export default defineComponent({
2021-07-27 18:54:59 +00:00
components: { InputNumeric, ToggleButton, ButtonCTA, Button },
2021-07-24 23:40:30 +00:00
props: {
tokenKey: { type: String, required: true },
},
setup(props) {
2021-07-27 19:16:38 +00:00
const { close} = useSidebar()
2021-07-26 22:19:20 +00:00
const { networkName, account } = useWeb3()
const { dsa } = useDSA()
const { getTokenByKey, valInt } = useToken()
2021-07-27 19:16:38 +00:00
const { getBalanceByKey, fetchBalances } = useBalances()
2021-07-26 22:19:20 +00:00
const { formatNumber, formatUsdMax, formatUsd } = useFormatting()
const { isZero, gt, plus } = useBigNumber()
const { parseSafeFloat } = useParsing()
2021-07-26 18:33:52 +00:00
2021-07-26 22:19:20 +00:00
const { status, displayPositions, maxLiquidation, liquidationPrice, liquidationMaxPrice } = useAaveV2Position({
overridePosition: (position) => {
if (rootTokenKey.value !== position.key) return position
2021-07-24 23:40:30 +00:00
2021-07-26 22:19:20 +00:00
return {
...position,
supply: plus(position.supply, amountParsed.value).toFixed(),
}
},
})
2021-07-26 18:33:52 +00:00
2021-07-26 22:19:20 +00:00
const amount = ref('')
const amountParsed = computed(() => parseSafeFloat(amount.value))
const rootTokenKey = computed(() => atokens[networkName.value].rootTokens.includes(props.tokenKey) ? props.tokenKey : 'eth')
2021-07-26 18:33:52 +00:00
2021-07-26 22:19:20 +00:00
const token = computed(() => getTokenByKey(rootTokenKey.value))
2021-07-26 18:33:52 +00:00
const symbol = computed(() => token.value?.symbol)
2021-07-26 22:19:20 +00:00
const decimals = computed(() => token.value?.decimals)
const balance = computed(() => getBalanceByKey(rootTokenKey.value))
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, validateIsLoggedIn } = useValidators()
const errors = computed(() => {
const hasAmountValue = !isZero(amount.value)
const liqValid = gt(factor.value, '0') ? validateLiquidation(status.value, maxLiquidation.value) : null
return {
amount: { message: validateAmount(amountParsed.value, balance.value), show: hasAmountValue },
liquidation: { message: liqValid, show: hasAmountValue },
auth: { message: validateIsLoggedIn(!!account.value), show: true },
}
})
const { errorMessages, isValid } = useValidation(errors)
const pending = ref(false)
2021-07-26 18:33:52 +00:00
2021-07-26 22:19:20 +00:00
async function cast() {
pending.value = true
2021-07-26 18:33:52 +00:00
2021-07-26 22:19:20 +00:00
const amount = isMaxAmount.value ? dsa.value.maxValue : valInt(amountParsed.value, decimals.value)
const spells = dsa.value.Spell()
spells.add({
connector: 'aave_v2',
method: 'deposit',
args: [address.value, amount, 0, 0],
})
const txHash = await dsa.value.cast({
spells,
from: account.value,
})
2021-07-27 19:16:38 +00:00
fetchBalances(true)
2021-07-26 22:19:20 +00:00
pending.value = false
2021-07-27 20:38:34 +00:00
2021-07-27 19:16:38 +00:00
close()
2021-07-26 22:19:20 +00:00
}
2021-07-24 23:40:30 +00:00
return {
2021-07-26 22:19:20 +00:00
pending,
cast,
errors,
2021-07-26 18:33:52 +00:00
amount,
2021-07-24 23:40:30 +00:00
status,
2021-07-26 18:33:52 +00:00
rootTokenKey,
token,
symbol,
balance,
2021-07-26 22:19:20 +00:00
formatNumber,
formatUsdMax,
formatUsd,
toggle,
isMaxAmount,
maxLiquidation,
liquidationPrice,
liquidationMaxPrice,
errorMessages,
isValid
2021-07-24 23:40:30 +00:00
}
},
})
</script>