Skip to content

Commit

Permalink
refactor: amount -> assets
Browse files Browse the repository at this point in the history
  • Loading branch information
MerlinEgalite committed Aug 11, 2023
1 parent 28014bd commit 9cf53c0
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 200 deletions.
94 changes: 47 additions & 47 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -156,150 +156,150 @@ contract Blue is IBlue {
/* SUPPLY MANAGEMENT */

/// @inheritdoc IBlue
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
{
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) IBlueSupplyCallback(msg.sender).onBlueSupply(amount, data);
if (data.length > 0) IBlueSupplyCallback(msg.sender).onBlueSupply(assets, data);

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

/// @inheritdoc IBlue
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
{
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.borrowableAsset).safeTransfer(receiver, assets);
}

/* BORROW MANAGEMENT */

/// @inheritdoc IBlue
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
{
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.borrowableAsset).safeTransfer(receiver, assets);
}

/// @inheritdoc IBlue
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
{
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) IBlueRepayCallback(msg.sender).onBlueRepay(amount, data);
if (data.length > 0) IBlueRepayCallback(msg.sender).onBlueRepay(assets, data);

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

/* COLLATERAL MANAGEMENT */

/// @inheritdoc IBlue
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_AMOUNT);
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) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(amount, data);
if (data.length > 0) IBlueSupplyCollateralCallback(msg.sender).onBlueSupplyCollateral(assets, data);

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

/// @inheritdoc IBlue
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_AMOUNT);
// 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.collateralAsset).safeTransfer(receiver, assets);
}

