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 amount -> assets #277

Merged
merged 3 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
106 changes: 53 additions & 53 deletions src/Morpho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,162 +155,162 @@ contract Morpho is IMorpho {
/* SUPPLY MANAGEMENT */

/// @inheritdoc IMorpho
function supply(Market memory market, uint256 amount, uint256 shares, address onBehalf, bytes calldata data)
function supply(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes calldata data)
external
returns (uint256, uint256)
{
Id id = market.id();
require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED);
require(UtilsLib.exactlyOneZero(amount, shares), ErrorsLib.INCONSISTENT_INPUT);
require(UtilsLib.exactlyOneZero(assets, shares), ErrorsLib.INCONSISTENT_INPUT);
require(onBehalf != address(0), ErrorsLib.ZERO_ADDRESS);

_accrueInterests(market, id);

if (amount > 0) shares = amount.toSharesDown(totalSupply[id], totalSupplyShares[id]);
else amount = shares.toAssetsUp(totalSupply[id], totalSupplyShares[id]);
if (assets > 0) shares = assets.toSharesDown(totalSupply[id], totalSupplyShares[id]);
else assets = shares.toAssetsUp(totalSupply[id], totalSupplyShares[id]);

supplyShares[id][onBehalf] += shares;
totalSupplyShares[id] += shares;
totalSupply[id] += amount;
totalSupply[id] += assets;

emit EventsLib.Supply(id, msg.sender, onBehalf, amount, shares);
emit EventsLib.Supply(id, msg.sender, onBehalf, assets, shares);

if (data.length > 0) IMorphoSupplyCallback(msg.sender).onMorphoSupply(amount, data);
if (data.length > 0) IMorphoSupplyCallback(msg.sender).onMorphoSupply(assets, data);

IERC20(market.borrowableAsset).safeTransferFrom(msg.sender, address(this), amount);
IERC20(market.borrowableToken).safeTransferFrom(msg.sender, address(this), assets);

return (amount, shares);
return (assets, shares);
}

/// @inheritdoc IMorpho
function withdraw(Market memory market, uint256 amount, uint256 shares, address onBehalf, address receiver)
function withdraw(Market memory market, uint256 assets, uint256 shares, address onBehalf, address receiver)
external
returns (uint256, uint256)
{
Id id = market.id();
require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED);
require(UtilsLib.exactlyOneZero(amount, shares), ErrorsLib.INCONSISTENT_INPUT);
require(UtilsLib.exactlyOneZero(assets, shares), ErrorsLib.INCONSISTENT_INPUT);
// No need to verify that onBehalf != address(0) thanks to the authorization check.
require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);
require(_isSenderAuthorized(onBehalf), ErrorsLib.UNAUTHORIZED);

_accrueInterests(market, id);

if (amount > 0) shares = amount.toSharesUp(totalSupply[id], totalSupplyShares[id]);
else amount = shares.toAssetsDown(totalSupply[id], totalSupplyShares[id]);
if (assets > 0) shares = assets.toSharesUp(totalSupply[id], totalSupplyShares[id]);
else assets = shares.toAssetsDown(totalSupply[id], totalSupplyShares[id]);

supplyShares[id][onBehalf] -= shares;
totalSupplyShares[id] -= shares;
totalSupply[id] -= amount;
totalSupply[id] -= assets;

emit EventsLib.Withdraw(id, msg.sender, onBehalf, receiver, amount, shares);
emit EventsLib.Withdraw(id, msg.sender, onBehalf, receiver, assets, shares);

require(totalBorrow[id] <= totalSupply[id], ErrorsLib.INSUFFICIENT_LIQUIDITY);

IERC20(market.borrowableAsset).safeTransfer(receiver, amount);
IERC20(market.borrowableToken).safeTransfer(receiver, assets);

return (amount, shares);
return (assets, shares);
}

/* BORROW MANAGEMENT */

