Source Code

;; title: credit-scoring
;; version: 1.0.0
;; summary: On-chain credit scoring
;; description: Calculate credit scores for users - Clarity 4

;; Constants
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-UNAUTHORIZED (err u5700))

;; Data Maps - Using stacks-block-time for Clarity 4
(define-map credit-scores principal {
  score: uint,
  history-length: uint,
  default-count: uint,
  updated-at: uint  ;; Clarity 4: Unix timestamp
})

;; Public Functions

(define-public (update-credit-score (user principal) (score uint))
  (let (
    (current-score (default-to {score: u0, history-length: u0, default-count: u0, updated-at: u0}
      (map-get? credit-scores user)))
  )
    (asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-UNAUTHORIZED)

    (map-set credit-scores user {
      score: score,
      history-length: (+ (get history-length current-score) u1),
      default-count: (get default-count current-score),
      updated-at: stacks-block-time
    })

    (ok true)
  )
)

;; Read-Only Functions

(define-read-only (get-credit-score (user principal))
  (map-get? credit-scores user)
)
;; Clarity 4 Enhanced Functions

;; 1. Clarity 4: principal-destruct? - Validate user principals
(define-read-only (validate-user (u principal))
  (principal-destruct? u)
)

;; 2. Clarity 4: int-to-ascii - Format credit scores
(define-read-only (format-score (score uint))
  (ok (int-to-ascii score))
)

;; 3. Clarity 4: string-to-uint? - Parse score from string
(define-read-only (parse-score (s-str (string-ascii 10)))
  (match (string-to-uint? s-str)
    s (ok s)
    (err u998)
  )
)

;; 4. Clarity 4: burn-block-height - Track credit scoring timestamps
(define-read-only (get-cs-timestamps)
  (ok {
    stacks-time: stacks-block-time,
    burn-time: burn-block-height
  })
)

Functions (6)

FunctionAccessArgs
update-credit-scorepublicuser: principal, score: uint
get-credit-scoreread-onlyuser: principal
validate-userread-onlyu: principal
parse-scoreread-onlys-str: (string-ascii 10
get-cs-timestampsread-only
format-scoreread-onlyscore: uint