From ea88ad92a9f1e16af4c20b3a94acc586a8736839 Mon Sep 17 00:00:00 2001 From: amish kohli Date: Wed, 13 Nov 2024 20:00:36 +0530 Subject: [PATCH] fix and min profit logic --- ops/modules/bot/main.tf | 4 ++++ ops/modules/bot/variables.tf | 4 ++++ ops/prod/base.tf | 1 + ops/prod/mode.tf | 1 + ops/prod/optimism.tf | 1 + .../bots/liquidator/src/liquidatePositions.ts | 23 +++++++++++++++++++ 6 files changed, 34 insertions(+) diff --git a/ops/modules/bot/main.tf b/ops/modules/bot/main.tf index 160d23b36..85cd78f55 100755 --- a/ops/modules/bot/main.tf +++ b/ops/modules/bot/main.tf @@ -61,6 +61,10 @@ resource "aws_ecs_task_definition" "liquidator_bot_ecs_task" { name = "DISCORD_SUCCESS_WEBHOOK_URL" value = "${var.discord_success_webhook_url}" }, + { + name = "UPTIME_LIQUIDATOR_API" + value = "${var.uptime_liquidator_api}" + }, { name = "LIFIAPIKEY" value = "${var.lifi_api_key}" diff --git a/ops/modules/bot/variables.tf b/ops/modules/bot/variables.tf index 94ce3756a..a8244928e 100755 --- a/ops/modules/bot/variables.tf +++ b/ops/modules/bot/variables.tf @@ -35,6 +35,10 @@ variable "bots_image_tag" { type = string } +variable "uptime_liquidator_api" { + description = "Uptime liquidator API" + type = string +} variable "web3_http_provider_urls" { type = string description = "List of Web3 HTTP Provider URLs" diff --git a/ops/prod/base.tf b/ops/prod/base.tf index 51aa35f50..1d6ae67e6 100644 --- a/ops/prod/base.tf +++ b/ops/prod/base.tf @@ -32,6 +32,7 @@ module "base_mainnet_liquidator_ecs" { web3_http_provider_urls = local.base_mainnet_rpcs target_chain_id = local.base_mainnet_chain_id ethereum_admin_account = var.ethereum_admin_account + uptime_liquidator_api = var.uptime_liquidator_api ethereum_admin_private_key = var.ethereum_admin_private_key ecs_service_name = "${var.liquidator_service_name}-base" desired_count = var.desired_count diff --git a/ops/prod/mode.tf b/ops/prod/mode.tf index 4ca6d44fd..f9c1244ef 100644 --- a/ops/prod/mode.tf +++ b/ops/prod/mode.tf @@ -28,6 +28,7 @@ module "mode_mainnet_liquidator_ecs" { ecr_repository_url = "${local.liquidator_ecr_repository_name}:${var.bots_image_tag}" bots_image_tag = var.bots_image_tag web3_http_provider_urls = local.mode_mainnet_rpcs + uptime_liquidator_api = var.uptime_liquidator_api target_chain_id = local.mode_mainnet_chain_id ethereum_admin_account = var.ethereum_admin_account ethereum_admin_private_key = var.ethereum_admin_private_key diff --git a/ops/prod/optimism.tf b/ops/prod/optimism.tf index 20ba1cc44..6f717cbe4 100644 --- a/ops/prod/optimism.tf +++ b/ops/prod/optimism.tf @@ -29,6 +29,7 @@ module "optimism_mainnet_liquidator_ecs" { web3_http_provider_urls = local.optimism_mainnet_rpc_0 target_chain_id = local.optimism_mainnet_chain_id ethereum_admin_account = var.ethereum_admin_account + uptime_liquidator_api = var.uptime_liquidator_api ethereum_admin_private_key = var.ethereum_admin_private_key ecs_service_name = "${var.liquidator_service_name}-optimism" desired_count = var.desired_count diff --git a/packages/bots/liquidator/src/liquidatePositions.ts b/packages/bots/liquidator/src/liquidatePositions.ts index e806e2663..09284ffd7 100644 --- a/packages/bots/liquidator/src/liquidatePositions.ts +++ b/packages/bots/liquidator/src/liquidatePositions.ts @@ -36,6 +36,7 @@ const PAGE_SIZE = 500; // Define the page size for pagination // const BATCH_SIZE = 100; // Define the batch size for processing assets const HF_MIN = parseEther("0.5"); const MAX_HEALTH_FACTOR = parseEther("1"); +const MIN_LIQUIDATION_USD = parseEther("0.01"); // Minimum liquidation value of $0.10 async function getFusePoolUsers(comptroller: Address, botType: BotType) { const poolUsers: PoolUserStruct[] = []; @@ -189,6 +190,14 @@ async function getPotentialPythLiquidation(borrower: PoolUserWithAssets, closeFa const debtAsset = borrower.debt[0]; const collateralAsset = borrower.collateral[0]; + // Calculate liquidation value in USD + const liquidationValueUSD = (debtAsset.borrowBalanceWei! * debtAsset.underlyingPrice) / SCALE_FACTOR_ONE_18_WEI; + + if (liquidationValueUSD < MIN_LIQUIDATION_USD) { + logger.info(`Liquidation value ${liquidationValueUSD.toString()} below minimum threshold, skipping liquidation`); + return undefined; + } + if (debtAsset.borrowBalanceWei! < 3877938057596160n) { logger.info(`Borrow too small, skipping liquidation. Vault: ${borrower.account}`); return undefined; @@ -239,6 +248,7 @@ async function getPotentialPythLiquidation(borrower: PoolUserWithAssets, closeFa cTokenCollateral: collateralAsset.cToken, collateralAssetUnderlyingToken: collateralAsset.underlyingToken, debtAssetUnderlyingToken: debtAsset.underlyingToken, + liquidationValueUSD: liquidationValueUSD }; } @@ -265,6 +275,15 @@ const getPotentialLiquidation = async ( borrower.collateral.sort((a, b) => (b.supplyBalanceWei! > a.supplyBalanceWei! ? 1 : -1)); const debtAsset = borrower.debt[0]; const collateralAsset = borrower.collateral[0]; + + // Calculate liquidation value in USD + const liquidationValueUSD = (debtAsset.borrowBalanceWei! * debtAsset.underlyingPrice) / SCALE_FACTOR_ONE_18_WEI; + + if (liquidationValueUSD < MIN_LIQUIDATION_USD) { + logger.info(`Liquidation value ${liquidationValueUSD.toString()} below minimum threshold, skipping liquidation`); + return undefined; + } + // Get debt and collateral prices const debtAssetUnderlyingPrice = debtAsset.underlyingPrice; const collateralAssetUnderlyingPrice = collateralAsset.underlyingPrice; @@ -311,6 +330,7 @@ const getPotentialLiquidation = async ( logger.info("Liquidation amount is zero, doing nothing"); return undefined; } + return { borrower: borrower.account, repayAmount, @@ -318,6 +338,7 @@ const getPotentialLiquidation = async ( cTokenCollateral: borrower.collateral[0].cToken as Address, collateralAssetUnderlyingToken, debtAssetUnderlyingToken, + liquidationValueUSD }; }; @@ -416,6 +437,7 @@ const liquidateUsers = async (poolUsers: PoolUserStruct[], pool: PublicPoolUserW `To: ${receipt.to}\n` + `Borrower: ${liquidationParams.borrower}\n` + `Repay Amount: ${liquidationParams.repayAmount.toString()}\n` + + `Liquidation Value: $${(Number(liquidationParams.liquidationValueUSD) / 1e18).toFixed(2)}\n` + `Block: ${receipt.blockNumber}\n` + `Gas Used: ${receipt.gasUsed}\n`; +`Status: **${receipt.status}**\n`; @@ -462,6 +484,7 @@ const liquidateUsers = async (poolUsers: PoolUserStruct[], pool: PublicPoolUserW logger.info(`cErc20 Address: ${liquidationParams.cErc20}`); logger.info(`cToken Collateral Address: ${liquidationParams.cTokenCollateral}`); logger.info(`Minimum Output Amount: ${liquidationParams.underlyingAmountSeized.toString()}`); + logger.info(`Liquidation Value: $${(Number(liquidationParams.liquidationValueUSD) / 1e18).toFixed(2)}`); if (liquidationParams.collateralAssetUnderlyingToken && liquidationParams.debtAssetUnderlyingToken) { logger.info(