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

Peckshield audit fixes #146

Merged
merged 2 commits into from
May 29, 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
12 changes: 6 additions & 6 deletions contracts/vortex/CarbonVortex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ contract CarbonVortex is ICarbonVortex, Upgradeable, ReentrancyGuardUpgradeable,
// revert if current price is not valid
_validPrice(currentPrice);
// calculate the trade input based on the current price
return MathEx.mulDivF(currentPrice.sourceAmount, targetAmount, currentPrice.targetAmount).toUint128();
return MathEx.mulDivC(currentPrice.sourceAmount, targetAmount, currentPrice.targetAmount).toUint128();
}

/**
Expand Down Expand Up @@ -625,11 +625,6 @@ contract CarbonVortex is ICarbonVortex, Upgradeable, ReentrancyGuardUpgradeable,
// transfer the tokens to caller
token.safeTransfer(msg.sender, targetAmount);

// if the target token is native, refund any excess native token to caller
lbeder marked this conversation as resolved.
Show resolved Hide resolved
if (_targetToken == NATIVE_TOKEN && msg.value > sourceAmount) {
payable(msg.sender).sendValue(msg.value - sourceAmount);
}

// if no final target token is defined, transfer the target token to `transferAddress`
if (Token.unwrap(_finalTargetToken) == address(0)) {
// safe due to nonreenrant modifier (forwards all available gas if token is native)
Expand All @@ -643,6 +638,11 @@ contract CarbonVortex is ICarbonVortex, Upgradeable, ReentrancyGuardUpgradeable,
_resetTrading(token, 0);
}

// if the target token is native, refund any excess native token to caller
if (_targetToken == NATIVE_TOKEN && msg.value > sourceAmount) {
payable(msg.sender).sendValue(msg.value - sourceAmount);
}

return sourceAmount;
}

Expand Down
60 changes: 0 additions & 60 deletions test/forge/CarbonVortex.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1627,66 +1627,6 @@ contract CarbonVortexTest is TestFixture {
assertEq(balanceSent, expectedSourceAmount);
}

/// @dev test should revert if the trade requires 0 target token on finalTargetToken -> targetToken trades
function testShouldRevertIfTradeRequiresZeroTargetToken() public {
vm.prank(admin);
// set fees
uint256 accumulatedFees = 100 ether;
carbonController.testSetAccumulatedFees(targetToken, accumulatedFees);

vm.startPrank(user1);

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

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

// advance time to a point where the price is very low
vm.warp(60 days);

// get the expected trade input for 1 ether of target token
uint128 expectedSourceAmount = carbonVortex.expectedTradeInput(targetToken, targetAmount);

// approve the source token
finalTargetToken.safeApprove(address(carbonVortex), expectedSourceAmount);
// trade
vm.expectRevert(ICarbonVortex.InvalidTrade.selector);
carbonVortex.trade(targetToken, 1);
}

/// @dev test should revert if the trade requires 0 tokens on targetToken -> token trades
function testShouldRevertIfTradeRequiresZeroTokens() public {
vm.prank(admin);

Token token = token1;
// 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 to a point where the price is very low
vm.warp(60 days);

// get the expected trade input for 1 ether of target token
uint128 expectedSourceAmount = carbonVortex.expectedTradeInput(token, targetAmount);

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

/// @dev test should revert if user hasn't sent enough target tokens for trade
function testShouldRevertIfUserHasntSentEnoughTargetTokenForTrade() public {
vm.prank(admin);
Expand Down
Loading