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

carbon vortex - handle edge case #153

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions contracts/vortex/CarbonVortex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ contract CarbonVortex is ICarbonVortex, Upgradeable, ReentrancyGuardUpgradeable,
if (sourceAmount == 0) {
revert InvalidTrade();
}
// revert if unnecessary native token is received
if (_targetToken != NATIVE_TOKEN && msg.value > 0) {
ivanzhelyazkov marked this conversation as resolved.
Show resolved Hide resolved
revert UnnecessaryNativeTokenReceived();
}
barakman marked this conversation as resolved.
Show resolved Hide resolved
// check enough target token (if target token is native) has been sent for the trade
if (_targetToken == NATIVE_TOKEN && msg.value < sourceAmount) {
revert InsufficientNativeTokenSent();
Expand Down Expand Up @@ -675,6 +679,10 @@ contract CarbonVortex is ICarbonVortex, Upgradeable, ReentrancyGuardUpgradeable,
if (sourceAmount == 0) {
revert InvalidTrade();
}
// revert if unnecessary native token is received
if (_finalTargetToken != NATIVE_TOKEN && msg.value > 0) {
revert UnnecessaryNativeTokenReceived();
}
barakman marked this conversation as resolved.
Show resolved Hide resolved

// check enough final target token (if final target token is native) has been sent for the trade
if (_finalTargetToken == NATIVE_TOKEN) {
Expand Down
1 change: 1 addition & 0 deletions contracts/vortex/interfaces/ICarbonVortex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface ICarbonVortex is IUpgradeable {
error PairDisabled();
error InsufficientNativeTokenSent();
error InsufficientAmountForTrading();
error UnnecessaryNativeTokenReceived();

struct Price {
uint128 sourceAmount;
Expand Down
58 changes: 58 additions & 0 deletions test/forge/CarbonVortex.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,64 @@ contract CarbonVortexTest is TestFixture {
assertEq(balanceGain, sourceAmount);
}

/// @dev test that sending any ETH with the transaction
/// @dev on target -> token trades should revert if targetToken != NATIVE_TOKEN
function testShouldRevertIfUnnecessaryNativeTokenSentOnTargetToFinalTargetTrades() public {
targetToken = bnt;
finalTargetToken = NATIVE_TOKEN;
Token token = token1;
// Deploy new Carbon Vortex with the target token set to a token different than native token
deployCarbonVortex(address(carbonController), vault, oldVortex, transferAddress, targetToken, finalTargetToken);
vm.prank(admin);
// set fees
uint256 accumulatedFees = 100 ether;
carbonController.testSetAccumulatedFees(token, accumulatedFees);

vm.startPrank(user1);

// execute
Token[] memory tokens = new Token[](1);
tokens[0] = token;
carbonVortex.execute(tokens);

// trade target for final target
uint128 targetAmount = 1 ether;

// advance time
vm.warp(45 days);

// trade
vm.expectRevert(ICarbonVortex.UnnecessaryNativeTokenReceived.selector);
carbonVortex.trade{ value: 1 }(token, targetAmount);
}

/// @dev test that sending any ETH with the transaction
/// @dev on final target -> target token trades should revert if finalTargetToken != NATIVE_TOKEN
function testShouldRevertIfUnnecessaryNativeTokenSentOnFinalTargetToTargetTrades() public {
Token token = targetToken;
vm.prank(admin);
// set fees
uint256 accumulatedFees = 100 ether;
carbonController.testSetAccumulatedFees(token, accumulatedFees);

vm.startPrank(user1);

// execute
Token[] memory tokens = new Token[](1);
tokens[0] = token;
carbonVortex.execute(tokens);

// trade target for final target
uint128 targetAmount = 1 ether;

// advance time
vm.warp(45 days);

// trade
vm.expectRevert(ICarbonVortex.UnnecessaryNativeTokenReceived.selector);
carbonVortex.trade{ value: 1 }(token, targetAmount);
}

/// @dev test that sending less than the sourceAmount of ETH with the transaction
/// @dev on final target -> target token trades should revert if finalTarget == NATIVE_TOKEN
function testShouldRevertIfInsufficientNativeTokenSentOnFinalTargetToTargetTokenTrade() public {
Expand Down
Loading