ERC-6932 Subscription token standard (Subscription-Based Token)
Draft · Tokens · created 2023-04-25
Adds subscription functionality to ERC-20 tokens so that once a user subscribes, the subscription fee is automatically deducted from their balance at a fixed interval as a recurring payment.
Why it is needed
To use a subscription service on a blockchain today, a user must manually send a payment transaction each time. Manually paying every time is cumbersome, and forgetting a payment can cause the service to be interrupted. There is also no common rule for implementing recurring payments, so if each service does it differently, wallets and apps find it hard to display or handle subscription status consistently. This standard proposes a common approach in which the subscription fee and payment interval are defined within the token contract, and the fee is automatically deducted from the subscribed address's balance each cycle. This lets services that need recurring payments, such as content subscriptions or game services, let users keep using the service without interruption while merchants receive payments automatically.
What to implement
function subscribers(uint idx) external view returns (address);: Returns the address at position idx in the list of subscribers. Returns address(0) if that position does not exist.function subscriptionInfo() external view returns ( uint, string memory, string memory, string memory );: Returns the subscription's metadata: subscriptionID, subscriptionName, subscriptionDesc, and subscriptionTandC.function subscribe() external;: Registers the calling address as a subscriber. Can be made payable.function unsubscribe() external;: The subscribed address cancels its own subscription.function subscriptionFee() external view returns (uint256);: Returns the subscription fee amount. Can be implemented as view or pure.function subscriptionFrequency() external view returns (uint);: Returns the interval at which the subscription fee is deducted. Can be implemented as view or pure.
In depth
ERC-20 extension structure
This standard is an extension that adds subscribe and unsubscribe functions to the ERC-20 token. The subscriptionFee and subscriptionFrequency variables define the subscription cost and interval, and the nextPaymentDate mapping tracks each subscriber's next payment date. The subscription fee is automatically deducted from the user's balance at the interval set by the merchant. It also proposes a renewSubscription method that a token holder uses to renew a subscription to a service that requires recurring payments.
Required components
The implementation required by the standard is as follows. subscribers returns the list of addresses subscribed to the subscription token contract. subscriptionInfo returns subscription metadata such as subscriptionID, subscriptionName, subscriptionDesc, and subscriptionTandC. subscriptionFee is the amount deducted every subscriptionFrequency interval while subscribed. subscriptionFrequency is the interval at which the subscription fee is charged, expressed in seconds, for example daily, weekly, or monthly. subscribe registers an address as a subscriber, and unsubscribe lets a subscribed address withdraw from the subscription.
How the deducted amount is calculated
The subscription fee is calculated with a mathematical formula and deducted periodically. The number of elapsed intervals is computed as intervals = (block.timestamp - info.start) / info.frequency, and the amount to deduct is calculated as amount = info.amount * intervals. If the accumulated amount exceeds the address's effective balance, effectiveBalance[account], the amount is capped at the effective balance. An address's token balance is computed using both the amount locked in the ongoing subscription and the effective balance, which is updated whenever a transfer occurs.
Supporting various subscription models
The standard accommodates multiple billing cycles such as monthly, quarterly, and annual. Merchants can define trial periods, plan upgrades and downgrades, and cancellation policies, making it usable as a foundation for various subscription-based businesses. Users can also check the remaining balance of the subscription token at any time to decide whether to continue the subscription. Security measures such as cryptographic signatures are used for subscription-related transactions to protect users and merchants from unauthorized access and malicious activity.
Backward compatibility and proposal status
The source states that no backward compatibility issues have been found. However, this proposal is still in Draft status, so its content may change or progress may stall. It is advisable to check the latest source before implementing it.
Things to watch out for
- Private key management: Subscription tokens may require transaction signatures or cryptographic proofs for subscription-related actions. Proper key management practices must be followed to protect users' private keys and prevent unauthorized access. Encouraging the use of hardware wallets or secure key storage can reduce the risk of key leakage.