EIPs Explained

ERC-4883 · Draft · ERC · 2022-03-08

ERC-4883: Composable SVG NFT

A one-function interface, renderTokenById, that returns an onchain NFT's SVG body as a string so other SVG NFTs can layer it into their own artwork.

What problem does it solve?

Fully onchain NFTs return their artwork as an SVG inside a data URI from tokenURI. That output is a complete, encoded document: base64 JSON wrapping a full <svg> element. It is hard for another contract to take that and draw it inside its own picture.

Composability is the fun part of onchain art: putting a hat NFT on a profile-picture NFT, or a fish NFT in a fish-tank NFT. ERC-4883 standardises a simple hook for that: each NFT exposes its raw SVG elements, and any other SVG NFT can concatenate them into its own image.

How it works

The whole interface is one function:

interface IERC4883 {
    function renderTokenById(uint256 id) external view returns (string memory);
}
  • It returns the SVG body for token id, meaning the elements that go inside an <svg> tag, not a full document and not a data URI.
  • The result must be either an empty string or valid SVG element(s).
  • Composition is plain string concatenation. SVG uses a painter's model, so whatever is concatenated later is drawn on top. The composing NFT decides the order, so another NFT can be used as background or foreground.
  • Sizing and scaling are not specified; the composing NFT can wrap the fragment in a <g transform="…"> if needed.

The function name comes from the Loogies project, so NFTs already using it are compatible.

Sample code

A fish tank that draws any IERC4883 fish on top of its own background:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IERC4883 {
    function renderTokenById(uint256 id) external view returns (string memory);
}

contract FishTank is IERC4883 {
    IERC4883 public immutable fishContract;
    mapping(uint256 => uint256) public fishOf; // tank id => fish token id

    constructor(IERC4883 _fish) { fishContract = _fish; }

    function renderTokenById(uint256 id) public view returns (string memory) {
        string memory water = '<rect width="400" height="400" fill="#7dd3fc"/>';
        string memory fish = fishContract.renderTokenById(fishOf[id]);
        // background first, fish painted on top, nudged into place
        return string.concat(water, '<g transform="translate(100,150)">', fish, '</g>');
    }

    function svg(uint256 id) external view returns (string memory) {
        return string.concat(
            '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">',
            renderTokenById(id),
            '</svg>'
        );
    }
}

In a real NFT, tokenURI would base64-encode svg(id) into the metadata; ownership checks for attaching a fish are omitted here.

Things to know

  • Status: Draft. The interface is tiny and stable in practice, but the proposal is not finalised.
  • Any composed NFT can completely cover the host's artwork, since later elements paint over earlier ones.
  • SVG is XML and can carry scripts or other hostile content. It cannot harm the contract, but marketplaces and frontends should sanitise SVG before displaying it.
  • Rendering a deep stack of composed NFTs is a chain of view calls; keep it shallow to avoid hitting RPC gas limits on eth_call.
  • For NFTs that own other tokens (rather than just drawing them), see ERC-998.

#nft #svg #onchain #composability #erc721

Details

Authors
Andrew B Coathup (@abcoathup), Alex (@AlexPartyPanda), Damian Martinelli (@damianmarti), blockdev (@0xbok), Austin Griffith (@austintgriffith)
Status
Draft
Type
Standards Track / ERC
Created
2022-03-08
Requires

Machine-readable: Markdown · JSON