assembly/composables/useValidators.ts

60 lines
1.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-07-26 22:19:20 +00:00
export function useValidators() {
2021-07-27 19:16:38 +00:00
const { formatNumber } = useFormatting()
2021-07-26 22:19:20 +00:00
const { isZero, minus, eq, gt } = useBigNumber();
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-07-27 19:16:38 +00:00
function validateLiquidity(borrow, availableLiquidity, tokenSymbol, withdraw = false) {
if (gt(borrow, availableLiquidity)) {
let action = 'borrow'
if (withdraw) {
action = 'withdraw'
}
return `Not enough liquidity to ${action} ${formatNumber(borrow, 2)} ${tokenSymbol}`
}
return null
}
2021-07-26 22:19:20 +00:00
return {
validateAmount,
validateLiquidation,
2021-07-27 19:16:38 +00:00
validateIsLoggedIn,
validateLiquidity
2021-07-26 22:19:20 +00:00
};
}