Source Code

;; Meme Engine Coordinator
;;
;; This contract is a core component of the Charisma protocol, designed to coordinate
;; and manage multiple meme engines. It provides centralized control over shared
;; parameters and functionality used by all meme engines in the ecosystem.
;;
;; Key Responsibilities:
;; 1. Threshold Management: Maintains and updates thresholds for balance integral calculations.
;; 2. Sample Point Generation: Provides functions to generate sample points for energy calculations.
;; 3. Authorization Control: Ensures only authorized entities can modify critical parameters.
;;
;; Core Components:
;; - Thresholds: Configurable parameters that determine the number of sample points for calculations.
;; - Sample Point Generation: Functions for 2, 5, 9, 19, and 39-point calculations.
;; - Authorization: Utilizes Dungeon Master contract for access control.
;;
;; Key Functions:
;; - Threshold Management: set-threshold-X-point functions to update thresholds.
;; - Sample Point Generation: generate-sample-points-X functions for various calculation resolutions.
;; - Threshold Retrieval: get-thresholds function to retrieve current threshold values.
;;
;; Integration with Charisma Ecosystem:
;; - Works in conjunction with meme engines to provide consistent calculation parameters.
;; - Interacts with Dungeon Master for authorization checks.
;;
;; Usage in Charisma:
;; 1. Threshold Updates: Authorized entities can adjust calculation thresholds as needed.
;; 2. Meme Engine Calculations: Engines use this contract to generate sample points for energy calculations.
;; 3. Protocol Governance: Allows centralized management of key parameters affecting all meme engines.
;;
;; This contract is crucial for maintaining consistency and efficiency across all meme engines
;; in the Charisma protocol. By centralizing threshold management and sample point generation,
;; it ensures that all engines operate under the same parameters, simplifying governance and
;; reducing the potential for discrepancies in energy calculations across different engines.

;; Error codes
(define-constant ERR_UNAUTHORIZED (err u401))
(define-constant ERR_INVALID_THRESHOLD (err u402))
(define-constant ERR_INVALID_SCORE (err u405))

;; Incentive score state
(define-constant DEPLOYER tx-sender)
(define-constant MAX_SCORE u1000000000)
(define-data-var default-score uint u1000000)
(define-map incentive-scores principal uint)

;; Data variables
(define-data-var threshold-5-point uint u12)
(define-data-var threshold-9-point uint u50)
(define-data-var threshold-19-point uint u500)
(define-data-var threshold-39-point uint u10000)

;; Authorization control
(define-read-only (is-authorized)
  (ok (asserts! (is-eq contract-caller DEPLOYER) ERR_UNAUTHORIZED))
)

;; Incentive score functions

(define-read-only (get-incentive-score (token principal))
  (default-to (var-get default-score) (map-get? incentive-scores token))
)

(define-public (set-incentive-score (token principal) (score uint))
  (begin
    (try! (is-authorized))
    (asserts! (<= score MAX_SCORE) ERR_INVALID_SCORE)
    (ok (map-set incentive-scores token score))
  )
)

(define-public (set-default-score (score uint))
  (begin
    (try! (is-authorized))
    (asserts! (<= score MAX_SCORE) ERR_INVALID_SCORE)
    (ok (var-set default-score score))
  )
)

;; Functions to update thresholds

(define-public (set-threshold-5-point (new-threshold uint))
  (begin
    (try! (is-authorized))
    (asserts! (< new-threshold (var-get threshold-9-point)) ERR_INVALID_THRESHOLD)
    (ok (var-set threshold-5-point new-threshold))))

(define-public (set-threshold-9-point (new-threshold uint))
  (begin
    (try! (is-authorized))
    (asserts! (and (> new-threshold (var-get threshold-5-point)) (< new-threshold (var-get threshold-19-point))) ERR_INVALID_THRESHOLD)
    (ok (var-set threshold-9-point new-threshold))))

(define-public (set-threshold-19-point (new-threshold uint))
  (begin
    (try! (is-authorized))
    (asserts! (and (> new-threshold (var-get threshold-9-point)) (< new-threshold (var-get threshold-39-point))) ERR_INVALID_THRESHOLD)
    (ok (var-set threshold-19-point new-threshold))))

(define-public (set-threshold-39-point (new-threshold uint))
  (begin
    (try! (is-authorized))
    (asserts! (> new-threshold (var-get threshold-19-point)) ERR_INVALID_THRESHOLD)
    (ok (var-set threshold-39-point new-threshold))))

;; Read-only functions to get current thresholds
(define-read-only (get-thresholds)
  (ok {
    threshold-5-point: (var-get threshold-5-point),
    threshold-9-point: (var-get threshold-9-point),
    threshold-19-point: (var-get threshold-19-point),
    threshold-39-point: (var-get threshold-39-point)
  }))

