Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines ERC-4353, which proposes a standardized way to retrieve the amount of tokens staked in an ERC-721 NFT.

This article translates, summarizes, and supplements the material linked below.

I have also covered other EIPs.

Overview

ERC-4353 proposes an interface for querying the quantity of tokens staked in an ERC-721 NFT.

Many use cases, including escrow, rewards, and benefits, involve depositing or associating other tokens with NFTs. However, there is no common interface for retrieving the amount of staked tokens.

ERC-4353 allows wallets and marketplaces to retrieve and display information about the tokens staked in an NFT.

Motivation

Even when NFT staking information exists on-chain, the lack of a standard retrieval mechanism makes it difficult to communicate that information to other users.

Wallets, marketplaces, and block explorers may be unable to display the staking state, potentially preventing the associated assets from being properly considered when evaluating an NFT.

Value associated with staked tokens can be an important consideration for NFT holders. Making that information visible and verifiable can improve transparency and help users assess an NFT's value.

Specification

IERC721Staked

IERC721Staked.sol
// SPDX-License-Identifier: CC0-1.0

pragma solidity ^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.
 *
 */
interface IERC721Staked {
    
     /**
     * @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.
     */
    function stakedAmount(uint256 tokenId) external view returns (uint256);
    
}

stakedAmount

Returns the amount of staked tokens associated with a particular tokenId, represented as a uint256.

Wallets and marketplaces can call the function to display how many tokens are currently staked in an NFT.

ERC-165 can be used to detect support for this interface. The interface ID is 0x3a3d855f.

See the following article for ERC-165.

[ERC-165] Checking Whether a Contract Supports a Specific Interface

Implementation Flow

The approach recommended by ERC-4353 is to stake tokens when the NFT is minted and allow withdrawal of the staked tokens only when the NFT is burned.

Staking Flow

The NFT creator deposits ERC-20 tokens into the NFT-associated staking mechanism, either during minting or at another permitted time.

Once staked, the tokens are intended to remain locked until the NFT is burned, preventing independent transfer or withdrawal from the staking mechanism.

This strengthens the association between the staked tokens and the NFT and supports greater confidence in the claimed backing.

Withdrawal Mechanism

Withdrawal is intended to accompany the NFT burn operation. Balance information must be updated correctly when tokens are staked or the NFT is burned.

Displaying Information in Wallets and Marketplaces

Wallets and marketplaces call stakedAmount(tokenId) to determine whether an NFT holds staked tokens and display the associated amount.

Reference Implementation

ERC721Staked.sol
// contracts/Token.sol
// SPDX-License-Identifier: MIT
pragma solidity ^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
 */
contract ERC721Staked is ERC721URIStorage, Ownable {
    /// @dev track original minter of tokenId
    mapping (uint256 => address payable) private payees;
    /// @dev map tokens to stored staked token value
    mapping (uint256 => uint256) private tokenValue;

    /// @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
    function mint(
        address payable _to,
        uint256 _tokenId,
        string calldata _uri
    )
        external 
        payable
        onlyOwner
    {
        _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
    function stakedAmount(
        uint256 _tokenId
    ) external view returns (uint256 _value) {
        _value = tokenValue[_tokenId];
        return _value;
    }

    /// @dev removes NFT & transfers crypto to minter
    /// @param _tokenId the NFT we want to remove
    function burn(
        uint256 _tokenId
    )
        external
        onlyOwner
    {
        super._burn(_tokenId);
        payees[_tokenId].transfer(tokenValue[_tokenId]);
        tokenValue[_tokenId] = 0;
    }

}

Additional Notes

ERC-4353 does not define how tokens are deposited into or managed by an NFT staking mechanism.

Designing and implementing the NFT's token economics is the responsibility of the contract creator or issuer. Issuers should communicate the staking mechanism clearly to buyers.

The recommended approach is to stake during minting and permit withdrawal during burning. Some DeFi applications may require dynamic staking and withdrawals, in which case additional interfaces can be implemented as appropriate.

Tests

js
const { expect } = require("chai");
const { ethers, waffle } = require("hardhat");
const provider = waffle.provider;

describe("StakedNFT", function () {
    let _id = 1234567890;
    let value = '1.5';
    let Token;
    let Interface;
    let owner;
    let addr1;
    let addr2;

    beforeEach(async function () {
        Token = await ethers.getContractFactory("ERC721Staked");
        [owner, addr1, ...addr2] = await ethers.getSigners();
        Interface = await Token.deploy();
    });

    describe("Staked NFT", function () {
        it("Should set the right owner", async function () {
            let mint = await Interface.mint(
                addr1.address, _id, 'http://foobar')
            expect(await Interface.ownerOf(_id)).to.equal(addr1.address);
        });

        it("Should not have staked balance without value", async function () {
            let mint = await Interface.mint(
                addr1.address, _id, 'http://foobar')
            expect(await Interface.stakedAmount(_id)).to.equal(
                ethers.utils.parseEther('0'));
        });

        it("Should set and return the staked amount", async function () {
            let mint = await Interface.mint(
                addr1.address, _id, 'http://foobar',
                {value: ethers.utils.parseEther(value)})
            expect(await Interface.stakedAmount(_id)).to.equal(
                ethers.utils.parseEther(value));
        });

        it("Should decrease owner eth balance on mint (deposit)", async function () {
            let balance1 = await provider.getBalance(owner.address);
            let mint = await Interface.mint(
                addr1.address, _id, 'http://foobar',
                {value: ethers.utils.parseEther(value)})
            let balance2 = await provider.getBalance(owner.address);
            let diff = parseFloat(ethers.utils.formatEther(
                balance1.sub(balance2))).toFixed(1);
            expect(diff === value);
        });

        it("Should add to payee's eth balance on burn (withdraw)", async function () {
            let balance1 = await provider.getBalance(addr1.address);
            let mint = await Interface.mint(
                addr1.address, _id, 'http://foobar',
                {value: ethers.utils.parseEther(value)})
            await Interface.burn(_id);
            let balance2 = await provider.getBalance(addr1.address);
            let diff = parseFloat(ethers.utils.formatEther(
                balance2.sub(balance1))).toFixed(1);
            expect(diff === value);
        });

        it("Should update balance after transfer", async function () {
            let mint = await Interface.mint(
                addr1.address, _id, 'http://foobar',
                {value: ethers.utils.parseEther(value)})
            await Interface.burn(_id);
            expect(await Interface.stakedAmount(_id)).to.equal(
                ethers.utils.parseEther('0'));
        });
    });
});

Security

Importance of Locking Mechanisms

If deposited tokens are not locked according to the intended rules, such as allowing withdrawal only when the NFT is burned, they may be withdrawn at arbitrary times. The displayed staking amount may then cease to reflect actual backing.

Updating Token Balances Correctly

Depending on the contract design, tokens may be transferable while the value returned by stakedAmount remains unchanged, resulting in inaccurate information.

Implementations must enforce their accounting and withdrawal rules consistently. A dedicated verification service could also analyze blockchain transactions to verify staking information associated with NFTs.

Citation

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.

Conclusion

ERC-4353 proposes a standardized interface for retrieving the amount of tokens staked in ERC-721 NFTs.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

I also publish articles on the following platforms.

https://chaldene.net/