Search code examples
blockchainethereumsolidity

Is it possible to reserve an address on Solidity smart contract creation for taxes collection?


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?


Solution

  • 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).