Getting ERC20: insufficient allowance even I'm calling approve function before calling transferFrom function in getRewards function, don't know what I'm missing in it.
Cafe Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
// Uncomment this line to use console.log
// import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract Cafe is Ownable {
address public cafeToken;
function setCafeToken(address _cafeToken) public onlyOwner {
cafeToken = _cafeToken;
}
//send token to other receipent
function sendToken(
address _receiverAddress,
uint _tokenAmount
) public onlyOwner {
require(_tokenAmount > 0, "token should be more than 0");
IERC20(cafeToken).transfer(_receiverAddress, _tokenAmount);
}
function getRewards() public {
require(
IERC20(cafeToken).balanceOf(msg.sender) >= 10000000000000000000,
"you should have more then 10 tokens..."
);
IERC20(cafeToken).approve(msg.sender, 10000000000000000000);
IERC20(cafeToken).transferFrom(
msg.sender,
address(this),
10000000000000000000
);
}
}
Cafe Token Contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract CAFToken is ERC20 {
constructor(
uint256 initialSupply,
address cafeAddress
) ERC20("Cafe", "CAF") {
_mint(cafeAddress, initialSupply); // mint intial supply to cafe contract
}
}
first I'm deploying Cafe Contract and then cafeToken contract with deployed address of cafe contract.