0

I'm really stumped on this one, and hoping someone might be able to point out my mistakes. I'm far from experienced with Solidity, but was unable to find any kind of solution online for this specific problem I'm having.

I'm designing my token so that every transfer has a transaction fee, which is automatically converted to ETH and sent to a charity wallet. My code for the transfer function and conversion function are here:

function _transferStandard(address sender, address recipient, uint256 amount)internal virtual {

        uint256 fee = (amount/100) * TX_FEE;
        

        uint256 senderBalance = _balances[sender];
        require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");

        unchecked {
            _balances[sender] = senderBalance - amount;
        }

        _balances[recipient] += (amount - fee);
        _balances[address(this)] += fee;
        
        emit Transfer(sender, recipient, (amount - fee));
        
        convertFeeToEth(fee);

    }

function convertFeeToEth(uint256 amount) internal virtual{
    
    address[] memory path = new address[](2);
    path[0] = address(this);
    path[1] = uniswapV2Router.WETH();

    _approve(address(this), address(uniswapV2Router), amount);

    uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
        amount,
        0, // accept any amount of ETH
        path,
        CharityWallet,
        block.timestamp
    );
    
}

My contract works perfectly when I send transfers between accounts. It subtracts the correct amount for the fee, converts it to ETH, and sends it to the charity wallet.

The part that isn't working is when I try to use Uniswap/PCS.

If I try to swap tokens for ETH, I get INSUFFICIENT_INPUT_AMOUNT.

If I try to swap ETH for the tokens, I get Transferhelper: TransferFrom Failed

I've narrowed the issue down to something within my convertFeeToEth function, since Uniswap works normally if I comment it out.

Thanks in advance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.