assembly/composables/useValidators.ts

102 lines
2.5 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-10 14:17:20 +00:00
import { useMakerdaoPosition } from "./useMakerdaoPosition";
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-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-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,
validateMakerDebtCeiling
2021-07-26 22:19:20 +00:00
};
}