All files / config mlrFunctions.ts

100% Statements 58/58
100% Branches 26/26
100% Functions 5/5
100% Lines 54/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 1712x 2x 2x   2x 6x       18x 18x 18x   6x       6x 6x       6x   6x 2x           4x 2x           2x 2x   2x       1x           1x     2x       9x   9x     9x 1x             8x 24x   24x     21x 18x         3x   3x         8x 8x 8x     8x 2x             6x 2x                   4x               4x 1x       2x             10x 2x           8x   8x 1x               7x 6x       4x       4x             3x    
import { formatUnits } from "viem";
import { publicClientReadIns } from "./commonFunctions";
import { MLR_RATIO_CONSTANT, USDT_DECIMAL } from "./constantVariables";
 
export const checkMLRBeforeBuyCover = async (amount: number) => {
  const fetchContractValue = async (
    functionName: string,
    decimals: number
  ): Promise<number> => {
    try {
      const response = await publicClientReadIns({ functionName });
      return Number(formatUnits(response as bigint, decimals));
    } catch (error) {
      return -1;
    }
  };
 
  const topNodeAmount = await fetchContractValue("getReserve", USDT_DECIMAL);
  const lockedLiquidity = await fetchContractValue(
    "lockedLiquidity",
    USDT_DECIMAL
  );
  const mlrRatio = await fetchContractValue("mlrRatio", 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 };
};
 
export const checkMLRBeforeRemoval = async (
  percent: number,
  availBalance: number
): Promise<any> => {
  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: string): Promise<number> => {
    try {
      // Await the Promise to get the actual value
      const response = await publicClientReadIns({ functionName });
 
      // Check if the response is of type bigint and convert it accordingly
      if (typeof response === "bigint") {
        return functionName === "mlrRatio"
          ? Number(formatUnits(response, MLR_RATIO_CONSTANT)) // Format to 4 decimal places
          : Number(formatUnits(response, 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 = checkMLR(
    amountToWithdraw,
    lockedLiquidity,
    topNodeAmount,
    mlrRatio,
    false
  );
 
  if (!mlrRes.success) return mlrRes;
  return { success: true };
};
 
// Simplified checkMLR function
export const checkMLR = (
  amount: number,
  currentLockedLiquidity: number,
  currentTotalLiquidity: number,
  currentMlrRatio: number,
  isAdd: boolean
) => {
  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 };
};