ERC-4885 · Stagnant · ERC · 2022-03-08
ERC-4885: Subscription NFTs and Multi Tokens
Proposed a subscription token that you buy with ERC-20 and that drains over time; while its balance is above zero you may use a linked ERC-721 or ERC-1155 NFT, giving creators programmable, tradable subscriptions.
What problem does it solve?
Subscriptions to music, video, courses, gyms or season tickets are run by central platforms, which set the terms and own the relationship. The author wanted creators to define their own subscription models on-chain, and wanted the subscription itself to be an asset that can be gifted, refunded or resold, for example a fan selling a season ticket to another fan.
Simply adding an expiry date to an NFT does not make the remaining time tradable. EIP-4885 separates the two: the NFT represents the asset or service, and a fungible subscription token represents the time left.
How it works
- A provider deploys a subscription-token contract tied to one NFT contract (ERC-721 or ERC-1155) and one ERC-20 "base token" used for payment. The NFT contract must approve the subscription contract as an operator.
subscribeToNFT(subscriber, tokenId, uri)gives the subscriber the NFT. AtokenIdof 0 lets the contract mint one;urioptionally points to per-subscriber terms.deposit(subscriber, tokenId, amount)takes ERC-20 from the subscriber and credits subscription tokens, extending the period. ADepositevent records the amount paid, tokens issued and seconds added.balanceOf(subscriber)is recommended to fall linearly to zero over time (or stay constant for lifetime subscriptions). It can be computed lazily in aview, so there is no gas cost for the decay. At zero, use of the NFT must not be allowed; what "not allowed" means (stop streaming, hide metadata) is up to the implementer.supportsInterface(0xC1A48422)must return true.
The contract borrows name, symbol and balanceOf from ERC-20; implementing the rest of ERC-20 to make subscriptions transferable is optional.
Sample code
A lazily decaying balance, using the spec's example of one token per day:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract DecayingBalance {
struct Sub { uint256 tokens; uint64 since; } // tokens in 1e18 units
mapping(address => Sub) internal subs;
uint256 constant PER_SECOND = 1e18 / 1 days; // 1 token per day
function balanceOf(address subscriber) public view returns (uint256) {
Sub memory s = subs[subscriber];
require(s.since != 0, "not started"); // spec: revert before first deposit
uint256 used = (block.timestamp - s.since) * PER_SECOND;
return used >= s.tokens ? 0 : s.tokens - used;
}
function _topUp(address subscriber, uint256 tokens) internal {
uint256 left = subs[subscriber].since == 0 ? 0 : balanceOf(subscriber);
subs[subscriber] = Sub(left + tokens, uint64(block.timestamp));
}
}
Things to know
- Status: Stagnant. It did not progress to Final.
- Payments are prepaid and cannot auto-renew: each top-up needs the subscriber's own approval of the ERC-20 deposit.
- Pausing is not specified; adding it means
balanceOfmust account for paused time. - Nothing in the standard guarantees the provider actually delivers the service after taking deposits.
- ERC-5643 is a simpler alternative that puts an expiry directly on ERC-721 tokens.