Source Code

;; sadaqah-stream -- recurring sadaqah (charity) streaming
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-AUTH (err u401))
(define-constant ERR-NONE (err u404))
(define-data-var stream-count uint u0)
(define-data-var total-streamed uint u0)
(define-map streams uint { donor: principal, recipient: principal, amount-per-block: uint, total-sent: uint, active: bool, start-block: uint })

(define-public (create-stream (recipient principal) (amount-per-block uint))
  (let ((id (+ (var-get stream-count) u1)))
    (var-set stream-count id)
    (map-set streams id { donor: tx-sender, recipient: recipient, amount-per-block: amount-per-block, total-sent: u0, active: true, start-block: stacks-block-height })
    (ok id)))

(define-public (send-payment (stream-id uint) (amount uint))
  (let ((s (unwrap! (map-get? streams stream-id) ERR-NONE)))
    (asserts! (is-eq tx-sender (get donor s)) ERR-AUTH)
    (var-set total-streamed (+ (var-get total-streamed) amount))
    (map-set streams stream-id (merge s { total-sent: (+ (get total-sent s) amount) }))
    (ok amount)))

(define-public (stop-stream (stream-id uint))
  (let ((s (unwrap! (map-get? streams stream-id) ERR-NONE)))
    (asserts! (is-eq tx-sender (get donor s)) ERR-AUTH)
    (map-set streams stream-id (merge s { active: false })) (ok true)))

(define-read-only (get-stream (id uint)) (map-get? streams id))
(define-read-only (get-total-streamed) (var-get total-streamed))

Functions (5)

FunctionAccessArgs
create-streampublicrecipient: principal, amount-per-block: uint
send-paymentpublicstream-id: uint, amount: uint
stop-streampublicstream-id: uint
get-streamread-onlyid: uint
get-total-streamedread-only