Yes, it's possible. All Ethereum token standards (ERC-20, ERC-721, ...) only define an interface and few other points (such as when to emit events). So you're free to implement the methods however you need.
Let's say you have a very simple transfer()
implementation without the fees.
Note: This doesn't follow the ERC-* standards and is vulnerable to integer overflow in Solidity <= 0.7.6. I've simplified it to better show the calculations.
function transfer(address _to, uint256 _amount) external {
balances[msg.sender] -= _amount;
balances[_to] += _amount;
}
Adding the fees is just a matter of a small calculation:
address admin = address(0x123);
function transfer(address _to, uint256 _amount) external returns (bool) {
uint256 fee = (_amount / 100) * 3; // Calculate 3% fee
balances[msg.sender] -= _amount; // subtract the full amount
balances[admin] += fee; // add the fee to the admin balance
balances[_to] += (_amount - fee); // add the remainder to the recipient balance
}
Note: This is to demonstrate the basics, and doesn't take into account few cases such as _amount
having value not divisible by 100 (the fee is not going to be precisely 3% in that case).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…