From 3ef8c6139dc167c223f76dd3b89e7fcc6be44fb8 Mon Sep 17 00:00:00 2001 From: 0xripleys <0xripleys@solend.fi> Date: Mon, 7 Aug 2023 12:00:30 -0400 Subject: [PATCH] changes --- liquidator/src/libs/refreshObligation.ts | 4 ++- liquidator/src/libs/utils.ts | 31 +++++++++++++++++++++++- liquidator/src/liquidate.ts | 11 +++------ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/liquidator/src/libs/refreshObligation.ts b/liquidator/src/libs/refreshObligation.ts index 664fec36..5e78ab49 100644 --- a/liquidator/src/libs/refreshObligation.ts +++ b/liquidator/src/libs/refreshObligation.ts @@ -84,6 +84,7 @@ export function calculateRefreshedObligation( mintAddress, marketValue, symbol, + addedBorrowWeightBPS: reserve.config.addedBorrowWeightBPS }); }); @@ -132,12 +133,13 @@ function getBorrrowedAmountWadsWithInterest( } } -type Borrow = { +export type Borrow = { borrowReserve: PublicKey; borrowAmountWads: BN; marketValue: BigNumber; mintAddress: string, symbol: string; + addedBorrowWeightBPS: BN; }; type Deposit = { diff --git a/liquidator/src/libs/utils.ts b/liquidator/src/libs/utils.ts index e2fcbb7b..65441aff 100644 --- a/liquidator/src/libs/utils.ts +++ b/liquidator/src/libs/utils.ts @@ -11,6 +11,7 @@ import { } from 'global'; import { findWhere } from 'underscore'; import { TokenOracleData } from './pyth'; +import { Borrow } from './refreshObligation'; export const WAD = new BigNumber(`1${''.padEnd(18, '0')}`); export const U64_MAX = '18446744073709551615'; @@ -78,7 +79,7 @@ export function getTokenInfo(market: MarketConfig, symbol: string) { } export function getTokenInfoFromMarket(market: MarketConfig, symbol: string) { - const liquidityToken: LiquidityToken = findWhere(market.reserves.map((reserve) => reserve.liquidityToken), { symbol }); + const liquidityToken: LiquidityToken = findWhere(market.reserves.map((reserve) => reserve.liquidityToken), { symbol })!; if (!liquidityToken) { throw new Error(`Could not find ${symbol} in config.assets`); } @@ -287,3 +288,31 @@ export const getLoanToValueRate = (reserve: Reserve): BigNumber => new BigNumber export const getLiquidationThresholdRate = (reserve: Reserve): BigNumber => new BigNumber( reserve.config.liquidationThreshold / 100, ); + +export const sortBorrows = (borrows: Borrow[]): Borrow[] => { + return borrows.sort((a, b) => { + if (a.addedBorrowWeightBPS.eq(b.addedBorrowWeightBPS)) { + return comparePubkeys(b.borrowReserve, a.borrowReserve); + } else { + // Otherwise, sort by addedBorrowWeightBPS in descending order + return b.addedBorrowWeightBPS.cmp(a.addedBorrowWeightBPS); + } + }); +}; + +// use the bytes representation to compare two addresses +export const comparePubkeys = (a: PublicKey, b: PublicKey): number => { + const a_bytes = a.toBytes(); + const b_bytes = b.toBytes(); + + for (let i = 0; i < 32; i++) { + if (a_bytes[i] < b_bytes[i]) { + return -1; + } + if (a_bytes[i] > b_bytes[i]) { + return 1; + } + } + + return 0; +}; diff --git a/liquidator/src/liquidate.ts b/liquidator/src/liquidate.ts index 01e38df5..22b70197 100644 --- a/liquidator/src/liquidate.ts +++ b/liquidator/src/liquidate.ts @@ -8,10 +8,10 @@ import { } from '@solana/web3.js'; import dotenv from 'dotenv'; import { - getObligations, getReserves, getWalletBalances, getWalletDistTarget, getWalletTokenData, wait, + getObligations, getReserves, getWalletBalances, getWalletDistTarget, getWalletTokenData, sortBorrows, wait, } from 'libs/utils'; import { getTokensOracleData } from 'libs/pyth'; -import { calculateRefreshedObligation } from 'libs/refreshObligation'; +import { Borrow, calculateRefreshedObligation } from 'libs/refreshObligation'; import { readSecret } from 'libs/secret'; import { liquidateAndRedeem } from 'libs/actions/liquidateAndRedeem'; import { rebalanceWallet } from 'libs/rebalanceWallet'; @@ -75,12 +75,7 @@ async function runLiquidator() { } // select repay token that has the highest market value - let selectedBorrow; - borrows.forEach((borrow) => { - if (!selectedBorrow || borrow.marketValue.gt(selectedBorrow.marketValue)) { - selectedBorrow = borrow; - } - }); + let selectedBorrow: Borrow | undefined = sortBorrows(borrows)[0]; // select the withdrawal collateral token with the highest market value let selectedDeposit;