(define-constant O tx-sender)
(define-constant E1 (err u920))
(define-constant E2 (err u921))
(define-map authorized-feeds principal bool)
(define-map prices (string-ascii 16) {price: uint, block: uint, source: principal})
(define-map price-history {asset: (string-ascii 16), idx: uint} {price: uint, block: uint})
(define-map hist-count (string-ascii 16) uint)
(define-public (authorize-feed (who principal))
(begin (asserts! (is-eq tx-sender O) E1)
(ok (map-set authorized-feeds who true))))
(define-public (submit-price (asset (string-ascii 16)) (price uint))
(begin
(asserts! (default-to false (map-get? authorized-feeds tx-sender)) E2)
(let ((idx (default-to u0 (map-get? hist-count asset))))
(map-set prices asset {price: price, block: stacks-block-height, source: tx-sender})
(map-set price-history {asset: asset, idx: idx}
{price: price, block: stacks-block-height})
(map-set hist-count asset (+ idx u1))
(ok true))))
(define-read-only (get-price (asset (string-ascii 16)))
(map-get? prices asset))
(define-read-only (get-price-at (asset (string-ascii 16)) (idx uint))
(map-get? price-history {asset: asset, idx: idx}))
(define-read-only (get-price-count (asset (string-ascii 16)))
(default-to u0 (map-get? hist-count asset)))