Skip to content

Commit

Permalink
Merge pull request #46 from BuiltByMom/bugfix/exact-liquidation-bond
Browse files Browse the repository at this point in the history
fix: replace sdk side NpTp function with a contract call
  • Loading branch information
imbaniac authored Jan 16, 2024
2 parents f676646 + 5ca46af commit 10be8aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/classes/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
updateInterest,
lenderInfo,
stampLoan,
interestRateInfo,
} from '../contracts/pool';
import {
borrowerInfo,
Expand Down Expand Up @@ -381,26 +380,27 @@ export abstract class Pool {
this.poolAddress,
borrowerAddress
);
const poolBorrowerInfoCall = this.contractMulti.borrowerInfo(borrowerAddress);

const response: Array<Array<BigNumber>> = await this.ethcallProvider.all([
poolPricesInfoCall,
poolLoansInfoCall,
borrowerInfoCall,
auctionStatusCall,
poolBorrowerInfoCall,
]);

const [, , , , lup] = response[0];
const [, , , pendingInflator] = response[1];
const [debt, collateral, t0np, tp] = response[2];
const [kickTimestamp] = response[3];
const [, , npTpRatio] = response[4];

const collateralization = debt.gt(0)
? wdiv(wmul(collateral, lup), wmul(debt, COLLATERALIZATION_FACTOR))
: toWad(1);
const np = wmul(t0np, pendingInflator);

const [rate] = await interestRateInfo(this.contract);
const npTpRatio = this.calculateNpTpRatio(rate);

return {
collateralization,
debt,
Expand All @@ -425,6 +425,7 @@ export abstract class Pool {
for (const loan of borrowerAddresses) {
calls.push(this.contractUtilsMulti.borrowerInfo(this.poolAddress, loan));
calls.push(this.contractUtilsMulti.auctionStatus(this.poolAddress, loan));
calls.push(this.contractMulti.borrowerInfo(loan));
}

// perform the multicall
Expand All @@ -437,14 +438,13 @@ export abstract class Pool {
const [, , , pendingInflator] = response[++i];

// calculate npTpRatio, needed for calculating liquidation bonds
const [rate] = await interestRateInfo(this.contract);
const npTpRatio = this.calculateNpTpRatio(rate);

// iterate through borrower info, offset by the 3 pool-level requests
let borrowerIndex = 0;
while (borrowerIndex < borrowerAddresses.length) {
const [debt, collateral, t0np, tp] = response[++i];
const kickTimestamp = response[++i][0];
const [, , npTpRatio] = response[++i];

const collateralization = debt.gt(0)
? wdiv(wmul(collateral, lup), wmul(debt, COLLATERALIZATION_FACTOR))
Expand Down Expand Up @@ -517,6 +517,7 @@ export abstract class Pool {
* @param borrowerDebt loan debt
* @returns required liquidation bond, in WAD precision
*/

calculateLiquidationBond(npTpRatio: BigNumber, borrowerDebt: BigNumber) {
// bondFactor = min((NP-to-TP-ratio - 1)/10, 0.03)
const bondFactor = min(wdiv(npTpRatio.sub(toWad(1)), toWad(10)), toWad(0.03));
Expand All @@ -535,7 +536,7 @@ export abstract class Pool {
}

/**
* Calculate neutral price to threshold price "ratio".
* Calculate neutral price to threshold price approx. "ratio".
* @param rate current pool "borrower" interest rate
* @returns relationship between the neutral price and threshold price
*/
Expand Down
5 changes: 3 additions & 2 deletions src/utils/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ class WrappedTransactionClass implements WrappedTransaction {
// works with some L2 chains (Base, Polygon, Optimism)
if (error?.error?.data) {
const errorHash = error.error.data.data;

return (
this.getCustomErrorFromHash(contract, errorHash) ??
error.data.data?.cause ??
error.data.data?.message
error.error.data?.cause ??
error.error.data?.message
);
}

Expand Down

0 comments on commit 10be8aa

Please sign in to comment.