Skip to content

Commit 240fc59

Browse files
committed
updated frontend snippet
1 parent 1ba5a39 commit 240fc59

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

pages/price-feeds/use-real-time-data/stacks.mdx

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ The Pyth protocol integration for Stacks is available as a Beta on both testnet
1616
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).
1717

1818
<Callout type="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
22+
maintained repo [here](https://github.com/Trust-Machines/stacks-pyth-bridge).
2023
</Callout>
2124

2225
### Read BTC prices from a Clarity smart contract
2326

2427
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.
2528

2629
<Callout type="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
31+
[`.pyth-oracle-v3`](https://explorer.hiro.so/txid/0x745a0e07ef9487ebb2190da515bda60f1531299553420750b33b3ba4a97729e1?chain=mainnet).
32+
This contract serves as the main entry point for updating and getting price
33+
feed data.
2834
</Callout>
2935

3036
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
108114
```
109115

110116
<Callout type="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.
112119
</Callout>
113120

114121
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.
115122

116123
## Write Front-End Code
117124

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.
119126

120127
```javascript copy
121-
import { PriceServiceConnection } from "@pythnetwork/price-service-client"
122-
import { Buffer } from "buffer"
128+
import { HermesClient } from "@pythnetwork/hermes-client";
123129

124130
// --snip--
125131
async function handleFetchLatestVaa() {
126-
const connection = new PriceServiceConnection("https://hermes.pyth.network", {
127-
priceFeedRequestConfig: {
128-
binary: true
129-
}
130-
})
132+
const connection = new HermesClient("https://hermes.pyth.network", {});
131133

132-
const btcPriceId = ["0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"]
134+
const priceIds = [
135+
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43",
136+
];
133137

134-
const response = await connection.getLatestVaas(btcPriceId)
135-
let messageBuffer = Buffer.from(response[0], "base64")
136-
const hexString = messageBuffer.toString("hex")
137-
let latestVaaHex = `0x${hexString}`
138+
const priceUpdates = await connection.getLatestPriceUpdates(priceIds);
139+
let latestVaaHex = `0x${priceUpdates.binary.data[0]}`;
138140

139-
return latestVaaHex
141+
return latestVaaHex;
140142
}
141143
// --snip--
142144
```
143145

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.
145147

146148
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.
147149

148150
```javascript copy
149-
let latestVaaHex = await handleFetchLatestVaa()
151+
let latestVaaHex = await handleFetchLatestVaa();
150152

151-
let postCond1 = Pc.principal("SP1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRCBGD7R").willSendLte(1).ustx()
153+
let postCond1 = Pc.principal("SP1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRCBGD7R")
154+
.willSendLte(1)
155+
.ustx();
152156

153157
const response = await request("stx_callContract", {
154158
contract: `SP1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRCBGD7R.benjamin-club`,
155159
functionName: "join-the-benjamin-club",
156160
functionArgs: [Cl.bufferFromHex(latestVaaHex)],
157161
network: "mainnet",
158162
postConditions: [postCond1],
159-
postConditionMode: "deny"
160-
})
163+
postConditionMode: "deny",
164+
});
161165
```
162166

163167
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
169173
- **[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.
170174
- **[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.
171175
- **[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

Comments
 (0)