Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
0xripleys committed Aug 7, 2023
1 parent 405daa6 commit 3ef8c61
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
4 changes: 3 additions & 1 deletion liquidator/src/libs/refreshObligation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function calculateRefreshedObligation(
mintAddress,
marketValue,
symbol,
addedBorrowWeightBPS: reserve.config.addedBorrowWeightBPS
});
});

Expand Down Expand Up @@ -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 = {
Expand Down
31 changes: 30 additions & 1 deletion liquidator/src/libs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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`);
}
Expand Down Expand Up @@ -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;
};
11 changes: 3 additions & 8 deletions liquidator/src/liquidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3ef8c61

Please sign in to comment.