1

here's my method to allow the user to sell "FRKC" ERC20 tokens to get ETH in return from my smart contract

When I log the allowance variable it stays at 0

On Remix I get the error: "Check the token allowance."

I made sure my user had enough tokens to sell and my contract has enough ETH to send Am I supposed to call the approve function somewhere? Couldn't find a good example

function sell(uint256 amount) public {
        // Allowance
        uint256 allowance = FrkcReserve.allowance(msg.sender, address(this));
        require(allowance >= amount, "Check the token allowance.");

        // Get tokens from the user
        bool sent = FrkcReserve.transferFrom(msg.sender, address(this), amount);
        require(sent, "Failed to transfer tokens from user to vendor");

        // Sent ETH to the user
        (sent, ) = msg.sender.call{value: amountOfETHToTransfer}("");
        require(sent, "Failed to send ETH to the user");

        emit Sold(amount);
    }
fbenfraj
  • 11
  • 2

1 Answers1

4

Am I supposed to call the approve function somewhere?

Yes. The user needs to execute the approve() function directly from their address (not through your contract). Specifically, they need to pass your contract address as the first param, and the amount as the second param.

The approved amount is then reflected in the value returned from allowance().

This is a common practice used by majority of DEXes. For example when you're selling an XYZ token on Uniswap, you need to approve() them first to pull the token from your wallet.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100