ERC-1620 · Stagnant · ERC · 2018-11-24
ERC-1620: Money Streaming
A standard for money streaming: a payer locks ERC-20 tokens in a contract and the recipient can withdraw them continuously as blocks pass, turning salaries and subscriptions into pay-as-you-go flows.
What problem does it solve?
Salaries, rent and subscriptions are paid in chunks, usually monthly, because traditional payment rails are expensive to use often. That forces one side to trust the other: the employee works a month before being paid, or the subscriber pays a month in advance.
On a blockchain, "money as a function of time" is practical. ERC-1620 standardises a contract where funds flow from sender to recipient block by block. The recipient can withdraw what has accrued at any moment, and either party can stop the stream and split the balance fairly.
How it works
A single master contract holds many streams. Each Stream records:
sender,recipientandtokenAddress(an ERC-20 token; Ether should not be accepted),balance: funds still in the stream,timeframe:startandstopblock numbers,rate:paymentreleased everyintervalblocks.
The flow:
create(recipient, token, startBlock, stopBlock, payment, interval): the sender deposits enough tokens for the whole period. A sender may run several streams in parallel.balanceOf(streamId, addr): shows what each party is currently entitled to. The recipient's share grows roughly aspayment rate × (current block − start block).withdraw(streamId, amount): only the recipient may pull accrued funds.redeem(streamId): any party may end the stream early. Accrued funds go to the recipient and the rest back to the sender.- Updates: changing the token, stop block or rate requires every party to confirm; either party can revoke a pending update.
Events (LogCreate, LogWithdraw, LogRedeem, LogConfirmUpdate, LogRevokeUpdate, LogExecuteUpdate) record each step.
Sample code
Core accounting for one stream, in modern Solidity (simplified, illustration only):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
struct Stream {
address sender;
address recipient;
address token;
uint256 deposit;
uint256 withdrawn;
uint256 startBlock;
uint256 stopBlock;
uint256 payment; // tokens released per interval
uint256 interval; // blocks per interval
}
library StreamMath {
/// Amount the recipient has earned so far but not yet withdrawn.
function recipientBalance(Stream memory s) internal view returns (uint256) {
if (block.number <= s.startBlock) return 0;
uint256 end = block.number < s.stopBlock ? block.number : s.stopBlock;
uint256 earned = ((end - s.startBlock) / s.interval) * s.payment;
if (earned > s.deposit) earned = s.deposit;
return earned - s.withdrawn;
}
/// What the sender would get back if the stream were redeemed now.
function senderBalance(Stream memory s) internal view returns (uint256) {
return s.deposit - s.withdrawn - recipientBalance(s);
}
}
Things to know
- Status is Stagnant. The spec is an early sketch, and its method list has inconsistencies (for example, the signatures shown under
confirmUpdateandrevokeUpdatedo not match their headings). Treat it as a concept, not a finished interface. - Block numbers stand in for time. If average block times change, a stream ends sooner or later in wall-clock time than expected. The spec discusses this at length and suggests renegotiating via
updateor using timestamps from an oracle. Usingblock.timestampinstead is a common alternative. - Lock the full deposit up front. Without it, the recipient's accrued balance is only a promise.
- The spec mentions future "multi-hop" streams (A to B to C) but does not define them.