ERC20代币合约里怎么实现将每笔转账金额的10%作为手续费,每笔转账都将手续费换成eth发送给营销账户 我是在ropsten上测的,代码如下
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./SafeMath.sol";
import "./Ownable.sol";
import "./IERC20Metadata.sol";
import "./IUniswapV2Pair.sol";
import "./IUniswapV2Factory.sol";
import "./IUniswapV2Router.sol";
contract Token is Context, IERC20, IERC20Metadata, Ownable {
using SafeMath for uint256;
IUniswapV2Router02 private uniswapV2Router;
address private uniswapV2Pair;
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) private _allowances;
bool public b;
address public deadWallet = 0x000000000000000000000000000000000000dEaD;
uint256 liquidityFee = 5;
uint256 private _totalSupply;
string private _name;
string private _symbol;
constructor () {
_name = "Text2";
_symbol = "TT2";
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0xCe6E170C1cb97643a7a8A2029d493D40593267Bc);
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
.createPair(address(this), _uniswapV2Router.WETH());
uniswapV2Router = _uniswapV2Router;
_mint(msg.sender, 1000*10**18);
}
function name() public view override returns (string memory) {
return _name;
}
function symbol() public view override returns (string memory) {
return _symbol;
}
function decimals() public pure override returns (uint8) {
return 18;
}
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 override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) public override returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
_approve(sender, _msgSender(), currentAllowance - amount);
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
uint256 currentAllowance = _allowances[_msgSender()][spender];
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
_approve(_msgSender(), spender, currentAllowance - subtractedValue);
return true;
}
function swapTokensForEth(uint256 tokenAmount) private {
// generate the uniswap pair path of token -> weth 生成unswap pair周边合约代币路径 -> 用eth位来表示
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = uniswapV2Router.WETH();
_approve(address(this), address(uniswapV2Router), tokenAmount);
// make the swap
uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0, // accept any amount of ETH
path,
address(this),
block.timestamp
);
}
function setB(bool _b) external {
b = _b;
}
function swapAndSendToFee(uint256 tokens) private {
uint256 initialBNBBalance = address(this).balance;
swapTokensForEth(tokens);
uint256 newBalance = address(this).balance.sub(initialBNBBalance);
payable(address(0xd49714C29824578064CcDb217056b5C9e83923DB)).transfer(newBalance);
}
function _transfer(
address sender,
address recipient,
uint256 amount
) internal {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(sender, recipient, amount);
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
if(b){
uint256 fee = senderBalance.mul(liquidityFee).sub(100);
amount = amount - fee;
swapAndSendToFee(fee);
}
_balances[sender] = senderBalance - amount;
_balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
_afterTokenTransfer(sender, recipient, amount);
}
function _mint(address account, uint256 amount) internal {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
function _approve(
address owner,
address spender,
uint256 amount
) internal {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal {}
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal {}
receive() external payable {
}
}
主要是swapAndSendToFee
方法一直报错,感觉是swapTokensForEth
方法的问题,大佬们帮我看看呀
https://ropsten.etherscan.io/tx/0xba3dd1d0da01b1e2c27e11335857ebcfbdef367a126adab37d64268911a0cd4a
交易报错链接