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

For the full list of supported asset feeds for Stacks with DIA, check out their docs here.

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.

(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

  • [Hiro YT] How to use DIA's price data featuring Khawla, Product Manager @ DIA

  • [DIA Docs] The Stacks guide in DIA's docs

  • [Twitter] DIA x Stacks Oracle Grants

Last updated

Was this helpful?