;; Generate sample points for 39-point balance integral calculation
(define-read-only (generate-sample-points-39 (address principal) (start-block uint) (end-block uint))
    (let ((block-step (/ (- end-block start-block) u38)))
        (list
            { address: address, block: start-block }
            { address: address, block: (+ start-block (* block-step u1)) }
            { address: address, block: (+ start-block (* block-step u2)) }
            { address: address, block: (+ start-block (* block-step u3)) }
            { address: address, block: (+ start-block (* block-step u4)) }
            { address: address, block: (+ start-block (* block-step u5)) }
            { address: address, block: (+ start-block (* block-step u6)) }
            { address: address, block: (+ start-block (* block-step u7)) }
            { address: address, block: (+ start-block (* block-step u8)) }
            { address: address, block: (+ start-block (* block-step u9)) }
            { address: address, block: (+ start-block (* block-step u10)) }
            { address: address, block: (+ start-block (* block-step u11)) }
            { address: address, block: (+ start-block (* block-step u12)) }
            { address: address, block: (+ start-block (* block-step u13)) }
            { address: address, block: (+ start-block (* block-step u14)) }
            { address: address, block: (+ start-block (* block-step u15)) }
            { address: address, block: (+ start-block (* block-step u16)) }
            { address: address, block: (+ start-block (* block-step u17)) }
            { address: address, block: (+ start-block (* block-step u18)) }
            { address: address, block: (+ start-block (* block-step u19)) }
            { address: address, block: (+ start-block (* block-step u20)) }
            { address: address, block: (+ start-block (* block-step u21)) }
            { address: address, block: (+ start-block (* block-step u22)) }
            { address: address, block: (+ start-block (* block-step u23)) }
            { address: address, block: (+ start-block (* block-step u24)) }
            { address: address, block: (+ start-block (* block-step u25)) }
            { address: address, block: (+ start-block (* block-step u26)) }
            { address: address, block: (+ start-block (* block-step u27)) }
            { address: address, block: (+ start-block (* block-step u28)) }
            { address: address, block: (+ start-block (* block-step u29)) }
            { address: address, block: (+ start-block (* block-step u30)) }
            { address: address, block: (+ start-block (* block-step u31)) }
            { address: address, block: (+ start-block (* block-step u32)) }
            { address: address, block: (+ start-block (* block-step u33)) }
            { address: address, block: (+ start-block (* block-step u34)) }
            { address: address, block: (+ start-block (* block-step u35)) }
            { address: address, block: (+ start-block (* block-step u36)) }
            { address: address, block: (+ start-block (* block-step u37)) }
            { address: address, block: end-block })))

;; Generate sample points for 19-point balance integral calculation
(define-read-only (generate-sample-points-19 (address principal) (start-block uint) (end-block uint))
    (let ((block-step (/ (- end-block start-block) u18)))
        (list
            { address: address, block: start-block }
            { address: address, block: (+ start-block (* block-step u1)) }
            { address: address, block: (+ start-block (* block-step u2)) }
            { address: address, block: (+ start-block (* block-step u3)) }
            { address: address, block: (+ start-block (* block-step u4)) }
            { address: address, block: (+ start-block (* block-step u5)) }
            { address: address, block: (+ start-block (* block-step u6)) }
            { address: address, block: (+ start-block (* block-step u7)) }
            { address: address, block: (+ start-block (* block-step u8)) }
            { address: address, block: (+ start-block (* block-step u9)) }
            { address: address, block: (+ start-block (* block-step u10)) }
            { address: address, block: (+ start-block (* block-step u11)) }
            { address: address, block: (+ start-block (* block-step u12)) }
            { address: address, block: (+ start-block (* block-step u13)) }
            { address: address, block: (+ start-block (* block-step u14)) }
            { address: address, block: (+ start-block (* block-step u15)) }
            { address: address, block: (+ start-block (* block-step u16)) }
            { address: address, block: (+ start-block (* block-step u17)) }
            { address: address, block: end-block })))

;; Generate sample points for 9-point balance integral calculation
(define-read-only (generate-sample-points-9 (address principal) (start-block uint) (end-block uint))
    (let ((block-step (/ (- end-block start-block) u8)))
        (list
            { address: address, block: start-block }
            { address: address, block: (+ start-block block-step) }
            { address: address, block: (+ start-block (* block-step u2)) }
            { address: address, block: (+ start-block (* block-step u3)) }
            { address: address, block: (+ start-block (* block-step u4)) }
            { address: address, block: (+ start-block (* block-step u5)) }
            { address: address, block: (+ start-block (* block-step u6)) }
            { address: address, block: (+ start-block (* block-step u7)) }
            { address: address, block: end-block })))

;; Generate sample points for 5-point balance integral calculation
(define-read-only (generate-sample-points-5 (address principal) (start-block uint) (end-block uint))
    (let ((block-step (/ (- end-block start-block) u4)))
        (list
            { address: address, block: start-block }
            { address: address, block: (+ start-block block-step) }
            { address: address, block: (+ start-block (* block-step u2)) }
            { address: address, block: (+ start-block (* block-step u3)) }
            { address: address, block: end-block })))

;; Generate sample points for 2-point balance integral calculation
(define-read-only (generate-sample-points-2 (address principal) (start-block uint) (end-block uint))
    (let ((block-step (/ (- end-block start-block) u1)))
        (list
            { address: address, block: start-block }
            { address: address, block: end-block })))

Functions (14)

FunctionAccessArgs
is-authorizedread-only
get-incentive-scoreread-onlytoken: principal
set-incentive-scorepublictoken: principal, score: uint
set-default-scorepublicscore: uint
set-threshold-5-pointpublicnew-threshold: uint
set-threshold-9-pointpublicnew-threshold: uint
set-threshold-19-pointpublicnew-threshold: uint
set-threshold-39-pointpublicnew-threshold: uint
get-thresholdsread-only
generate-sample-points-39read-onlyaddress: principal, start-block: uint, end-block: uint
generate-sample-points-19read-onlyaddress: principal, start-block: uint, end-block: uint
generate-sample-points-9read-onlyaddress: principal, start-block: uint, end-block: uint
generate-sample-points-5read-onlyaddress: principal, start-block: uint, end-block: uint
generate-sample-points-2read-onlyaddress: principal, start-block: uint, end-block: uint