# Cannot use transferFrom, even though I have set allowance

**URL:** <https://forum.openzeppelin.com/t/cannot-use-transferfrom-even-though-i-have-set-allowance/10348>\
**Category:** Contracts\
**Tags:** erc20\
**Created:** [June 11, 2021, 11:07pm UTC](https://forum.openzeppelin.com/t/cannot-use-transferfrom-even-though-i-have-set-allowance/10348 "2021-06-11T23:07:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Numis\_Matic](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/numis_matic/32/6524_2.png) [@Numis\_Matic](https://forum.openzeppelin.com/u/Numis_Matic)\
**Post date:** [June 11, 2021, 11:07pm UTC](https://forum.openzeppelin.com/t/cannot-use-transferfrom-even-though-i-have-set-allowance/10348/1 "2021-06-11T23:07:56Z")

</div>

Hi, no matter what I try, I am unable to call **token.transferFrom** without getting **ERC20: transfer amount exceeds allowance** - even though I have approved the allowance.

Code speaks better than words:

**Strategy**

```auto
contract Strategy is ReentrancyGuard {
  IERC20 private paymentToken;
  Token private strategyToken;

  constructor(IERC20 _paymentToken, Token _strategyToken) {
    paymentToken = _paymentToken;
    strategyToken = _strategyToken;
  }

  function deposit(uint256 amount) external payable nonReentrant {
    // Transfer payment token from msg.sender to contract
    paymentToken.transferFrom(msg.sender, address(this), amount);
  }
}

```

In my test (this is where it fails):

```auto
import { expect} from "chai";
import { ethers } from "hardhat";
import { Strategy } from "../typechain/Strategy";
import { Token } from "../typechain/Token";

describe("Strategy", () => {
  let strategy: Strategy;
  let paymentToken: Token;
  let receivedStrategyToken: Token;
  before(async () => {
    const Strategy = await ethers.getContractFactory("Strategy");
    const TokenMock = await ethers.getContractFactory("Token");

    paymentToken = await TokenMock.deploy("Payment token mock", "PAYMOCK") as Token;
    receivedStrategyToken = await TokenMock.deploy("Received token mock", "RECEIVEMOCK") as Token;

    strategy = await Strategy.deploy(paymentToken.address, receivedStrategyToken.address) as Strategy;
  });

  describe("Strategy contract", () => {
    it("It will return a strategy token when depositing", async () => {
      const [addr] = await ethers.getSigners();
      await paymentToken.mint(addr.address, "1000");
      await paymentToken.increaseAllowance(addr.address, "1000");
      expect(await strategy.deposit(1)).to.be.ok;
    });
  });
});

```

As stated above, it complains with **ERC20: transfer amount exceeds allowance** - but I don’t know why, because I have increased the allowance?

---

<div class="post-metadata">

**Author:** ![0f0crypto](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/0f0crypto/32/6056_2.png) [@0f0crypto](https://forum.openzeppelin.com/u/0f0crypto)\
**Post date:** [June 12, 2021, 12:08am UTC](https://forum.openzeppelin.com/t/cannot-use-transferfrom-even-though-i-have-set-allowance/10348/2 "2021-06-12T00:08:32Z")

</div>

This line `paymentToken.transferFrom(msg.sender, msg.sender, amount);` doesn’t make sense - you’re setting the `msg.sender` both as the sender and the recipient… Also, when you call `deposit` it is the `Strategy` contract which is doing the transfer … so the sender must approve the `Strategy` contract to transfer the amount

---

<div class="post-metadata">

**Author:** ![Numis\_Matic](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/numis_matic/32/6524_2.png) [@Numis\_Matic](https://forum.openzeppelin.com/u/Numis_Matic)\
**Post date:** [June 12, 2021, 12:52am UTC](https://forum.openzeppelin.com/t/cannot-use-transferfrom-even-though-i-have-set-allowance/10348/3 "2021-06-12T00:52:11Z")

</div>

Oh darn. I was just playing around. It should be `address(this)`. I will change the quesiton.

---

<div class="post-metadata">

**Author:** ![Numis\_Matic](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/numis_matic/32/6524_2.png) [@Numis\_Matic](https://forum.openzeppelin.com/u/Numis_Matic)\
**Post date:** [June 12, 2021, 1:30am UTC](https://forum.openzeppelin.com/t/cannot-use-transferfrom-even-though-i-have-set-allowance/10348/4 "2021-06-12T01:30:13Z")

</div>

It seems the problem is because I need to call **increaseAllowance** for the contract address. Can someone explain why? You see, the reason I am getting confused is when I look at the source of **transferFrom**

```auto
    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) public virtual override returns (bool) {
        _transfer(sender, recipient, amount);

        uint256 currentAllowance = _allowances[sender][_msgSender()];
        require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
        unchecked {
            _approve(sender, _msgSender(), currentAllowance - amount);
        }

        return true;
    }

```

Specifically, the error that is causing things to fail are these lines:

```auto
uint256 currentAllowance = _allowances[sender][_msgSender()];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");

```

If I am not mistaken, the _recipient_ is the contract, however nothing is being checked for the recipient. Perhaps _\_msgSender()_ is the contract address in this capacity, but why (since it returns _msg.sender_ as well)?
