Sorry if my question sounds a bit vague, but I'm looking at writing an NFT staking contract that will allow holders of the ERC721 token to transfer those tokens in return for our ERC20 token. There are 3 contracts in this scenario: The ERC721 NFT contract, the staking contract, and the ERC20 contract. I understand the logic behind how staking an NFT works but what is confusing me is how the staking contract is allowed to transfer ERC20 tokens to the NFT holder, on behalf of the ERC20 contract.
One example that I had found online looked something like this:
IERC20(tokenAddress).transfer(holderAccount,tokenAmount)
I did notice that tokenAddress
is being set in the constructor of the staking contract like so:
address tokenAddress;
constructor(address _tokenAddress,...){
tokenAddress = _tokenAddress
.
.
.
}
I just don't understand how the staking contract is allowed to transfer tokens on the ERC20 contracts behalf, otherwise anyone would be able to do this to any contract. Is there some sort of approval set in the ERC20 contract that says: "allow this contract address to transfer tokens on my behalf"?
I tried looking online for this but I'm not finding any answers regarding this question. If anyone has any ideas, or can explain to me how this works a bit better, that would be great. TIA!