Skip to content

Commit

Permalink
Merge pull request #95 from morpho-labs/refactor/uint256
Browse files Browse the repository at this point in the history
Style `uint` -> `uint256`
  • Loading branch information
MerlinEgalite authored Jul 10, 2023
2 parents 0a1b22c + d872653 commit 28a3995
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 110 deletions.
3 changes: 0 additions & 3 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
[profile.default]
via-ir = true

[fmt]
int_types = "short"

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
71 changes: 36 additions & 35 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {IOracle} from "src/interfaces/IOracle.sol";
import {MathLib} from "src/libraries/MathLib.sol";
import {SafeTransferLib} from "src/libraries/SafeTransferLib.sol";

uint constant WAD = 1e18;
uint constant ALPHA = 0.5e18;
uint256 constant WAD = 1e18;
uint256 constant ALPHA = 0.5e18;

// Market id.
type Id is bytes32;
Expand All @@ -21,7 +21,7 @@ struct Market {
IOracle borrowableOracle;
IOracle collateralOracle;
IIrm irm;
uint lltv;
uint256 lltv;
}

using {toId} for Market;
Expand All @@ -31,33 +31,33 @@ function toId(Market calldata market) pure returns (Id) {
}

contract Blue {
using MathLib for uint;
using MathLib for uint256;
using SafeTransferLib for IERC20;

// Storage.

// Owner.
address public owner;
// User' supply balances.
mapping(Id => mapping(address => uint)) public supplyShare;
mapping(Id => mapping(address => uint256)) public supplyShare;
// User' borrow balances.
mapping(Id => mapping(address => uint)) public borrowShare;
mapping(Id => mapping(address => uint256)) public borrowShare;
// User' collateral balance.
mapping(Id => mapping(address => uint)) public collateral;
mapping(Id => mapping(address => uint256)) public collateral;
// Market total supply.
mapping(Id => uint) public totalSupply;
mapping(Id => uint256) public totalSupply;
// Market total supply shares.
mapping(Id => uint) public totalSupplyShares;
mapping(Id => uint256) public totalSupplyShares;
// Market total borrow.
mapping(Id => uint) public totalBorrow;
mapping(Id => uint256) public totalBorrow;
// Market total borrow shares.
mapping(Id => uint) public totalBorrowShares;
mapping(Id => uint256) public totalBorrowShares;
// Interests last update (used to check if a market has been created).
mapping(Id => uint) public lastUpdate;
mapping(Id => uint256) public lastUpdate;
// Enabled IRMs.
mapping(IIrm => bool) public isIrmEnabled;
// Enabled LLTVs.
mapping(uint => bool) public isLltvEnabled;
mapping(uint256 => bool) public isLltvEnabled;

// Constructor.

Expand All @@ -82,7 +82,7 @@ contract Blue {
isIrmEnabled[irm] = true;
}

function enableLltv(uint lltv) external onlyOwner {
function enableLltv(uint256 lltv) external onlyOwner {
require(lltv < WAD, "LLTV too high");
isLltvEnabled[lltv] = true;
}
Expand All @@ -100,7 +100,7 @@ contract Blue {

// Supply management.

function supply(Market calldata market, uint amount) external {
function supply(Market calldata market, uint256 amount) external {
Id id = market.toId();
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");
Expand All @@ -111,7 +111,7 @@ contract Blue {
supplyShare[id][msg.sender] = WAD;
totalSupplyShares[id] = WAD;
} else {
uint shares = amount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id]);
uint256 shares = amount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id]);
supplyShare[id][msg.sender] += shares;
totalSupplyShares[id] += shares;
}
Expand All @@ -121,14 +121,14 @@ contract Blue {
market.borrowableAsset.safeTransferFrom(msg.sender, address(this), amount);
}

function withdraw(Market calldata market, uint amount) external {
function withdraw(Market calldata market, uint256 amount) external {
Id id = market.toId();
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");

accrueInterests(market, id);

uint shares = amount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id]);
uint256 shares = amount.wMul(totalSupplyShares[id]).wDiv(totalSupply[id]);
supplyShare[id][msg.sender] -= shares;
totalSupplyShares[id] -= shares;

Expand All @@ -141,7 +141,7 @@ contract Blue {

// Borrow management.

function borrow(Market calldata market, uint amount) external {
function borrow(Market calldata market, uint256 amount) external {
Id id = market.toId();
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");
Expand All @@ -152,7 +152,7 @@ contract Blue {
borrowShare[id][msg.sender] = WAD;
totalBorrowShares[id] = WAD;
} else {
uint shares = amount.wMul(totalBorrowShares[id]).wDiv(totalBorrow[id]);
uint256 shares = amount.wMul(totalBorrowShares[id]).wDiv(totalBorrow[id]);
borrowShare[id][msg.sender] += shares;
totalBorrowShares[id] += shares;
}
Expand All @@ -165,14 +165,14 @@ contract Blue {
market.borrowableAsset.safeTransfer(msg.sender, amount);
}

function repay(Market calldata market, uint amount) external {
function repay(Market calldata market, uint256 amount) external {
Id id = market.toId();
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");

accrueInterests(market, id);

uint shares = amount.wMul(totalBorrowShares[id]).wDiv(totalBorrow[id]);
uint256 shares = amount.wMul(totalBorrowShares[id]).wDiv(totalBorrow[id]);
borrowShare[id][msg.sender] -= shares;
totalBorrowShares[id] -= shares;

Expand All @@ -184,7 +184,7 @@ contract Blue {
// Collateral management.

/// @dev Don't accrue interests because it's not required and it saves gas.
function supplyCollateral(Market calldata market, uint amount) external {
function supplyCollateral(Market calldata market, uint256 amount) external {
Id id = market.toId();
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");
Expand All @@ -196,7 +196,7 @@ contract Blue {
market.collateralAsset.safeTransferFrom(msg.sender, address(this), amount);
}

function withdrawCollateral(Market calldata market, uint amount) external {
function withdrawCollateral(Market calldata market, uint256 amount) external {
Id id = market.toId();
require(lastUpdate[id] != 0, "unknown market");
require(amount != 0, "zero amount");
Expand All @@ -212,7 +212,7 @@ contract Blue {

// Liquidation.

function liquidate(Market calldata market, address borrower, uint seized) external {
function liquidate(Market calldata market, address borrower, uint256 seized) external {
Id id = market.toId();
require(lastUpdate[id] != 0, "unknown market");
require(seized != 0, "zero amount");
Expand All @@ -222,9 +222,10 @@ contract Blue {
require(!isHealthy(market, id, borrower), "cannot liquidate a healthy position");

// The liquidation incentive is 1 + ALPHA * (1 / LLTV - 1).
uint incentive = WAD + ALPHA.wMul(WAD.wDiv(market.lltv) - WAD);
uint repaid = seized.wMul(market.collateralOracle.price()).wDiv(incentive).wDiv(market.borrowableOracle.price());
uint repaidShares = repaid.wMul(totalBorrowShares[id]).wDiv(totalBorrow[id]);
uint256 incentive = WAD + ALPHA.wMul(WAD.wDiv(market.lltv) - WAD);
uint256 repaid =
seized.wMul(market.collateralOracle.price()).wDiv(incentive).wDiv(market.borrowableOracle.price());
uint256 repaidShares = repaid.wMul(totalBorrowShares[id]).wDiv(totalBorrow[id]);

borrowShare[id][borrower] -= repaidShares;
totalBorrowShares[id] -= repaidShares;
Expand All @@ -246,12 +247,12 @@ contract Blue {
// Interests management.

function accrueInterests(Market calldata market, Id id) private {
uint marketTotalSupply = totalSupply[id];
uint256 marketTotalSupply = totalSupply[id];

if (marketTotalSupply != 0) {
uint marketTotalBorrow = totalBorrow[id];
uint borrowRate = market.irm.borrowRate(market);
uint accruedInterests = marketTotalBorrow.wMul(borrowRate).wMul(block.timestamp - lastUpdate[id]);
uint256 marketTotalBorrow = totalBorrow[id];
uint256 borrowRate = market.irm.borrowRate(market);
uint256 accruedInterests = marketTotalBorrow.wMul(borrowRate).wMul(block.timestamp - lastUpdate[id]);
totalSupply[id] = marketTotalSupply + accruedInterests;
totalBorrow[id] = marketTotalBorrow + accruedInterests;
}
Expand All @@ -262,12 +263,12 @@ contract Blue {
// Health check.

function isHealthy(Market calldata market, Id id, address user) private view returns (bool) {
uint borrowShares = borrowShare[id][user];
uint256 borrowShares = borrowShare[id][user];
if (borrowShares == 0) return true;
// totalBorrowShares[id] > 0 when borrowShares > 0.
uint borrowValue =
uint256 borrowValue =
borrowShares.wMul(totalBorrow[id]).wDiv(totalBorrowShares[id]).wMul(market.borrowableOracle.price());
uint collateralValue = collateral[id][user].wMul(market.collateralOracle.price());
uint256 collateralValue = collateral[id][user].wMul(market.collateralOracle.price());
return collateralValue.wMul(market.lltv) >= borrowValue;
}
}
2 changes: 1 addition & 1 deletion src/interfaces/IIrm.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pragma solidity >=0.5.0;
import {Market} from "src/Blue.sol";

interface IIrm {
function borrowRate(Market calldata market) external returns (uint);
function borrowRate(Market calldata market) external returns (uint256);
}
2 changes: 1 addition & 1 deletion src/interfaces/IOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
pragma solidity >=0.5.0;

interface IOracle {
function price() external view returns (uint);
function price() external view returns (uint256);
}
8 changes: 4 additions & 4 deletions src/libraries/MathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ pragma solidity ^0.8.0;

/// @notice Maths utils.
library MathLib {
uint internal constant WAD = 1e18;
uint256 internal constant WAD = 1e18;

function min(uint x, uint y) internal pure returns (uint z) {
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = x < y ? x : y;
}

/// @dev Rounds towards zero.
function wMul(uint x, uint y) internal pure returns (uint z) {
function wMul(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = (x * y) / WAD;
}

/// @dev Rounds towards zero.
function wDiv(uint x, uint y) internal pure returns (uint z) {
function wDiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = (x * WAD) / y;
}
}
4 changes: 2 additions & 2 deletions src/libraries/SafeTransferLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {IERC20} from "src/interfaces/IERC20.sol";
/// @dev Use with caution! Some functions in this library knowingly create dirty bits at the destination of the free memory pointer.
/// @dev Note that none of the functions in this library check that a token has code at all! That responsibility is delegated to the caller.
library SafeTransferLib {
function safeTransferFrom(IERC20 token, address from, address to, uint amount) internal {
function safeTransferFrom(IERC20 token, address from, address to, uint256 amount) internal {
bool success;

/// @solidity memory-safe-assembly
Expand Down Expand Up @@ -38,7 +38,7 @@ library SafeTransferLib {
require(success, "TRANSFER_FROM_FAILED");
}

function safeTransfer(IERC20 token, address to, uint amount) internal {
function safeTransfer(IERC20 token, address to, uint256 amount) internal {
bool success;

/// @solidity memory-safe-assembly
Expand Down
2 changes: 1 addition & 1 deletion src/mocks/ERC20Mock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {ERC20} from "solmate/tokens/ERC20.sol";
contract ERC20Mock is ERC20 {
constructor(string memory _name, string memory _symbol, uint8 _decimals) ERC20(_name, _symbol, _decimals) {}

function setBalance(address owner, uint amount) external {
function setBalance(address owner, uint256 amount) external {
balanceOf[owner] = amount;
}
}
6 changes: 3 additions & 3 deletions src/mocks/IrmMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import {MathLib} from "src/libraries/MathLib.sol";
import "src/Blue.sol";

contract IrmMock is IIrm {
using MathLib for uint;
using MathLib for uint256;

Blue public immutable blue;

constructor(Blue blueInstance) {
blue = Blue(blueInstance);
}

function borrowRate(Market calldata market) external view returns (uint) {
function borrowRate(Market calldata market) external view returns (uint256) {
Id id = Id.wrap(keccak256(abi.encode(market)));
uint utilization = blue.totalBorrow(id).wDiv(blue.totalSupply(id));
uint256 utilization = blue.totalBorrow(id).wDiv(blue.totalSupply(id));

// 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
4 changes: 2 additions & 2 deletions src/mocks/OracleMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pragma solidity 0.8.20;
import {IOracle} from "src/interfaces/IOracle.sol";

contract OracleMock is IOracle {
uint public price;
uint256 public price;

function setPrice(uint newPrice) external {
function setPrice(uint256 newPrice) external {
price = newPrice;
}
}
Loading

0 comments on commit 28a3995

Please sign in to comment.