/// @inheritdoc IMorpho
function borrow(Market memory market, uint256 amount, uint256 shares, address onBehalf, address receiver)
function borrow(Market memory market, uint256 assets, uint256 shares, address onBehalf, address receiver)
external
returns (uint256, uint256)
{
Id id = market.id();
require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED);
require(UtilsLib.exactlyOneZero(amount, shares), ErrorsLib.INCONSISTENT_INPUT);
require(UtilsLib.exactlyOneZero(assets, shares), ErrorsLib.INCONSISTENT_INPUT);
// No need to verify that onBehalf != address(0) thanks to the authorization check.
require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);
require(_isSenderAuthorized(onBehalf), ErrorsLib.UNAUTHORIZED);

_accrueInterests(market, id);

if (amount > 0) shares = amount.toSharesUp(totalBorrow[id], totalBorrowShares[id]);
else amount = shares.toAssetsDown(totalBorrow[id], totalBorrowShares[id]);
if (assets > 0) shares = assets.toSharesUp(totalBorrow[id], totalBorrowShares[id]);
else assets = shares.toAssetsDown(totalBorrow[id], totalBorrowShares[id]);

borrowShares[id][onBehalf] += shares;
totalBorrowShares[id] += shares;
totalBorrow[id] += amount;
totalBorrow[id] += assets;

emit EventsLib.Borrow(id, msg.sender, onBehalf, receiver, amount, shares);
emit EventsLib.Borrow(id, msg.sender, onBehalf, receiver, assets, shares);

require(_isHealthy(market, id, onBehalf), ErrorsLib.INSUFFICIENT_COLLATERAL);
require(totalBorrow[id] <= totalSupply[id], ErrorsLib.INSUFFICIENT_LIQUIDITY);

IERC20(market.borrowableAsset).safeTransfer(receiver, amount);
IERC20(market.borrowableToken).safeTransfer(receiver, assets);

return (amount, shares);
return (assets, shares);
}

/// @inheritdoc IMorpho
function repay(Market memory market, uint256 amount, uint256 shares, address onBehalf, bytes calldata data)
function repay(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes calldata data)
external
returns (uint256, uint256)
{
Id id = market.id();
require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED);
require(UtilsLib.exactlyOneZero(amount, shares), ErrorsLib.INCONSISTENT_INPUT);
require(UtilsLib.exactlyOneZero(assets, shares), ErrorsLib.INCONSISTENT_INPUT);
require(onBehalf != address(0), ErrorsLib.ZERO_ADDRESS);

_accrueInterests(market, id);

if (amount > 0) shares = amount.toSharesDown(totalBorrow[id], totalBorrowShares[id]);
else amount = shares.toAssetsUp(totalBorrow[id], totalBorrowShares[id]);
if (assets > 0) shares = assets.toSharesDown(totalBorrow[id], totalBorrowShares[id]);
else assets = shares.toAssetsUp(totalBorrow[id], totalBorrowShares[id]);

borrowShares[id][onBehalf] -= shares;
totalBorrowShares[id] -= shares;
totalBorrow[id] -= amount;
totalBorrow[id] -= assets;

emit EventsLib.Repay(id, msg.sender, onBehalf, amount, shares);
emit EventsLib.Repay(id, msg.sender, onBehalf, assets, shares);

if (data.length > 0) IMorphoRepayCallback(msg.sender).onMorphoRepay(amount, data);
if (data.length > 0) IMorphoRepayCallback(msg.sender).onMorphoRepay(assets, data);

IERC20(market.borrowableAsset).safeTransferFrom(msg.sender, address(this), amount);
IERC20(market.borrowableToken).safeTransferFrom(msg.sender, address(this), assets);

return (amount, shares);
return (assets, shares);
}

/* COLLATERAL MANAGEMENT */

/// @inheritdoc IMorpho
function supplyCollateral(Market memory market, uint256 amount, address onBehalf, bytes calldata data) external {
function supplyCollateral(Market memory market, uint256 assets, address onBehalf, bytes calldata data) external {
Id id = market.id();
require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED);
require(amount != 0, ErrorsLib.ZERO_AMOUNT);
require(assets != 0, ErrorsLib.ZERO_ASSETS);
require(onBehalf != address(0), ErrorsLib.ZERO_ADDRESS);

// Don't accrue interests because it's not required and it saves gas.

collateral[id][onBehalf] += amount;
collateral[id][onBehalf] += assets;

emit EventsLib.SupplyCollateral(id, msg.sender, onBehalf, amount);
emit EventsLib.SupplyCollateral(id, msg.sender, onBehalf, assets);

if (data.length > 0) IMorphoSupplyCollateralCallback(msg.sender).onMorphoSupplyCollateral(amount, data);
if (data.length > 0) IMorphoSupplyCollateralCallback(msg.sender).onMorphoSupplyCollateral(assets, data);

IERC20(market.collateralAsset).safeTransferFrom(msg.sender, address(this), amount);
IERC20(market.collateralToken).safeTransferFrom(msg.sender, address(this), assets);
}

