Skip to content

Commit 9d1bc30

Browse files
authored
add pyth patterns info in the pyth-connector example (#828)
* add pyth patterns info Signed-off-by: master_jedy <[email protected]> * fix minor issues Signed-off-by: master_jedy <[email protected]> * add more info about patterns Signed-off-by: master_jedy <[email protected]> * minor fix Signed-off-by: master_jedy <[email protected]> * fix review issues Signed-off-by: master_jedy <[email protected]> * remove trailing whitespaces Signed-off-by: master_jedy <[email protected]> * clarify evaa flow, add pyth connector flow Signed-off-by: master_jedy <[email protected]> * fix pre-commit Signed-off-by: master_jedy <[email protected]> --------- Signed-off-by: master_jedy <[email protected]>
1 parent bf92854 commit 9d1bc30

File tree

1 file changed

+205
-1
lines changed
  • pages/price-feeds/core/use-real-time-data/pull-integration

1 file changed

+205
-1
lines changed

pages/price-feeds/core/use-real-time-data/pull-integration/ton.mdx

Lines changed: 205 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: Consume Pyth Network prices in TON applications
33
---
44

5-
import { Tabs } from "nextra/components";
5+
import { Tabs, Callout } from "nextra/components";
66

77
# How to Use Real-Time Data in TON Contracts
88

@@ -126,6 +126,208 @@ This code snippet does the following:
126126
4. Prepares the update data and calculates the update fee.
127127
5. Updates the price feeds on the TON contract.
128128

129+
## Patterns for Providing Pyth Data to Your Contract
130+
131+
There are typically two main scenarios: either you call a method supplying TON, or you transfer jettons.
132+
133+
- **TON proxy**: `User → Pyth → EVAA Master → ... (further processing)`
134+
Use this method if you only need to send TON to your contract or simply call a contract method, without involving jettons.
135+
136+
- **Jetton on-chain getter**: `User → Jetton Wallet → EVAA Master → Pyth → EVAA Master → ... (further processing)`
137+
In this pattern, your contract first receives the Pyth data, then forwards it to the Pyth contract for validation, and finally gets the validated prices back.
138+
This approach is useful when you want to transfer jettons to your contract while also providing price data.
139+
<Callout type="info" emoji="ℹ️">
140+
This data flow is simplified. In reality, the "Jetton Wallet" step consists
141+
of a sequence of transactions: User's jetton wallet → EVAA jetton wallet →
142+
EVAA master. These internal details are omitted here to highlight the main
143+
flow and the interaction with Pyth.
144+
</Callout>
145+
146+
They both are demonstrated in the [Pyth Connector example](https://github.com/pyth-network/pyth-examples/tree/main/price_feeds/ton/pyth-connector). <br/>
147+
These same patterns are also used in the [EVAA Protocol code](https://github.com/evaafi/contracts/tree/v8) for implementing following operations:
148+
149+
- Pyth proxy pattern: liquidate TON / supply_withdraw TON.
150+
- Onchain-getter pattern: liquidate jetton / supply_withdraw jetton.
151+
152+
Choose the pattern that best fits your use case and how you want to handle assets and price updates in your application.
153+
154+
Each operation described above can result in either a successful outcome or an error. It is important to consider and handle both scenarios for every pattern.
155+
156+
### Pyth proxy: Success
157+
158+
#### EVAA flow
159+
160+
In the EVAA protocol, the operations that implement the Pyth proxy pattern are <b>`liquidate (TON)`</b> and <b>`supply_withdraw (TON)`</b>. In these cases, the user sends a request to the Pyth contract using the native TON asset. As a result of the operation, the user may receive either TON or JETTON tokens back, depending on the outcome of the transaction.
161+
162+
```mermaid
163+
sequenceDiagram
164+
autonumber
165+
participant U as User
166+
participant P as Pyth Contract
167+
participant M as EVAA Master
168+
169+
note over M: master.fc:121 — received from Pyth (op 0x5)
170+
U->>P: op 0x5 parse_price_feed_updates (price feeds + update data)<br/>payload (op 0x3 liquidate_master | 0x4 supply_withdraw_master)
171+
note right of U: send request to Pyth Contract with update data and operation payload<br/>destination address is EVAA Master contract
172+
P-->>M: op 0x5 parse_price_feed_updates (price feeds + prices)<br/>payload (op 0x3 liquidate_master | 0x4 supply_withdraw_master)
173+
note right of P: Pyth Contract validates update data and <br/> sends prices with payload to EVAA Master contract
174+
note over M: EVAA Master validates sender <br/> parses payload and <br/> processes the transaction
175+
```
176+
177+
- Related code (GitHub):
178+
- [entry for the Pyth message: master.fc](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/master.fc#L121)
179+
- [process the supply_withdraw operation (TON): master.fc](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/master.fc#L192-L211)
180+
- [process the liquidate operation (TON): master.fc](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/master.fc#L171-L190)
181+
182+
#### Pyth Connector flow
183+
184+
The Pyth Connector example also has a similar flow. It has two main operations: proxy and onchain-getter.
185+
They have no practical purpose other than to demonstrate the patterns described above.
186+
The data flow is practically the same as the EVAA protocol, only operation codes are different.
187+
188+
```mermaid
189+
sequenceDiagram
190+
autonumber
191+
participant U as User
192+
participant P as Pyth Contract
193+
participant M as Pyth Connector
194+
195+
note over M: pyth_connector.fc:78 — received from Pyth (op 0x5)
196+
U->>P: op 0x5 parse_price_feed_updates (price feeds + update data)<br/>payload (op 0x4 connector_proxy_operation)
197+
note right of U: send request to the Pyth Contract with update data and operation payload<br/>destination address is the Pyth Connector contract
198+
P-->>M: op 0x5 parse_price_feed_updates (price feeds + prices)<br/>payload (op 0x4 connector_proxy_operation)
199+
note right of P: Pyth Contract validates update data and <br/> sends prices with payload to Pyth Connector contract
200+
note over M: Pyth Connector validates sender <br/> parses payload and <br/> processes the transaction
201+
```
202+
203+
- Related code (GitHub):
204+
- [entry for the Pyth message: pyth-connector.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/pyth_connector.fc#L78)
205+
- [detect the connector_proxy_operation: pyth-connector.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/pyth_connector.fc#L97)
206+
- [process the connector_proxy_operation: proxy_operation.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/operations/proxy_operation.fc#L17-L40)
207+
208+
### Pyth proxy: Error handling
209+
210+
In the Pyth proxy pattern, when an error occurs (i.e., Pyth cannot process the request and sends a `response_error` with op 0x10002), the error report is sent directly back to the user who initiated the transaction, not to a contract. This is different from the on-chain getter pattern, where the error is returned to the EVAA Master contract for further handling and potential refund logic. In the proxy case, the user receives the error response from the Pyth contract, including the error code and the original query ID, allowing the user to identify and handle the failure on their side.
211+
212+
```mermaid
213+
sequenceDiagram
214+
autonumber
215+
participant U as User
216+
participant P as Pyth Contract
217+
218+
U->>P: request
219+
P-->>U: response_error (op 0x10002) with error_code and query_id
220+
```
221+
222+
### Pyth onchain-getter: Success
223+
224+
#### EVAA flow
225+
226+
```mermaid
227+
sequenceDiagram
228+
autonumber
229+
participant U as User
230+
participant JW as Jetton Wallet
231+
participant M as EVAA Master
232+
participant P as Pyth Contract
233+
234+
U->>JW: op transfer_jetton, forward_payload<br/>(liquidate_master_jetton | supply_withdraw_master_jetton)
235+
note right of U: transfer jetton with forward payload:
236+
JW->>M: op transfer_notification, forward_payload<br/>(liquidate_master_jetton | supply_withdraw_master_jetton)
237+
note right of JW: transfer notification with forward payload:
238+
M->>P: op 0x5 parse_price_feed_updates + update data + target feeds<br/>+ payload(liquidate_master_jetton_process | supply_withdraw_master_jetton_process)
239+
P-->>M: op 0x5 parse_price_feed_updates(price feeds + prices)<br/>+ payload(liquidate_master_jetton_process | supply_withdraw_master_jetton_process)
240+
note over P: Pyth Contract validates update data and <br/> sends prices with payload to EVAA Master contract
241+
note over M: EVAA Master validates sender <br/> parses payload and <br/> processes the transaction
242+
```
243+
244+
- Related code (GitHub):
245+
- [entry for jetton-transfer notification: master.fc](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/master.fc#L502)
246+
- [request to Pyth (op liquidate_jetton)](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/core/master-liquidate.fc#L728-L742)
247+
- [request to Pyth (op supply_withdraw jetton)](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/core/master-supply-withdrawal.fc#L446-L461)
248+
- [entry point for Pyth response: master.fc (op pyth_parse_price_feed_updates)](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/master.fc#L121)
249+
- [handle pyth response: liquidate_jetton and supply_withdraw_jetton](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/master.fc#L131-L167)
250+
251+
#### Pyth Connector flow
252+
253+
Pyth Connector's onchain-getter operation has a simplified flow compared to the EVAA protocol.
254+
255+
```mermaid
256+
sequenceDiagram
257+
autonumber
258+
participant U as User
259+
participant JW as Jetton Wallet
260+
participant M as Pyth Connector
261+
participant P as Pyth Contract
262+
263+
U->>JW: op transfer_jetton, forward_payload: (onchain_getter_operation)
264+
note right of U: transfer jetton with forward payload
265+
JW->>M: op transfer_notification, forward_payload: (onchain_getter_operation)
266+
note right of JW: transfer notification with forward payload
267+
M->>P: op 0x5 parse_price_feed_updates + update data + target feeds<br/>+ payload(onchain_getter_operation)
268+
P-->>M: op 0x5 parse_price_feed_updates(price feeds + prices)<br/>+ payload(onchain_getter_operation)
269+
note over P: Pyth Contract validates update data and <br/> sends prices with payload to Pyth Connector contract
270+
note over M: Pyth Connector validates sender <br/> parses payload and <br/> processes the transaction
271+
```
272+
273+
- Related code (GitHub):
274+
- [entry for jetton-transfer notification: pyth_connector.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/pyth_connector.fc#L109)
275+
- [onchain_getter_operation request: pyth-connector.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/pyth_connector.fc#L126)
276+
- [send request to the Pyth Contract: onchain_getter_operation.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/operations/onchain_getter_operation.fc#L71)
277+
- [entry for the Pyth response: pyth-connector.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/pyth_connector.fc#L78)
278+
- [onchain_getter_operation process: pyth-connector.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/pyth_connector.fc#L90)
279+
280+
### Pyth onchain-getter: Pyth error
281+
282+
Pyth sends an error response (`response_error`, op 0x10002) when it cannot process the price feed update request. This can happen if the request is malformed, contains invalid or outdated feed data, or if the requested feeds are unavailable. In such cases, the error response includes an error code and the original operation payload, allowing the original sender to handle the failure and refund the user if necessary.
283+
284+
#### EVAA flow
285+
286+
The error response is sent directly back to the user who initiated the transaction, not to a contract. This is different from the proxy case, where the error is returned to the EVAA Master contract for further handling and potential refund logic. In the onchain-getter case, the user receives the error response from the Pyth contract, including the error code and the original query ID, allowing the user to identify and handle the failure on their side.
287+
288+
```mermaid
289+
sequenceDiagram
290+
autonumber
291+
participant U as User
292+
participant JW as Jetton Wallet
293+
participant M as EVAA Master
294+
participant P as Pyth Contract
295+
296+
U->>JW: transfer with forward_payload
297+
JW->>M: transfer_notification
298+
M->>P: request (op 0x5 parse_price_feed_updates)
299+
P-->>M: response_error (op 0x10002)
300+
M-->>U: refund with error code
301+
```
302+
303+
- Related code (GitHub):
304+
- [entry point for the Pyth error message: master.fc (pyth_response_error)](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/master.fc#L92-L119)
305+
- [refund the liquidate jetton operation: master-liquidate.fc](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/core/master-liquidate.fc#L753-L786)
306+
- [refund the supply_withdraw_jetton operation: master-supply-withdrawal.fc](https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/core/master-supply-withdrawal.fc#L899-L935)
307+
308+
#### Pyth Connector
309+
310+
The Pyth Connector error handling flow looks the same as the EVAA protocol.
311+
312+
```mermaid
313+
sequenceDiagram
314+
autonumber
315+
participant U as User
316+
participant JW as Jetton Wallet
317+
participant M as Pyth Connector
318+
participant P as Pyth Contract
319+
320+
U->>JW: transfer with forward_payload
321+
JW->>M: transfer_notification
322+
M->>P: request (op 0x5 parse_price_feed_updates)
323+
P-->>M: response_error (op 0x10002)
324+
M-->>U: refund with error code
325+
```
326+
327+
- [entry point for the Pyth error message: pyth_connector.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/pyth_connector.fc#L31)
328+
- [validate sender and detect onchain_getter_operation: pyth_connector.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/pyth_connector.fc#L32-L43)
329+
- [handle the operation failure, refund jettons: onchain_getter_operation.fc](https://github.com/pyth-network/pyth-examples/blob/main/price_feeds/ton/pyth-connector/contracts/PythConnector/operations/onchain_getter_operation.fc#L21-L35)
330+
129331
## Additional Resources
130332

131333
You may find these additional resources helpful for developing your TON application:
@@ -136,3 +338,5 @@ You may find these additional resources helpful for developing your TON applicat
136338
- [Pyth TON SDK](https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ton/sdk)
137339
- [Pyth TON SDK Example](https://github.com/pyth-network/pyth-examples/tree/main/price_feeds/ton/sdk_js_usage)
138340
- [Pyth TON Send USD Example](https://github.com/pyth-network/pyth-examples/tree/main/price_feeds/ton/send_usd)
341+
- [Pyth Connector Example](https://github.com/pyth-network/pyth-examples/tree/main/price_feeds/ton/pyth-connector)
342+
- [EVAA Protocol Code](https://github.com/evaafi/contracts/tree/v8)

0 commit comments

Comments
 (0)