A Clarity smart contract enabling continuous Bitcoin (STX) payments over time on the Stacks blockchain. Stream payments to recipients who can withdraw funds as they accrue, with full cancellation and refund capabilities.
This protocol allows users to create payment streams that distribute funds continuously over a specified duration. Recipients can withdraw earned funds at any time, and senders can cancel streams to recover unstreamed amounts.
- Continuous Payment Streams: Distribute payments block-by-block over any duration
- Flexible Withdrawals: Recipients withdraw anytime without waiting for stream completion
- Cancellation & Refunds: Senders can cancel and recover unstreamed funds
- Batch Operations: Withdraw from multiple streams in a single transaction
- Stream Top-ups: Add more funds to existing active streams
- Tracking & Analytics: Monitor stream progress, status, and withdrawal history
- Protocol Fees: Built-in fee mechanism (1% default) for protocol sustainability
(create-stream recipient total-amount duration)- recipient: Principal address receiving the stream
- total-amount: Total STX to stream (minimum: 1,000 micro-STX)
- duration: Stream length in blocks (minimum: 1 block)
- Returns: Stream ID
- Fee: 1% of total amount (adjustable by contract owner)
(withdraw stream-id)- Withdraws all earned funds up to current block
- Only callable by stream recipient
- Calculates earnings based on elapsed blocks × rate-per-block
(batch-withdraw (list stream-id-1 stream-id-2 ...))- Withdraw from up to 20 streams simultaneously
- Failed withdrawals return 0, successful ones return amount
(cancel-stream stream-id)- Only callable by stream sender
- Transfers earned amount to recipient
- Refunds unstreamed amount to sender
- Cannot cancel already-completed streams
(top-up-stream stream-id additional-amount)- Add more funds to active stream
- Only callable by original sender
- Subject to protocol fee
(get-stream stream-id)Returns complete stream information including sender, recipient, amounts, blocks, and status.
(calculate-withdrawable stream-id)Returns exact amount available for withdrawal at current block.
(get-stream-status stream-id)Returns:
- active: Boolean indicating if stream is currently active
- progress: Completion percentage (0-10000 basis points)
- withdrawn-percent: Percentage of total amount withdrawn
(get-user-streams principal)
(get-recipient-streams principal)Returns list of stream IDs for sender or recipient.
Streams distribute funds linearly:
Amount per block = Total Amount ÷ Duration
Earned Amount = Blocks Elapsed × Amount per Block
Withdrawable = Earned Amount - Already Withdrawn
Example:
- Total: 100,000 STX
- Duration: 1,000 blocks
- Rate: 100 STX/block
- After 250 blocks: 25,000 STX earned
| Code | Error | Description |
|---|---|---|
| u100 | ERR_UNAUTHORIZED | Caller not authorized for action |
| u101 | ERR_STREAM_NOT_FOUND | Stream ID doesn't exist |
| u102 | ERR_INSUFFICIENT_BALANCE | Not enough balance for operation |
| u103 | ERR_INVALID_PARAMS | Invalid input parameters |
| u104 | ERR_STREAM_ENDED | Stream already completed |
| u105 | ERR_STREAM_CANCELLED | Stream was cancelled |
| u106 | ERR_NOTHING_TO_WITHDRAW | No funds available to withdraw |
| u107 | ERR_ALREADY_EXISTS | Resource already exists |
;; Stream 30,000 STX over ~30 days (4,320 blocks at 10min/block)
(contract-call? .payment-stream create-stream
'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7
u30000000000 ;; 30,000 STX in micro-STX
u4320) ;; ~30 days(contract-call? .payment-stream withdraw u0)(contract-call? .payment-stream cancel-stream u0)- Authorization: All state-changing functions verify caller identity
- Input Validation: Minimum amounts and durations enforced
- Safe Math: Prevents overflow/underflow in calculations
- Atomic Operations: State updates occur atomically with transfers
- No Reentrancy: Uses Clarity's built-in protection mechanisms
Contract owner can:
- Update protocol fee (max 10%):
(update-protocol-fee new-fee) - Withdraw collected fees:
(withdraw-fees)
- Maximum 100 streams per user/recipient (list size limit)
- Maximum 20 streams in batch operations
- Minimum stream amount: 1,000 micro-STX
- Minimum duration: 1 block
- Payroll: Stream employee salaries continuously
- Subscriptions: Pay for services over time
- Vesting: Token or payment vesting schedules
- Grants: Distribute funding progressively
- Rent/Leases: Automated recurring payments
- Freelance: Milestone-based continuous payments