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

Relative daily limits for the erc20-family bridges #287

Open
akolotov opened this issue Sep 15, 2019 · 12 comments
Open

Relative daily limits for the erc20-family bridges #287

akolotov opened this issue Sep 15, 2019 · 12 comments

Comments

@akolotov
Copy link
Collaborator

All three bridge mode (native-to-erc20, erc20-to-erc20 and erc20-to-native) operates with absolute daily limits. For example, if the limit for the xDai bridge is equal 100000 xDai it means that if daily value of tokens sent through the bridge in one direction exceeds this limit, bridge operation to transfer tokens in this direction will be restricted till the end of this day.

But it is not correct from security point of view for the cases when the bridge balance is less than the limit. It will allow to dry out the bridge balance in one transfer request.

In order to support relative daily limits instead of current absolute limits it is suggested to introduce two additional parameters:

  • relative limit threshold
  • relative limit

The relative limit threshold defines a minimal bridge balance when the relative limit is not applied.
The relative limit defines a part of the bridge balance that is allowed to be transferred through the bridge per one day.

The logic can be described in the following pseudocode:

balance = Token.balanceOf(bridge_address);
accumulated_through_day += value_to_transfer;
if (balance > relative_limit_threshold) {
    if (accumulated_through_day <= (balance * relative_limit / 1 ether)) {
        allow_transfer = true;
    }
    else {
        allow_transfer = false;
    }
} else {
    allow_transfer = true;
}
@k1rill-fedoseev
Copy link
Member

It is also possible to make relative_limit parameter in the proposed solution computable.
Let's take a parabola function as a dependency of relative limit on current bridge balance. Also let's take some target relative limit - target_relative_limit, which will be applied after some upper bound of a bridge balance - max_balance.
Then the relative_limit can be computed using the following pseudocode:

if (balance > max_balance) {
    relative_limit = target_relative_limit;
} else {
    a = (1 ether - target_relative_limit) / (max_balance - relative_limit_threshold) ** 2;
    b = -2 * a * max_balance;
    c = target_relative_limit + a * max_balance ** 2
    relative_limit = a * balance ** 2 + b * balance + c;
}

The dependency of relative limit on current balance then will look like the following:
Screenshot 2019-10-04 at 22 25 15

@akolotov
Copy link
Collaborator Author

akolotov commented Oct 4, 2019

Thanks @k1rill-fedoseev, it is better to use minPerTx() instead of relative_limit_threshold. And max_balance actually means some minimal balance: below this threshold your formula can be used, above this threshold some fixed value (target_relative_limit) will be applied.

@maxaleks
Copy link
Contributor

maxaleks commented Oct 8, 2019

Hi @k1rill-fedoseev and @akolotov
I don't understand some things. Are target_relative_limit and relative_limit percentages? If yes, then on the image we have 50 ether limit at 1000 ether balance and about 160 ether limit at 400 ether balance. Is this correct?

@varasev
Copy link
Contributor

varasev commented Oct 8, 2019

@k1rill-fedoseev Please make an example with the explicit numbers for all of the variables in your pseudocode shown above and please recheck the formulas.

@k1rill-fedoseev
Copy link
Member

Hi, @maxaleks, @varasev
Let's consider a concrete example:

We have a relative_limit_threshold = 20 ether, which is indeed, as @akolotov mentioned, can equal to minPerTx().
Now we have max_balance = 1000 ether, which describes the upper bound on argument in our formula, for balance > max_balance we simply take target_relative_limit as our relative limit.
target_relative_limit and relative_limit are evaluated with respect to 1 ether, so target limit of 5% will correspond to 0.05 ether.

Let's now evaluate relative limit that corresponds to bridge balance of 400 ether:

a = (1 ether - 0.05 ether) / (1000 ether - 20 ether) ** 2 = 0.95 ether / (980 ether)^2 = 0.95 / (980^2 ether) = 0.000001 / ether
b = -2 * a * 1000 ether = -1900 ether / (980^2 ether) = -0.002
c = 0.05 ether + a * (1000 ether) ** 2 = 0.05 ether + 0.95 ether * (1000/980) ** 2 = 1.04 ether
relative_limit = a * (400 ether)^2 + b * 400 ether + c = 0.16 ether - 0.8 ether + 1.04 ether = 0.4 ether = 40%

