-
Notifications
You must be signed in to change notification settings - Fork 322
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
ToB code quality 3: Refactor & comment _adjustTrove #258
Conversation
{ | ||
uint newTCR = _getNewTCRFromTroveChange(_collChange, _isCollIncrease, _debtChange, _isDebtIncrease, _price); | ||
require(newTCR >= CCR, "BorrowerOps: An operation that would result in TCR < CCR is not permitted"); | ||
function _requireNewICRisAboveOldICR(uint _newICR, uint _oldICR) internal pure { |
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.
We now have four similar functions: requireICRisAbove...
, requireNewICRisAbove...
, requireNewTCRsAbove..
, etc.
Perhaps we should just change that to a single:
requireCRsAboveThreshold(uint CR, uint _threshold, string memory _error)
and pass the appropriate ICR, TCR, MCR, etc.
We'd have to pass different error strings in each call though if we want good information in tests, which makes the top-level code messy again, and the calls are no longer more concise than a naked require
. Not sure which I prefer.
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, maybe it’s ok as it is, also I wonder if passing the strings may increase gas
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.
Nice refactor!
Addresses ToB's code quality recommendation 3: #245
"Consider refactoring the adjustTrove function. This adjustTrove function implements some important operations, but it is difficult to review because of thelack of inline documentation and their checks cover a large variety of corner cases. This will make the code easier to understand, maintain, and review."
I've extracted a couple of sub-functions, renamed some
requires
and added comments in an attempt to make the code clearer.There's only so much we can do though - it's still a complicated function, given the number of branches and different cases it covers.
(PS, ugh - I wrote "adjustLoan" in commit messages. That was its name for a long time, I have clearly not fully "adjusted"...)
Fixes #245