I'm facing error while using approve() in my contract, It is working fine with the functions provided by the ERC20 contract. But when I try to write my own function to do so, it is not working.
Here is my code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Testx {
event Approval(address owner, address spender, uint256 value);
event Transfer(address from, address to, uint256 value);
uint256 amount = 100000;
address public token;
address public buyer = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
address public seller = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;
constructor (address _token) {
token = _token;
}
function approveAmount() public {
require(msg.sender == buyer, "only buyer can approve the transaction");
IERC20(token).approve(address(this), amount); // not working
emit Approval(buyer, address(this), amount);
}
function transferFund() public payable {
// condition: when the fund should be transfered from buyer wallet to seller wallet
IERC20(token).transferFrom(buyer, seller, amount); // working
emit Transfer(buyer, seller, amount);
}
}
can anyone know why IERC20(token).approve(address(this), amount); is not working as intended ?
Thanks in advance....
I've invested my lots of time debugging it, but not be able to solve it.