// SPDX-License-Identifier: MIT pragma solidity ^0.8.23; import…
- User
// 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); } } }}
Assistant
The provided Solidity code defines a Turbo contract that extends the ERC20 contract and implements a fee-on-transfer mechanism. Here's a breakdown of the code:
-
Contract Initialization:
- The constructor initializes the contract with the following parameters:
name_andsymbol_: Name and symbol of the token._stableCoinAddress: Address of the stablecoin used for fee swapping._swapRouterAddressV3: Address of the Uniswap V3 swap router._swapRouterAddressV2: Address of the Uniswap V2 swap router._feePercentage: Percentage of fee charged on transfers._uniswapV3FactoryAddress: Address of the Uniswap V3 factory._uniswapV2FactoryAddress: Address of the Uniswap V2 factory._poolFee: Uniswap V3 pool fee.
- The constructor initializes the contract with the following parameters:
-
Fee-on-Transfer Mechanism:
- The
transferandtransferFromfunctions implement a fee-on-transfer mechanism. - When a transfer occurs, a fee is calculated as a percentage of the transferred amount.
- The fee is then swapped for a stablecoin using Uniswap V3 or V2, depending on which one is successful.
- The swapped stablecoins are accumulated in the contract's balance.
- The
-
Uniswap V3 and V2 Swapping:
- The
swapOnv3andswapOnV2functions are used to swap tokens on Uniswap V3 and V2, respectively. - These functions are used to swap the fee collected during transfers for a stablecoin.
- The
-
Buyback Mechanism:
- The
checkAndBuybackfunction checks if a buyback is needed based on current price conditions. - It calculates the current price of the token using Uniswap V3 pool data.
- If the current price is below a certain threshold, the function swaps stablecoins for the token, effectively buying back the token.
- The
-
Events:
- The contract emits
FeeSwappedevents when fees are swapped for a stablecoin. - It also emits
Buybackevents when a buyback is performed.
- The contract emits
In summary, the Turbo contract implements a fee-on-transfer mechanism, swaps fees for a stablecoin using Uniswap V3 or V2, and has a buyback mechanism to maintain a certain price floor.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.