EIP-721 tokens can be deposited or staked in NFTs for a variety of reasons including escrow, rewards, benefits, and others. There is currently no means of retrieving the number of tokens staked and/or bound to an NFT. This proposal outlines a standard that may be implemented by all wallets and marketplaces easily to correctly retrieve the staked token amount of an NFT.
Motivation
Without staked token data, the actual amount of staked tokens cannot be conveyed from token owners to other users, and cannot be displayed in wallets, marketplaces, or block explorers. The ability to identify and verify an exogenous value derived from the staking process may be critical to the aims of an NFT holder.
Specification
// SPDX-License-Identifier: CC0-1.0
pragmasolidity^0.8.0;/**
* @dev Interface of the ERC4353 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-4353.
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others.
*
* Note: The ERC-165 identifier for this interface is 0x3a3d855f.
*
*/interfaceIERC721Staked{/**
* @dev Returns uint256 amount of on-chain tokens staked to the NFT.
*
* @dev Wallets and marketplaces would need to call this for displaying
* the amount of tokens staked and/or bound to the NFT.
*/functionstakedAmount(uint256tokenId)externalviewreturns(uint256);}
Suggested flow:
Constructor/deployment
Creator - the owner of an NFT with its own rules for depositing tokens at and/or after the minting of a token.
Token Amount - the current amount of on-chain EIP-20 or derived tokens bound to an NFT from one or more deposits.
Withdraw Mechanism - rules based approach for withdrawing staked tokens and making sure to update the balance of the staked tokens.
Staking at mint and locking tokens in NFT
The suggested and intended implementation of this standard is to stake tokens at the time of minting an NFT, and not implementing any outbound transfer of tokens outside of burn. Therefore, only to stake at minting and withdraw only at burning.
NFT displayed in wallet or marketplace
A wallet or marketplace checks if an NFT has publicly staked tokens available for display - if so, call stakedAmount(tokenId) to get the current amount of tokens staked and/or bound to the NFT.
The logical code looks something like this and inspired by William Entriken:
// contracts/Token.sol
// SPDX-License-Identifier: MIT
pragmasolidity^0.8.0;import"@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";import"@openzeppelin/contracts/access/Ownable.sol";/**
* @title Token
* @dev Very simple ERC721 example with stake interface example.
* Note this implementation enforces recommended procedure:
* 1) stake at mint
* 2) withdraw at burn
*/contractERC721StakedisERC721URIStorage,Ownable{/// @dev track original minter of tokenId
mapping(uint256=>addresspayable)privatepayees;/// @dev map tokens to stored staked token value
mapping(uint256=>uint256)privatetokenValue;/// @dev metadata
constructor()ERC721("Staked NFT","SNFT"){}/// @dev mints a new NFT
/// @param _to address that will own the minted NFT
/// @param _tokenId id the NFT
/// @param _uri metadata
functionmint(addresspayable_to,uint256_tokenId,stringcalldata_uri)externalpayableonlyOwner{_mint(_to,_tokenId);_setTokenURI(_tokenId,_uri);payees[_tokenId]=_to;tokenValue[_tokenId]=msg.value;}/// @dev staked interface
/// @param _tokenId id of the NFT
/// @return _value staked value
functionstakedAmount(uint256_tokenId)externalviewreturns(uint256_value){_value=tokenValue[_tokenId];return_value;}/// @dev removes NFT & transfers crypto to minter
/// @param _tokenId the NFT we want to remove
functionburn(uint256_tokenId)externalonlyOwner{super._burn(_tokenId);payees[_tokenId].transfer(tokenValue[_tokenId]);tokenValue[_tokenId]=0;}}
Rationale
This standard is completely agnostic to how tokens are deposited or handled by the NFT. It is, therefore, the choice and responsibility of the author to encode and communicate the encoding of their tokenomics to purchasees of their token and/or to make their contracts viewable by purchasees.
Although the intention of this standard is for tokens staked at mint and withdrawable only upon burn, the interface may be modified for dynamic withdrawing and depositing of tokens especially under DeFi application settings. In its current form, the contract logic may be the determining factor whether a deviation from the standard exists.
Backward Compatibility
TBD
Test Cases
const{expect}=require("chai");const{ethers,waffle}=require("hardhat");constprovider=waffle.provider;describe("StakedNFT",function(){let_id=1234567890;letvalue='1.5';letToken;letInterface;letowner;letaddr1;letaddr2;beforeEach(asyncfunction(){Token=awaitethers.getContractFactory("ERC721Staked");[owner,addr1,...addr2]=awaitethers.getSigners();Interface=awaitToken.deploy();});describe("Staked NFT",function(){it("Should set the right owner",asyncfunction(){letmint=awaitInterface.mint(addr1.address,_id,'http://foobar')expect(awaitInterface.ownerOf(_id)).to.equal(addr1.address);});it("Should not have staked balance without value",asyncfunction(){letmint=awaitInterface.mint(addr1.address,_id,'http://foobar')expect(awaitInterface.stakedAmount(_id)).to.equal(ethers.utils.parseEther('0'));});it("Should set and return the staked amount",asyncfunction(){letmint=awaitInterface.mint(addr1.address,_id,'http://foobar',{value:ethers.utils.parseEther(value)})expect(awaitInterface.stakedAmount(_id)).to.equal(ethers.utils.parseEther(value));});it("Should decrease owner eth balance on mint (deposit)",asyncfunction(){letbalance1=awaitprovider.getBalance(owner.address);letmint=awaitInterface.mint(addr1.address,_id,'http://foobar',{value:ethers.utils.parseEther(value)})letbalance2=awaitprovider.getBalance(owner.address);letdiff=parseFloat(ethers.utils.formatEther(balance1.sub(balance2))).toFixed(1);expect(diff===value);});it("Should add to payee's eth balance on burn (withdraw)",asyncfunction(){letbalance1=awaitprovider.getBalance(addr1.address);letmint=awaitInterface.mint(addr1.address,_id,'http://foobar',{value:ethers.utils.parseEther(value)})awaitInterface.burn(_id);letbalance2=awaitprovider.getBalance(addr1.address);letdiff=parseFloat(ethers.utils.formatEther(balance2.sub(balance1))).toFixed(1);expect(diff===value);});it("Should update balance after transfer",asyncfunction(){letmint=awaitInterface.mint(addr1.address,_id,'http://foobar',{value:ethers.utils.parseEther(value)})awaitInterface.burn(_id);expect(awaitInterface.stakedAmount(_id)).to.equal(ethers.utils.parseEther('0'));});});});
Security Considerations
The purpose of this standard is to simply and publicly identify whether an NFT claims to have staked tokens.
Staked claims will be unreliable without a locking mechanism enforced, for example, if staked tokens can only be transferred at burn. Otherwise, tokens may be deposited and/or withdrawn at any time via arbitrary methods. Also, contracts that may allow arbitrary transfers without updating the correct balance will result in potential issues. A strict rules-based approach should be taken with these edge cases in mind.
A dedicated service may exist to verify the claims of a token by analyzing transactions on the explorer. In this manner, verification may be automated to ensure a token’s claims are valid. The logical extension of this method may be to extend the interface and support flagging erroneous claims, all the while maintaining a simple goal of validating and verifying a staked amount exists to benefit the operator experience.
Rex Creed (@aug2uag), Dane Scarborough <dane@nftapps.us>, "ERC-4353: Interface for Staked Tokens in NFTs [DRAFT]," Ethereum Improvement Proposals, no. 4353, October 2021. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-4353.