/* LIQUIDATION */
Expand Down Expand Up @@ -350,14 +350,14 @@ contract Blue is IBlue {
/* 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);

IBlueFlashLoanCallback(msg.sender).onBlueFlashLoan(token, amount, data);
IBlueFlashLoanCallback(msg.sender).onBlueFlashLoan(token, assets, data);

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

/* AUTHORIZATION */
Expand Down Expand Up @@ -418,7 +418,7 @@ contract Blue is IBlue {
uint256 feeShares;
if (fee[id] != 0) {
uint256 feeAmount = accruedInterests.wMulDown(fee[id]);
// The fee amount is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated.
// The fee assets is subtracted from the total supply in this calculation to compensate for the fact that total supply is already updated.
feeShares = feeAmount.mulDivDown(totalSupplyShares[id], totalSupply[id] - feeAmount);
supplyShares[id][feeRecipient] += feeShares;
totalSupplyShares[id] += feeShares;
Expand Down
62 changes: 31 additions & 31 deletions src/interfaces/IBlue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ interface IBlue is IFlashLender {
/// @notice The `user`'s collateral balance on the market defined by the given `id`.
function collateral(Id id, address user) external view returns (uint256);

/// @notice The total amount of assets supplied to the market defined by the given `id`.
/// @notice The total assets supplied to the market defined by the given `id`.
/// @dev The value can be incaccurate since it does not take into account the accrued interests.
function totalSupply(Id id) external view returns (uint256);

/// @notice The total supply shares of the market defined by the given `id`.
function totalSupplyShares(Id id) external view returns (uint256);

/// @notice The total amount of assets borrowed from the market defined by the given `id`.
/// @notice The total assets borrowed from the market defined by the given `id`.
/// @dev The value can be incaccurate since it does not take into account the accrued interests.
function totalBorrow(Id id) external view returns (uint256);

Expand Down Expand Up @@ -97,78 +97,78 @@ interface IBlue is IFlashLender {
/// @notice Creates `market`.
function createMarket(Market memory market) external;

/// @notice Supplies the given `amount` of assets or `shares` to the given `market` on behalf of `onBehalf`,
/// @notice Supplies the given amount of `assets` or `shares` to the given `market` on behalf of `onBehalf`,
/// optionally calling back the caller's `onBlueSupply` function with the given `data`.
/// @dev Either `amount` or `shares` should be zero.
/// Most usecases should rely on `amount` as an input so the caller
/// is guaranteed to have `amount` tokens pulled from their balance,
/// but the possibility to mint a specific amount of shares is given
/// @dev Either `assets` or `shares` should be zero.
/// Most usecases should rely on `assets` as an input so the caller
/// is guaranteed to have `assets` tokens pulled from their balance,
/// but the possibility to mint a specific assets of shares is given
/// for full compatibility and precision.
/// @param market The market to supply assets to.
/// @param amount The amount of assets to supply.
/// @param assets The amount of assets to supply.
/// @param shares The amount of shares to mint.
/// @param onBehalf The address that will receive the position.
/// @param data Arbitrary data to pass to the `onBlueSupply` callback. Pass empty data if not needed.
function supply(Market memory market, uint256 amount, uint256 shares, address onBehalf, bytes memory data)
function supply(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes memory data)
external;

/// @notice Withdraws the given `amount` of assets or `shares` from the given `market` on behalf of `onBehalf`.
/// @dev Either `amount` or `shares` should be zero.
/// @notice Withdraws the given `assets` or `shares` from the given `market` on behalf of `onBehalf`.
/// @dev Either `assets` or `shares` should be zero.
/// To withdraw the whole position, pass the `shares`'s balance of `onBehalf`.
/// @dev If `msg.sender != onBehalf`, `msg.sender` must be authorized to withdraw from `onBehalf`.
/// @param market The market to withdraw assets from.
/// @param shares The amount of amount to withdraw.
/// @param shares The amount of assets to withdraw.
/// @param shares The amount of shares to burn.
/// @param onBehalf The address of the owner of the withdrawn assets.
/// @param receiver The address that will receive the withdrawn assets.
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;

/// @notice Borrows the given `amount` of assets or `shares` from the given `market` on behalf of `onBehalf`.
/// @dev Either `amount` or `shares` should be zero.
/// Most usecases should rely on `amount` as an input so the caller
/// is guaranteed to borrow `amount` of tokens,
/// but the possibility to burn a specific amount of shares is given
/// @notice Borrows the given `assets` or `shares` from the given `market` on behalf of `onBehalf`.
/// @dev Either `assets` or `shares` should be zero.
/// Most usecases should rely on `assets` as an input so the caller
/// is guaranteed to borrow `assets` of tokens,
/// but the possibility to burn a specific assets of shares is given
/// for full compatibility and precision.
/// @param market The market to borrow assets from.
/// @param amount The amount of assets to borrow.
/// @param assets The amount of assets to borrow.
/// @param shares The amount of shares to mint.
/// @param onBehalf The address of the owner of the debt.
/// @param receiver The address that will receive the debt.
/// @dev If `msg.sender != onBehalf`, `msg.sender` must be authorized to withdraw from `onBehalf`.
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;

/// @notice Repays the given `amount` of assets or `shares` to the given `market` on behalf of `onBehalf`,
/// @notice Repays the given `assets` or `shares` to the given `market` on behalf of `onBehalf`,
/// optionally calling back the caller's `onBlueReplay` function with the given `data`.
/// @dev Either `amount` or `shares` should be zero.
/// @dev Either `assets` or `shares` should be zero.
/// To repay the whole debt, pass the `shares`'s balance of `onBehalf`.
/// @param market The market to repay assets to.
/// @param amount The amount of assets to repay.
/// @param assets The amount of assets to repay.
/// @param shares The amount of shares to burn.
/// @param onBehalf The address of the owner of the debt.
/// @param data Arbitrary data to pass to the `onBlueRepay` callback. Pass empty data if not needed.
function repay(Market memory market, uint256 amount, uint256 shares, address onBehalf, bytes memory data)
function repay(Market memory market, uint256 assets, uint256 shares, address onBehalf, bytes memory data)
external;

/// @notice Supplies the given `amount` of collateral to the given `market` on behalf of `onBehalf`,
/// @notice Supplies the given `assets` of collateral to the given `market` on behalf of `onBehalf`,
/// optionally calling back the caller's `onBlueSupplyCollateral` function with the given `data`.
/// @dev Interests are not accrued since it's not required and it saves gas.
/// @param market The market to supply collateral to.
/// @param amount The amount of collateral to supply.
/// @param assets The amount of collateral to supply.
/// @param onBehalf The address that will receive the collateral.
/// @param data Arbitrary data to pass to the `onBlueSupplyCollateral` callback. Pass empty data if not needed.
function supplyCollateral(Market memory market, uint256 amount, address onBehalf, bytes memory data) external;
function supplyCollateral(Market memory market, uint256 assets, address onBehalf, bytes memory data) external;

/// @notice Withdraws the given `amount` of collateral from the given `market` on behalf of `onBehalf`.
/// @notice Withdraws the given `assets` of collateral from the given `market` on behalf of `onBehalf`.
/// @dev If `msg.sender != onBehalf`, `msg.sender` must be authorized to withdraw from `onBehalf`.
/// @param market The market to withdraw collateral from.
/// @param amount The amount of collateral to withdraw.
/// @param assets The amount of collateral to withdraw.
/// @param onBehalf The address of the owner of the collateral.
/// @param receiver The address that will receive the withdrawn collateral.
function withdrawCollateral(Market memory market, uint256 amount, address onBehalf, address receiver) external;
function withdrawCollateral(Market memory market, uint256 assets, address onBehalf, address receiver) external;

/// @notice Liquidates the given `seized` amount to the given `market` of the given `borrower`'s position,
/// @notice Liquidates the given `seized` assets to the given `market` of the given `borrower`'s position,
/// optionally calling back the caller's `onBlueLiquidate` function with the given `data`.
/// @param market The market of the position.
/// @param borrower The owner of the position.
Expand Down
Loading

0 comments on commit 9cf53c0

Please sign in to comment.