/// @inheritdoc IMorpho
function withdrawCollateral(Market memory market, uint256 amount, address onBehalf, address receiver) external {
function withdrawCollateral(Market memory market, uint256 assets, address onBehalf, address receiver) external {
Id id = market.id();
require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED);
require(amount != 0, ErrorsLib.ZERO_AMOUNT);
require(assets != 0, ErrorsLib.ZERO_ASSETS);
// No need to verify that onBehalf != address(0) thanks to the authorization check.
require(receiver != address(0), ErrorsLib.ZERO_ADDRESS);
require(_isSenderAuthorized(onBehalf), ErrorsLib.UNAUTHORIZED);

_accrueInterests(market, id);

collateral[id][onBehalf] -= amount;
collateral[id][onBehalf] -= assets;

emit EventsLib.WithdrawCollateral(id, msg.sender, onBehalf, receiver, amount);
emit EventsLib.WithdrawCollateral(id, msg.sender, onBehalf, receiver, assets);

require(_isHealthy(market, id, onBehalf), ErrorsLib.INSUFFICIENT_COLLATERAL);

IERC20(market.collateralAsset).safeTransfer(receiver, amount);
IERC20(market.collateralToken).safeTransfer(receiver, assets);
}

/* LIQUIDATION */
Expand All @@ -319,7 +319,7 @@ contract Morpho is IMorpho {
function liquidate(Market memory market, address borrower, uint256 seized, bytes calldata data) external {
Id id = market.id();
require(lastUpdate[id] != 0, ErrorsLib.MARKET_NOT_CREATED);
require(seized != 0, ErrorsLib.ZERO_AMOUNT);
require(seized != 0, ErrorsLib.ZERO_ASSETS);

_accrueInterests(market, id);

Expand Down Expand Up @@ -349,26 +349,26 @@ contract Morpho is IMorpho {
borrowShares[id][borrower] = 0;
}

IERC20(market.collateralAsset).safeTransfer(msg.sender, seized);
IERC20(market.collateralToken).safeTransfer(msg.sender, seized);

emit EventsLib.Liquidate(id, msg.sender, borrower, repaid, repaidShares, seized, badDebtShares);

if (data.length > 0) IMorphoLiquidateCallback(msg.sender).onMorphoLiquidate(repaid, data);

IERC20(market.borrowableAsset).safeTransferFrom(msg.sender, address(this), repaid);
IERC20(market.borrowableToken).safeTransferFrom(msg.sender, address(this), repaid);
}

/* FLASH LOANS */

/// @inheritdoc IFlashLender
function flashLoan(address token, uint256 amount, bytes calldata data) external {
IERC20(token).safeTransfer(msg.sender, amount);
function flashLoan(address token, uint256 assets, bytes calldata data) external {
IERC20(token).safeTransfer(msg.sender, assets);

emit EventsLib.FlashLoan(msg.sender, token, amount);
emit EventsLib.FlashLoan(msg.sender, token, assets);

IMorphoFlashLoanCallback(msg.sender).onMorphoFlashLoan(amount, data);
IMorphoFlashLoanCallback(msg.sender).onMorphoFlashLoan(assets, data);

IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
IERC20(token).safeTransferFrom(msg.sender, address(this), assets);
}

/* AUTHORIZATION */
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/IFlashLender.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pragma solidity >=0.5.0;
interface IFlashLender {
/// @notice Executes a flash loan.
/// @param token The token to flash loan.
/// @param amount The amount to flash loan.
/// @param assets The amount of assets to flash loan.
/// @param data Arbitrary data to pass to the `onMorphoFlashLoan` callback.
function flashLoan(address token, uint256 amount, bytes calldata data) external;
function flashLoan(address token, uint256 assets, bytes calldata data) external;
}
Loading