admin管理员组文章数量:1355581
The _transfer() function only handles ERC20 token transfers, so I can't implement any logic for ETH or BNB within it.
I integrated some functions, but they were not callable on PancakeSwap. When I added liquidity for the corresponding ERC20 token and swapped it with tBNB, the tax was not deducted in tBNB as expected.
- Here is the full source code of my smart contract
contract text3 is ERC20, Ownable {
uint256 public maxAmount = 20000000 * 10 ** uint256(decimals());
uint256 public constant Tax = 400; //4%
uint256 public taxThreshold = 100 * 10**uint256(decimals());
address public ownerWallet;
IUniswapV2Router02 public immutable uniswapV2Router;
address public immutable uniswapPair;
bool private swapping;
event UpdateThreshold(uint256 newThreshold);
constructor()ERC20("test3", "tst3") Ownable(msg.sender){
uint256 totalSupply = 1000000;
_mint(msg.sender, totalSupply * (10**decimals()));
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(
0xD99D1c33F9fC3444f8101754aBC46c52416550D1 //binance testnet
);
uniswapV2Router = _uniswapV2Router;
uniswapPair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(
address(this),
_uniswapV2Router.WETH()
);
_approve(msg.sender, address(uniswapV2Router), type(uint256).max);
_approve(address(this), address(uniswapV2Router), type(uint256).max);
}
function setTaxThreshold(uint256 threshold) external onlyOwner {
require(threshold > 0 && threshold <= 5 * 10**5 * 10**18, "Amount should be more than zero and less than 500k tokens");
taxThreshold = threshold;
emit UpdateThreshold(taxThreshold);
}
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
);
uint256 ethReceived = address(this).balance;
uint256 taxAmount = (ethReceived * Tax) / 10000; // 4% Tax
uint256 amountAfterTax = ethReceived - taxAmount;
// Transfer ETH tax to owner
payable(owner()).transfer(taxAmount);
// Transfer remaining ETH to seller
payable(msg.sender).transfer(amountAfterTax);
}
function swapETHForTokens(uint256 ethAmount) external payable {
require(msg.value >= ethAmount, "Insufficient ETH sent");
uint256 taxAmount = (msg.value * Tax) / 10000; // 4% Tax
uint256 amountAfterTax = msg.value - taxAmount; // Deduct tax
// Transfer tax to owner
payable(owner()).transfer(taxAmount);
address[] memory path = new address[](2);
path[0] = uniswapV2Router.WETH();
path[1] = address(this);
uniswapV2Router.swapExactETHForTokensSupportingFeeOnTransferTokens{
value: amountAfterTax
}(
0,
path,
msg.sender,
block.timestamp
);
}
function swapAndLiquify() internal {
uint256 contractTokenBalance = balanceOf(address(this));
uint256 liqHalf = contractTokenBalance / 2;
uint256 tokensToSwap = contractTokenBalance - liqHalf;
swapTokensForEth(tokensToSwap);
uint256 newBalance = address(this).balance;
addLiquidity(liqHalf, newBalance);
}
function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
_approve(address(this), address(uniswapV2Router), tokenAmount);
// add the liquidity
uniswapV2Router.addLiquidityETH{value: ethAmount}(
address(this),
tokenAmount,
0,
0,
address(this),
block.timestamp
);
}
function _calculateTax(uint256 amount, uint256 taxPercentage) private pure returns (uint256) {
return amount * (taxPercentage) / (10000);
}
receive() external payable {}
}
I tried implementing the tax deduction functionality in the ERC20 contract. I was able to easily deduct the tax in the form of tokens, but I want to deduct the tax in BNB or ETH instead. Specifically, when a user buys or sells tokens, I want to deduct a 4% tax in BNB or ETH and send it to the owner’s address.
When I called this function, it successfully deducted the tax in tBNB and sent it to the owner address. How can I integrate this function into my smart contract?
function swapETHForTokens(uint256 ethAmount) external payable {
require(msg.value >= ethAmount, "Insufficient ETH sent");
uint256 taxAmount = (msg.value * Tax) / 10000; // 4% Tax
uint256 amountAfterTax = msg.value - taxAmount; // Deduct tax
// Transfer tax to owner
payable(owner()).transfer(taxAmount);
address[] memory path = new address[](2);
path[0] = uniswapV2Router.WETH();
path[1] = address(this);
uniswapV2Router.swapExactETHForTokensSupportingFeeOnTransferTokens{
value: amountAfterTax
}(
0,
path,
msg.sender,
block.timestamp
);
}
本文标签:
版权声明:本文标题:blockchain - Can I implement a tax deduction feature in an ERC20 contract where the tax is deducted in BNB or ETH when a user sw 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744050918a2582406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论