i intend to use my erc20 token for doing some payment for minting nft. but when i call testPay function always got ERC20: insufficient allowance error.
already looking for answer but can't find one.
can someone tellme what i do wrong here?
msg.sender already have some erc20 token. address(this) don't have token. this contract is not my real nft minting contract, i just want to test the payment function.
contract GameItem is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
ERC20 public GXGToken;
constructor(address mytoken) ERC721("GameItem", "ITM") {
GXGToken = ERC20(mytoken);
}
function awardItem(address player, string memory tokenURI)
public
returns (uint256)
{
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_mint(player, newItemId);
_setTokenURI(newItemId, tokenURI);
return newItemId;
}
function approval(uint amount) public{
GXGToken.approve(msg.sender,amount);
}
function myallowance() view public returns (uint){
return GXGToken.allowance(msg.sender,address(this));
}
function testpay(uint _amount) public{
GXGToken.approve(msg.sender,_amount);
pay(_amount);
}
function pay(uint _amount) internal{
GXGToken.transferFrom(msg.sender,address(this),_amount);
}
}
Edited: ok got it, thanks for all for the replies.
this is what i'm doing.
so i call approve function on erc20 first, approving erc721 address, and amount, then call the transferFrom on erc721 contract, param1 is sender and param2 is erc721 address.
and it's working.