1

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!

1 Answer 1

1

Here is the general model.

Note that it is possible for some of the entities to be combined.

  • The staking sponsor might be built into the ERC-20 token contract
  • The staking sponsor might be built into the ERC-721 token contract
  • The staking contract might be have its own tokens (no need for sponsor)
  • The ERC-721 might contain the staking contract and also be the sponsor

The only thing you can't combine is the ERC-721 NFT contract and the ERC-20 contract.

It's also possible to create a generic contract to stake any kind of NFT and receive tokens specific to that NFT. This might become the vampire squid that takes custody of all the NFTs.

P.S. If you want to talk through any of these, I host a public Community Service Hour to answer questions live. This is at https://phor.net/#speaking

enter image description here

1
  • This is a good diagram, thank you. Could you explain what you mean by the "Sponsor" role? My understanding is that when the ERC20 contract is created, a specific amount of that token is minted in the constructor during deployment by the contract owner (me). This would be the address that the staking contract would be transferring tokens out of. So is this middle man "sponsor" necessary in this example and why?
    – Michael
    Jan 7 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.