I am playing with solidity programming language and trying to implement a contract that every made transaction is deducted with tax fee and this tax fee should be transferred to some specific address of a contract which is being created. Is that possible?
Asked
Active
Viewed 2,761 times
1 Answers
4
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).

Petr Hejda
- 40,554
- 8
- 72
- 100
-
3The ERC-20 standard transfer assumes that the send amount is sent fully and the full amount is available to sent. It would break on all decentralised and centralised exchange. Unless you plan to use to the token just for yourself, your modified `transfer()` token could not connect to larger DeFi ecosystem any way. – Mikko Ohtamaa Mar 21 '21 at 15:43
-
2@MikkoOhtamaa That's correct. It's also one of the reasons why I've written the note "is vulnerable to integer overflow". Since Solidity 0.8, the validation whether the amount is available, is done automatically through the underflow/overflow check. But in earlier versions, one should implement a custom check through `require` or use a library such as `SafeMath`. – Petr Hejda Mar 21 '21 at 15:48
-
1Thanks you both. Would it work for exchanges if I send this fee to separate contract instead of current contract admin? Deducted amount goes to recipient and fee amount to other contract which collects taxes. Would it work? – Saulius Mar 21 '21 at 16:45
-
1@PetrHejda , let say I will use SafeMath for calculation to ensure that all sent amount is delivered to recipients. But I still have one question. How to I manipulate my admin address defined? Let say my contract created, 1000 tx executed, now I want to take a tokens from address(0x123), how do I do it? – Saulius Mar 21 '21 at 19:15
-
2@Saulius You should define the `admin` address value to your address that you hold the private key to. Then you can transfer the tokens (from the admin address) the same way as you would transfer any other token - by calling the `transfer()` function. – Petr Hejda Mar 21 '21 at 19:18
-
1@PetrHejda and last question :D, how do I convert wallet hash into address id? Let say I have ddf46grhgfhtr34554hbgh wallet hash. This is not accepted by Solidity, how to convert it to address so Solidity understands it? – Saulius Mar 21 '21 at 21:26
-
2@Saulius I'm not sure what you mean by the "wallet hash". If you mean the private key, you can use for example the web3.js [privateKeyToAccount()](https://web3js.readthedocs.io/en/v1.2.11/web3-eth-accounts.html#privatekeytoaccount) function or the Metamask Import feature. If you mean the keccak256 hash of the address, then it's impossible to convert it back to an address... Also keep in mind that the address are [checkum](https://coincodex.com/article/2078/ethereum-address-checksum-explained/) and later versions of Solidity do not accept addresses with incorrect checkum. – Petr Hejda Mar 21 '21 at 21:42
-
2@Saulius So I'd recommend to better describe the issue in a separate question. – Petr Hejda Mar 21 '21 at 21:42
-
how to define buy/sell taxes differently? – Wang90925 Dec 22 '21 at 19:58