From b6ecee582aa67240000c69c252204dfe67e34635 Mon Sep 17 00:00:00 2001 From: traceurl <25892474+traceurl@users.noreply.github.com> Date: Thu, 18 Jan 2024 08:36:29 +0800 Subject: [PATCH] use custom error to reduce build size --- contracts/DODOV3MM/D3Vault/D3Vault.sol | 24 +++---- contracts/DODOV3MM/D3Vault/D3VaultFunding.sol | 18 ++--- .../DODOV3MM/D3Vault/D3VaultLiquidation.sol | 32 ++++----- contracts/DODOV3MM/D3Vault/D3VaultStorage.sol | 12 ++-- contracts/DODOV3MM/D3Vault/Errors.sol | 72 +++++++++---------- test/DODOV3MM/D3Vault/D3Vault.t.sol | 28 ++++---- test/DODOV3MM/D3Vault/D3VaultFunding.t.sol | 16 ++--- .../DODOV3MM/D3Vault/D3VaultLiquidation.t.sol | 40 +++++------ 8 files changed, 121 insertions(+), 121 deletions(-) diff --git a/contracts/DODOV3MM/D3Vault/D3Vault.sol b/contracts/DODOV3MM/D3Vault/D3Vault.sol index 921e065..b78f93d 100644 --- a/contracts/DODOV3MM/D3Vault/D3Vault.sol +++ b/contracts/DODOV3MM/D3Vault/D3Vault.sol @@ -11,7 +11,7 @@ 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); @@ -19,7 +19,7 @@ contract D3Vault is D3VaultFunding, D3VaultLiquidation { } 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); @@ -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; @@ -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); @@ -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; @@ -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); diff --git a/contracts/DODOV3MM/D3Vault/D3VaultFunding.sol b/contracts/DODOV3MM/D3Vault/D3VaultFunding.sol index 6f324b4..bdcf599 100644 --- a/contracts/DODOV3MM/D3Vault/D3VaultFunding.sol +++ b/contracts/DODOV3MM/D3Vault/D3VaultFunding.sol @@ -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 { @@ -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); @@ -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; @@ -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; @@ -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); } diff --git a/contracts/DODOV3MM/D3Vault/D3VaultLiquidation.sol b/contracts/DODOV3MM/D3Vault/D3VaultLiquidation.sol index 27eb523..23070c7 100644 --- a/contracts/DODOV3MM/D3Vault/D3VaultLiquidation.sol +++ b/contracts/DODOV3MM/D3Vault/D3VaultLiquidation.sol @@ -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) { @@ -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); @@ -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); @@ -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; @@ -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 } diff --git a/contracts/DODOV3MM/D3Vault/D3VaultStorage.sol b/contracts/DODOV3MM/D3Vault/D3VaultStorage.sol index 7585288..1fde34d 100644 --- a/contracts/DODOV3MM/D3Vault/D3VaultStorage.sol +++ b/contracts/DODOV3MM/D3Vault/D3VaultStorage.sol @@ -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(); _; } } diff --git a/contracts/DODOV3MM/D3Vault/Errors.sol b/contracts/DODOV3MM/D3Vault/Errors.sol index f4b6371..31d1956 100644 --- a/contracts/DODOV3MM/D3Vault/Errors.sol +++ b/contracts/DODOV3MM/D3Vault/Errors.sol @@ -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(); } diff --git a/test/DODOV3MM/D3Vault/D3Vault.t.sol b/test/DODOV3MM/D3Vault/D3Vault.t.sol index 28c56e7..e2f6812 100644 --- a/test/DODOV3MM/D3Vault/D3Vault.t.sol +++ b/test/DODOV3MM/D3Vault/D3Vault.t.sol @@ -22,7 +22,7 @@ contract D3VaultTest is TestContext { function testAddAndRemoveD3Pool() public { // remove a pool never added before vm.prank(vaultOwner); - vm.expectRevert(bytes(Errors.POOL_NOT_ADDED)); + vm.expectRevert(Errors.D3VaultPoolNotAdded.selector); d3Vault.removeD3Pool(address(1234)); address pool = d3MMFactory.breedD3Pool(address(1234), address(1234), 100000, 0); @@ -30,7 +30,7 @@ contract D3VaultTest is TestContext { assertEq(d3Vault.creatorPoolMap(address(1234), 0), pool); // D3MMFactory add an already added pool - vm.expectRevert(bytes(Errors.POOL_ALREADY_ADDED)); + vm.expectRevert(Errors.D3VaultPoolAlreadyAdded.selector); d3MMFactory.addD3Pool(pool); vm.prank(vaultOwner); @@ -43,12 +43,12 @@ contract D3VaultTest is TestContext { // add a pool which has already been added vm.prank(vaultOwner); - vm.expectRevert(bytes(Errors.POOL_ALREADY_ADDED)); + vm.expectRevert(Errors.D3VaultPoolAlreadyAdded.selector); d3Vault.addD3Pool(pool); address pool2 = d3MMFactory.breedD3Pool(address(1234), address(1234), 100000, 0); vm.prank(vaultOwner); - vm.expectRevert(bytes(Errors.HAS_POOL_PENDING_REMOVE)); + vm.expectRevert(Errors.D3VaultHasPoolPendingRemove.selector); d3Vault.removeD3Pool(pool2); vm.startPrank(vaultOwner); @@ -101,7 +101,7 @@ contract D3VaultTest is TestContext { // pool is in "liquidation" state when pending remove vm.prank(user2); - vm.expectRevert(bytes(Errors.ALREADY_IN_LIQUIDATION)); + vm.expectRevert(Errors.D3VaultAlreadyInLiquidation.selector); d3Vault.liquidate(address(d3MM), address(token1), 100 ether, address(token2), 100 ether); vm.prank(vaultOwner); @@ -198,11 +198,11 @@ contract D3VaultTest is TestContext { function testAddNewToken() public { vm.startPrank(vaultOwner); - vm.expectRevert(bytes(Errors.TOKEN_ALREADY_EXIST)); + vm.expectRevert(Errors.D3VaultTokenAlreadyExist.selector); d3Vault.addNewToken(address(token1), 0, 0, 0, 0, 0); - vm.expectRevert(bytes(Errors.WRONG_WEIGHT)); + vm.expectRevert(Errors.D3VaultWrongWeight.selector); d3Vault.addNewToken(address(token4), 0, 0, 0, 0, 0); - vm.expectRevert(bytes(Errors.WRONG_RESERVE_FACTOR)); + vm.expectRevert(Errors.D3VaultWrongReserveFactor.selector); d3Vault.addNewToken(address(token4), 0, 0, 80e16, 120e16, 2e18); d3Vault.addNewToken(address(token4), 0, 0, 80e16, 120e16, 20e16); vm.stopPrank(); @@ -210,11 +210,11 @@ contract D3VaultTest is TestContext { function testSetToken() public { vm.startPrank(vaultOwner); - vm.expectRevert(bytes(Errors.TOKEN_NOT_EXIST)); + vm.expectRevert(Errors.D3VaultTokenNotExist.selector); d3Vault.setToken(address(token4), 0, 0, 0, 0, 0); - vm.expectRevert(bytes(Errors.WRONG_WEIGHT)); + vm.expectRevert(Errors.D3VaultWrongWeight.selector); d3Vault.setToken(address(token1), 0, 0, 0, 0, 0); - vm.expectRevert(bytes(Errors.WRONG_RESERVE_FACTOR)); + vm.expectRevert(Errors.D3VaultWrongReserveFactor.selector); d3Vault.setToken(address(token1), 0, 0, 80e16, 120e16, 2e18); d3Vault.setToken(address(token1), 0, 0, 80e16, 120e16, 20e16); vm.stopPrank(); @@ -293,14 +293,14 @@ contract D3VaultTest is TestContext { logAssetInfo(address(token2)); vm.prank(vaultOwner); - vm.expectRevert(bytes(Errors.WITHDRAW_AMOUNT_EXCEED)); + vm.expectRevert(Errors.D3VaultWithdrawAmountExceed.selector); d3Vault.withdrawReserves(address(token2), 101); // if _MAINTAINER_ is not set, withdrawReserves should revert vm.prank(vaultOwner); d3Vault.setMaintainer(address(0)); vm.prank(vaultOwner); - vm.expectRevert(bytes(Errors.MAINTAINER_NOT_SET)); + vm.expectRevert(Errors.D3VaultMaintainerNotSet.selector); d3Vault.withdrawReserves(address(token2), 101); } @@ -324,7 +324,7 @@ contract D3VaultTest is TestContext { token2.transfer(address(d3Vault), 1000 ether); // userDeposit is blocked - vm.expectRevert(bytes(Errors.EXCEED_MAX_DEPOSIT_AMOUNT)); + vm.expectRevert(Errors.D3VaultExceedMaxDepositAmount.selector); vm.prank(user1); d3Proxy.userDeposit(user1, address(token2), 500 ether, 0); diff --git a/test/DODOV3MM/D3Vault/D3VaultFunding.t.sol b/test/DODOV3MM/D3Vault/D3VaultFunding.t.sol index 318f722..8920f28 100644 --- a/test/DODOV3MM/D3Vault/D3VaultFunding.t.sol +++ b/test/DODOV3MM/D3Vault/D3VaultFunding.t.sol @@ -35,12 +35,12 @@ contract D3VaultFundingTest is TestContext { // case 0: fail - minimum amount required for the first deposit mockUserQuota.setUserQuota(user1, address(token1), 2000); vm.prank(user1); - vm.expectRevert(bytes(Errors.MINIMUM_DTOKEN)); + vm.expectRevert(Errors.D3VaultMinimumDToken.selector); d3Proxy.userDeposit(user1, address(token1), DEFAULT_MINIMUM_DTOKEN - 1, 0); // case 1: fail - exceed quota vm.prank(user1); - vm.expectRevert(bytes(Errors.EXCEED_QUOTA)); + vm.expectRevert(Errors.D3VaultExceedQuota.selector); d3Proxy.userDeposit(user1, address(token1), 500 * 1e8, 0); // case 2: success @@ -57,7 +57,7 @@ contract D3VaultFundingTest is TestContext { assertEq(token1.balanceOf(user2), 500 * 1e8); vm.prank(user2); - vm.expectRevert(bytes(Errors.EXCEED_MAX_DEPOSIT_AMOUNT)); + vm.expectRevert(Errors.D3VaultExceedMaxDepositAmount.selector); d3Proxy.userDeposit(user2, address(token1), 1, 0); assertEq(token1.balanceOf(user2), 500 * 1e8); } @@ -76,7 +76,7 @@ contract D3VaultFundingTest is TestContext { assertEq(balance2 - balance1, 100 * 1e8 - DEFAULT_MINIMUM_DTOKEN); // case: withdraw amount larger than dToken balance - vm.expectRevert(bytes(Errors.DTOKEN_BALANCE_NOT_ENOUGH)); + vm.expectRevert(Errors.D3VaultDTokenBalanceNotEnough.selector); userWithdraw(user1, address(token1), 100 * 1e8); } @@ -113,7 +113,7 @@ contract D3VaultFundingTest is TestContext { addressList[0] = address(d3MM); quotaList[0] = 1; poolQuota.setPoolQuota(address(token1), addressList, quotaList); - vm.expectRevert(bytes(Errors.EXCEED_QUOTA)); + vm.expectRevert(Errors.D3VaultExceedQuota.selector); poolBorrow(address(d3MM), address(token1), 100 * 1e8); // case 2.2: borrow not exceed pool quota quotaList[0] = 1000 * 1e8; @@ -133,7 +133,7 @@ contract D3VaultFundingTest is TestContext { poolBorrow(address(d3MM), address(token1), 200 * 1e8); // case 4: pool borrow asset which is not the collateral token - vm.expectRevert(bytes(Errors.AMOUNT_EXCEED_VAULT_BALANCE)); + vm.expectRevert(Errors.D3VaultAmountExceedVaultBalance.selector); poolBorrow(address(d3MM), address(token2), 1 ether); vm.prank(user1); @@ -205,7 +205,7 @@ contract D3VaultFundingTest is TestContext { assertEq(newBorrows2, 4795615411); // case: repay more than borrows - vm.expectRevert(bytes(Errors.AMOUNT_EXCEED)); + vm.expectRevert(Errors.D3VaultAmountExceed.selector); d3Vault.poolRepay(address(token1), 4795615411 + 1); (,,,,,,,,,, uint256 balanceBefore) = d3Vault.getAssetInfo(address(token1)); @@ -261,7 +261,7 @@ contract D3VaultFundingTest is TestContext { userWithdraw(user1, address(token1), 10 * 1e8 - DEFAULT_MINIMUM_DTOKEN); userWithdraw(address(1), address(token1), DEFAULT_MINIMUM_DTOKEN); - vm.expectRevert(bytes(Errors.AMOUNT_EXCEED_VAULT_BALANCE)); + vm.expectRevert(Errors.D3VaultAmountExceedVaultBalance.selector); poolBorrow(address(d3MM), address(token1), 1); uint256 getURatio = d3Vault.getUtilizationRatio(address(token1)); assertEq(getURatio, 0); diff --git a/test/DODOV3MM/D3Vault/D3VaultLiquidation.t.sol b/test/DODOV3MM/D3Vault/D3VaultLiquidation.t.sol index a423f88..3ada02f 100644 --- a/test/DODOV3MM/D3Vault/D3VaultLiquidation.t.sol +++ b/test/DODOV3MM/D3Vault/D3VaultLiquidation.t.sol @@ -167,12 +167,12 @@ contract D3VaultLiquidationTest is TestContext { function testCannotBeLiquidated() public { contextCannotBeLiquidated(); vm.prank(user2); - vm.expectRevert(bytes(Errors.CANNOT_BE_LIQUIDATED)); + vm.expectRevert(Errors.D3VaultCannotBeLiquidated.selector); d3Vault.liquidate(address(d3MM), address(token1), 100 ether, address(token2), 100 ether); vm.prank(vaultOwner); d3Vault.addLiquidator(address(this)); - vm.expectRevert(bytes(Errors.CANNOT_BE_LIQUIDATED)); + vm.expectRevert(Errors.D3VaultCannotBeLiquidated.selector); d3Vault.startLiquidation(address(d3MM)); } @@ -181,32 +181,32 @@ contract D3VaultLiquidationTest is TestContext { vm.prank(vaultOwner); d3Vault.addLiquidator(address(this)); - vm.expectRevert(bytes(Errors.NO_BAD_DEBT)); + vm.expectRevert(Errors.D3VaultNoBadDebt.selector); d3Vault.startLiquidation(address(d3MM)); // Case 1: pool has no token1 as collateral, should fail vm.prank(user2); - vm.expectRevert(bytes(Errors.COLLATERAL_AMOUNT_EXCEED)); + vm.expectRevert(Errors.D3VaultCollateralAmountExceed.selector); d3Vault.liquidate(address(d3MM), address(token1), 100 ether, address(token2), 100 ether); // Case 2: token2 cannot be collateral, should fail vm.prank(user2); - vm.expectRevert(bytes(Errors.INVALID_COLLATERAL_TOKEN)); + vm.expectRevert(Errors.D3VaultInvalidCollateralToken.selector); d3Vault.liquidate(address(d3MM), address(token2), 100 ether, address(token3), 100 ether); // Case 3: token3 cannot be debt, should fail vm.prank(user2); - vm.expectRevert(bytes(Errors.INVALID_DEBT_TOKEN)); + vm.expectRevert(Errors.D3VaultInvalidDebtToken.selector); d3Vault.liquidate(address(d3MM), address(token3), 100 ether, address(token3), 100 ether); // Case 4: the collateral amount passed in is larger than token3 balance in pool, should fail vm.prank(user2); - vm.expectRevert(bytes(Errors.COLLATERAL_AMOUNT_EXCEED)); + vm.expectRevert(Errors.D3VaultCollateralAmountExceed.selector); d3Vault.liquidate(address(d3MM), address(token3), 200 ether, address(token2), 100 ether); // Case 5: the debt to cover amount is larger than debt, should fail vm.prank(user2); - vm.expectRevert(bytes(Errors.DEBT_TO_COVER_EXCEED)); + vm.expectRevert(Errors.D3VaultDebtToCoverExceed.selector); d3Vault.liquidate(address(d3MM), address(token3), 10 ether, address(token2), 100 ether); // Case 6: In this case, user2 try to use 1 token2 to get 20 token3, with the price discount, @@ -219,7 +219,7 @@ contract D3VaultLiquidationTest is TestContext { token3.approve(address(d3Vault), type(uint256).max); vm.prank(user2); - vm.expectRevert(bytes(Errors.COLLATERAL_AMOUNT_EXCEED)); + vm.expectRevert(Errors.D3VaultCollateralAmountExceed.selector); d3Vault.liquidate(address(d3MM), address(token3), 20 ether, address(token2), 1 ether); // Case 7: User2 try to use 2 token2 to get 25 token3, with the price discount, @@ -253,25 +253,25 @@ contract D3VaultLiquidationTest is TestContext { contextBadDebt(); vm.prank(user2); - vm.expectRevert(bytes(Errors.HAS_BAD_DEBT)); + vm.expectRevert(Errors.D3VaultHasBadDebt.selector); d3Vault.liquidate(address(d3MM), address(token1), 100 ether, address(token2), 100 ether); - vm.expectRevert(bytes(Errors.NOT_ALLOWED_LIQUIDATOR)); + vm.expectRevert(Errors.D3VaultNotAllowedLiquidator.selector); d3Vault.startLiquidation(address(d3MM)); vm.prank(vaultOwner); d3Vault.addLiquidator(address(this)); d3Vault.startLiquidation(address(d3MM)); - vm.expectRevert(bytes(Errors.ALREADY_IN_LIQUIDATION)); + vm.expectRevert(Errors.D3VaultAlreadyInLiquidation.selector); d3Vault.startLiquidation(address(d3MM)); // When pool is in liquidation, cannot repay vm.startPrank(address(d3MM)); token1.approve(address(d3Vault), type(uint256).max); - vm.expectRevert(bytes(Errors.ALREADY_IN_LIQUIDATION)); + vm.expectRevert(Errors.D3VaultAlreadyInLiquidation.selector); d3Vault.poolRepay(address(token1), 100 * 1e8); - vm.expectRevert(bytes(Errors.ALREADY_IN_LIQUIDATION)); + vm.expectRevert(Errors.D3VaultAlreadyInLiquidation.selector); d3Vault.poolRepayAll(address(token1)); vm.stopPrank(); } @@ -281,7 +281,7 @@ contract D3VaultLiquidationTest is TestContext { vm.prank(vaultOwner); d3Vault.addLiquidator(liquidator); - vm.expectRevert(bytes(Errors.NOT_IN_LIQUIDATION)); + vm.expectRevert(Errors.D3VaultNotInLiquidation.selector); liquidateSwap(address(d3MM), address(token3), address(token2), 12 ether); vm.prank(liquidator); @@ -299,7 +299,7 @@ contract D3VaultLiquidationTest is TestContext { token3.approve(address(d3Vault), type(uint256).max); router.setSlippage(90); - vm.expectRevert(bytes(Errors.EXCEED_DISCOUNT)); + vm.expectRevert(Errors.D3VaultExceedDiscount.selector); liquidateSwap(address(d3MM), address(token3), address(token2), 12 ether); router.setSlippage(100); @@ -340,7 +340,7 @@ contract D3VaultLiquidationTest is TestContext { vm.prank(vaultOwner); d3Vault.removeRouter(address(liquidationRouter)); - vm.expectRevert(bytes(Errors.NOT_ALLOWED_ROUTER)); + vm.expectRevert(Errors.D3VaultNotAllowedRouter.selector); liquidateSwap(address(d3MM), address(token3), address(token2), 12 ether); } @@ -349,7 +349,7 @@ contract D3VaultLiquidationTest is TestContext { d3Vault.addLiquidator(liquidator); vm.prank(liquidator); - vm.expectRevert(bytes(Errors.NOT_IN_LIQUIDATION)); + vm.expectRevert(Errors.D3VaultNotInLiquidation.selector); d3Vault.finishLiquidation(address(d3MM)); // pool has @@ -368,7 +368,7 @@ contract D3VaultLiquidationTest is TestContext { token2.approve(address(d3Vault), type(uint256).max); vm.prank(liquidator); - vm.expectRevert(bytes(Errors.LIQUIDATION_NOT_DONE)); + vm.expectRevert(Errors.D3VaultLiquidationNotDone.selector); d3Vault.finishLiquidation(address(d3MM)); uint256 token2CashBefore = d3Vault.getCash(address(token2)); @@ -429,7 +429,7 @@ contract D3VaultLiquidationTest is TestContext { router.setSlippage(96); liquidateSwap(address(d3MM), address(token3), address(token2), 50 ether); vm.prank(liquidator); - vm.expectRevert(bytes(Errors.LIQUIDATION_NOT_DONE)); + vm.expectRevert(Errors.D3VaultLiquidationNotDone.selector); d3Vault.finishLiquidation(address(d3MM)); } }