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

use custom error to reduce build size #9

Merged
merged 1 commit into from
Jan 19, 2024
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
24 changes: 12 additions & 12 deletions contracts/DODOV3MM/D3Vault/D3Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ contract D3Vault is D3VaultFunding, D3VaultLiquidation {
// ---------- Setting ----------

function addD3PoolByFactory(address pool) external onlyFactory {
require(allPoolAddrMap[pool] == false, Errors.POOL_ALREADY_ADDED);
if (allPoolAddrMap[pool] == true) revert Errors.D3VaultPoolAlreadyAdded();
allPoolAddrMap[pool] = true;
address creator = ID3MM(pool)._CREATOR_();
creatorPoolMap[creator].push(pool);
emit AddPool(pool);
}

function addD3Pool(address pool) external onlyOwner {
require(allPoolAddrMap[pool] == false, Errors.POOL_ALREADY_ADDED);
if (allPoolAddrMap[pool] == true) revert Errors.D3VaultPoolAlreadyAdded();
allPoolAddrMap[pool] = true;
address creator = ID3MM(pool)._CREATOR_();
creatorPoolMap[creator].push(pool);
Expand All @@ -38,8 +38,8 @@ contract D3Vault is D3VaultFunding, D3VaultLiquidation {
/// @notice if the pool doesn't have borrows, we just need two steps:
/// @notice removeD3Pool() -> finishPoolRemove()
function removeD3Pool(address pool) external onlyOwner {
require(_PENDING_REMOVE_POOL_ == address(0), Errors.HAS_POOL_PENDING_REMOVE);
require(allPoolAddrMap[pool] == true, Errors.POOL_NOT_ADDED);
if (_PENDING_REMOVE_POOL_ != address(0)) revert Errors.D3VaultHasPoolPendingRemove();
if (allPoolAddrMap[pool] == false) revert Errors.D3VaultPoolNotAdded();
ID3MM(pool).startLiquidation();

allPoolAddrMap[pool] = false;
Expand Down Expand Up @@ -152,9 +152,9 @@ contract D3Vault is D3VaultFunding, D3VaultLiquidation {
uint256 debtWeight,
uint256 reserveFactor
) external onlyOwner {
require(!tokens[token], Errors.TOKEN_ALREADY_EXIST);
require(collateralWeight < 1e18 && debtWeight > 1e18, Errors.WRONG_WEIGHT);
require(reserveFactor < 1e18, Errors.WRONG_RESERVE_FACTOR);
if (tokens[token]) revert Errors.D3VaultTokenAlreadyExist();
if (collateralWeight >= 1e18 || debtWeight <= 1e18) revert Errors.D3VaultWrongWeight();
if (reserveFactor >= 1e18) revert Errors.D3VaultWrongReserveFactor();
tokens[token] = true;
tokenList.push(token);
address dToken = createDToken(token);
Expand Down Expand Up @@ -184,9 +184,9 @@ contract D3Vault is D3VaultFunding, D3VaultLiquidation {
uint256 debtWeight,
uint256 reserveFactor
) external onlyOwner {
require(tokens[token], Errors.TOKEN_NOT_EXIST);
require(collateralWeight < 1e18 && debtWeight > 1e18, Errors.WRONG_WEIGHT);
require(reserveFactor < 1e18, Errors.WRONG_RESERVE_FACTOR);
if (!tokens[token]) revert Errors.D3VaultTokenNotExist();
if (collateralWeight >= 1e18 || debtWeight <= 1e18) revert Errors.D3VaultWrongWeight();
if (reserveFactor >= 1e18) revert Errors.D3VaultWrongReserveFactor();
AssetInfo storage info = assetInfo[token];
info.maxDepositAmount = maxDeposit;
info.maxCollateralAmount = maxCollateral;
Expand All @@ -197,12 +197,12 @@ contract D3Vault is D3VaultFunding, D3VaultLiquidation {
}

function withdrawReserves(address token, uint256 amount) external nonReentrant allowedToken(token) onlyOwner {
require(_MAINTAINER_ != address(0), Errors.MAINTAINER_NOT_SET);
if (_MAINTAINER_ == address(0)) revert Errors.D3VaultMaintainerNotSet();
accrueInterest(token);
AssetInfo storage info = assetInfo[token];
uint256 totalReserves = info.totalReserves;
uint256 withdrawnReserves = info.withdrawnReserves;
require(amount <= totalReserves - withdrawnReserves, Errors.WITHDRAW_AMOUNT_EXCEED);
if (amount > totalReserves - withdrawnReserves) revert Errors.D3VaultWithdrawAmountExceed();
info.withdrawnReserves = info.withdrawnReserves + amount;
info.balance = info.balance - amount;
IERC20(token).safeTransfer(_MAINTAINER_, amount);
Expand Down
18 changes: 9 additions & 9 deletions contracts/DODOV3MM/D3Vault/D3VaultFunding.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ contract D3VaultFunding is D3VaultStorage {
AssetInfo storage info = assetInfo[token];
uint256 realBalance = IERC20(token).balanceOf(address(this));
uint256 amount = realBalance - info.balance;
require(ID3UserQuota(_USER_QUOTA_).checkQuota(user, token, amount), Errors.EXCEED_QUOTA);
if (!ID3UserQuota(_USER_QUOTA_).checkQuota(user, token, amount)) revert Errors.D3VaultExceedQuota();
uint256 exchangeRate = _getExchangeRate(token);
uint256 totalDToken = IDToken(info.dToken).totalSupply();
require(totalDToken.mul(exchangeRate) + amount <= info.maxDepositAmount, Errors.EXCEED_MAX_DEPOSIT_AMOUNT);
if (totalDToken.mul(exchangeRate) + amount > info.maxDepositAmount) revert Errors.D3VaultExceedMaxDepositAmount();
dTokenAmount = amount.div(exchangeRate);

if (totalDToken == 0) {
// permanently lock a very small amount of dTokens into address(1), which reduces potential issues with rounding,
// and also prevents the pool from ever being fully drained
require(dTokenAmount > DEFAULT_MINIMUM_DTOKEN, Errors.MINIMUM_DTOKEN);
if (dTokenAmount <= DEFAULT_MINIMUM_DTOKEN) revert Errors.D3VaultMinimumDToken();
IDToken(info.dToken).mint(address(1), DEFAULT_MINIMUM_DTOKEN);
IDToken(info.dToken).mint(user, dTokenAmount - DEFAULT_MINIMUM_DTOKEN);
} else {
Expand All @@ -60,7 +60,7 @@ contract D3VaultFunding is D3VaultStorage {
function userWithdraw(address to, address user, address token, uint256 dTokenAmount) external nonReentrant allowedToken(token) returns(uint256 amount) {
accrueInterest(token);
AssetInfo storage info = assetInfo[token];
require(dTokenAmount <= IDToken(info.dToken).balanceOf(msg.sender), Errors.DTOKEN_BALANCE_NOT_ENOUGH);
if (dTokenAmount > IDToken(info.dToken).balanceOf(msg.sender)) revert Errors.D3VaultDTokenBalanceNotEnough();

amount = dTokenAmount.mul(_getExchangeRate(token));
IDToken(info.dToken).burn(msg.sender, dTokenAmount);
Expand All @@ -83,8 +83,8 @@ contract D3VaultFunding is D3VaultStorage {
AssetInfo storage info = assetInfo[token];
BorrowRecord storage record = info.borrowRecord[msg.sender];
uint256 usedQuota = _borrowAmount(record.amount, record.interestIndex, info.borrowIndex); // borrowAmount = record.amount * newIndex / oldIndex
require(amount + usedQuota <= quota, Errors.EXCEED_QUOTA);
require(amount <= info.balance - (info.totalReserves - info.withdrawnReserves), Errors.AMOUNT_EXCEED_VAULT_BALANCE);
if (amount + usedQuota > quota) revert Errors.D3VaultExceedQuota();
if (amount > info.balance - (info.totalReserves - info.withdrawnReserves)) revert Errors.D3VaultAmountExceedVaultBalance();

uint256 interests = usedQuota - record.amount;

Expand All @@ -98,14 +98,14 @@ contract D3VaultFunding is D3VaultStorage {
}

function poolRepay(address token, uint256 amount) external nonReentrant allowedToken(token) onlyPool {
require(!ID3MM(msg.sender).isInLiquidation(), Errors.ALREADY_IN_LIQUIDATION);
if (ID3MM(msg.sender).isInLiquidation()) revert Errors.D3VaultAlreadyInLiquidation();

accrueInterest(token);

AssetInfo storage info = assetInfo[token];
BorrowRecord storage record = info.borrowRecord[msg.sender];
uint256 borrows = _borrowAmount(record.amount, record.interestIndex, info.borrowIndex); // borrowAmount = record.amount * newIndex / oldIndex
require(amount <= borrows, Errors.AMOUNT_EXCEED);
if (amount > borrows) revert Errors.D3VaultAmountExceed();

uint256 interests = borrows - record.amount;

Expand All @@ -123,7 +123,7 @@ contract D3VaultFunding is D3VaultStorage {
}

function poolRepayAll(address token) external nonReentrant allowedToken(token) onlyPool {
require(!ID3MM(msg.sender).isInLiquidation(), Errors.ALREADY_IN_LIQUIDATION);
if (ID3MM(msg.sender).isInLiquidation()) revert Errors.D3VaultAlreadyInLiquidation();
_poolRepayAll(msg.sender, token);
}

Expand Down
32 changes: 16 additions & 16 deletions contracts/DODOV3MM/D3Vault/D3VaultLiquidation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ contract D3VaultLiquidation is D3VaultFunding {
) external nonReentrant {
accrueInterests();

require(!ID3MM(pool).isInLiquidation(), Errors.ALREADY_IN_LIQUIDATION);
require(!checkBadDebtAfterAccrue(pool), Errors.HAS_BAD_DEBT);
require(checkCanBeLiquidatedAfterAccrue(pool), Errors.CANNOT_BE_LIQUIDATED);
require(isPositiveNetWorthAsset(pool, collateral), Errors.INVALID_COLLATERAL_TOKEN);
require(!isPositiveNetWorthAsset(pool, debt), Errors.INVALID_DEBT_TOKEN);
require(getPositiveNetWorthAsset(pool, collateral) >= collateralAmount, Errors.COLLATERAL_AMOUNT_EXCEED);
if (ID3MM(pool).isInLiquidation()) revert Errors.D3VaultAlreadyInLiquidation();
if (checkBadDebtAfterAccrue(pool)) revert Errors.D3VaultHasBadDebt();
if (!checkCanBeLiquidatedAfterAccrue(pool)) revert Errors.D3VaultCannotBeLiquidated();
if (!isPositiveNetWorthAsset(pool, collateral)) revert Errors.D3VaultInvalidCollateralToken();
if (isPositiveNetWorthAsset(pool, debt)) revert Errors.D3VaultInvalidDebtToken();
if (getPositiveNetWorthAsset(pool, collateral) < collateralAmount) revert Errors.D3VaultCollateralAmountExceed();

uint256 collateralTokenPrice = ID3Oracle(_ORACLE_).getPrice(collateral);
uint256 debtTokenPrice = ID3Oracle(_ORACLE_).getPrice(debt);
uint256 collateralAmountMax = debtToCover.mul(debtTokenPrice).div(collateralTokenPrice.mul(DISCOUNT));
require(collateralAmount <= collateralAmountMax, Errors.COLLATERAL_AMOUNT_EXCEED);
if (collateralAmount > collateralAmountMax) revert Errors.D3VaultCollateralAmountExceed();

AssetInfo storage info = assetInfo[debt];
BorrowRecord storage record = info.borrowRecord[pool];
uint256 borrows = _borrowAmount(record.amount, record.interestIndex, info.borrowIndex); // borrowAmount = record.amount * newIndex / oldIndex
require(debtToCover <= borrows, Errors.DEBT_TO_COVER_EXCEED);
if (debtToCover > borrows) revert Errors.D3VaultDebtToCoverExceed();
IERC20(debt).safeTransferFrom(msg.sender, address(this), debtToCover);

if (info.totalBorrows < debtToCover) {
Expand All @@ -74,13 +74,13 @@ contract D3VaultLiquidation is D3VaultFunding {
function startLiquidation(address pool) external onlyLiquidator nonReentrant {
accrueInterests();

require(!ID3MM(pool).isInLiquidation(), Errors.ALREADY_IN_LIQUIDATION);
require(checkCanBeLiquidatedAfterAccrue(pool), Errors.CANNOT_BE_LIQUIDATED);
if (ID3MM(pool).isInLiquidation()) revert Errors.D3VaultAlreadyInLiquidation();
if (!checkCanBeLiquidatedAfterAccrue(pool)) revert Errors.D3VaultCannotBeLiquidated();
ID3MM(pool).startLiquidation();

uint256 totalAssetValue = getTotalAssetsValue(pool);
uint256 totalDebtValue = _getTotalDebtValue(pool);
require(totalAssetValue < totalDebtValue, Errors.NO_BAD_DEBT);
if (totalAssetValue >= totalDebtValue) revert Errors.D3VaultNoBadDebt();

uint256 ratio = totalAssetValue.div(totalDebtValue);

Expand All @@ -100,7 +100,7 @@ contract D3VaultLiquidation is D3VaultFunding {
bytes calldata routeData,
address router
) external onlyLiquidator onlyRouter(router) nonReentrant {
require(ID3MM(pool).isInLiquidation(), Errors.NOT_IN_LIQUIDATION);
if (!ID3MM(pool).isInLiquidation()) revert Errors.D3VaultNotInLiquidation();

uint256 toTokenReserve = IERC20(order.toToken).balanceOf(address(this));
uint256 fromTokenValue = DecimalMath.mul(ID3Oracle(_ORACLE_).getPrice(order.fromToken), order.fromAmount);
Expand All @@ -120,14 +120,14 @@ contract D3VaultLiquidation is D3VaultFunding {
uint256 receivedToToken = IERC20(order.toToken).balanceOf(address(this)) - toTokenReserve;
uint256 toTokenValue = DecimalMath.mul(ID3Oracle(_ORACLE_).getPrice(order.toToken), receivedToToken);

require(toTokenValue >= fromTokenValue.mul(DISCOUNT), Errors.EXCEED_DISCOUNT);
if (toTokenValue < fromTokenValue.mul(DISCOUNT)) revert Errors.D3VaultExceedDiscount();
IERC20(order.toToken).safeTransfer(pool, receivedToToken);
ID3MM(pool).updateReserveByVault(order.fromToken);
ID3MM(pool).updateReserveByVault(order.toToken);
}

function finishLiquidation(address pool) external onlyLiquidator nonReentrant {
require(ID3MM(pool).isInLiquidation(), Errors.NOT_IN_LIQUIDATION);
if (!ID3MM(pool).isInLiquidation()) revert Errors.D3VaultNotInLiquidation();
accrueInterests();

bool hasPositiveBalance;
Expand All @@ -139,10 +139,10 @@ contract D3VaultLiquidation is D3VaultFunding {
uint256 debt = liquidationTarget[pool][token];
int256 difference = int256(balance) - int256(debt);
if (difference > 0) {
require(!hasNegativeBalance, Errors.LIQUIDATION_NOT_DONE);
if (hasNegativeBalance) revert Errors.D3VaultLiquidationNotDone();
hasPositiveBalance = true;
} else if (difference < 0) {
require(!hasPositiveBalance, Errors.LIQUIDATION_NOT_DONE);
if (hasPositiveBalance) revert Errors.D3VaultLiquidationNotDone();
hasNegativeBalance = true;
debt = balance; // if balance is less than target amount, just repay with balance
}
Expand Down
12 changes: 6 additions & 6 deletions contracts/DODOV3MM/D3Vault/D3VaultStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,32 @@ contract D3VaultStorage is ReentrancyGuard, Ownable {
event WithdrawReserves(address indexed token, uint256 amount);

modifier onlyLiquidator() {
require(allowedLiquidator[msg.sender], Errors.NOT_ALLOWED_LIQUIDATOR);
if (!allowedLiquidator[msg.sender]) revert Errors.D3VaultNotAllowedLiquidator();
_;
}

modifier onlyRouter(address router) {
require(allowedRouter[router], Errors.NOT_ALLOWED_ROUTER);
if (!allowedRouter[router]) revert Errors.D3VaultNotAllowedRouter();
_;
}

modifier onlyPool() {
require(allPoolAddrMap[msg.sender], Errors.NOT_D3POOL);
if (!allPoolAddrMap[msg.sender]) revert Errors.D3VaultNotD3Pool();
_;
}

modifier allowedToken(address token) {
require(tokens[token], Errors.NOT_ALLOWED_TOKEN);
if (!tokens[token]) revert Errors.D3VaultNotAllowedToken();
_;
}

modifier onlyFactory() {
require(msg.sender == _D3_FACTORY_, Errors.NOT_D3_FACTORY);
if (msg.sender != _D3_FACTORY_) revert Errors.D3VaultNotD3Factory();
_;
}

modifier onlyRemovingPool() {
require(msg.sender == _PENDING_REMOVE_POOL_, Errors.NOT_PENDING_REMOVE_POOL);
if (msg.sender != _PENDING_REMOVE_POOL_) revert Errors.D3VaultNotPendingRemovePool();
_;
}
}
72 changes: 36 additions & 36 deletions contracts/DODOV3MM/D3Vault/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@
pragma solidity 0.8.16;

library Errors {
string public constant POOL_ALREADY_ADDED = "D3VAULT_POOL_ALREADY_ADDED";
string public constant POOL_NOT_ADDED = "D3VAULT_POOL_NOT_ADDED";
string public constant HAS_POOL_PENDING_REMOVE = "D3VAULT_HAS_POOL_PENDING_REMOVE";
string public constant AMOUNT_EXCEED_VAULT_BALANCE = "D3VAULT_AMOUNT_EXCEED_VAULT_BALANCE";
string public constant NOT_ALLOWED_ROUTER = "D3VAULT_NOT_ALLOWED_ROUTER";
string public constant NOT_ALLOWED_LIQUIDATOR = "D3VAULT_NOT_ALLOWED_LIQUIDATOR";
string public constant NOT_PENDING_REMOVE_POOL = "D3VAULT_NOT_PENDING_REMOVE_POOL";
string public constant NOT_D3POOL = "D3VAULT_NOT_D3POOL";
string public constant NOT_ALLOWED_TOKEN = "D3VAULT_NOT_ALLOWED_TOKEN";
string public constant NOT_D3_FACTORY = "D3VAULT_NOT_D3_FACTORY";
string public constant TOKEN_ALREADY_EXIST = "D3VAULT_TOKEN_ALREADY_EXIST";
string public constant TOKEN_NOT_EXIST = "D3VAULT_TOKEN_NOT_EXIST";
string public constant WRONG_WEIGHT = "D3VAULT_WRONG_WEIGHT";
string public constant WRONG_RESERVE_FACTOR = "D3VAULT_RESERVE_FACTOR";
string public constant WITHDRAW_AMOUNT_EXCEED = "D3VAULT_WITHDRAW_AMOUNT_EXCEED";
string public constant MAINTAINER_NOT_SET = "D3VAULT_MAINTAINER_NOT_SET";
error D3VaultPoolAlreadyAdded();
error D3VaultPoolNotAdded();
error D3VaultHasPoolPendingRemove();
error D3VaultAmountExceedVaultBalance();
error D3VaultNotAllowedRouter();
error D3VaultNotAllowedLiquidator();
error D3VaultNotPendingRemovePool();
error D3VaultNotD3Pool();
error D3VaultNotAllowedToken();
error D3VaultNotD3Factory();
error D3VaultTokenAlreadyExist();
error D3VaultTokenNotExist();
error D3VaultWrongWeight();
error D3VaultWrongReserveFactor();
error D3VaultWithdrawAmountExceed();
error D3VaultMaintainerNotSet();

// ---------- funding ----------
string public constant EXCEED_QUOTA = "D3VAULT_EXCEED_QUOTA";
string public constant EXCEED_MAX_DEPOSIT_AMOUNT = "D3VAULT_EXCEED_MAX_DEPOSIT_AMOUNT";
string public constant DTOKEN_BALANCE_NOT_ENOUGH = "D3TOKEN_BALANCE_NOT_ENOUGH";
string public constant POOL_NOT_SAFE = "D3VAULT_POOL_NOT_SAFE";
string public constant NOT_ENOUGH_COLLATERAL_FOR_BORROW = "D3VAULT_NOT_ENOUGH_COLLATERAL_FOR_BORROW";
string public constant AMOUNT_EXCEED = "D3VAULT_AMOUNT_EXCEED";
string public constant NOT_RATE_MANAGER = "D3VAULT_NOT_RATE_MANAGER";
string public constant MINIMUM_DTOKEN = "MINIMUM_DTOKEN";
error D3VaultExceedQuota();
error D3VaultExceedMaxDepositAmount();
error D3VaultDTokenBalanceNotEnough();
error D3VaultPoolNotSafe();
error D3VaultNotEnoughCollateralForBorrow();
error D3VaultAmountExceed();
error D3VaultNotRateManager();
error D3VaultMinimumDToken();

// ---------- liquidation ----------
string public constant COLLATERAL_AMOUNT_EXCEED = "D3VAULT_COLLATERAL_AMOUNT_EXCEED";
string public constant CANNOT_BE_LIQUIDATED = "D3VAULT_CANNOT_BE_LIQUIDATED";
string public constant INVALID_COLLATERAL_TOKEN = "D3VAULT_INVALID_COLLATERAL_TOKEN";
string public constant INVALID_DEBT_TOKEN = "D3VAULT_INVALID_DEBT_TOKEN";
string public constant DEBT_TO_COVER_EXCEED = "D3VAULT_DEBT_TO_COVER_EXCEED";
string public constant ALREADY_IN_LIQUIDATION = "D3VAULT_ALREADY_IN_LIQUIDATION";
string public constant STILL_UNDER_MM = "D3VAULT_STILL_UNDER_MM";
string public constant NO_BAD_DEBT = "D3VAULT_NO_BAD_DEBT";
string public constant NOT_IN_LIQUIDATION = "D3VAULT_NOT_IN_LIQUIDATION";
string public constant EXCEED_DISCOUNT = "D3VAULT_EXCEED_DISCOUNT";
string public constant LIQUIDATION_NOT_DONE = "D3VAULT_LIQUIDATION_NOT_DONE";
string public constant HAS_BAD_DEBT = "D3VAULT_HAS_BAD_DEBT";
error D3VaultCollateralAmountExceed();
error D3VaultCannotBeLiquidated();
error D3VaultInvalidCollateralToken();
error D3VaultInvalidDebtToken();
error D3VaultDebtToCoverExceed();
error D3VaultAlreadyInLiquidation();
error D3VaultStillUnderMM();
error D3VaultNoBadDebt();
error D3VaultNotInLiquidation();
error D3VaultExceedDiscount();
error D3VaultLiquidationNotDone();
error D3VaultHasBadDebt();
}
Loading
Loading