-
Notifications
You must be signed in to change notification settings - Fork 0
fix: Add tolerance to simple strategy #29
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
Changes from all commits
ea397a4
41e1cd6
f16bbd4
75e621d
40df721
3d19a15
9d183b1
5c7d254
16ad4b0
25d0a9c
45d956d
93c0eb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,11 @@ import { | |
| } from '../interfaces/IStrategy.js'; | ||
|
|
||
| export class Strategy implements IStrategy { | ||
| /** | ||
| * @param tolerance Value used to prevent rebalancing amounts that are already close to the target | ||
| */ | ||
| constructor(private readonly tolerance: bigint = 0n) {} | ||
|
|
||
| /** | ||
| * Get the optimized routes that will rebalance all chains to the same balance | ||
| */ | ||
|
|
@@ -16,14 +21,21 @@ export class Strategy implements IStrategy { | |
| const total = entries.reduce((sum, [, balance]) => sum + balance, 0n); | ||
| // Get the average balance | ||
| const target = total / BigInt(entries.length); | ||
|
|
||
| // Skip rebalancing when the average balance is very small | ||
| if (target < this.tolerance) { | ||
| return []; | ||
| } | ||
|
Comment on lines
25
to
28
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not understanding the use of this condition. Why would you add a tolerance bigger than the average balance? Shouldn't this throw if that condition is met? Like warning the user that's configuring something odd.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might happen when all collaterals from the route have a really low balance, resulting in a target that is below the tolerance
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we have a test for this case?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #30 🥲 |
||
|
|
||
| const surpluss: { chain: ChainName; amount: bigint }[] = []; | ||
| const deficits: { chain: ChainName; amount: bigint }[] = []; | ||
|
|
||
| // Group balances by balances with surplus or deficit | ||
| // Group balances by balances with surplus or deficit. | ||
| // The tolerance is used to consider "balanced" chains that are already close to the target | ||
| for (const [chain, balance] of entries) { | ||
| if (balance < target) { | ||
| if (balance < target - this.tolerance) { | ||
| deficits.push({ chain, amount: target - balance }); | ||
| } else if (balance > target) { | ||
| } else if (balance > target + this.tolerance) { | ||
| surpluss.push({ chain, amount: balance - target }); | ||
| } else { | ||
| // Do nothing as the balance is already on target | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we let the user know that they should express the tolerance in integer and in "wei" (or whatever the amount of decimals that the tokens have)?
Or this is too obvious to be explained?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added it to the command description 45d956d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect!