test-kohin-sdk
Version:
The Kohin JS is a comprehensive developer toolkit designed to integrate Kohin's decentralized insurance system seamlessly into your applications. It enables efficient interaction with Kohin smart contracts and backend APIs, facilitating management and ana
128 lines (127 loc) • 5.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkMLR = exports.checkMLRBeforeRemoval = exports.checkMLRBeforeBuyCover = void 0;
const viem_1 = require("viem");
const commonFunctions_1 = require("./commonFunctions");
const constantVariables_1 = require("./constantVariables");
const checkMLRBeforeBuyCover = async (amount) => {
const fetchContractValue = async (functionName, decimals) => {
try {
const response = await (0, commonFunctions_1.publicClientReadIns)({ functionName });
return Number((0, viem_1.formatUnits)(response, decimals));
}
catch (error) {
return -1;
}
};
const topNodeAmount = await fetchContractValue("getReserve", constantVariables_1.USDT_DECIMAL);
const lockedLiquidity = await fetchContractValue("lockedLiquidity", constantVariables_1.USDT_DECIMAL);
const mlrRatio = await fetchContractValue("mlrRatio", constantVariables_1.MLR_RATIO_CONSTANT);
if ([topNodeAmount, lockedLiquidity, mlrRatio].includes(-1)) {
return {
success: false,
error: `Unable to fetch required info to move further. Please try again!`,
};
}
if (topNodeAmount < amount) {
return {
success: false,
error: `There is insufficient liquidity to insure the bet amount on platform. Please try later.`,
};
}
const newLockedLiquidity = lockedLiquidity + amount;
const MAX_PERCENTAGE = 1;
if (topNodeAmount <= 0 ||
newLockedLiquidity * MAX_PERCENTAGE > topNodeAmount * mlrRatio) {
return {
success: false,
error: `The platform's available liquidity is not sufficient to buy this cover. Please check back later or contact our support team on Discord for assistance.`,
};
}
return { success: true };
};
exports.checkMLRBeforeBuyCover = checkMLRBeforeBuyCover;
const checkMLRBeforeRemoval = async (percent, availBalance) => {
const maxPercentage = 100;
const amountToWithdraw = (availBalance * percent) / maxPercentage;
// Validate percentage
if (percent > maxPercentage || percent <= 0) {
return {
success: false,
error: `The percentage value provided ${percent} is invalid. Please enter a valid percentage and try again.`,
};
}
// Function to fetch contract values
const fetchContractValue = async (functionName) => {
try {
// Await the Promise to get the actual value
const response = await (0, commonFunctions_1.publicClientReadIns)({ functionName });
// Check if the response is of type bigint and convert it accordingly
if (typeof response === "bigint") {
return functionName === "mlrRatio"
? Number((0, viem_1.formatUnits)(response, constantVariables_1.MLR_RATIO_CONSTANT)) // Format to 4 decimal places
: Number((0, viem_1.formatUnits)(response, constantVariables_1.USDT_DECIMAL)); // Format to 6 decimal places
}
// Handle unexpected response types (if necessary)
return -1;
}
catch (error) {
return -1;
}
};
// Fetch contract values
const topNodeAmount = await fetchContractValue("getReserve");
const lockedLiquidity = await fetchContractValue("lockedLiquidity");
const mlrRatio = await fetchContractValue("mlrRatio");
// Handle errors fetching contract values
if ([topNodeAmount, lockedLiquidity, mlrRatio].includes(-1)) {
return {
success: false,
error: `Unable to fetch required info to move further. Please try again!`,
};
}
// Validate liquidity
if (topNodeAmount <= 0 || availBalance <= 0) {
return {
success: false,
error: topNodeAmount <= 0
? `Reserve amount must be greater than zero`
: `Deposit balance must be greater than zero`,
};
}
// MLR check
const mlrRes = (0, exports.checkMLR)(amountToWithdraw, lockedLiquidity, topNodeAmount, mlrRatio, false);
if (!mlrRes.success)
return mlrRes;
return { success: true };
};
exports.checkMLRBeforeRemoval = checkMLRBeforeRemoval;
// Simplified checkMLR function
const checkMLR = (amount, currentLockedLiquidity, currentTotalLiquidity, currentMlrRatio, isAdd) => {
if (currentMlrRatio === 0) {
return {
success: false,
error: `MLR Error: Try Later OR Contact support team for more information.`,
};
}
const requiredLiquidity = (currentLockedLiquidity * 1) / currentMlrRatio;
if (isAdd && currentTotalLiquidity + amount < requiredLiquidity) {
return {
success: false,
error: `MLRViolation: Total liquidity after addition (${currentTotalLiquidity + amount}) is less than required liquidity (${requiredLiquidity}).`,
};
}
if (!isAdd) {
if (currentTotalLiquidity < amount || // Ensure sufficient liquidity exists to remove
currentTotalLiquidity - amount < requiredLiquidity // Ensure remaining liquidity is sufficient
) {
const differLiquidity = (currentTotalLiquidity - requiredLiquidity).toFixed(2);
return {
success: false,
error: `The withdrawal amount exceeds the platform's available liquidity. Please try amount < (${differLiquidity}) USDT or check back later. If the issue persists, reach out to our support team on Discord for assistance.`,
};
}
}
return { success: true };
};
exports.checkMLR = checkMLR;