|
| 1 | +(define-constant ERR-NOT-AUTHORIZED (err u1000)) |
| 2 | + |
| 3 | +(define-constant ONE_8 u100000000) |
| 4 | + |
| 5 | +;; -- token implementation |
| 6 | + |
| 7 | +(define-fungible-token runes-inu) |
| 8 | + |
| 9 | +(define-data-var contract-owner principal tx-sender) |
| 10 | +(define-data-var token-name (string-ascii 32) "SATOSHI NAKAMOTO INU (RUNES)") |
| 11 | +(define-data-var token-symbol (string-ascii 32) "INU") |
| 12 | +(define-data-var token-uri (optional (string-utf8 256)) (some u"https://cdn.alexlab.co/metadata/runes-inu.json")) |
| 13 | + |
| 14 | +(define-data-var token-decimals uint u8) |
| 15 | + |
| 16 | +(define-map approved-contracts principal bool) |
| 17 | + |
| 18 | +;; read-only calls |
| 19 | + |
| 20 | +(define-read-only (get-contract-owner) |
| 21 | + (ok (var-get contract-owner)) |
| 22 | +) |
| 23 | + |
| 24 | +(define-read-only (get-name) |
| 25 | + (ok (var-get token-name)) |
| 26 | +) |
| 27 | + |
| 28 | +(define-read-only (get-symbol) |
| 29 | + (ok (var-get token-symbol)) |
| 30 | +) |
| 31 | + |
| 32 | +(define-read-only (get-decimals) |
| 33 | + (ok (var-get token-decimals)) |
| 34 | +) |
| 35 | + |
| 36 | +(define-read-only (get-balance (who principal)) |
| 37 | + (ok (ft-get-balance runes-inu who)) |
| 38 | +) |
| 39 | + |
| 40 | +(define-read-only (get-total-supply) |
| 41 | + (ok (ft-get-supply runes-inu)) |
| 42 | +) |
| 43 | + |
| 44 | +(define-read-only (get-token-uri) |
| 45 | + (ok (var-get token-uri)) |
| 46 | +) |
| 47 | + |
| 48 | +;; @desc fixed-to-decimals |
| 49 | +;; @params amount |
| 50 | +;; @returns uint |
| 51 | +(define-read-only (fixed-to-decimals (amount uint)) |
| 52 | + (/ (* amount (pow-decimals)) ONE_8) |
| 53 | +) |
| 54 | + |
| 55 | +;; @desc get-total-supply-fixed |
| 56 | +;; @params token-id |
| 57 | +;; @returns (response uint) |
| 58 | +(define-read-only (get-total-supply-fixed) |
| 59 | + (ok (decimals-to-fixed (unwrap-panic (get-total-supply)))) |
| 60 | +) |
| 61 | + |
| 62 | +;; @desc get-balance-fixed |
| 63 | +;; @params token-id |
| 64 | +;; @params who |
| 65 | +;; @returns (response uint) |
| 66 | +(define-read-only (get-balance-fixed (account principal)) |
| 67 | + (ok (decimals-to-fixed (unwrap-panic (get-balance account)))) |
| 68 | +) |
| 69 | + |
| 70 | +;; governance calls |
| 71 | + |
| 72 | +(define-public (set-contract-owner (owner principal)) |
| 73 | + (begin |
| 74 | + (try! (check-is-owner)) |
| 75 | + (ok (var-set contract-owner owner)) |
| 76 | + ) |
| 77 | +) |
| 78 | + |
| 79 | +(define-public (set-name (new-name (string-ascii 32))) |
| 80 | + (begin |
| 81 | + (try! (check-is-owner)) |
| 82 | + (ok (var-set token-name new-name)) |
| 83 | + ) |
| 84 | +) |
| 85 | + |
| 86 | +(define-public (set-symbol (new-symbol (string-ascii 10))) |
| 87 | + (begin |
| 88 | + (try! (check-is-owner)) |
| 89 | + (ok (var-set token-symbol new-symbol)) |
| 90 | + ) |
| 91 | +) |
| 92 | + |
| 93 | +(define-public (set-decimals (new-decimals uint)) |
| 94 | + (begin |
| 95 | + (try! (check-is-owner)) |
| 96 | + (ok (var-set token-decimals new-decimals)) |
| 97 | + ) |
| 98 | +) |
| 99 | + |
| 100 | +(define-public (set-token-uri (new-uri (optional (string-utf8 256)))) |
| 101 | + (begin |
| 102 | + (try! (check-is-owner)) |
| 103 | + (ok (var-set token-uri new-uri)) |
| 104 | + ) |
| 105 | +) |
| 106 | + |
| 107 | +(define-public (add-approved-contract (new-approved-contract principal)) |
| 108 | + (begin |
| 109 | + (try! (check-is-owner)) |
| 110 | + (ok (map-set approved-contracts new-approved-contract true)) |
| 111 | + ) |
| 112 | +) |
| 113 | + |
| 114 | +(define-public (set-approved-contract (owner principal) (approved bool)) |
| 115 | + (begin |
| 116 | + (try! (check-is-owner)) |
| 117 | + (ok (map-set approved-contracts owner approved)) |
| 118 | + ) |
| 119 | +) |
| 120 | + |
| 121 | +;; priviliged calls |
| 122 | + |
| 123 | +;; @desc mint |
| 124 | +;; @restricted ContractOwner/Approved Contract |
| 125 | +;; @params token-id |
| 126 | +;; @params amount |
| 127 | +;; @params recipient |
| 128 | +;; @returns (response bool) |
| 129 | +(define-public (mint (amount uint) (recipient principal)) |
| 130 | + (begin |
| 131 | + (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) |
| 132 | + (ft-mint? runes-inu amount recipient) |
| 133 | + ) |
| 134 | +) |
| 135 | + |
| 136 | +;; @desc burn |
| 137 | +;; @restricted ContractOwner/Approved Contract |
| 138 | +;; @params token-id |
| 139 | +;; @params amount |
| 140 | +;; @params sender |
| 141 | +;; @returns (response bool) |
| 142 | +(define-public (burn (amount uint) (sender principal)) |
| 143 | + (begin |
| 144 | + (asserts! (or (is-ok (check-is-approved)) (is-ok (check-is-owner))) ERR-NOT-AUTHORIZED) |
| 145 | + (ft-burn? runes-inu amount sender) |
| 146 | + ) |
| 147 | +) |
| 148 | + |
| 149 | +;; @desc mint-fixed |
| 150 | +;; @params token-id |
| 151 | +;; @params amount |
| 152 | +;; @params recipient |
| 153 | +;; @returns (response bool) |
| 154 | +(define-public (mint-fixed (amount uint) (recipient principal)) |
| 155 | + (mint (fixed-to-decimals amount) recipient) |
| 156 | +) |
| 157 | + |
| 158 | +;; @desc burn-fixed |
| 159 | +;; @params token-id |
| 160 | +;; @params amount |
| 161 | +;; @params sender |
| 162 | +;; @returns (response bool) |
| 163 | +(define-public (burn-fixed (amount uint) (sender principal)) |
| 164 | + (burn (fixed-to-decimals amount) sender) |
| 165 | +) |
| 166 | + |
| 167 | +(define-public (mint-fixed-many (recipients (list 200 { amount: uint, to: principal}))) |
| 168 | + (fold mint-many-iter recipients (ok true)) |
| 169 | +) |
| 170 | + |
| 171 | +;; public calls |
| 172 | + |
| 173 | +(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) |
| 174 | + (begin |
| 175 | + (asserts! (is-eq sender tx-sender) ERR-NOT-AUTHORIZED) |
| 176 | + (try! (ft-transfer? runes-inu amount sender recipient)) |
| 177 | + (match memo to-print (print to-print) 0x) |
| 178 | + (ok true) |
| 179 | + ) |
| 180 | +) |
| 181 | + |
| 182 | +;; @desc transfer-fixed |
| 183 | +;; @params token-id |
| 184 | +;; @params amount |
| 185 | +;; @params sender |
| 186 | +;; @params recipient |
| 187 | +;; @returns (response bool) |
| 188 | +(define-public (transfer-fixed (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) |
| 189 | + (transfer (fixed-to-decimals amount) sender recipient memo) |
| 190 | +) |
| 191 | + |
| 192 | +(define-public (transfer-fixed-many (recipients (list 200 { amount: uint, to: principal}))) |
| 193 | + (fold transfer-many-iter recipients (ok true)) |
| 194 | +) |
| 195 | + |
| 196 | +;; private calls |
| 197 | + |
| 198 | +(define-private (check-is-owner) |
| 199 | + (ok (asserts! (is-eq tx-sender (var-get contract-owner)) ERR-NOT-AUTHORIZED)) |
| 200 | +) |
| 201 | + |
| 202 | +(define-private (check-is-approved) |
| 203 | + (ok (asserts! (default-to false (map-get? approved-contracts tx-sender)) ERR-NOT-AUTHORIZED)) |
| 204 | +) |
| 205 | + |
| 206 | +;; @desc decimals-to-fixed |
| 207 | +;; @params amount |
| 208 | +;; @returns uint |
| 209 | +(define-private (decimals-to-fixed (amount uint)) |
| 210 | + (/ (* amount ONE_8) (pow-decimals)) |
| 211 | +) |
| 212 | + |
| 213 | +;; @desc pow-decimals |
| 214 | +;; @returns uint |
| 215 | +(define-private (pow-decimals) |
| 216 | + (pow u10 (unwrap-panic (get-decimals))) |
| 217 | +) |
| 218 | + |
| 219 | +(define-private (mint-many-iter (recipient { amount: uint, to: principal }) (previous-response (response bool uint))) |
| 220 | + (match previous-response prev-ok (mint-fixed (get amount recipient) (get to recipient)) prev-err previous-response) |
| 221 | +) |
| 222 | + |
| 223 | +(define-private (transfer-many-iter (recipient { amount: uint, to: principal }) (previous-response (response bool uint))) |
| 224 | + (match previous-response prev-ok (transfer-fixed (get amount recipient) tx-sender (get to recipient) none) prev-err previous-response) |
| 225 | +) |
| 226 | + |
| 227 | +;; contract initialisation |
| 228 | +(contract-call? .alex-vault-v1-1 set-approved-token .runes-inu true) |
| 229 | +;; (set-contract-owner .executor-dao) |
0 commit comments