// SPDX-License-Identifier: MIT pragma solidity ^0.8.23; import…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
// SPDX-License-Identifier: MIT pragma solidity ^0.8.23;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol"; import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Factory.sol"; import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol"; // Import Uniswap V3 Pool interface import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol"; import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol"; import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
contract Turbo is ERC20, ReentrancyGuard { using SafeERC20 for IERC20;
address private _stableCoinAddress;
address private _swapRouterAddressV3;
address private _swapRouterAddressV2;
uint256 private _feePercentage;
address private _uniswapV3FactoryAddress;
address private _uniswapV2FactoryAddress;
uint24 private _poolFee;
uint256 private totalSupply_;
event FeeSwapped(address indexed from, address indexed to, uint256 amount);
event Buyback(address indexed buyer, uint256 amount);
constructor(string memory name_,string memory symbol_) ERC20(name_, symbol_) {
_mint(msg.sender, totalSupply_ = 1e27);
_stableCoinAddress = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
_swapRouterAddressV3 = 0xE592427A0AEce92De3Edee1F18E0157C05861564;
_swapRouterAddressV2 = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
_feePercentage = 30;
_uniswapV3FactoryAddress = 0x1F98431c8aD98523631AE4a59f267346ea31F984;
_uniswapV2FactoryAddress = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
_poolFee = 500;
}
function transfer(address recipient, uint256 amount) public override nonReentrant returns (bool) {
require(amount > 0, "Amount must be > 0");
uint256 fee = (amount * _feePercentage) / 10000;
uint256 amountAfterFee = amount - fee;
super.transfer(recipient, amountAfterFee);
if (fee > 0) {
super._transfer(_msgSender(), address(this), fee);
swapOnv3(fee, address(this), _stableCoinAddress);
//if it fails in the v3 pool, then it uses the v2 pool
swapOnV2(fee, address(this), _stableCoinAddress);
}
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public override nonReentrant returns (bool) {
require(amount > 0, "Amount must be > 0");
uint256 fee = (amount * _feePercentage) / 10000;
uint256 amountAfterFee = amount - fee;
super.transferFrom(sender, recipient, amountAfterFee);
if (fee > 0) {
super._transfer(sender, address(this), fee);
swapOnv3(fee, address(this), _stableCoinAddress);
//if it fails in the v3 pool, then it uses the v2 pool
swapOnV2(fee, address(this), _stableCoinAddress);
}
return true;
}
function swapOnv3(uint256 amount, address _tokenIn, address _tokenOut) internal nonReentrant returns (bool) {
IERC20(_tokenIn).safeIncreaseAllowance(_swapRouterAddressV3, amount);
ISwapRouter(_swapRouterAddressV3).exactInputSingle(ISwapRouter.ExactInputSingleParams({
tokenIn: _tokenIn,
tokenOut: _tokenOut,
fee: _poolFee,
recipient: address(this),
deadline: block.timestamp + 600,
amountIn: amount,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
})
);
emit FeeSwapped(_tokenIn, _tokenOut, amount);
return true;
}
function swapOnV2(uint256 amount,address _tokenIn,address _tokenOut) private nonReentrant returns (bool) {
address[] memory path = new address[](2);
path[0] = _tokenIn;
path[1] = _tokenOut;
IUniswapV2Router02(_swapRouterAddressV2).swapExactTokensForTokens(amount,0,path,address(this),block.timestamp + 600);
emit FeeSwapped(_tokenIn, _tokenOut, amount);
return true;
}
function checkAndBuyback() external nonReentrant {
require(_uniswapV3FactoryAddress != address(0), "Invalid factory");
require(_stableCoinAddress != address(0), "Invalid USDT address");
require(_poolFee > 0, "Invalid pool fee");
address poolAddress = IUniswapV3Factory(_uniswapV3FactoryAddress).getPool(address(this), _stableCoinAddress, _poolFee);
require(poolAddress != address(0), "Uniswap V3 pool does not exist");
(uint160 sqrtPriceX96,,,,,,) = IUniswapV3Pool(poolAddress).slot0();
uint256 currentPrice = (sqrtPriceX96 * sqrtPriceX96 * 1e18) / (2**192);
uint256 usdtBalance = IERC20(_stableCoinAddress).balanceOf(address(this));
if (usdtBalance == 0) {
return;
}
uint256 floorPrice = (usdtBalance * 1e18) / totalSupply();
require(currentPrice < floorPrice, "Buyback not needed");
uint256 tokensAtCurrentPrice = (usdtBalance * 1e18) / currentPrice;
uint256 tokensAtFloorPrice = (usdtBalance * 1e18) / floorPrice;
if (tokensAtCurrentPrice < tokensAtFloorPrice) {
uint256 buybackAmount = tokensAtFloorPrice - tokensAtCurrentPrice;
if (buybackAmount > 0) {
// First, try swapping on Uniswap V3
if(swapOnv3(buybackAmount, _stableCoinAddress, address(this)) == false){
swapOnV2(buybackAmount, _stableCoinAddress, address(this));
}
emit Buyback(msg.sender, buybackAmount);
}
}
}
}
function swapOnV2(uint256 _amount, address _toToken, address _recipient) internal { address[] memory path = new address; path[0] = address(_token1); path[1] = _toToken;
_token1.approve(address(_uniswapV2Router), _amount);
_uniswapV2Router.swapExactTokensForTokensSupportingFeeOnTransferTokens(
_amount,
0, // accept any amount of Tokens
path,
_recipient,
block.timestamp.add(300)
);
}
}