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
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):
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?
uint256 currentAllowance = _allowances[sender][_msgSender()]; require(currentAllowance >= amount, "ERC20: transfer amount exceeds