Specialized tax token

I’ll try to keep this short. I’ve been working on a project for a couple months and am creating the final tax feature it needs. I’ve got something that will take the project token in as tax although I’d like to take in ETH instead, this way the project token price isn’t hurt when we sell it for operating expenses. I’ll leave the relevant lines of code and hopefully someone smarter than me can point me in the right direction as nothing I’ve tried has worked:

pragma solidity ^0.8.20;

import “@openzeppelin/contracts/token/ERC20/ERC20.sol”;

contract TaxFeature is ERC20 {

address public immutable fund;

constructor(address fund_) ERC20("TaxFeature", "Tax") {
    fund = fund_;

function _transfer(
    address sender,
    address recipient,
    uint256 amount
) internal virtual override {
    uint tax = (amount / 100) * 2; // 2% tax

    super._transfer(sender, recipient, amount - tax);
    super._transfer(sender, fund, tax);


1 Like

You are asking for a technical solution, but the fact of the matter is that the only way for your idea to make financial sense, is if the contract knows the market price of your token in ETH units at any given moment.

This is because the user’s input amount is given in units of your token, not in units of ETH.

For example, suppose that the price of your token in units of ETH is currently 3/8.

In order to apply a 2% tax, you’d need to transfer tax * 3 / 8 ETH from the sender to the fund wallet.

Now, in order for the contract to know the market price of your token in ETH units at any given moment, two properties must be ensured:

  1. There exists a reliable market price of your token in ETH units, which would typically be the case only when your token is widely recognized
  2. You are able to inject that price into your contract, frequently enough to keep it reflecting the market in a reliable manner at any given moment

So in short, you have a more fundamental issue to deal with, prior to the technical problem of deducting the fee in ETH.


You’d have to measure the market is I think what is said here. This is not an easy mechanism to put in place as more than one formula can be found for monitoring the market. You also need to think about gas usage. This can be done, but it will take a great deal of engineering.