So relative_limit at balance = 400 ether is about 40%, which is about 160 ether.

@varasev
Copy link
Contributor

varasev commented Oct 8, 2019

@k1rill-fedoseev thanks for the clarification

@maxaleks
Copy link
Contributor

maxaleks commented Oct 8, 2019

@k1rill-fedoseev thanks

But I think that the final limit should be maximum at point 1000 in [0, 1000] range

Example:
image

@akolotov what do you think?

@akolotov
Copy link
Collaborator Author

@k1rill-fedoseev thanks

But I think that the final limit should be maximum at point 1000 in [0, 1000] range

@akolotov what do you think?

what do you mean under "final limit"?

@maxaleks
Copy link
Contributor

what do you mean under "final limit"?

Tokens amount. "C" column in the screenshot above

@akolotov
Copy link
Collaborator Author

The idea was to make the limit dependable on the bridge balance if the balance is below some threshold. Lower balance allows to withdraw bigger part of its part. The balance approaching to the threshold value allows to withdraw the value approaching to the daily limit.

image
Correlation between the daily limit and the bridge balance
Red line: balance; Blue line: daily limit

@akolotov
Copy link
Collaborator Author

The logic how the limits for the generic bridge contracts work is covered in https://docs.tokenbridge.net/about-tokenbridge/transfer-limits.

@akolotov
Copy link
Collaborator Author

It is necessary to note the following requirements for this feature:

  1. If we consider two operations deposit and withdrawal such as the balance of the bridge is increased and after the first one and reduced after the second one, so the relative daily limits must be applied only for the withdrawals. For the deposits absolute limits should be used still.

  2. The value of the relative daily limit must be calculated once per day with the first withdrawal came to the bridge. For the first iteration of the feature development it is suggested to implement the approach Limit fixed for a day. Below is explanations how it should behave:

Bridge balance is 10'000 tokens. The relative daily limit is 6%.

  1. A withdraws 100 tokens on Monday
    • the daily limit is calculated as 600 tokens
    • new balance is 9'900 tokens
    • the remaining amount of tokens allowable to be withdrawn on Monday is 500 tokens
  2. B withdraws 150 tokens on Monday
    • new balance is 9'750 tokens
    • the remaining amount of tokens allowable to be withdrawn on Monday is 350 tokens
  3. C deposits 3'000 tokens on Monday
    • new balance is 12'750 tokens
  4. D withdraws 300 tokens on Monday
    • new balance is 12'450 tokens
    • the remaining amount of tokens allowable to be withdrawn on Monday is 50 tokens
  5. E withdraws 500 tokens on Tuesday
    • the daily limit is calculated as 747 tokens
    • new balance is 11'950 tokens
    • the remaining amount of tokens allowable to be withdrawn on Tuesday is 147 tokens

Later, the approach can be changed to the approach Limit is increased by deposits

Bridge balance is 10'000 tokens. The relative daily limit is 6%.

  1. A withdraws 100 tokens on Monday
    • the daily limit is calculated as 600 tokens
    • new balance is 9'900 tokens
    • the remaining amount of tokens allowable to be withdrawn on Monday is 500 tokens
  2. B withdraws 150 tokens on Monday
    • new balance is 9'750 tokens
    • the remaining amount of tokens allowable to be withdrawn on Monday is 350 tokens
  3. C deposits 3'000 tokens on Monday
    • new balance is 12'750 tokens
    • the remaining amount of tokens allowable to be withdrawn on Monday is 530 tokens
  4. D withdraws 500 tokens on Monday
    • new balance is 12'250 tokens
    • the remaining amount of tokens allowable to be withdrawn on Monday is 30 tokens
  5. E withdraws 400 tokens on Tuesday
    • the daily limit is calculated as 735 tokens
    • new balance is 11'850 tokens
    • the remaining amount of tokens allowable to be withdrawn on Tuesday is 235 tokens

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

No branches or pull requests

4 participants