|
| 1 | +# Using DIA with Stacks |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +### DIA's Clarity Contracts <a href="#oracle-details" id="oracle-details"></a> |
| 8 | + |
| 9 | +{% hint style="info" %} |
| 10 | +For the full list of supported asset feeds for Stacks with DIA, check out their docs [here](https://www.diadata.org/docs/nexus/how-to-guides/fetch-price-data/chain-specific-guide/stacks). |
| 11 | +{% endhint %} |
| 12 | + |
| 13 | +<table><thead><tr><th width="122.22265625">Chain</th><th>Address</th></tr></thead><tbody><tr><td>Mainnet</td><td><a href="https://explorer.hiro.so/txid/SP1G48FZ4Y7JY8G2Z0N51QTCYGBQ6F4J43J77BQC0.dia-oracle?chain=mainnet">SP1G48FZ4Y7JY8G2Z0N51QTCYGBQ6F4J43J77BQC0.dia-oracle</a></td></tr><tr><td>Testnet</td><td><a href="https://explorer.hiro.so/txid/ST1S5ZGRZV5K4S9205RWPRTX9RGS9JV40KQMR4G1J.dia-oracle?chain=testnet">ST1S5ZGRZV5K4S9205RWPRTX9RGS9JV40KQMR4G1J.dia-oracle</a></td></tr></tbody></table> |
| 14 | + |
| 15 | +### Example |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +It does this by: |
| 20 | + |
| 21 | +* Reading the **current sBTC/USD price** from DIA’s on-chain oracle |
| 22 | +* Reading the caller’s **sBTC balance** |
| 23 | +* Calculating the USD value of that balance |
| 24 | +* Whitelisting the user if they meet a minimum threshold |
| 25 | + |
| 26 | +In short: **prove you hold enough sBTC (by USD value) to be eligible for a whitelist.** |
| 27 | + |
| 28 | +{% hint style="warning" %} |
| 29 | +All price feeds from DIA's Clarity contracts all have an implicit decimal place of 8 unless specified otherwise. |
| 30 | +{% endhint %} |
| 31 | + |
| 32 | +<pre class="language-clarity"><code class="lang-clarity">(define-constant MIN-SBTC-BALANCE u100) |
| 33 | +(define-constant ERR_READING_SBTC_BALANCE (err u7001)) |
| 34 | +(define-constant ERR_NOT_ENOUGH_SBTC (err u7002)) |
| 35 | +(define-constant ERR_NOT_OWNER (err u7003)) |
| 36 | +(define-constant SBTC-PRICE-EXPO 8) |
| 37 | + |
| 38 | +(define-map whitelist |
| 39 | + principal |
| 40 | + bool |
| 41 | +) |
| 42 | + |
| 43 | +(define-public (check-eligibility) |
| 44 | + (let ( |
| 45 | +<strong> (sbtc-price-data (unwrap-panic (contract-call? 'SP1G48FZ4Y7JY8G2Z0N51QTCYGBQ6F4J43J77BQC0.dia-oracle |
| 46 | +</strong><strong> get-value "sBTC/USD" |
| 47 | +</strong><strong> ))) |
| 48 | +</strong> (sbtc-usd-price (to-int (get value sbtc-price-data))) |
| 49 | + (price-denomination (pow 10 SBTC-PRICE-EXPO)) |
| 50 | + (adjusted-price (to-uint (/ sbtc-usd-price price-denomination))) |
| 51 | + (user-sbtc-balance (unwrap! |
| 52 | + (contract-call? 'SM3VDXK3WZZSA84XXFKAFAF15NNZX32CTSG82JFQ4.sbtc-token |
| 53 | + get-balance-available tx-sender |
| 54 | + ) |
| 55 | + ERR_READING_SBTC_BALANCE |
| 56 | + )) |
| 57 | + ) |
| 58 | + (if (> (/ (* user-sbtc-balance adjusted-price) (to-uint price-denomination)) |
| 59 | + MIN-SBTC-BALANCE |
| 60 | + ) |
| 61 | + (ok (map-set whitelist tx-sender true)) |
| 62 | + ERR_NOT_ENOUGH_SBTC |
| 63 | + ) |
| 64 | + ) |
| 65 | +) |
| 66 | + |
| 67 | +</code></pre> |
| 68 | + |
| 69 | +*** |
| 70 | + |
| 71 | +### Additional Resources |
| 72 | + |
| 73 | +* \[[Hiro YT](https://youtu.be/bhWQxHGpv2s?si=RsKaCe169Vu6zw_e)] How to use DIA's price data featuring Khawla, Product Manager @ DIA |
| 74 | +* \[[DIA Docs](https://www.diadata.org/docs/nexus/how-to-guides/fetch-price-data/chain-specific-guide/stacks)] The Stacks guide in DIA's docs |
| 75 | +* \[[Twitter](https://x.com/DIAdata_org/status/1945476086110032147)] DIA x Stacks Oracle Grants |
0 commit comments