Verified contractPartial
BuyAndSell
Address0x29375fa554b0debf7726184f58fffbb3605b9a3e
- Balance
0 ETH
- Transactions
4
- Compiler
solc 0.8.0
- Same code
Only this contract
Details
- Compiler
- solc 0.8.0+commit.c7dfd78e
- Language
- solidity
- Optimization
- No
- License
- Not stated
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 17,192,693
- Creation transaction
- 0x43e8f14191…e1c646363b
BuyAndSell.sol
// File: @uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router01.sol
pragma solidity >=0.6.2;
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function removeLiquidity(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB);
function removeLiquidityETH(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountToken, uint amountETH);
function removeLiquidityWithPermit(
address tokenA,
address tokenB,
uint liquidity,
uint amountAMin,
uint amountBMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountA, uint amountB);
function removeLiquidityETHWithPermit(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountETH);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}
// File: @uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol
pragma solidity >=0.6.2;
interface IUniswapV2Router02 is IUniswapV2Router01 {
function removeLiquidityETHSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external returns (uint amountETH);
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
address token,
uint liquidity,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline,
bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountETH);
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}
// File: @openzeppelin/contracts/token/ERC20/IERC20.sol
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool);
}
// File: Trader.sol
pragma solidity ^0.8.0;
contract BuyAndSell {
IUniswapV2Router02 public uniswapV2Router;
IERC20 public token;
address public owner;
modifier onlyOwner() {
require(msg.sender == owner, "Caller is not the owner.");
_;
}
constructor(address _router, address _token) {
uniswapV2Router = IUniswapV2Router02(_router);
token = IERC20(_token);
owner = msg.sender;
}
function buyAndSell(uint deadline) external payable onlyOwner {
require(msg.value > 0, "Must send ETH to execute the trade.");
address[] memory path = new address[](2);
path[0] = uniswapV2Router.WETH();
path[1] = address(token);
uint[] memory amountsIn = uniswapV2Router.getAmountsIn(msg.value, path);
uint tokenAmountOut = amountsIn[1];
// Check slippage for buy trade
uint maxTokenAmountOut = (tokenAmountOut * 1001) / 1000;
// Buy the token with ETH sent to the contract
uniswapV2Router.swapExactETHForTokens{value: msg.value}(tokenAmountOut, path, address(this), deadline);
// Approve the Uniswap router to spend the purchased tokens
uint tokenBalance = token.balanceOf(address(this));
token.approve(address(uniswapV2Router), tokenBalance);
// Check slippage for sell trade
uint[] memory amountsOut = uniswapV2Router.getAmountsOut(tokenBalance, path);
uint ethAmountOut = amountsOut[1];
uint minEthAmountOut = (ethAmountOut * 1000) / 1001;
// Sell the token back to ETH
uniswapV2Router.swapExactTokensForETH(tokenBalance, minEthAmountOut, path, msg.sender, deadline);
}
function executeMultipleTrades(uint numTrades, uint deadline) external payable onlyOwner {
require(numTrades > 0, "Number of trades must be greater than 0.");
uint ethPerTrade = msg.value / numTrades;
require(ethPerTrade > 0, "Not enough ETH for the specified number of trades.");
for (uint i = 0; i < numTrades; i++) {
this.buyAndSell{value: ethPerTrade}(deadline);
}
}
function buyAndSellTokenAmount(uint tokenAmount, uint deadline) external payable onlyOwner {
require(msg.value > 0, "Must send ETH to execute the trade.");
require(tokenAmount > 0, "Token amount must be greater than 0.");
address[] memory path = new address[](2);
path[0] = uniswapV2Router.WETH();
path[1] = address(token);
uint[] memory amountsIn = uniswapV2Router.getAmountsIn(msg.value, path);
uint ethAmountRequired = amountsIn[0];
require(msg.value >= ethAmountRequired, "Not enough ETH to buy the specified token amount.");
// Check slippage for buy trade
uint maxTokenAmountOut = (tokenAmount * 1001) / 1000;
// Buy the token with ETH sent to the contract
uniswapV2Router.swapExactETHForTokens{value: msg.value}(tokenAmount, path, address(this), deadline);
// Approve the Uniswap router to spend the purchased tokens
uint tokenBalance = token.balanceOf(address(this));
token.approve(address(uniswapV2Router), tokenBalance);
// Check slippage for sell trade
uint[] memory amountsOut = uniswapV2Router.getAmountsOut(tokenBalance, path);
uint ethAmountOut = amountsOut[1];
uint minEthAmountOut = (ethAmountOut * 1000) / 1001;
// Sell the token back to ETH
uniswapV2Router.swapExactTokensForETH(tokenBalance, minEthAmountOut, path, msg.sender, deadline);
}
function withdrawETH() external onlyOwner {
uint balance = address(this).balance;
require(balance > 0, "No ETH balance to withdraw.");
(bool success, ) = msg.sender.call{value: balance}("");
require(success, "ETH withdrawal failed.");
}
}Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": false
}
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.