Using DIA with Stacks
DIA's oracle protocol utilizes a push price model. As the developer, you would only need to fetch prices directly from DIA's Clarity contracts.
Price data can be fetched in Clarity by making an external call to the DIA .dia-oracle contract’s get-value read-only function with the desired asset pair.
DIA's Clarity Contracts
Example
Below is an example where we are fetching the sBTC price and determining if a user has the required minimum balance to join a whitelist.
It does this by:
Reading the current sBTC/USD price from DIA’s on-chain oracle
Reading the caller’s sBTC balance
Calculating the USD value of that balance
Whitelisting the user if they meet a minimum threshold
In short: prove you hold enough sBTC (by USD value) to be eligible for a whitelist.
All price feeds from DIA's Clarity contracts all have an implicit decimal place of 8 unless specified otherwise.
(define-constant MIN-SBTC-BALANCE u100)
(define-constant ERR_READING_SBTC_BALANCE (err u7001))
(define-constant ERR_NOT_ENOUGH_SBTC (err u7002))
(define-constant ERR_NOT_OWNER (err u7003))
(define-constant SBTC-PRICE-EXPO 8)
(define-map whitelist
principal
bool
)
(define-public (check-eligibility)
(let (
(sbtc-price-data (unwrap-panic (contract-call? 'SP1G48FZ4Y7JY8G2Z0N51QTCYGBQ6F4J43J77BQC0.dia-oracle
get-value "sBTC/USD"
)))
(sbtc-usd-price (to-int (get value sbtc-price-data)))
(price-denomination (pow 10 SBTC-PRICE-EXPO))
(adjusted-price (to-uint (/ sbtc-usd-price price-denomination)))
(user-sbtc-balance (unwrap!
(contract-call? 'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token
get-balance-available tx-sender
)
ERR_READING_SBTC_BALANCE
))
)
(if (> (/ (* user-sbtc-balance adjusted-price) (to-uint price-denomination))
MIN-SBTC-BALANCE
)
(ok (map-set whitelist tx-sender true))
ERR_NOT_ENOUGH_SBTC
)
)
)
Additional Resources
Last updated
Was this helpful?
