Source Code

;; SatGuard Governance Voting
(define-constant err-nf (err u210))
(define-constant err-dup (err u211))
(define-constant err-unauth (err u212))
(define-map gov-votes {pid: uint,v: principal} {app: bool,wt: uint,va: uint})
(define-map tallies {pid: uint} {vf: uint,va: uint,tc: uint})
(define-read-only (get-gov-vote (pid uint) (v principal)) (map-get? gov-votes {pid: pid,v: v}))
(define-read-only (get-tally (pid uint)) (map-get? tallies {pid: pid}))
(define-public (cast-gov-vote (pid uint) (app bool))
  (let ((prop (unwrap! (contract-call? .sg-gov-proposals get-proposal pid) err-nf))
        (contrib (unwrap! (contract-call? .insurance-pool get-contrib u1 tx-sender) err-unauth))
        (wt (get a contrib))
        (ex (map-get? gov-votes {pid: pid,v: tx-sender}))
        (tl (default-to {vf: u0,va: u0,tc: u0} (map-get? tallies {pid: pid}))))
    (asserts! (is-none ex) err-dup)
    (asserts! (> wt u0) err-unauth)
    (map-set gov-votes {pid: pid,v: tx-sender} {app: app,wt: wt,va: block-height})
    (map-set tallies {pid: pid} {vf: (if app (+ (get vf tl) wt) (get vf tl)),va: (if app (get va tl) (+ (get va tl) wt)),tc: (+ (get tc tl) u1)})
    (print {e: "gov-vote",pid: pid,v: tx-sender,app: app})
    (ok true)))
(define-read-only (tally-result (pid uint))
  (let ((tl (default-to {vf: u0,va: u0,tc: u0} (map-get? tallies {pid: pid}))))
    (ok {approved: (> (get vf tl) (get va tl)),vf: (get vf tl),va: (get va tl),tc: (get tc tl)})))

Functions (4)

FunctionAccessArgs
get-gov-voteread-onlypid: uint, v: principal
get-tallyread-onlypid: uint
cast-gov-votepublicpid: uint, app: bool
tally-resultread-onlypid: uint