// 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
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;
}
}
}
This is a Solidity contract that represents a modified ERC20 token called "turbo." It includes a 3% transfer fee, which is swapped to USDT using Uniswap V3 and sent to the contract. The contract also includes a buyback mechanism that buys back turbo tokens when the price drops below a certain threshold.
The contract includes the following functions:
transfer: Transfers a specified amount of turbo tokens to a recipient, deducting a 3% transfer fee.transferFrom: Transfers a specified amount of turbo tokens from a sender to a recipient, deducting a 3% transfer fee.Swap: Swaps a specified amount of tokens for another token using Uniswap V3, with the specified fee tier.checkAndBuyback: Checks if the price of turbo tokens has dropped below a certain threshold, and if so, buys back turbo tokens using USDT.swapToToken: Swaps a specified amount of USDT for turbo tokens using Uniswap V3, with the specified fee tier._calculatePrice: Calculates the price of 1 wei of token0 in terms of token1, given the square root price._calculateFloorPrice: Calculates the floor price of turbo tokens, which is the USDT balance of the contract converted to token value divided by the total supply._calculateBuybackAmount: Calculates the USDT amount needed to buy back enough tokens to reach the floor price.The contract also includes the following events:
FeeSwapped: Emitted when a transfer fee is swapped to USDT.Buyback: Emitted when turbo tokens are bought back using USDT.Overall, this contract implements a token with a transfer fee and a buyback mechanism, using Uniswap V3 for swapping tokens.