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

ToB code quality 3: Refactor & comment _adjustTrove #258

Merged
merged 5 commits into from
Jan 25, 2021

Conversation

RickGriff
Copy link
Collaborator

@RickGriff RickGriff commented Jan 21, 2021

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

{
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 {
Copy link
Collaborator Author

@RickGriff RickGriff Jan 22, 2021

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.

Copy link
Collaborator

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

@RickGriff RickGriff changed the title Refactor & comment _adjustLoan ToB code quality 3: Refactor & comment _adjustLoan Jan 22, 2021
@RickGriff RickGriff requested a review from bingen January 22, 2021 00:09
@RickGriff RickGriff changed the title ToB code quality 3: Refactor & comment _adjustLoan ToB code quality 3: Refactor & comment _adjustTrove Jan 22, 2021
@RickGriff RickGriff self-assigned this Jan 22, 2021
Copy link
Collaborator

@bingen bingen left a comment

Choose a reason for hiding this comment

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

Nice refactor!

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

Successfully merging this pull request may close these issues.

ToB audit, code quality recommendations 3
2 participants