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 - add maxInput #161

Merged
merged 4 commits into from
Aug 19, 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
4 changes: 2 additions & 2 deletions contracts/helpers/TestReenterCarbonVortex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ contract TestReenterCarbonVortex {
_carbonVortex.execute(tokens);
}

function tryReenterCarbonVortexTrade(Token token, uint128 targetAmount) external payable {
_carbonVortex.trade{ value: msg.value }(token, targetAmount);
function tryReenterCarbonVortexTrade(Token token, uint128 targetAmount, uint128 maxInput) external payable {
_carbonVortex.trade{ value: msg.value }(token, targetAmount, maxInput);
}
}
19 changes: 14 additions & 5 deletions contracts/vortex/CarbonVortex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -614,23 +614,28 @@ contract CarbonVortex is ICarbonVortex, Upgradeable, ReentrancyGuardUpgradeable,
*/
function trade(
Token token,
uint128 targetAmount
uint128 targetAmount,
uint128 maxInput
) external payable nonReentrant validToken(token) greaterThanZero(targetAmount) {
uint128 sourceAmount;
if (token == _targetToken) {
sourceAmount = _sellTargetForFinalTarget(targetAmount);
sourceAmount = _sellTargetForFinalTarget(targetAmount, maxInput);
} else {
sourceAmount = _sellTokenForTargetToken(token, targetAmount);
sourceAmount = _sellTokenForTargetToken(token, targetAmount, maxInput);
}
emit TokenTraded({ caller: msg.sender, token: token, sourceAmount: sourceAmount, targetAmount: targetAmount });
}

function _sellTokenForTargetToken(Token token, uint128 targetAmount) private returns (uint128) {
function _sellTokenForTargetToken(Token token, uint128 targetAmount, uint128 maxInput) private returns (uint128) {
uint128 sourceAmount = expectedTradeInput(token, targetAmount);
// revert if trade requires 0 target token
if (sourceAmount == 0) {
revert InvalidTrade();
}
// revert if trade requires more than maxInput
if (sourceAmount > maxInput) {
revert GreaterThanMaxInput();
}
// revert if unnecessary native token is received
if (_targetToken != NATIVE_TOKEN && msg.value > 0) {
revert UnnecessaryNativeTokenReceived();
Expand Down Expand Up @@ -673,12 +678,16 @@ contract CarbonVortex is ICarbonVortex, Upgradeable, ReentrancyGuardUpgradeable,
return sourceAmount;
}

function _sellTargetForFinalTarget(uint128 targetAmount) private returns (uint128) {
function _sellTargetForFinalTarget(uint128 targetAmount, uint128 maxInput) private returns (uint128) {
uint128 sourceAmount = expectedTradeInput(_targetToken, targetAmount);
// revert if trade requires 0 finalTarget tokens
if (sourceAmount == 0) {
revert InvalidTrade();
}
// revert if trade requires more than maxInput
if (sourceAmount > maxInput) {
revert GreaterThanMaxInput();
}

// check enough final target token (if final target token is native) has been sent for the trade
if (_finalTargetToken == NATIVE_TOKEN) {
Expand Down
4 changes: 3 additions & 1 deletion contracts/vortex/interfaces/ICarbonVortex.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ICarbonVortex is IUpgradeable {
error InvalidTrade();
error TradingDisabled();
error PairDisabled();
error GreaterThanMaxInput();
error InsufficientNativeTokenSent();
error InsufficientAmountForTrading();
error UnnecessaryNativeTokenReceived();
Expand Down Expand Up @@ -196,8 +197,9 @@ interface ICarbonVortex is IUpgradeable {
* @notice trades *targetToken* for *targetAmount* of *token* based on the current token price (trade by target amount)
* @notice if token == *targetToken*, trades *finalTargetToken* for amount of *targetToken* and also
* @notice resets the current token sale amount if it's below the min amount after a trade
* @notice reverts if source amount required is greater than maxInput
*/
function trade(Token token, uint128 targetAmount) external payable;
function trade(Token token, uint128 targetAmount, uint128 maxInput) external payable;
barakman marked this conversation as resolved.
Show resolved Hide resolved

/**
* @notice withdraws the fees of the provided token from Carbon and
Expand Down
34 changes: 20 additions & 14 deletions deployments/run-testnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ if [ -z "$network_id" ] || [ "$network_id" == "null" ]; then
network_id=${TENDERLY_NETWORK_ID:-"1"}
fi

# Ensure network_id is a number
network_id=$((network_id + 0))

echo "Creating a $network_name Tenderly Testnet with Chain Id $network_id... "
echo

# API Endpoint for creating a testnet
TENDERLY_TESTNET_API="https://api.tenderly.co/api/v1/account/${username}/project/${project}/testnet/container"
TENDERLY_TESTNET_API="https://api.tenderly.co/api/v1/account/${username}/project/${project}/vnets"
# Get the current timestamp to use as a unique testnet slug
timestamp=$(date +"%s")

# Setup cleanup function
cleanup() {
Expand All @@ -50,23 +55,24 @@ trap cleanup TERM EXIT
response=$(curl -sX POST "$TENDERLY_TESTNET_API" \
-H "Content-Type: application/json" -H "X-Access-Key: ${TENDERLY_ACCESS_KEY}" \
-d '{
"displayName": "Carbon Contracts Testnet",
"description": "",
"visibility": "TEAM",
"tags": {
"purpose": "development"
"slug": "carbon-contracts-testnet-'${timestamp}'",
"display_name": "Carbon Contracts Testnet",
"fork_config": {
"network_id": '"${network_id}"',
"block_number": "latest"
},
"networkConfig": {
"networkId": "'${network_id}'",
"blockNumber": "latest",
"baseFeePerGas": "1"
"virtual_network_config": {
"chain_config": {
"chain_id": '"${network_id}"'
}
},
"private": true,
"syncState": false
"sync_state_config": {
"enabled": false
}
}')

testnet_id=$(echo "$response" | jq -r '.container.id')
provider_url=$(echo "$response" | jq -r '.container.connectivityConfig.endpoints[0].uri')
testnet_id=$(echo "$response" | jq -r '.id')
provider_url=$(echo "$response" | jq -r '.rpcs[0].url')

echo "Created Tenderly Testnet ${testnet_id} at ${username}/${project}..."
echo
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"clean": "rm -rf artifacts cache coverage typechain-types"
},
"dependencies": {
"hardhat": "2.15.0"
"hardhat": "2.22.8"
},
"devDependencies": {
"@anders-t/ethers-ledger": "^1.0.4",
Expand Down
Loading
Loading