From e469beb56d7bdc775c4467a07f265692756826b8 Mon Sep 17 00:00:00 2001 From: Daniel Simon Date: Mon, 21 Jun 2021 12:48:30 +0700 Subject: [PATCH] fix: improve gas cost of hint recovery when inserting into the end of 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. --- packages/lib-ethers/src/PopulatableEthersLiquity.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/lib-ethers/src/PopulatableEthersLiquity.ts b/packages/lib-ethers/src/PopulatableEthersLiquity.ts index 3e5efbf85..d39bd6b79 100644 --- a/packages/lib-ethers/src/PopulatableEthersLiquity.ts +++ b/packages/lib-ethers/src/PopulatableEthersLiquity.ts @@ -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]> {