Help with tax deduction function with transfer

I need help with a tax function I need to add. Basically, I need to deduct a % from each transfer and transfer it to a 2nd account. I’m having trouble figuring out how to connect _transfer and getTax. I had the 2nd address in the tax_acc but removed it for this post.

  function _transfer(address sender, address recipient, address tax_acc, uint256 amount, uint256 amountin, uint256 tax) internal {
require(sender != address(0), "BEP20: transfer from the zero address");
require(recipient != address(0), "BEP20: transfer to the zero address");
require(tax_acc == address(2nd address here));

_balances[sender] = _balances[sender].sub(amount, "Coin: transfer amount exceeds balance");
_balances[recipient] = _balances[recipient].add(amountin);
_balances[tax_acc] = _balances[tax_acc].add(tax);
emit Transfer(sender, recipient, amountin);
emit Transfer(sender, tax_acc, tax);
  }
  
  function get_tax(uint256 amountin, uint256 amount, uint256 tax) pure public returns (uint256, uint256) {
     tax = (amount / 100) * 3;
     amountin = amount - tax;
     //_tax = amount * 0.03;
        return (tax, amountin);

    }

What do you mean by connecting these two functions? You can call get_tax in the _transfer function. And the get_tax function does not need the amountin and tax parameters if you look closely. And the get_tax function can be rewritten as

function get_tax(uint256 amount) internal pure returns(uint256 tax, uint256 amountin) {
    tax = amount * 3 / 100;
    amountin = amount - tax;
}

Oh, so I don’t need to call it in the transfer since it already does the math with them already?

How would I do it in the transfer function only? I couldn’t get it to work that way before.

You can either call this new get_tax function in _transfer function or disassemble it and put arguments in _transfer.

  function _transfer(address sender, address recipient, address tax_acc, uint256 amount) internal {
require(sender != address(0), "BEP20: transfer from the zero address");
require(recipient != address(0), "BEP20: transfer to the zero address");
require(tax_acc == address(2nd address here));

(uint256 tax, uint256 amountin) = get_tax(amount);  // Calling get_tax function.
// uint256 tax = amount * 3 / 100;  // Embed arguments in this function.
// uint256 amountin = amount - tax;

_balances[sender] = _balances[sender].sub(amount, "Coin: transfer amount exceeds balance");
_balances[recipient] = _balances[recipient].add(amountin);
_balances[tax_acc] = _balances[tax_acc].add(tax);
emit Transfer(sender, recipient, amountin);
emit Transfer(sender, tax_acc, tax);
  }

Thanks. That helps a lot.

1 Like