Source Code

;; Charisma Mine Interaction
;;
;; Deep within the dungeon lies an ancient vein where adventurers can harvest
;; powerful Charisma tokens into being. Through complex extraction and refinement,
;; tokens can be extracted from the dungeon's raw essence or burned back into its depths.
;; These arduous operations require both energy from the adventurer and governance 
;; tokens as catalysts for the transmutation.
;;
;; Actions:
;; - MINT: Convert governance tokens into Charisma tokens
;; - BURN: Convert Charisma tokens back into governance tokens
;;
;; Costs:
;; - Energy cost via Fatigue interaction
;; - Required tokens for wrapping and unwrapping

;; Traits
(impl-trait .dao-traits-v9.interaction-trait)
(use-trait rulebook-trait .dao-traits-v9.rulebook-trait)

;; Constants
(define-constant ERR_UNAUTHORIZED (err u401))
(define-constant CONTRACT_OWNER tx-sender)

;; Data Variables
(define-data-var contract-uri (optional (string-utf8 256)) 
  (some u"https://charisma.rocks/api/v0/interactions/charisma-mine"))

;; Read-only functions

(define-read-only (get-interaction-uri)
  (ok (var-get contract-uri)))

;; Public functions

(define-public (execute (rulebook <rulebook-trait>) (action (string-ascii 32)))
  (begin
    (try! (contract-call? .registry authorize rulebook))
    (if (is-eq action "MINT") (mint-action rulebook)
    (if (is-eq action "BURN") (burn-action rulebook)
    (err "INVALID_ACTION")))))

;; Action Handlers

(define-private (mint-action (rulebook <rulebook-trait>))
  (let ((fatigue-response (unwrap-panic (contract-call? .fatigue-rc7 execute rulebook "BURN"))))
    (if (is-eq fatigue-response "ENERGY_BURNED") (mint-attempt)
    (if (is-eq fatigue-response "ENERGY_NOT_BURNED") (handle-insufficient-energy)
    (handle-fatigue-error)))))

(define-private (burn-action (rulebook <rulebook-trait>))
  (let ((fatigue-response (unwrap-panic (contract-call? .fatigue-rc7 execute rulebook "BURN"))))
    (if (is-eq fatigue-response "ENERGY_BURNED") (burn-attempt)
    (if (is-eq fatigue-response "ENERGY_NOT_BURNED") (handle-insufficient-energy)
    (handle-fatigue-error)))))

;; Token Operation Handlers

(define-private (mint-attempt)
  (let ((amount (unwrap-panic (contract-call? .charisma-token get-max-liquidity-flow))))
    (match (contract-call? .charisma-token wrap amount)
      success (handle-mint-success)
      error (if (is-eq error u402) (handle-mint-locked)
            (if (is-eq error u403) (handle-mint-unprepared)
            (if (is-eq error u405) (handle-mint-invalid)
            (handle-mint-unknown-error)))))))

(define-private (burn-attempt)
  (let ((amount (unwrap-panic (contract-call? .charisma-token get-max-liquidity-flow))))
    (match (contract-call? .charisma-token unwrap amount)
      success (handle-burn-success)
      error   (if (is-eq error u402) (handle-burn-locked)
              (if (is-eq error u403) (handle-burn-unprepared)
              (if (is-eq error u405) (handle-burn-invalid)
              (handle-burn-unknown-error)))))))

;; Energy Cost Handlers

(define-private (handle-insufficient-energy)
  (begin
    (print "The sender lacks the energy required to wrap tokens.")
    (ok "INSUFFICIENT_ENERGY")))

(define-private (handle-fatigue-error)
  (begin
    (print "The senders is too exhausted to wrap tokens.")
    (ok "ENERGY_ERROR")))

;; Response Handlers

(define-private (handle-mint-success)
  (begin
    (print "The sender sucessfully minted new Charisma tokens!")
    (ok "TOKENS_WRAPPED")))

(define-private (handle-burn-success)
  (begin
    (print "The Charisma tokens dissolve back into the dungeon's mysterious aether.")
    (ok "TOKENS_UNWRAPPED")))

(define-private (handle-mint-locked)
  (begin
    (print "The dungeon's minting crucible is currently sealed by magical wards.")
    (ok "LIQUIDITY_LOCKED")))

(define-private (handle-burn-locked)
  (begin
    (print "The dungeon's burning altar is currently sealed by magical wards.")
    (ok "LIQUIDITY_LOCKED")))

(define-private (handle-mint-unprepared)
  (begin
    (print "The adventurer hasn't been red-pilled and was unable to mint Charisma tokens.")
    (ok "NOT_RED_PILLED")))

(define-private (handle-burn-unprepared)
  (begin
    (print "The adventurer hasn't been red-pilled and was unable to mint Charisma tokens.")
    (ok "NOT_RED_PILLED")))

(define-private (handle-mint-invalid)
  (begin
    (print "The mystical crystallization fails as the arcane formulae go awry.")
    (ok "INVALID_INPUT")))

(define-private (handle-burn-invalid)
  (begin
    (print "The sacrificial ritual falters as the arcane formulae go awry.")
    (ok "INVALID_INPUT")))

(define-private (handle-mint-unknown-error)
  (begin
    (print "Unknown forces within the dungeon disrupt the minting ritual.")
    (ok "UNKNOWN_ERROR")))

(define-private (handle-burn-unknown-error)
  (begin
    (print "Unknown forces within the dungeon disrupt the burning ritual.")
    (ok "UNKNOWN_ERROR")))

;; Admin functions

(define-public (set-contract-uri (new-uri (optional (string-utf8 256))))
  (begin
    (asserts! (is-eq tx-sender CONTRACT_OWNER) ERR_UNAUTHORIZED)
    (ok (var-set contract-uri new-uri))))

Functions (19)

FunctionAccessArgs
handle-burn-unknown-errorprivate
handle-burn-invalidprivate
handle-mint-unknown-errorprivate
get-interaction-uriread-only
executepublicrulebook: <rulebook-trait>, action: (string-ascii 32
mint-actionprivaterulebook: <rulebook-trait>
burn-actionprivaterulebook: <rulebook-trait>
mint-attemptprivate
burn-attemptprivate
handle-insufficient-energyprivate
handle-fatigue-errorprivate
handle-mint-successprivate
handle-burn-successprivate
handle-mint-lockedprivate
handle-burn-lockedprivate
handle-mint-unpreparedprivate
handle-burn-unpreparedprivate
handle-mint-invalidprivate
set-contract-uripublicnew-uri: (optional (string-utf8 256