Skip to content

Commit

Permalink
carbon pol eth conversion - minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanzhelyazkov committed Nov 20, 2023
1 parent d5b249c commit 9d22be9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
33 changes: 24 additions & 9 deletions contracts/pol/CarbonPOL.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
*/
Expand Down
8 changes: 6 additions & 2 deletions contracts/pol/interfaces/ICarbonPOL.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ interface ICarbonPOL is IUpgradeable {
error InvalidTrade();
error TradingDisabled();
error InsufficientNativeTokenSent();
error InsufficientTokenBalance();
error InsufficientEthForSale();
error InsufficientAmountForTrading();

struct Price {
uint128 sourceAmount;
Expand Down Expand Up @@ -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
*/
Expand Down
28 changes: 14 additions & 14 deletions test/forge/CarbonPOL.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 9d22be9

Please sign in to comment.