mirror of
https://github.com/Instadapp/assembly.git
synced 2024-07-29 22:37:06 +00:00
46 lines
1.0 KiB
TypeScript
46 lines
1.0 KiB
TypeScript
|
import { useBigNumber } from "./useBigNumber";
|
||
|
|
||
|
export function useValidators() {
|
||
|
const { isZero, minus, eq, gt } = useBigNumber();
|
||
|
|
||
|
function validateAmount(amountParsed, balance = null, options = null) {
|
||
|
const mergedOptions = Object.assign(
|
||
|
{ msg: "Your amount exceeds your balance." },
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
validateAmount,
|
||
|
validateLiquidation,
|
||
|
validateIsLoggedIn
|
||
|
};
|
||
|
}
|