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

Refactor input validation #45

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 27 additions & 34 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)));
Copy link
Contributor Author

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

Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor Author

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

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 = toId(market);

accrueInterests(id);

Expand All @@ -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 = toId(market);

accrueInterests(id);

Expand All @@ -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 = toId(market);

accrueInterests(id);

Expand All @@ -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 = toId(market);

accrueInterests(id);

Expand All @@ -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 = toId(market);

accrueInterests(id);

Expand All @@ -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 = toId(market);

accrueInterests(id);

Expand All @@ -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 = toId(market);

accrueInterests(id);

Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would put the modifier at the top of the contract instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 toId(Market calldata market) private view returns (Id id) {
id = Id.wrap(keccak256(abi.encode(market)));
require(lastUpdate[id] != 0, "unknown market");
}
}