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
feat: auto sweep earnings and accurate free balance rpc (PRO-856) #4145
Conversation
PRO-856 RPC for LP Free Balance
Needed for frontend LP app RPC needs to account for balance that is currently held inside positions (i.e. fees that have been earned, but not collected). |
801d8e5
to
71bdc7f
Compare
@@ -1106,6 +1118,62 @@ pub struct UnidirectionalPoolDepth { | |||
} | |||
|
|||
impl<T: Config> Pallet<T> { | |||
fn inner_sweep(lp: &T::AccountId) -> DispatchResult { | |||
// Collect to avoid undefined behaviour (See StorsgeMap::iter_keys documentation) |
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.
I think that iterating over the keys and modifying only the values should be ok (and in fact preferable from a memory usage POV), but I agree the documentation seems to suggest otherwise.
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.
It specifically says it is undefined
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.
I know but I looked at the code, and the implementation of translate
which does basically this internally.
And depending on which of the hierarchy of traits you read the documentation for, it sometimes says 'if you alter the value' but sometimes 'if you add or remove a value'. The latter makes sense, the former not really (why would changing the value affect the iteration of keys?).
Feel free to leave as is, I was just adding some context.
.cloned() | ||
.map(|limit_orders_cache| (side, limit_orders_cache)) | ||
}) | ||
.collect::<Vec<_>>() |
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.
Is this collect necessary?
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.
yes it will not compile otherwise, as pool is mutated inside the loop
71bdc7f
to
45a408b
Compare
worth adding a test that demonstrates the new behaviour |
Codecov Report
@@ Coverage Diff @@
## main #4145 +/- ##
=====================================
Coverage 72% 72%
=====================================
Files 378 378
Lines 60857 60921 +64
Branches 60857 60921 +64
=====================================
+ Hits 43664 43744 +80
+ Misses 14926 14904 -22
- Partials 2267 2273 +6
... and 7 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
This makes it so the free balance rpc considers the fees that have not been collected yet, and the extrinsics sweep/collect all positions before doing anything. Thereby making it seem as if earnings are always returned directly to an lp's free balance.