-
Notifications
You must be signed in to change notification settings - Fork 61
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
Refactor input validation #45
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,11 +23,6 @@ struct Market { | |
uint lLTV; | ||
} | ||
|
||
using {toId} for Market; | ||
function toId(Market calldata market) pure returns (Id) { | ||
return Id.wrap(keccak256(abi.encode(market))); | ||
} | ||
|
||
function irm(uint utilization) pure returns (uint) { | ||
// Divide by the number of seconds in a year. | ||
// This is a very simple model (to refine later) where x% utilization corresponds to x% APR. | ||
|
@@ -60,18 +55,16 @@ contract Blue { | |
// Markets management. | ||
|
||
function createMarket(Market calldata market) external { | ||
Id id = market.toId(); | ||
Id id = Id.wrap(keccak256(abi.encode(market))); | ||
require(lastUpdate[id] == 0, "market already exists"); | ||
|
||
accrueInterests(id); | ||
} | ||
|
||
// Supply management. | ||
|
||
function supply(Market calldata market, uint amount) external { | ||
Id id = market.toId(); | ||
require(lastUpdate[id] != 0, "unknown market"); | ||
require(amount > 0, "zero amount"); | ||
function supply(Market calldata market, uint amount) external nonZero(amount) { | ||
Id id = toExistingId(market); | ||
|
||
accrueInterests(id); | ||
|
||
|
@@ -89,10 +82,8 @@ contract Blue { | |
market.borrowableAsset.safeTransferFrom(msg.sender, address(this), amount); | ||
} | ||
|
||
function withdraw(Market calldata market, uint amount) external { | ||
Id id = market.toId(); | ||
require(lastUpdate[id] != 0, "unknown market"); | ||
require(amount > 0, "zero amount"); | ||
function withdraw(Market calldata market, uint amount) external nonZero(amount) { | ||
Id id = toExistingId(market); | ||
|
||
accrueInterests(id); | ||
|
||
|
@@ -109,10 +100,8 @@ contract Blue { | |
|
||
// Borrow management. | ||
|
||
function borrow(Market calldata market, uint amount) external { | ||
Id id = market.toId(); | ||
require(lastUpdate[id] != 0, "unknown market"); | ||
require(amount > 0, "zero amount"); | ||
function borrow(Market calldata market, uint amount) external nonZero(amount) { | ||
Id id = toExistingId(market); | ||
|
||
accrueInterests(id); | ||
|
||
|
@@ -133,10 +122,8 @@ contract Blue { | |
market.borrowableAsset.safeTransfer(msg.sender, amount); | ||
} | ||
|
||
function repay(Market calldata market, uint amount) external { | ||
Id id = market.toId(); | ||
require(lastUpdate[id] != 0, "unknown market"); | ||
require(amount > 0, "zero amount"); | ||
function repay(Market calldata market, uint amount) external nonZero(amount) { | ||
Id id = toExistingId(market); | ||
|
||
accrueInterests(id); | ||
|
||
|
@@ -151,10 +138,8 @@ contract Blue { | |
|
||
// Collateral management. | ||
|
||
function supplyCollateral(Market calldata market, uint amount) external { | ||
Id id = market.toId(); | ||
require(lastUpdate[id] != 0, "unknown market"); | ||
require(amount > 0, "zero amount"); | ||
function supplyCollateral(Market calldata market, uint amount) external nonZero(amount) { | ||
Id id = toExistingId(market); | ||
|
||
accrueInterests(id); | ||
|
||
|
@@ -163,10 +148,8 @@ contract Blue { | |
market.collateralAsset.safeTransferFrom(msg.sender, address(this), amount); | ||
} | ||
|
||
function withdrawCollateral(Market calldata market, uint amount) external { | ||
Id id = market.toId(); | ||
require(lastUpdate[id] != 0, "unknown market"); | ||
require(amount > 0, "zero amount"); | ||
function withdrawCollateral(Market calldata market, uint amount) external nonZero(amount) { | ||
Id id = toExistingId(market); | ||
|
||
accrueInterests(id); | ||
|
||
|
@@ -179,10 +162,8 @@ contract Blue { | |
|
||
// Liquidation. | ||
|
||
function liquidate(Market calldata market, address borrower, uint seized) external { | ||
Id id = market.toId(); | ||
require(lastUpdate[id] != 0, "unknown market"); | ||
require(seized > 0, "zero amount"); | ||
function liquidate(Market calldata market, address borrower, uint seized) external nonZero(seized) { | ||
Id id = toExistingId(market); | ||
|
||
accrueInterests(id); | ||
|
||
|
@@ -238,4 +219,16 @@ contract Blue { | |
uint collateralValue = collateral[id][user].wMul(market.collateralOracle.price()); | ||
return collateralValue.wMul(market.lLTV) >= borrowValue; | ||
} | ||
|
||
// Input validation | ||
|
||
modifier nonZero(uint amount) { | ||
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 would put the modifier at the top of the contract instead 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. The idea was to group the input validation functions, we want them to look for them easily |
||
require(amount > 0, "zero amount"); | ||
_; | ||
} | ||
|
||
function toExistingId(Market calldata market) private view returns (Id id) { | ||
id = Id.wrap(keccak256(abi.encode(market))); | ||
require(lastUpdate[id] != 0, "unknown market"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Annoying that we have to do that, but this solution ensures that we cannot miss the input validation of
id
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.
Did not see that my changes were already merged lol
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.
why not externalizing the logic? Keeping toId as it was and creating a specific function for the input check?
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.
This is to ensure that we cannot miss the input validation of
id
: when you externalize the logic then it's easy to use the wrong function and not do the require on the id