Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve(InventoryClient): Align allocation percentage computations between Rebalancer and Relayer #1523

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

nicholaspai
Copy link
Member

@nicholaspai nicholaspai commented May 14, 2024

Motivation

As part of a larger inventory client cleanup/refactor I noticed that there is possibly a bug in how we compute "allocation" percentages when (1) determining repayment chain for fills and (2) computing possible rebalances. This PR is therefore a prerequisite to any follow up refactors that reuse code to compute allocations for (1) and (2)

Problem description

For (2), we subtract the "token shortfall" from the numerator but in (1) we subtract it from both the numerator and the denominator. This is inconsistent which I think could lead to unexpected rebalancing/repayment behavior. I think its most important that the relayer and rebalancer use the same logic when computing how to rebalance inventory between all L2s. Choosing where to be repaid and rebalancing out of L1 inventory both rebalance inventory.

A shortfall represents a lack of tokens on a chain suggesting that we need that amount of tokens to fill a deposit.

The cumulative balance technically is the total inventory across all chains. This should intuitively include all outstanding transfers between L1 and L2 because this is still part of the inventory, its just in-flight between chains.

A shortfall will lead to an outstanding transfer but it will also lead to a subtraction in the total inventory once the transfer finalizes and part of that new L2 balance is used to make a fill.

However, if the shortfall is subtracted from the denominator in the rebalancer/(2) function, then the amount of tokens bridged over to the L2 will be insufficient to fulfill the shortfall. The whole point of the shortfall subtraction from the numerator (and not the denominator) in rebalancing is to force rebalances due to the L2 chain showing an "underallocation" when counting shortfalls.

Summary

This PR makes the allocation % computation consistent between (1) and (2) by removing the shortfall subtraction from the denominator in (1). The end result is that allocation computations for determining refund chain will be lower if there are short-falls, so we'll favor taking repayment on preferred chains slightly.

nicholaspai and others added 2 commits May 13, 2024 13:07
…ns between Rebalancer and Relayer

As part of a larger inventory client cleanup/refactor I noticed that there is possibly a bug in how we compute "allocation" percentages when (1) determining repayment chain for fills and (2) computing possible rebalances.

For (2), we subtract the "token shortfall" from the numerator but in (2) we subtract it from both the numerator and the denominator. This is inconsistent and I don't think it makes sense.

A shortfall represents a lack of tokens on a chain suggesting that we need that amount of tokens to fill a deposit. The rebalancer (i.e. function (2) above) it designed to rebalance inventory from L1 to L2 to fulfill these shortfalls. Therefore, it makes sense to me that in (2) the shortfall is subtracted from the numerator to compute the allocation percentage on the L2.

The cumulative balance technically is the total inventory across all chains. This should intuitively include all outstanding transfers between L1 and L2 because this is still part of the inventory, its just in-flight between chains.

A shortfall will lead to an outstanding transfer but it will also lead to a subtraction in the total inventory once the transfer finalizes and part of that new L2 balance is used to make a fill.

However, if the shortfall is subtracted from the denominator in the rebalancer/(2) function, then the amount of tokens bridged over to the L2 will be insufficient to fulfill the shortfall.

This PR makes the allocation % computation consistent between (1) and (2) by removing the shortfall subtraction from the denominator in (2).
@nicholaspai nicholaspai changed the title WIP: improve(InventoryClient): Align allocation percentage computations between Rebalancer and Relayer improve(InventoryClient): Align allocation percentage computations between Rebalancer and Relayer May 15, 2024
@nicholaspai nicholaspai marked this pull request as ready for review May 15, 2024 06:48
Copy link
Contributor

@james-a-morris james-a-morris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first pass - will look again

src/clients/InventoryClient.ts Outdated Show resolved Hide resolved
pxrl
pxrl previously approved these changes Jul 17, 2024
Copy link
Contributor

@pxrl pxrl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been over this a couple of times. I find the surrounding logic quite dense and not the easiest to follow, but I think the change is OK; it doesn't make sense to consider pending repayments on the candidate repayment chain without also considering "global" pending repayments across all chains.

Copy link
Contributor

@mrice32 mrice32 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this mostly makes sense (and you've thought way more about this than I have :)).

Only part I'm struggling with is the last sentence in your description. Why should we intuitively assume that the shortfall will be removed from the L2 balance but will not be removed from global inventory if we're truly trying to compute the allocation after the relay?

@nicholaspai
Copy link
Member Author

I think this mostly makes sense (and you've thought way more about this than I have :)).

Only part I'm struggling with is the last sentence in your description. Why should we intuitively assume that the shortfall will be removed from the L2 balance but will not be removed from global inventory if we're truly trying to compute the allocation after the relay?

So I think in all cases (1) and (2), shortfalls are tokens that will be removed from the cumulative balance and the chain balance where the shortfall exists.

I think this mostly makes sense (and you've thought way more about this than I have :)).

Only part I'm struggling with is the last sentence in your description. Why should we intuitively assume that the shortfall will be removed from the L2 balance but will not be removed from global inventory if we're truly trying to compute the allocation after the relay?

I've reworded the PR description a bit. I think the key point I'm trying to make is not whether shortfalls should be subtracted from cumulative balance or not, but that the rebalancing and repayment chain choice computations should be consistent, right? Its important when rebalancing not to count shortfalls in the denominator, otherwise the rebalancer will never send inventory from L1 to an L2 in order to fill a large deposit that creates a shortfall. Therefore, shouldn't the repayment chain choice also use similar logic in a situation where there is a large unfilled deposit creating a shortfall? In this situation, the repayment chain should ideally be the chain with the shortfall so that the relayer can fill that deposit ASAP.

@nicholaspai nicholaspai requested a review from mrice32 August 18, 2024 22:59
@nicholaspai nicholaspai requested a review from dohaki as a code owner January 2, 2025 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants