Skip to content

Commit

Permalink
fix and min profit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
amish kohli authored and amish kohli committed Nov 13, 2024
1 parent 910d471 commit ea88ad9
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ops/modules/bot/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
4 changes: 4 additions & 0 deletions ops/modules/bot/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions ops/prod/base.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ops/prod/mode.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ops/prod/optimism.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions packages/bots/liquidator/src/liquidatePositions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -239,6 +248,7 @@ async function getPotentialPythLiquidation(borrower: PoolUserWithAssets, closeFa
cTokenCollateral: collateralAsset.cToken,
collateralAssetUnderlyingToken: collateralAsset.underlyingToken,
debtAssetUnderlyingToken: debtAsset.underlyingToken,
liquidationValueUSD: liquidationValueUSD

Check failure on line 251 in packages/bots/liquidator/src/liquidatePositions.ts

View workflow job for this annotation

GitHub Actions / lint-bots

Insert `,`
};
}

Expand All @@ -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;
Expand Down Expand Up @@ -311,13 +330,15 @@ const getPotentialLiquidation = async (
logger.info("Liquidation amount is zero, doing nothing");
return undefined;
}

return {
borrower: borrower.account,
repayAmount,
cErc20: borrower.debt[0].cToken as Address,
cTokenCollateral: borrower.collateral[0].cToken as Address,
collateralAssetUnderlyingToken,
debtAssetUnderlyingToken,
liquidationValueUSD

Check failure on line 341 in packages/bots/liquidator/src/liquidatePositions.ts

View workflow job for this annotation

GitHub Actions / lint-bots

Insert `,`
};
};

Expand Down Expand Up @@ -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`;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit ea88ad9

Please sign in to comment.