// 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
contract turbo is ERC20, ReentrancyGuard { using SafeERC20 for IERC20;
address private _usdtAddress; address private _swapRouterAddress; uint256 private _feePercentage; address private _uniswapV3FactoryAddress; uint24 private _poolFee; uint256 private totalSupply_; event FeeSwapped(address indexed from, address indexed to, uint256 amount); event Buyback(address indexed buyer, uint256 amount); // Balances and allowances constructor( string memory name_, string memory symbol_ ) ERC20(name_, symbol_) { _mint(msg.sender, totalSupply_ = 1000000000000000000000000000000); _usdtAddress = 0xdAC17F958D2ee523a2206206994597C13D831ec7; _swapRouterAddress = 0xE592427A0AEce92De3Edee1F18E0157C05861564; _feePercentage = 30; _uniswapV3FactoryAddress = 0x1F98431c8aD98523631AE4a59f267346ea31F984; _poolFee = 500; } function transfer(address recipient, uint256 amount) public override nonReentrant returns (bool) { uint256 fee = (amount * _feePercentage) / 10000; uint256 amountAfterFee = amount - fee; super.transfer(recipient, amountAfterFee); // Call the ERC20 transfer if (fee > 0) { super._transfer(_msgSender(), address(this), fee); // Transfer the fee to the contract Swap(fee,address(this),_usdtAddress); // Handle the fee } return true; } function transferFrom(address sender,address recipient,uint256 amount) public override nonReentrant returns (bool) { uint256 fee = (amount * _feePercentage) / 10000; uint256 amountAfterFee = amount - fee; super.transferFrom(sender, recipient, amountAfterFee); // Call the ERC20 transferFrom if (fee > 0) { super._transfer(sender, address(this), fee); // Transfer the fee to the contract Swap(fee,address(this),_usdtAddress); // Handle the fee } return true; } function Swap(uint256 amount,address _tokenIn,address _tokenOut) internal nonReentrant{ IERC20(_tokenIn).safeIncreaseAllowance(_swapRouterAddress, amount); ISwapRouter(_swapRouterAddress).exactInputSingle( ISwapRouter.ExactInputSingleParams({ tokenIn: _tokenIn, tokenOut: _tokenOut, fee: _poolFee, recipient: address(this), deadline: block.timestamp + 600, amountIn: amount, amountOutMinimum: 0, sqrtPriceLimitX96: 0 }) ); emit FeeSwapped(msg.sender, _usdtAddress, amount); } function checkAndBuyback() external nonReentrant { require(_uniswapV3FactoryAddress != address(0), "Invalid factory"); require(_usdtAddress != address(0), "Invalid USDT address"); require(_poolFee > 0, "Invalid pool fee"); address poolAddress = IUniswapV3Factory(_uniswapV3FactoryAddress).getPool(address(this), _usdtAddress, _poolFee); require(poolAddress != address(0), "Uniswap V3 pool does not exist"); (uint160 sqrtPriceX96,,,,,,) = IUniswapV3Pool(poolAddress).slot0(); uint256 currentPrice = _calculatePrice(sqrtPriceX96); uint256 floorPrice = _calculateFloorPrice(); if (currentPrice < floorPrice) { uint256 buybackAmount = _calculateBuybackAmount(currentPrice, floorPrice); if (buybackAmount > 0) { swapToToken(buybackAmount); Swap(buybackAmount,_usdtAddress,address(this)); emit Buyback(msg.sender, buybackAmount); } } } function swapToToken(uint256 usdtAmount) internal nonReentrant { IERC20(_usdtAddress).safeIncreaseAllowance(_swapRouterAddress, usdtAmount); ISwapRouter(_swapRouterAddress).exactInputSingle( ISwapRouter.ExactInputSingleParams({ tokenIn: _usdtAddress, tokenOut: address(this), fee: _poolFee, recipient: address(this), deadline: block.timestamp + 600, amountIn: usdtAmount, amountOutMinimum: 0, sqrtPriceLimitX96: 0 }) ); } function _calculatePrice(uint160 sqrtPriceX96) internal pure returns(uint256) { // Price of 1 wei of token0 in terms of token1 return (uint256(sqrtPriceX96) * uint256(sqrtPriceX96) * 1e18) >> (96 * 2); } function _calculateFloorPrice() internal view returns (uint256) { // The floor price is calculated as the USDT balance of the contract converted to token value divided by the total supply uint256 usdtBalance = IERC20(_usdtAddress).balanceOf(address(this)); return (usdtBalance * 1e18) / totalSupply(); // Assuming USDT has 6 decimals } function _calculateBuybackAmount(uint256 currentPrice, uint256 floorPrice) internal view returns (uint256) { // Calculate the USDT amount needed to buy back enough tokens to reach the floor price uint256 usdtBalance = IERC20(_usdtAddress).balanceOf(address(this)); // Ensure there's enough USDT for the buyback if (usdtBalance == 0) { return 0; } // Calculate the amount of tokens that the current USDT balance can buy at the current price uint256 tokensAtCurrentPrice = (usdtBalance * 1e18) / currentPrice; // Calculate the amount of tokens that the USDT balance should be able to buy at the floor price uint256 tokensAtFloorPrice = (usdtBalance * 1e18) / floorPrice; // The buyback amount is the difference needed to reach the floor price if (tokensAtCurrentPrice < tokensAtFloorPrice) { return tokensAtFloorPrice - tokensAtCurrentPrice; } else { return 0; } }}
Assistant
This is a Solidity contract for a token called "turbo" that uses the ERC20 standard and implements a fee-based buyback mechanism using Uniswap V3.
When a transfer or transferFrom is called, a fee percentage (default 3000 basis points) is calculated and deducted from the amount being transferred. The fee is then transferred to the contract's address and swapped for USDT using the Uniswap V3 Swap Router. The Swap function also emits a FeeSwapped event, which logs the amount of the fee that was swapped.
The contract also includes a checkAndBuyback function that checks if the current price of the token is below a specified floor price, calculated as the USDT balance of the contract divided by the total supply of the token. If the current price is below the floor price, the contract will calculate the amount of tokens needed to buy back to reach the floor price and call the swapToToken function to execute the buyback. The swapToToken function swaps USDT for the token using the Uniswap V3 Swap Router, and then calls the Swap function to swap the bought tokens back to USDT and transfer them to the contract's address. The checkAndBuyback function also emits a Buyback event, which logs the amount of tokens that were bought back.
The contract also includes functions for calculating the current price of the token based on the sqrtPriceX96 value returned by the Uniswap V3 Pool contract, and for calculating the floor price and buyback amount based on the USDT balance of the contract and the total supply of the token.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.