Source Code

(define-map ratings {escrow-id: uint, rater: principal} {score: uint, comment: (string-ascii 100)})
(define-map user-ratings principal {total-score: uint, count: uint})
(define-read-only (get-rating (escrow-id uint) (rater principal)) (map-get? ratings {escrow-id: escrow-id, rater: rater}))
(define-read-only (get-user-avg (user principal))
  (match (map-get? user-ratings user)
    r (/ (get total-score r) (get count r))
    u0))
(define-public (rate-user (escrow-id uint) (user principal) (score uint) (comment (string-ascii 100)))
  (begin
    (map-set ratings {escrow-id: escrow-id, rater: tx-sender} {score: score, comment: comment})
    (match (map-get? user-ratings user)
      existing (map-set user-ratings user {total-score: (+ (get total-score existing) score), count: (+ (get count existing) u1)})
      (map-set user-ratings user {total-score: score, count: u1}))
    (ok true)))

Functions (3)

FunctionAccessArgs
get-ratingread-onlyescrow-id: uint, rater: principal
get-user-avgread-onlyuser: principal
rate-userpublicescrow-id: uint, user: principal, score: uint, comment: (string-ascii 100