Source Code

;; title: performance-tracker
;; version: 1.0.0
;; summary: Performance metrics tracking
;; description: Track vault and strategy performance - Clarity 4

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

;; Data Variables
(define-data-var next-snapshot-id uint u1)

;; Data Maps - Using stacks-block-time for Clarity 4
(define-map vault-performance principal {
  total-return: uint,
  apy: uint,
  sharpe-ratio: uint,
  max-drawdown: uint,
  total-deposits: uint,
  total-withdrawals: uint,
  last-updated: uint  ;; Clarity 4: Unix timestamp
})

(define-map performance-snapshots uint {
  vault: principal,
  tvl: uint,
  apy: uint,
  timestamp: uint,  ;; Clarity 4: Unix timestamp
  pps: uint  ;; Price per share
})

(define-map strategy-metrics principal {
  win-rate: uint,
  avg-profit: uint,
  avg-loss: uint,
  total-trades: uint,
  profitable-trades: uint,
  last-updated: uint  ;; Clarity 4: Unix timestamp
})

;; Public Functions

(define-public (update-vault-performance
  (vault principal)
  (total-return uint)
  (apy uint)
  (sharpe-ratio uint)
  (max-drawdown uint))
  (begin
    (asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-UNAUTHORIZED)

    (let (
      (perf-data (default-to
        { total-return: u0, apy: u0, sharpe-ratio: u0, max-drawdown: u0,
          total-deposits: u0, total-withdrawals: u0, last-updated: u0 }
        (map-get? vault-performance vault)))
    )
      (map-set vault-performance vault {
        total-return: total-return,
        apy: apy,
        sharpe-ratio: sharpe-ratio,
        max-drawdown: max-drawdown,
        total-deposits: (get total-deposits perf-data),
        total-withdrawals: (get total-withdrawals perf-data),
        last-updated: stacks-block-time
      })

      (ok true)
    )
  )
)

(define-public (snapshot-performance
  (vault principal)
  (tvl uint)
  (apy uint)
  (pps uint))
  (let (
    (snapshot-id (var-get next-snapshot-id))
  )
    (asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-UNAUTHORIZED)

    (map-set performance-snapshots snapshot-id {
      vault: vault,
      tvl: tvl,
      apy: apy,
      timestamp: stacks-block-time,
      pps: pps
    })

    (var-set next-snapshot-id (+ snapshot-id u1))

    (ok snapshot-id)
  )
)

(define-public (update-strategy-metrics
  (strategy principal)
  (win-rate uint)
  (avg-profit uint)
  (avg-loss uint)
  (total-trades uint)
  (profitable-trades uint))
  (begin
    (asserts! (is-eq tx-sender CONTRACT-OWNER) ERR-UNAUTHORIZED)

    (map-set strategy-metrics strategy {
      win-rate: win-rate,
      avg-profit: avg-profit,
      avg-loss: avg-loss,
      total-trades: total-trades,
      profitable-trades: profitable-trades,
      last-updated: stacks-block-time
    })

    (ok true)
  )
)

;; Read-Only Functions

(define-read-only (get-vault-performance (vault principal))
  (map-get? vault-performance vault)
)

(define-read-only (get-snapshot (snapshot-id uint))
  (map-get? performance-snapshots snapshot-id)
)

(define-read-only (get-strategy-metrics (strategy principal))
  (map-get? strategy-metrics strategy)
)

;; Clarity 4 Enhanced Functions

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

;; 2. Clarity 4: int-to-ascii - Format performance metrics
(define-read-only (format-win-rate (strategy principal))
  (match (map-get? strategy-metrics strategy)
    metrics (ok (int-to-ascii (get win-rate metrics)))
    (err u404)
  )
)

;; 3. Clarity 4: string-to-uint? - Parse performance values
(define-read-only (parse-performance-value (value-str (string-ascii 20)))
  (match (string-to-uint? value-str)
    value (ok value)
    (err u998)
  )
)

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

Functions (10)

FunctionAccessArgs
update-vault-performancepublicvault: principal, total-return: uint, apy: uint, sharpe-ratio: uint, max-drawdown: uint
snapshot-performancepublicvault: principal, tvl: uint, apy: uint, pps: uint
update-strategy-metricspublicstrategy: principal, win-rate: uint, avg-profit: uint, avg-loss: uint, total-trades: uint, profitable-trades: uint
get-vault-performanceread-onlyvault: principal
get-snapshotread-onlysnapshot-id: uint
get-strategy-metricsread-onlystrategy: principal
validate-strategyread-onlystrategy: principal
format-win-rateread-onlystrategy: principal
parse-performance-valueread-onlyvalue-str: (string-ascii 20
get-performance-timestampsread-only