Hello guys,
I'm a junior developer going into web3 and i'm experiencing some test with the BSC and pancakeswap.
I've already created some tokens for test with the openzeppelin contract, and it works fine.
However, i'm trying now to add fees only on BUYS and SELLS, I did read some topic for a solution but it doesnt work for me.
The contract is well deployed, but when I try to add some liquidity on the BSC test net -> https://pancake.kiemtienonline360.com/ with this router adress -> 0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3
When I want to supply the token I got this error ->
"Fail with error 'TransferHelper::transferFrom: transferFrom failed'"
I have multiple questions, when I create this function, is it overriding the native _transfer function from OZ ?
The internal override parameters of the function are right ?
I struggle to fix the bug, if a kind soul is around, I would appreciate some help.
Thank you in advance.
Code to reproduce
pragma solidity 0.8.7;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
contract Test is ERC20 {
using SafeMath for uint256;
address private marketing_wallet = "the marketing wallet address;
address ROUTER = "the router adress";
mapping(address => uint256) private _balances;
constructor(uint256 initialSupply) ERC20 ("Test", "TST"){
_mint(msg.sender, initialSupply);
}
function _transfer(address from, address to, uint256 amount) internal override {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
}
if(msg.sender == ROUTER || to == ROUTER) {
uint256 fee = 2 * 10;
uint256 feeAmount = amount / 100 * fee;
amount -= feeAmount;
_balances[marketing_wallet] += feeAmount;
}
_balances[to] += amount;
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
}
Environment
Remix IDE online