Hello,
I am encountering an issue when deploying my smart contract on the network. The contract deploys successfully without the inclusion of a function that handles a 2% liquidity tax, which is supposed to be added to the Uniswap liquidity pool. However, when I include this function, I face a problem with high gas requirements, and the contract cannot be deployed. The error message states: "Gas requirement of function is infinite: If the gas requirement of a function is higher than the block gas limit, it cannot be executed."
Here are the main points I would like to discuss:
- Function Optimization: What are the options for optimizing this function to reduce gas consumption? Is it possible that some operations within the function are too demanding in terms of gas?
- Issues with Loops and External Calls: Does this function contain any inefficient loops or external calls that could be causing high gas requirements?
- Alternative Approaches: Are there alternative approaches or practices that I should consider for processing the liquidity tax and adding it to Uniswap?
Any suggestions or recommendations would be very valuable. Thank you for your help.
here is the contract code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol";
contract CustomToken is ERC20, ERC20Burnable, ReentrancyGuard, Ownable {
address public marketingWallet;
address public uniswapV2RouterAddress;
IUniswapV2Router02 public uniswapV2Router;
address public uniswapV2Pair;
uint256 public constant TOTAL_SUPPLY = 450000000000000 * 10**18;
uint256 public minEthBeforeLiquidity = 0.05 ether;
uint256 private accumulatedTokenAmount = 0;
uint256 private accumulatedETHAmount = 0;
constructor(
address _marketingWallet,
address _uniswapV2RouterAddress,
address initialOwner
) ERC20("test", "GRC") Ownable(initialOwner) {
marketingWallet = _marketingWallet;
uniswapV2RouterAddress = _uniswapV2RouterAddress;
uniswapV2Router = IUniswapV2Router02(uniswapV2RouterAddress);
uniswapV2Pair = IUniswapV2Factory(uniswapV2Router.factory()).createPair(address(this), uniswapV2Router.WETH());
_mint(initialOwner, TOTAL_SUPPLY);
}
function _update(address from, address to, uint256 amount) internal virtual override {
uint256 marketingFee = (amount * 2) / 100;
uint256 liquidityFee = (amount * 2) / 100;
uint256 fees = marketingFee + liquidityFee;
uint256 sendAmount = amount - fees;
super._update(from, to, sendAmount);
if (marketingFee > 0) {
super._update(from, marketingWallet, marketingFee);
}
if (liquidityFee > 0 && from != uniswapV2Pair) {
accumulatedTokenAmount += liquidityFee;
uint256 initialBalance = address(this).balance;
swapTokensForEth(liquidityFee);
uint256 receivedEth = address(this).balance - initialBalance;
accumulatedETHAmount += receivedEth;
if (accumulatedETHAmount >= minEthBeforeLiquidity) {
addLiquidity(accumulatedTokenAmount, accumulatedETHAmount);
accumulatedTokenAmount = 0;
accumulatedETHAmount = 0;
}
}
}
function swapTokensForEth(uint256 tokenAmount) private {
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = uniswapV2Router.WETH();
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0,
path,
address(this),
block.timestamp
);
}
function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
_approve(address(this), address(uniswapV2Router), tokenAmount);
uniswapV2Router.addLiquidityETH{value: ethAmount}(
address(this),
tokenAmount,
0,
0,
owner(),
block.timestamp
);
}
}