Skip to content

handle 0 collateralBalance #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/liquidatorBot/bot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const FRAXLEND_PAIR_HELPER_ADDRESS = '0x26fa88b783cE712a2Fa10E91296Caf3daAE0AB37
const provider = new ethers.providers.AlchemyProvider('homestead', process.env.MAINNET_KEY);

const main = async () => {
console.log('\nentering main...\n')
// Get list of pairs
const fraxlendPairDeployer = new ethers.Contract(FRAXLEND_DEPLOYER_ADDRESS, fraxlendPairDeployerAbi, provider);
const fraxlendPairAddresses = await fraxlendPairDeployer.getAllPairAddresses();
Expand Down Expand Up @@ -78,11 +79,18 @@ const checkPositionThenArb = async (
) => {
// Get some user accounting information
const [ , borrowShares, collateralBalance] = await fraxlendPairHelper.getUserSnapshot(pairAddress, borrowerAddress);


if (collateralBalance == 0) {
console.log('cant calculate LTV, collateralBalance is 0\n');
return;
}

// Calculate LTV
const userBorrowAmount = borrowShares.mul(totalBorrowAmount).div(totalBorrowShares);
console.log('userBorrowAmount: ', (Number(userBorrowAmount) / 1e18).toLocaleString('en-US', {style: 'currency', currency: 'USD', minimumFractionDigits:2}))
const userLTV = userBorrowAmount.mul(exchangeRate).div(BigInt(1e18)).mul(1e5).div(collateralBalance);
if (userLTV.gt(75000)) {
console.log('userLTV: ', (Number(userLTV) / 1e5).toLocaleString(undefined, {style: 'percent', minimumFractionDigits:2}), '\n')
if (userLTV.gt(75000)) { // a user with a current LTV > 75% is liquidatable
const fraxlendPair = new ethers.Contract(pairAddress, fraxlendPairAbi, new ethers.Wallet(process.env.PRIVATE_KEY, provider));
const blockInfo = await provider.getBlock(await provider.getBlockNumber());

Expand All @@ -93,4 +101,8 @@ const checkPositionThenArb = async (
}
}
};
main();
while (true) {

main();
await new Promise(r => setTimeout(r, 10000));
}