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

Fix: pallet-pool-system - Ratio Calculation #1856

Merged
merged 10 commits into from
Jun 6, 2024

Conversation

mustermeiszer
Copy link
Collaborator

Description

The current ratio calculation has the following bug. The logic assumes that Sum(tranche.supply) = total_assets, but as the residual tranche does only receive its variable interest after the ratio calculation, this is not true.

In order to fix this, the following change does give the automatically all the remaining ratio to the residual tranche.

Changes and Descriptions

  • Adds a test to ensure ratios are actually adapting accordingly
  • Changes ratio calculation during fn do_execute_epoch

Checklist:

  • I have added Rust doc comments to structs, enums, traits and functions
  • I have made corresponding changes to the documentation
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or that my feature works

@mustermeiszer mustermeiszer added I2-bug The code fails to follow expected behaviour. D0-ready Pull request can be merged without special precaution and notification. labels Jun 4, 2024
Copy link

codecov bot commented Jun 4, 2024

Codecov Report

Attention: Patch coverage is 88.46154% with 3 lines in your changes missing coverage. Please review.

Project coverage is 46.68%. Comparing base (6e50915) to head (d175010).

Files Patch % Lines
pallets/pool-system/src/lib.rs 90.00% 2 Missing ⚠️
pallets/pool-system/src/tranches.rs 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1856      +/-   ##
==========================================
+ Coverage   46.64%   46.68%   +0.03%     
==========================================
  Files         167      167              
  Lines       13112    13129      +17     
==========================================
+ Hits         6116     6129      +13     
- Misses       6996     7000       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

AFAICT, looks good! Just wondering whether the single test is sufficient.

pallets/pool-system/src/tests/ratios.rs Outdated Show resolved Hide resolved
pallets/pool-system/src/tests/ratios.rs Outdated Show resolved Hide resolved
pallets/pool-system/src/tests/ratios.rs Outdated Show resolved Hide resolved
Comment on lines 1243 to 1261
let mut tranche_ratios = epoch
.tranches
.non_residual_tranches()
.map(|tranches| {
tranches
.iter()
// NOTE: Reversing amounts, as residual amount is on top.
// NOTE: Iterator of executed amounts is one time larger than the
// non_residual_tranche-iterator, but we anyways push all reamaining
// ratio to the residual tranche.
.zip(executed_amounts.rev())
.map(|(tranche, &(invest, redeem))| {
Ok(Perquintill::from_rational(
tranche.supply.ensure_add(invest)?.ensure_sub(redeem)?,
total_assets,
))
})
.collect::<Result<Vec<_>, ArithmeticError>>()
})
Copy link
Contributor

Choose a reason for hiding this comment

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

Can"t we simplify this by using existing Tranches API or does this not work because all API includes the residual tranche?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it is already using tranches, but not the combine as I, as you said, need to exclude the residual tranche.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Happy to add a cleaner solution if you have one at hand.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See 7886f40

@mustermeiszer
Copy link
Collaborator Author

@wischli what level of testing for the ratios would be fine for you? 3 tranche pool and 1 tranche pool additionally, or more movement in the ratios?

@mustermeiszer
Copy link
Collaborator Author

@wischli tried to clean it up a bit here. What do you think - better or worse? ^^

@wischli
Copy link
Contributor

wischli commented Jun 5, 2024

@wischli tried to clean it up a bit here. What do you think - better or worse? ^^

Thanks a lot for providing an alternate solution! IMO the new version is easier to follow and thus better readable but the first version cleaner codewise. So I vote for keeping the current version 2. Which one do you prefer?

@mustermeiszer
Copy link
Collaborator Author

Would keep the first version

Copy link
Contributor

@lemunozm lemunozm left a comment

Choose a reason for hiding this comment

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

I left some questions to know if I understand this correctly, but apart from them, LGTM!

total_assets,
)
} else {
Perquintill::one().ensure_sub(sum_non_residual_tranche_ratios)?
Copy link
Contributor

Choose a reason for hiding this comment

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

The sum of tranche ratios can never be higher than one, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this line is annulated by line 1263. No matter which value sum_non_residual_tranche_ratios has, at the end of this iteration sum_non_residual_tranche_ratios will always have the value one(). Is this the expected behavior?

pallets/pool-system/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

AFAICT, the 3 tranche test needs some minor fixes! Thanks for adding these three tests. Definitely makes everything much clearer for me.

pallets/pool-system/src/tests/ratios.rs Show resolved Hide resolved
Perquintill::from_rational(1u64, 3),
];

// Ensure ratios are 100
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC

Suggested change
// Ensure ratios are 100
// Ensure ratios are 1/3 each

pallets/pool-system/src/tests/ratios.rs Show resolved Hide resolved
pallets/pool-system/src/tests/ratios.rs Outdated Show resolved Hide resolved
wischli
wischli previously approved these changes Jun 5, 2024
Copy link
Contributor

@wischli wischli left a comment

Choose a reason for hiding this comment

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

LGTM!

@mustermeiszer mustermeiszer enabled auto-merge (squash) June 5, 2024 15:40
@mustermeiszer mustermeiszer merged commit 1e8c994 into main Jun 6, 2024
12 checks passed
@wischli wischli deleted the fix/pool-system/ratio-calculation branch June 6, 2024 09:32
@wischli wischli added this to the Centrifuge 1029 milestone Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D0-ready Pull request can be merged without special precaution and notification. I2-bug The code fails to follow expected behaviour.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants