You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/price-feeds/use-real-time-data/stacks.mdx
+27-23Lines changed: 27 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,15 +16,21 @@ The Pyth protocol integration for Stacks is available as a Beta on both testnet
16
16
Unlike other smart contract languages, there is no importing modules into Clarity smart contracts for certain security reasons. So the Pyth integration on Stacks is implemented as its own set of Clarity contracts where developers will invoke a `contract-call?` to the main Pyth Clarity [contract](https://explorer.hiro.so/txid/SP3R4F6C1J3JQWWCVZ3S7FRRYPMYG6ZW6RZK31FXY.pyth-oracle-v3?chain=mainnet).
17
17
18
18
<Callouttype="info"emoji="ℹ️">
19
-
Currently, the Pyth protocol integration is currently maintained by Trust Machines. It currently supports real-time price feeds for BTC, STX, ETH, and USDC. To request more price feeds, open an issue in Trust Machine's Pyth maintained repo [here](https://github.com/Trust-Machines/stacks-pyth-bridge).
19
+
Currently, the Pyth protocol integration is currently maintained by Trust
20
+
Machines. It currently supports real-time price feeds for BTC, STX, ETH, and
21
+
USDC. To request more price feeds, open an issue in Trust Machine's Pyth
For our example, we will imagine a contract that mints an NFT in exchange for $100 of sBTC. In order to determine the USD value of a user's sBTC amount, we'll need to use Pyth. Since market pricing for sBTC isn't supported currently, we'll use the price data from the BTC/USD price feed.
25
28
26
29
<Callouttype="info"emoji="ℹ️">
27
-
The maintained Pyth integration contract for Stacks is called [`.pyth-oracle-v3`](https://explorer.hiro.so/txid/0x745a0e07ef9487ebb2190da515bda60f1531299553420750b33b3ba4a97729e1?chain=mainnet). This contract serves as the main entry point for updating and getting price feed data.
30
+
The maintained Pyth integration contract for Stacks is called
This contract serves as the main entry point for updating and getting price
33
+
feed data.
28
34
</Callout>
29
35
30
36
You'll notice in the Clarity snippet below we open up `let` bindings of our function to:
@@ -108,56 +114,54 @@ We can then determine the USD amount of sBTC the user owns and decide if it is e
108
114
```
109
115
110
116
<Callouttype="warning"emoji="⚠️">
111
-
The `verify-and-update-price-feeds` of the `.pyth-oracle-v3` contract applies a fee of 1 uSTX, or 1 micro-stx, which is 0.000001 STX.
117
+
The `verify-and-update-price-feeds` of the `.pyth-oracle-v3` contract applies
118
+
a fee of 1 uSTX, or 1 micro-stx, which is 0.000001 STX.
112
119
</Callout>
113
120
114
121
Alternatively, developers can just invoke the `read-price-feed` public function of the `pyth-oracle-v3.clar` contract. This will simply return the price feed from the last updated feed. But it's always encouraged to determine if a staleness check is viable for your application.
115
122
116
123
## Write Front-End Code
117
124
118
-
In your front-end application code, you can install and use the methods brought by Pyth Network's Javascript SDK to fetch the latest price update, known as a VAA (Verified Action Approvals) message.
125
+
In your front-end application code, you can install and use the methods brought by Pyth Network's `hermes-client`Javascript SDK to fetch the latest price update, known as a VAA (Verified Action Approvals) message.
let latestVaaHex =`0x${priceUpdates.binary.data[0]}`;
138
140
139
-
return latestVaaHex
141
+
return latestVaaHex;
140
142
}
141
143
// --snip--
142
144
```
143
145
144
-
The binary data returned from the Pyth SDK will be in base64 format which we'll need to convert to hexadecimal in order for the Pyth contract to properly ingest it. We'll then take this hexadecimal VAA message and pass it into our Clarity function as an argument.
146
+
The binary data returned from the Pyth SDK will already be in hexadecimal format. We'll then take this hexadecimal VAA message and pass it into our Clarity function as an argument.
145
147
146
148
Using Stacks Connect of the [stacks.js](https://github.com/hirosystems/stacks.js) monorepo, we'll open up a stx_callContract request and invoke our public function while passing in the latestVaaHex as the function argument.
147
149
148
150
```javascript copy
149
-
let latestVaaHex =awaithandleFetchLatestVaa()
151
+
let latestVaaHex =awaithandleFetchLatestVaa();
150
152
151
-
let postCond1 =Pc.principal("SP1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRCBGD7R").willSendLte(1).ustx()
153
+
let postCond1 =Pc.principal("SP1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRCBGD7R")
If you noticed, we set a post-condition statement of our user transferring less than or equal to 1 uSTX, which is 0.000001 STX. This is because the `verify-and-update-price-feeds` of the `.pyth-oracle-v3` contract applies a fee for this. Setting a separate post-condition statement on the actual sbtc token transfer in our example will also be needed. Beforehand, you could invoke the `decode-price-feeds` function with the `latestVaaHex` to simply have the contained price data decoded and returned. From there you could pre-determine the estimated amount of sbtc tokens to be transferred and set in a separate post-condition.
@@ -169,4 +173,4 @@ You may find these additional resources helpful for developing your Stacks appli
169
173
-**[Hiro Docs](https://docs.hiro.so/resources/guides/using-pyth-price-feeds):** Check out the dedicated guide for using Pyth in Stacks applications in Hiro's documentation. In this guide you'll see a visual architecture overview of using Pyth in Stacks, how you can test your implementation, how to fetch VAAs on the front-end, learn best practices, and more.
170
174
-**[Hiro How-To Tutorial](https://youtu.be/eybqQVRh_hw?si=KNfUp3RS3CnaST91):** Watch the dedicated video tutorial on using Pyth in Stacks and learn how a major Stacks DeFi app, Granite, is using Pyth.
171
175
-**[Trust Machine's Pyth Github](https://github.com/Trust-Machines/stacks-pyth-bridge):** Check out the open-source repo for the Pyth integration Clarity contracts.
172
-
-**[pyth-oracle-v3.clar](https://explorer.hiro.so/txid/SP3R4F6C1J3JQWWCVZ3S7FRRYPMYG6ZW6RZK31FXY.pyth-oracle-v3?chain=mainnet):** The latest Pyth integration contract on Stacks' mainnet.
176
+
-**[pyth-oracle-v3.clar](https://explorer.hiro.so/txid/SP3R4F6C1J3JQWWCVZ3S7FRRYPMYG6ZW6RZK31FXY.pyth-oracle-v3?chain=mainnet):** The latest Pyth integration contract on Stacks' mainnet.
0 commit comments