Skip to content

Commit

Permalink
fix: improve gas cost of hint recovery when inserting into the end of…
Browse files Browse the repository at this point in the history
… SortedTroves

When trying to create a new Trove that has a lower ICR than the current lowest
ICR (i.e. inserting into the tail of the list), `findInsertPosition()` returns
`(addressOfLowestICRTrove, address(0))`. If we blindly pass these as hints to
`openTrove()` but the transaction ends up pending for so long that the backend
needs to look for a new insert position, we start the traversal in the wrong
direction (from the wrong end of the list), and end up having to traverse over
almost the entire list.

Work around this by replacing the zero address with the other hint.

Workaround for #600.
  • Loading branch information
danielattilasimon committed Jun 21, 2021
1 parent f140d04 commit e469beb
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/lib-ethers/src/PopulatableEthersLiquity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,13 @@ export class PopulatableEthersLiquity

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

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

return prev === AddressZero ? [next, next] : next === AddressZero ? [prev, prev] : [prev, next];
}

private async _findHints(trove: Trove): Promise<[string, string]> {
Expand Down

0 comments on commit e469beb

Please sign in to comment.