ERC-1620 Money streaming (continuous payment) (Money Streaming)
Stagnant · Vaults & DeFi · created 2018-11-24
A stream contract standard in which two parties deposit ERC20 tokens that flow to the recipient at a fixed rate per unit time, measured by block number. The recipient can withdraw the amount accrued over the elapsed blocks at any time.
Why it is needed
Long-term monetary commitments such as salaries, subscriptions, rent, and consulting fees are typically paid in a single lump sum on a fixed date. Under this approach, the recipient receives no compensation proportional to the time actually worked or the service actually provided until the settlement date, and if the payer's circumstances change in the meantime, there is no way to reverse funds already sent. On blockchains, without a standard, payments with similar characteristics, such as salaries, subscriptions, rent, and crowdsale refunds, each have to be implemented with differently designed contracts, and wallets or apps cannot query, withdraw from, or halt them through shared functions and events. Changing terms or stopping midway also relies solely on trust between the parties, with no standardized mechanism for agreement.
What to implement
function balanceOf(uint256 _streamId, address _addr): Returns the withdrawable funds for a given stream id and address.function getStream(uint256 _streamId) returns (address sender, address recipient, address tokenAddress, uint256 balance, uint256 startBlock, uint256 stopBlock, uint256 payment, uint256 interval): If id refers to a valid stream, returns the full data for that stream.function create(address _recipient, address _tokenAddress, uint256 _startBlock, uint256 _stopBlock, uint256 _payment, uint256 _interval): Creates a new stream between msg.sender and _recipient.function withdraw(uint256 _streamId, uint256 _funds): Only the recipient can withdraw all or part of the available funds.function redeem(uint256 _streamId): Closes the stream and distributes the remaining funds to the sender and recipient.function update(uint256 _streamId, address _tokenAddress, uint256 _stopBlock, uint256 _payment, uint256 _interval): The function listed under the confirmUpdate entry in the original text, through which one of the involved parties signals agreement to a change in the stream's terms.function confirmUpdate(uint256 _streamId, address _tokenAddress, uint256 _stopBlock, uint256 _payment, uint256 _interval): The function listed under the revokeUpdate entry in the original text, through which a party cancels an update it had proposed.event LogCreate(uint256 indexed _streamId, address indexed _sender, address indexed _recipient, address _tokenAddress, uint256 _startBlock, uint256 _stopBlock, uint256 _payment, uint256 _interval): Emitted when create is called successfully.event LogWithdraw(uint256 indexed _streamId, address indexed _recipient, uint256 _funds): Emitted when withdraw is called successfully.event LogRedeem(uint256 indexed _streamId, address indexed _sender, address indexed _recipient, uint256 _senderBalance, uint256 _recipientBalance): Emitted when redeem is called successfully.event LogConfirmUpdate(uint256 indexed _streamId, address indexed _confirmer, address _newTokenAddress, uint256 _newStopBlock, uint256 _newPayment, uint256 _newInterval);: Emitted when confirmUpdate is called successfully.event LogRevokeUpdate(uint256 indexed _streamId, address indexed revoker, address _newTokenAddress, uint256 _newStopBlock, uint256 _newPayment, uint256 _newInterval): Emitted when revokeUpdate is called successfully.event LogExecuteUpdate(uint256 indexed _newStreamId, address indexed _sender, address indexed _recipient, address _newTokenAddress, uint256 _newStopBlock, uint256 _newPayment, uint256 _newInterval): Emitted when all involved parties have agreed to the update and the final approval is given.
In depth
Stream struct
A stream consists of sender (the address funding it), recipient (the address receiving the money), tokenAddress (the ERC20 token address used for payment), balance (the total funds remaining in the stream), timeframe, and rate. timeframe represents the stream's start and end points as two block numbers, start and stop, and rate defines the payment speed via payment (how much to send) and interval (how often to send it).
Time measurement and solvency calculation
This standard uses block number as a proxy for time instead of an actual clock. The amount the recipient can withdraw up to a given point, i.e. ongoing solvency, is calculated as payment rate * (current block height - start block height). Block number serves as the basis for this approach because it is a trustworthy reference that all participants can verify identically.
The create function
create establishes a new stream between msg.sender and _recipient. It specifies the token address, start and end blocks, payment, and interval together. A single sender MUST be able to open multiple streams at once, and it SHOULD use only ERC20-compatible tokens rather than accepting ether. A successful call emits the LogCreate event.
The withdraw function
withdraw pulls out all or part of the available funds for a given stream. This action MUST be performable only by the recipient. A successful call emits the LogWithdraw event.
The redeem function
redeem closes the stream and distributes the remaining funds to the sender and recipient. It is RECOMMENDED that either party be able to call this. A successful call emits the LogRedeem event, which records _senderBalance and _recipientBalance, the respective shares after distribution.
Update agreement process (confirmUpdate / revokeUpdate)
Changing a stream's terms (token, end block, payment, interval) requires agreement from all involved parties, and MUST NOT take effect from only one party's agreement. When a party signals agreement, LogConfirmUpdate is emitted, and once the last party agrees, LogExecuteUpdate is emitted and the change takes effect. A proposed update can be withdrawn via revoke, which emits LogRevokeUpdate. Note that in the original specification, the code listed under the confirmUpdate description is actually named update, and the code listed under the revokeUpdate description is actually named confirmUpdate, so the function names and section headings are mismatched.
Uncertainty in block time and ways to correct for it
Because Ethereum block times fluctuate within a range on average, a configured period can end up diverging from the duration actually intended on a real clock. If the average block time becomes slower than expected, the recipient receives funds later; if it becomes faster, the recipient receives funds sooner. The original text proposes two responses. One is for participants to agree on a call to update to correct the terms, and the other is to use past block timestamps to automatically checkpoint the stream every so many blocks so it self-corrects for the error, though this is noted as still being at the research stage due to the gas cost burden. Also mentioned is the option of setting the stream's duration to a multiple of the chain's average block time (GCD).
Proposal status and out-of-scope items
This proposal's status is Stagnant, meaning it is a draft that has stalled. The original text mentions, only as areas for future exploration not included in this specification's scope, alternatives such as payment channels or Plasma chains, implementations on sidechains (POA Network, xDai, etc.), oracles that would supply time information instead of block numbers, and multi-hop streams that pass stream funds along an A-B-C chain without an intermediate step.