assembly/composables/useValidators.ts

184 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-07-26 22:19:20 +00:00
import { useBigNumber } from "./useBigNumber";
2021-07-27 19:16:38 +00:00
import { useFormatting } from "./useFormatting";
2021-08-13 20:58:50 +00:00
import { useMakerdaoPosition } from "~/composables/protocols/useMakerdaoPosition";
2021-08-21 11:20:16 +00:00
import { useLiquityPosition } from "./protocols/useLiquityPosition";
2021-08-27 21:16:37 +00:00
import { useReflexerPosition } from "./protocols/useReflexerPosition";
2021-07-26 22:19:20 +00:00
export function useValidators() {
2021-08-10 14:17:20 +00:00
const { formatNumber } = useFormatting();
const { isZero, minus, eq, gt, lt, gte, plus } = useBigNumber();
const { minDebt: makerMinDebt, vaultTypes } = useMakerdaoPosition();
2021-08-27 21:16:37 +00:00
const { minDebt: reflexerMinDebt, safeTypes } = useReflexerPosition();
2021-08-21 11:20:16 +00:00
const {
minDebt: liquityMinDebt,
liquidationReserve: liquityLiquidationReserve,
troveOpened: liquityTroveOpened
} = useLiquityPosition();
2021-08-27 21:16:37 +00:00
2021-07-26 22:19:20 +00:00
function validateAmount(amountParsed, balance = null, options = null) {
const mergedOptions = Object.assign(
2021-08-05 17:51:26 +00:00
{ msg: "Your amount exceeds your maximum limit." },
2021-07-26 22:19:20 +00:00
options
);
if (isZero(amountParsed)) {
return "Please provide a valid amount.";
} else if (balance !== null && gt(amountParsed, balance)) {
return mergedOptions.msg;
}
return null;
}
function validateLiquidation(status, liquidation, isWithdraw = false) {
if (eq(status, liquidation) && isZero(status) && isWithdraw) {
return null;
}
if (gt(status, minus(liquidation, "0.0001"))) {
return "Position will liquidate.";
}
return null;
}
function validateIsLoggedIn(isLoggedIn) {
if (!isLoggedIn) {
return "Please connect to a web3 wallet.";
}
return null;
}
2021-08-10 14:17:20 +00:00
function validateLiquidity(
borrow,
availableLiquidity,
tokenSymbol,
withdraw = false
) {
2021-07-27 19:16:38 +00:00
if (gt(borrow, availableLiquidity)) {
2021-08-10 14:17:20 +00:00
let action = "borrow";
2021-07-27 19:16:38 +00:00
if (withdraw) {
2021-08-10 14:17:20 +00:00
action = "withdraw";
2021-07-27 19:16:38 +00:00
}
2021-08-10 14:17:20 +00:00
return `Not enough liquidity to ${action} ${formatNumber(
borrow,
2
)} ${tokenSymbol}`;
2021-07-27 19:16:38 +00:00
}
2021-08-10 14:17:20 +00:00
return null;
}
function validateMakerDebt(
debtParsed,
minDebt = makerMinDebt.value,
vaultId
) {
if (lt(debtParsed, minDebt) && gt(debtParsed, "0")) {
const vaultText = vaultId
? vaultId !== "0"
? `on vault #${vaultId}`
: `on new vault`
: "";
return `Minimum debt requirement is ${minDebt} DAI ${vaultText}`;
}
return null;
}
function validateMakerDebtCeiling(vaultType, debtParsed = 0) {
const vault = vaultTypes.value.find(v => v.type === vaultType);
const { debtCeiling, totalDebt } = vault || {};
if (!isZero(debtCeiling) && !isZero(totalDebt)) {
const total = plus(totalDebt, debtParsed);
return gte(total, debtCeiling)
? `${vaultType} Collateral reached debt ceiling`
: null;
}
return null;
2021-07-27 19:16:38 +00:00
}
2021-08-27 21:16:37 +00:00
function validateReflexerDebtCeiling(safeType, debtParsed = 0) {
const safe = safeTypes.value.find(v => v.type === safeType);
const { debtCeiling, totalDebt } = safe || {};
if (!isZero(debtCeiling) && !isZero(totalDebt)) {
const total = plus(totalDebt, debtParsed);
return gte(total, debtCeiling)
? `${safeType} Collateral reached debt ceiling`
: null;
}
return null;
}
function validateReflexerDebt(
debtParsed,
minDebt = reflexerMinDebt.value,
2021-09-07 19:17:15 +00:00
vaultId,
2021-08-27 21:16:37 +00:00
) {
if (lt(debtParsed, minDebt) && gt(debtParsed, "0")) {
const vaultText = vaultId
? vaultId !== "0"
? `on vault #${vaultId}`
: `on new vault`
: "";
2021-09-07 19:17:15 +00:00
return `Minimum debt requirement is ${minDebt} RAI ${vaultText}` ;
}
return null;
}
function validateReflexerPaybackDebt(
debtParsed,
minDebt = reflexerMinDebt.value,
vaultId,
) {
if (lt(debtParsed, minDebt) && gt(debtParsed, "0")) {
2021-09-13 19:25:19 +00:00
return `Min debt requirement is ${minDebt} RAI. Payback additional ${debtParsed} RAI`;
2021-08-27 21:16:37 +00:00
}
return null;
}
2021-08-21 11:20:16 +00:00
function validateLiquityDebt(
debtParsed,
minDebt = liquityMinDebt.value,
liquidationReserve = liquityLiquidationReserve.value
) {
const totalDebt = plus(debtParsed, liquidationReserve);
if (isZero(totalDebt))
return `Minimum total debt requirement is ${minDebt} LUSD`;
if (lt(totalDebt, minDebt) && gt(totalDebt, "0")) {
return `Minimum total debt requirement is ${minDebt} LUSD`;
}
return null;
}
function validateLiquityTroveExists() {
if (!liquityTroveOpened.value) {
return "You should open new trove first";
}
return null;
}
2021-07-26 22:19:20 +00:00
return {
validateAmount,
validateLiquidation,
2021-07-27 19:16:38 +00:00
validateIsLoggedIn,
2021-08-10 14:17:20 +00:00
validateLiquidity,
validateMakerDebt,
2021-08-21 11:20:16 +00:00
validateMakerDebtCeiling,
validateLiquityDebt,
2021-08-27 21:16:37 +00:00
validateLiquityTroveExists,
validateReflexerDebtCeiling,
validateReflexerDebt,
2021-09-07 19:17:15 +00:00
validateReflexerPaybackDebt,
2021-07-26 22:19:20 +00:00
};
}