diff --git a/scripts/liquidation_bot/index.ts b/scripts/liquidation_bot/index.ts index f046317..1ae55a7 100644 --- a/scripts/liquidation_bot/index.ts +++ b/scripts/liquidation_bot/index.ts @@ -61,7 +61,7 @@ async function main() { let lastBlockNumber: number | undefined; - // Compound V3 风格:while(true) 轮询 + // while(true) 轮询 while (true) { try { const currentBlockNumber = await hre.ethers.provider.getBlockNumber(); diff --git a/scripts/liquidation_bot/liquidateUnderwaterBorrowers.ts b/scripts/liquidation_bot/liquidateUnderwaterBorrowers.ts index d0c0c50..7d2a48a 100644 --- a/scripts/liquidation_bot/liquidateUnderwaterBorrowers.ts +++ b/scripts/liquidation_bot/liquidateUnderwaterBorrowers.ts @@ -2,7 +2,7 @@ import hre from 'hardhat'; import { Signer } from 'ethers'; const LOOKBACK_BLOCKS = 50000; // 查询最近 50000 个区块 -const LIQUIDATION_THRESHOLD = 10e6; // $10 (USDC 6 decimals) +const LIQUIDATION_THRESHOLD = 10; // $10 的最小清算阈值(美元单位) /** * 获取最近活跃的地址(通过多个事件) @@ -91,7 +91,6 @@ export async function getUniqueAddresses( /** * 检查并清算可清算账户 - * 参考:comet/scripts/liquidation_bot/liquidateUnderwaterBorrowers.ts */ export async function liquidateUnderwaterBorrowers( lendingContract: any, @@ -121,14 +120,20 @@ export async function liquidateUnderwaterBorrowers( const borrowBalance = await lendingContract.borrowBalanceOf(address); const baseToken = await lendingContract.baseToken(); const basePrice = await priceFeedContract.getPrice(baseToken); - - // debtValue 计算:borrowBalance (6 decimals) * basePrice (30 decimals) / 1e6 - const debtValue = (BigInt(borrowBalance) * BigInt(basePrice)) / BigInt(10) ** BigInt(6); - const debtValueInBaseUnit = Number(debtValue / (BigInt(10) ** BigInt(30))); // 转换为 USDC 单位 - - // LIQUIDATION_THRESHOLD 是 6 decimals,需要转换为 30 decimals 来和 debtValue 比较 - // 10e6 * 1e24 = 10e30 (代表 $10) - if (debtValue >= BigInt(LIQUIDATION_THRESHOLD) * BigInt(10) ** BigInt(24)) { + + // 动态获取 baseToken 的 decimals + const baseTokenContract = await hre.ethers.getContractAt('IERC20Metadata', baseToken); + const baseDecimals = await baseTokenContract.decimals(); + + // debtValue 计算:borrowBalance * basePrice / 10^decimals + // 结果精度为 30 decimals (价格精度) + const debtValue = (BigInt(borrowBalance) * BigInt(basePrice)) / BigInt(10) ** BigInt(baseDecimals); + const debtValueInBaseUnit = Number(debtValue / (BigInt(10) ** BigInt(30))); // 转换为美元单位 + + // 将清算阈值(美元)转换为 30 decimals 精度进行比较 + const thresholdValue = BigInt(LIQUIDATION_THRESHOLD) * BigInt(10) ** BigInt(30); + + if (debtValue >= thresholdValue) { console.log(`💰 Liquidatable: ${address}, Debt: $${debtValueInBaseUnit.toFixed(2)}`); liquidatableAccounts.push(address); } else {