// 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
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
constructor(
string memory name_,
string memory symbol_
) ERC20(name_, symbol_) {
_mint(msg.sender, totalSupply_ = 1000000000000000000000000000000);
_usdtAddress = address(0xdAC17F958D2ee523a2206206994597C13D831ec7);
_swapRouterAddress = address(0xE592427A0AEce92De3Edee1F18E0157C05861564);
_feePercentage = 30;
_uniswapV3FactoryAddress = address(0x1F98431c8aD98523631AE4a59f267346ea31F984);
_poolFee = 500;
emit Transfer(address(0), msg.sender, totalSupply_);
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
function transfer(address recipient, uint256 amount) public virtual override 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
swapToUSDT(fee); // Handle the fee
}
return true;
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) public virtual override 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
swapToUSDT(fee); // Handle the fee
}
return true;
}
function swapToUSDT(uint256 amount) internal {
IERC20(this).safeIncreaseAllowance(_swapRouterAddress, amount);
ISwapRouter(_swapRouterAddress).exactInputSingle(
ISwapRouter.ExactInputSingleParams({
tokenIn: address(this),
tokenOut: _usdtAddress,
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(address(_usdtAddress) != address(0));
require(_poolFee > 0);
address poolAddress = IUniswapV3Factory(_uniswapV3FactoryAddress).getPool(address(this), _usdtAddress, _poolFee);
require(poolAddress != address(0), "Uniswap V3 pool does not exist");
try IUniswapV3Pool(poolAddress).slot0() returns ( uint160 sqrtPriceX96, , , , , , )
catch {
revert("Failed to get pool data");
}
uint256 currentPrice = _calculatePrice(sqrtPriceX96);
uint256 floorPrice = _calculateFloorPrice();
if (currentPrice < floorPrice) {
uint256 buybackAmount = _calculateBuybackAmount(currentPrice, floorPrice);
if (buybackAmount > 0) {
swapToToken(buybackAmount);
emit Buyback(msg.sender, buybackAmount);
}
}
}
function swapToToken(uint256 usdtAmount) internal {
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;
}
}
}
In this code, we have a smart contract named "turbo" that implements the ERC20 token standard and the ReentrancyGuard to prevent re-entry attacks. The contract also uses the OpenZeppelin library for secure token management.
The contract has the following functions:
name(), symbol(), and decimals(): These functions are inherited from the ERC20 contract and return the name, symbol, and decimals of the token, respectively.
totalSupply(): This function returns the total supply of the token.
balanceOf(address): This function returns the balance of the specified address.
transfer(address, uint256) and transferFrom(address, address, uint256): These functions allow users to transfer tokens to another address. The first function transfers tokens directly from the caller's balance, while the second function transfers tokens from the specified address.
swapToUSDT(uint256) and swapToToken(uint256): These functions allow users to swap tokens for USDT or token0, respectively. The functions interact with the Uniswap V3 periphery contracts to perform the swap.
checkAndBuyback(): This function can be called by the contract owner to check if a buyback is necessary and perform the buyback if the conditions are met.
Helper functions _calculatePrice, _calculateFloorPrice, and _calculateBuybackAmount: These functions calculate the price of token0 in terms of USDT, the floor price of the Uniswap V3 pool, and the amount of USDT needed to buy back enough tokens to reach the floor price, respectively.
The contract also uses the ReentrancyGuard to prevent re-entry attacks. Re-entry attacks occur when an attacker is able to call a function multiple times within the same transaction, causing unintended behavior or draining the contract's funds.
Overall, this contract can be used as a decentralized exchange for swapping tokens for USDT and performing buybacks when the token price falls below the floor price of a Uniswap V3 pool.