Payment streams
Shows: anyone may poke (11) · seed-authorized custody (13) · clock assertion (17) · async message for strangers (9)
Goal
A salary paid by the second. The recipient can be private and offline. Nobody runs a server for the payer.
Actors
| Actor | What it holds | Placement |
|---|---|---|
(stream, streams) | recipient, rate, start time, seed, amount released so far | public PDA |
(stream, native) | the money not yet released | same account |
The stream is public: anyone must be able to move it forward, and only a public actor allows that.
Messages
enum Message {
/// Once, by whoever sends it first. The payer checks it, then funds the stream.
Open { recipient: AccountId, rate_per_ms: u128, start: Timestamp, seed: PdaSeed },
/// By anyone, at any time.
Release { now: Timestamp },
}
Handler
Message::Release { now } => {
let mut stream = Stream::from_bytes(&input.pre_state);
let due = (now - stream.start) as u128 * stream.rate_per_ms - stream.released;
stream.released += due;
Response::set_state(stream.to_bytes())
// Only valid from `now` on, checked against the block being built.
.timestamp_window(now..)
// Pay from the stream's own balance. The credit is an AsyncMessage.
.send(native_token::custody_transfer(
input.receiver.account_id, stream.seed, stream.recipient, due))
}
Open writes the stream’s settings once (write_once). It also checks that the seed is its own (pattern 13): AccountId::for_public_pda(&program, &seed) == input.receiver.account_id. Funding is a plain transfer to the stream’s account. Open needs no authorization, so whoever sends it first sets the stream: the payer reads the recorded settings before funding, as in Payment channels.
What to notice
The handler does not read the time: the caller states now and the validity window confirms it. Reading the clock with ReadTimestamp would also work, at the cost of a reply. Stating an earlier time releases less. Stating a later time fails.
Release needs no authorization: the recipient sends it when they want their money, and so can anyone else. The handler only does what the schedule allows, so who asks does not matter.
No account key can touch the funds: they sit on a PDA, and only this program can grant its seed, which it does only in Release, as long as the program is immutable.
The payout is an AsyncMessage, so the handler is the same for a public or a private recipient.
A Release whose due exceeds the stream’s balance fails, since the custody transfer cannot debit it (InsufficientBalance); stating an earlier now releases at most what the stream holds.
How it runs with a private recipient
- The payer opens and funds the stream, naming the recipient’s address. A private recipient’s address needs its recovery binding on chain, because
Releasepays it in the clear from a public actor. - Someone pokes
Release. The credit is stored on chain. - The recipient collects when convenient.
With a public recipient there is no third step:
Public and private
| Payer | Recipient | What an observer sees |
|---|---|---|
| Public | Public | everything |
| Public | Private | payer, rate, amounts, destination address |
| Private | Private | rate, amounts, and both parties’ blinded addresses |
A private payer funds the stream under a blinded address. The schedule is public in every case, because the stream is. Privacy here is about who, not how much.
Going further
- Vesting. Add a cliff: release nothing before it.
- Inheritance. A vault that pays an heir’s address once its owner has sent no
Heartbeatfor a year. Same three patterns. - What does not work: streaming out of a private account by poke. Only its owner can run a private handler. For per-payment privacy, see Payment channels.