From 9d22be91d5565d8c662306db86b4cec14bbb442d Mon Sep 17 00:00:00 2001 From: Ivan Zhelyazkov Date: Mon, 20 Nov 2023 14:20:41 +0200 Subject: [PATCH] carbon pol eth conversion - minor fixes --- contracts/pol/CarbonPOL.sol | 33 ++++++++++++++++++------- contracts/pol/interfaces/ICarbonPOL.sol | 8 ++++-- test/forge/CarbonPOL.t.sol | 28 ++++++++++----------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/contracts/pol/CarbonPOL.sol b/contracts/pol/CarbonPOL.sol index e5817f96..5a01356e 100644 --- a/contracts/pol/CarbonPOL.sol +++ b/contracts/pol/CarbonPOL.sol @@ -207,6 +207,13 @@ contract CarbonPOL is ICarbonPOL, Upgradeable, ReentrancyGuardUpgradeable, Utils return _tradingEnabled(token); } + /** + * @inheritdoc ICarbonPOL + */ + function amountAvailableForTrading(Token token) external view returns (uint128) { + return _amountAvailableForTrading(token); + } + /** * @inheritdoc ICarbonPOL */ @@ -218,9 +225,9 @@ contract CarbonPOL is ICarbonPOL, Upgradeable, ReentrancyGuardUpgradeable, Utils uint128 tradeReturn = MathEx .mulDivF(currentPrice.targetAmount, tradeInput, currentPrice.sourceAmount) .toUint128(); - // revert if not enough token balance - if (tradeReturn > token.balanceOf(address(this))) { - revert InsufficientTokenBalance(); + // revert if not enough amount available for trade + if (tradeReturn > _amountAvailableForTrading(token)) { + revert InsufficientAmountForTrading(); } return tradeReturn; } @@ -229,9 +236,9 @@ contract CarbonPOL is ICarbonPOL, Upgradeable, ReentrancyGuardUpgradeable, Utils * @inheritdoc ICarbonPOL */ function expectedTradeInput(Token token, uint128 tokenAmount) public view validToken(token) returns (uint128) { - // revert if not enough token balance for trade - if (tokenAmount > token.balanceOf(address(this))) { - revert InsufficientTokenBalance(); + // revert if not enough amount available for trade + if (tokenAmount > _amountAvailableForTrading(token)) { + revert InsufficientAmountForTrading(); } Price memory currentPrice = tokenPrice(token); // revert if current price is not valid @@ -302,9 +309,6 @@ contract CarbonPOL is ICarbonPOL, Upgradeable, ReentrancyGuardUpgradeable, Utils } function _sellETHForBNT(uint128 amount) private returns (uint128) { - if (_ethSaleAmount.current < amount) { - revert InsufficientEthForSale(); - } uint128 bntRequired = expectedTradeInput(NATIVE_TOKEN, amount); // revert if trade requires 0 bnt if (bntRequired == 0) { @@ -387,6 +391,17 @@ contract CarbonPOL is ICarbonPOL, Upgradeable, ReentrancyGuardUpgradeable, Utils emit EthSaleAmountUpdated(prevEthSaleAmount, newEthSaleAmount); } + /** + * @dev returns the token amount available for trading + */ + function _amountAvailableForTrading(Token token) private view returns (uint128) { + if (token == NATIVE_TOKEN) { + return _ethSaleAmount.current; + } else { + return uint128(token.balanceOf(address(this))); + } + } + /** * @dev validate token helper */ diff --git a/contracts/pol/interfaces/ICarbonPOL.sol b/contracts/pol/interfaces/ICarbonPOL.sol index 0837c316..c4368c28 100644 --- a/contracts/pol/interfaces/ICarbonPOL.sol +++ b/contracts/pol/interfaces/ICarbonPOL.sol @@ -13,8 +13,7 @@ interface ICarbonPOL is IUpgradeable { error InvalidTrade(); error TradingDisabled(); error InsufficientNativeTokenSent(); - error InsufficientTokenBalance(); - error InsufficientEthForSale(); + error InsufficientAmountForTrading(); struct Price { uint128 sourceAmount; @@ -76,6 +75,11 @@ interface ICarbonPOL is IUpgradeable { */ function tradingEnabled(Token token) external view returns (bool); + /** + * @notice returns the amount available for trading for the token + */ + function amountAvailableForTrading(Token token) external view returns (uint128); + /** * @notice returns the expected trade output (tokens received) given an token amount sent */ diff --git a/test/forge/CarbonPOL.t.sol b/test/forge/CarbonPOL.t.sol index 9d9b7ea7..01516cb3 100644 --- a/test/forge/CarbonPOL.t.sol +++ b/test/forge/CarbonPOL.t.sol @@ -953,21 +953,21 @@ contract CarbonPOLTest is TestFixture { uint256 tokenBalance = token1.balanceOf(address(carbonPOL)); uint128 amount = uint128(tokenBalance) + 1; // get expected trade input - vm.expectRevert(ICarbonPOL.InsufficientTokenBalance.selector); + vm.expectRevert(ICarbonPOL.InsufficientAmountForTrading.selector); carbonPOL.expectedTradeInput(token1, amount); } - /// @dev test should revert expected input for native token if not enough token balance - function testShouldRevertExpectedTradeInputIfNotEnoughNativeTokenBalanceForTrade() public { + /// @dev test should revert expected input for native token if not enough current sale amount for trade + function testShouldRevertExpectedTradeInputIfNotEnoughNativeTokenSaleAmountForTrade() public { vm.prank(admin); // enable native token to test carbonPOL.enableTradingETH(ICarbonPOL.Price({ sourceAmount: 10000, targetAmount: 100000000 })); // set timestamp to 5 days vm.warp(5 days); - uint256 ethBalance = NATIVE_TOKEN.balanceOf(address(carbonPOL)); + uint128 saleAmount = carbonPOL.ethSaleAmount().current; // get expected trade input - vm.expectRevert(ICarbonPOL.InsufficientTokenBalance.selector); - carbonPOL.expectedTradeInput(NATIVE_TOKEN, uint128(ethBalance) + 1e18); + vm.expectRevert(ICarbonPOL.InsufficientAmountForTrading.selector); + carbonPOL.expectedTradeInput(NATIVE_TOKEN, saleAmount + 1e18); } /// @dev test should revert expected return if not enough token balance @@ -983,24 +983,24 @@ contract CarbonPOLTest is TestFixture { uint128 expectedInput = carbonPOL.expectedTradeInput(token1, uint128(tokenBalance)); uint128 amount = expectedInput + 1; // expect revert - vm.expectRevert(ICarbonPOL.InsufficientTokenBalance.selector); + vm.expectRevert(ICarbonPOL.InsufficientAmountForTrading.selector); carbonPOL.expectedTradeReturn(token1, amount); } - /// @dev test should revert expected return if not enough token balance - function testShouldRevertExpectedReturnIfNotEnoughNativeTokenBalanceForTrade() public { + /// @dev test should revert expected return for native token if not enough current sale amount for the trade + function testShouldRevertExpectedReturnIfNotEnoughNativeTokenSaleAmountForTrade() public { vm.startPrank(admin); // enable token to test carbonPOL.enableTradingETH(ICarbonPOL.Price({ sourceAmount: 10000, targetAmount: 100000000 })); vm.stopPrank(); // set timestamp to 5 days vm.warp(5 days); - uint256 ethBalance = address(carbonPOL).balance; + uint128 saleAmount = carbonPOL.ethSaleAmount().current; // get expected trade input - uint128 expectedInput = carbonPOL.expectedTradeInput(NATIVE_TOKEN, uint128(ethBalance)); - uint128 amount = uint128(expectedInput) + 1e18; + uint128 expectedInput = carbonPOL.expectedTradeInput(NATIVE_TOKEN, saleAmount); + uint128 amount = expectedInput + 1; // expect revert - vm.expectRevert(ICarbonPOL.InsufficientTokenBalance.selector); + vm.expectRevert(ICarbonPOL.InsufficientAmountForTrading.selector); carbonPOL.expectedTradeReturn(NATIVE_TOKEN, amount); } @@ -1084,7 +1084,7 @@ contract CarbonPOLTest is TestFixture { // trade a bit over 100 eth uint128 sourceAmount = 100 ether + 1; // expect trade to revert - vm.expectRevert(ICarbonPOL.InsufficientEthForSale.selector); + vm.expectRevert(ICarbonPOL.InsufficientAmountForTrading.selector); carbonPOL.trade(token, sourceAmount); vm.stopPrank(); }