Skip to content

Commit

Permalink
fix: work around huge gas cost when reducing ICR of bottom Trove
Browse files Browse the repository at this point in the history
Fixes #659.
  • Loading branch information
danielattilasimon committed Aug 2, 2021
1 parent daf2d0f commit 878942a
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions packages/lib-ethers/src/PopulatableEthersLiquity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@ export class PopulatableEthersLiquity
}

private async _findHintsForNominalCollateralRatio(
nominalCollateralRatio: Decimal
nominalCollateralRatio: Decimal,
ownAddress?: string
): Promise<[string, string]> {
const { sortedTroves, hintHelpers } = _getContracts(this._readable.connection);
const numberOfTroves = await this._readable.getNumberOfTroves();
Expand Down Expand Up @@ -733,21 +734,35 @@ export class PopulatableEthersLiquity

const { hintAddress } = results.reduce((a, b) => (a.diff.lt(b.diff) ? a : b));

const [prev, next] = await sortedTroves.findInsertPosition(
let [prev, next] = await sortedTroves.findInsertPosition(
nominalCollateralRatio.hex,
hintAddress,
hintAddress
);

return prev === AddressZero ? [next, next] : next === AddressZero ? [prev, prev] : [prev, next];
if (ownAddress) {
if (prev === ownAddress) {
prev = await sortedTroves.getPrev(prev);
} else if (next === ownAddress) {
next = await sortedTroves.getNext(next);
}
}

if (prev === AddressZero) {
prev = next;
} else if (next === AddressZero) {
next = prev;
}

return [prev, next];
}

private async _findHints(trove: Trove): Promise<[string, string]> {
private async _findHints(trove: Trove, ownAddress?: string): Promise<[string, string]> {
if (trove instanceof TroveWithPendingRedistribution) {
throw new Error("Rewards must be applied to this Trove");
}

return this._findHintsForNominalCollateralRatio(trove._nominalCollateralRatio);
return this._findHintsForNominalCollateralRatio(trove._nominalCollateralRatio, ownAddress);
}

private async _findRedemptionHints(
Expand Down Expand Up @@ -775,7 +790,10 @@ export class PopulatableEthersLiquity
partialRedemptionLowerHint
] = partialRedemptionHintNICR.isZero()
? [AddressZero, AddressZero]
: await this._findHintsForNominalCollateralRatio(decimalify(partialRedemptionHintNICR));
: await this._findHintsForNominalCollateralRatio(
decimalify(partialRedemptionHintNICR)
// XXX: if we knew the partially redeemed Trove's address, we'd pass it here
);

return [
decimalify(truncatedLUSDamount),
Expand Down Expand Up @@ -942,7 +960,7 @@ export class PopulatableEthersLiquity

const currentBorrowingRate = decayBorrowingRate(0);
const adjustedTrove = trove.adjust(normalizedParams, currentBorrowingRate);
const hints = await this._findHints(adjustedTrove);
const hints = await this._findHints(adjustedTrove, address);

const {
maxBorrowingRate,
Expand Down Expand Up @@ -1139,7 +1157,7 @@ export class PopulatableEthersLiquity
await stabilityPool.estimateAndPopulate.withdrawETHGainToTrove(
{ ...overrides },
compose(addGasForPotentialListTraversal, addGasForLQTYIssuance),
...(await this._findHints(finalTrove))
...(await this._findHints(finalTrove, address))
)
);
}
Expand Down

0 comments on commit 878942a

Please sign in to comment.