// 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 for a token called "turbo" that uses the ERC20 standard and includes a fee-based buyback mechanism. The contract is based on the OpenZeppelin Contracts library and includes the ReentrancyGuard contract to prevent reentrancy attacks.
The contract imports several other OpenZeppelin Contracts libraries, including ERC20, SafeERC20, and ReentrancyGuard, as well as the Uniswap V3 periphery and core contracts.
The constructor initializes the token with a name and symbol, mints an initial supply of tokens to the deployer's address, and sets several parameters, including the address of USDT, the address of the Uniswap V3 swap router, the fee percentage for buybacks, the address of the Uniswap V3 factory contract, and the pool fee for the Uniswap V3 pool.
The contract includes the ERC20 functions transfer and transferFrom, which override the base ERC20 functions to include a fee-based buyback mechanism. When a transfer or transferFrom is made, a fee is calculated based on the amount transferred and the fee percentage. This fee is then transferred to the contract's address and used to buy back tokens from the Uniswap V3 pool.
The contract includes a function called Swap that is used to swap tokens through the Uniswap V3 swap router. The function increases the allowance for the swap router to transfer tokens from the contract's address, and then calls the exactInputSingle function on the swap router to make the swap.
The contract includes a function called checkAndBuyback that checks whether the current price of the token on the Uniswap V3 pool is below a floor price calculated based on the token's total supply and the contract's USDT balance. If the current price is below the floor price, the function calculates the amount of tokens needed to be bought back to reach the floor price and makes the swap using the swapToToken function.
The contract includes several internal functions used to calculate the current price of the token, the floor price, and the amount of tokens needed to be bought back to reach the floor price. These functions are used by the checkAndBuyback and swapToToken functions.
Overall, this contract is a complex implementation of a token with a fee-based buyback mechanism that uses the Uniswap V3 protocol to make trades. It is important to thoroughly test this contract and ensure that it functions correctly before deploying it to a live network.