Chirag Gupta
Chirag Gupta

Reputation: 485

Reason provided by the contract: "ERC20: transfer amount exceeds allowance"

I'm new to solidity and I wanted to develop a subscription contract where a user can subscribe to a merchants plan and pay. But I'm unable to subscribe function and transfer the token to merchant.

I'm using open zeppelin ERC20 standard token to transfer.

    IERC20 token = IERC20(plans[planId].token);
    token.transferFrom(
        payable(msg.sender),
        payable(plan.merchant),
        plan.amount
    );

I don't know why but this keeps giving me allowance error even though I have increased the allowance of the user.

Upvotes: 0

Views: 483

Answers (1)

Petr Hejda
Petr Hejda

Reputation: 43481

Since you're invoking the token.transferFrom() from the subscription contract, the token owner needs to increaseAllowance() for the subscription contract to manipulate their tokens.

Example:

  • Your subscription contract is deployed on address 0x123.
  • Token address is 0x456, and it has 18 decimals.
  • The user needs to execute increaseAllowance(0x123, 500 * 1e18) on the token contract in order to increase the allowance by 500 tokens.
    • Mind the subscription contract address as the first param, and the decimals as the second param (so the actual value passed to the second param is 500000000000000000000 because it includes the decimals as units as well).

Upvotes: 1

Related Questions