|
| 1 | +#include "imports/stdlib.fc"; |
| 2 | +#include "common/errors.fc"; |
| 3 | +#include "common/storage.fc"; |
| 4 | +#include "common/op.fc"; |
| 5 | +#include "Wormhole.fc"; |
| 6 | +#include "Pyth.fc"; |
| 7 | + |
| 8 | +;; @title Pyth Network Price Oracle Contract for TON |
| 9 | +;; @notice This contract serves as the main entry point for the Pyth Network price oracle on TON. |
| 10 | +;; @dev The contract handles various operations including: |
| 11 | +;; - Updating guardian sets for Wormhole message verification |
| 12 | +;; - Updating price feeds with the latest price data |
| 13 | +;; - Executing governance actions |
| 14 | +;; - Upgrading the contract code |
| 15 | +;; - Parsing price feed updates for clients |
| 16 | +;; |
| 17 | +;; The contract uses Wormhole's cross-chain messaging protocol to verify price updates |
| 18 | +;; and governance actions. It maintains a dictionary of price feeds indexed by price ID. |
| 19 | +;; Each price feed contains the current price, confidence interval, exponent, and publish time. |
| 20 | + |
| 21 | +;; Internal message handler |
| 22 | +;; @param my_balance - Current contract balance |
| 23 | +;; @param msg_value - Amount of TON sent with the message |
| 24 | +;; @param in_msg_full - Full incoming message cell |
| 25 | +;; @param in_msg_body - Message body as a slice |
| 26 | +;; @returns () - No return value |
| 27 | +() recv_internal(int my_balance, int msg_value, cell in_msg_full, slice in_msg_body) impure { |
| 28 | + if (in_msg_body.slice_empty?()) { ;; ignore empty messages |
| 29 | + return (); |
| 30 | + } |
| 31 | + |
| 32 | + ;; * A 32-bit (big-endian) unsigned integer `op`, identifying the `operation` to be performed, or the `method` of the smart contract to be invoked. |
| 33 | + int op = in_msg_body~load_uint(32); |
| 34 | + cell data = in_msg_body~load_ref(); |
| 35 | + slice data_slice = data.begin_parse(); |
| 36 | + |
| 37 | + ;; Get sender address from message |
| 38 | + slice cs = in_msg_full.begin_parse(); |
| 39 | + int flags = cs~load_uint(4); |
| 40 | + if (flags & 1) { ;; ignore all bounced messages |
| 41 | + return (); |
| 42 | + } |
| 43 | + slice sender_address = cs~load_msg_addr(); ;; load sender address |
| 44 | + |
| 45 | + ;; * The remainder of the message body is specific for each supported value of `op`. |
| 46 | + if (op == OP_UPDATE_GUARDIAN_SET) { |
| 47 | + ;; @notice Updates the guardian set based on a Wormhole VAA |
| 48 | + ;; @param data_slice - Slice containing the VAA with guardian set update information |
| 49 | + update_guardian_set(data_slice); |
| 50 | + } elseif (op == OP_UPDATE_PRICE_FEEDS) { |
| 51 | + ;; @notice Updates price feeds with the latest price data |
| 52 | + ;; @param msg_value - Amount of TON sent with the message (used for fee calculation) |
| 53 | + ;; @param data_slice - Slice containing the price feed update data |
| 54 | + update_price_feeds(msg_value, data_slice); |
| 55 | + } elseif (op == OP_EXECUTE_GOVERNANCE_ACTION) { |
| 56 | + ;; @notice Executes a governance action based on a Wormhole VAA |
| 57 | + ;; @param data_slice - Slice containing the VAA with governance action information |
| 58 | + execute_governance_action(data_slice); |
| 59 | + } elseif (op == OP_UPGRADE_CONTRACT) { |
| 60 | + ;; @notice Upgrades the contract code |
| 61 | + ;; @param data - Cell containing the new contract code |
| 62 | + execute_upgrade_contract(data); |
| 63 | + } elseif (op == OP_PARSE_PRICE_FEED_UPDATES) { |
| 64 | + ;; @notice Parses price feed updates and returns the results to the caller |
| 65 | + ;; @param msg_value - Amount of TON sent with the message (used for fee calculation) |
| 66 | + ;; @param data_slice - Slice containing the price feed update data |
| 67 | + ;; @param price_ids_slice - Slice containing the price IDs to filter for |
| 68 | + ;; @param min_publish_time - Minimum publish time for price updates to be considered |
| 69 | + ;; @param max_publish_time - Maximum publish time for price updates to be considered |
| 70 | + ;; @param sender_address - Address of the sender (for response) |
| 71 | + ;; @param target_address - Address to send the response to |
| 72 | + ;; @param custom_payload - Custom payload to include in the response |
| 73 | + cell price_ids_cell = in_msg_body~load_ref(); |
| 74 | + slice price_ids_slice = price_ids_cell.begin_parse(); |
| 75 | + int min_publish_time = in_msg_body~load_uint(64); |
| 76 | + int max_publish_time = in_msg_body~load_uint(64); |
| 77 | + slice target_address = in_msg_body~load_msg_addr(); |
| 78 | + cell custom_payload_cell = in_msg_body~load_ref(); |
| 79 | + slice custom_payload = custom_payload_cell.begin_parse(); |
| 80 | + parse_price_feed_updates(msg_value, data_slice, price_ids_slice, min_publish_time, max_publish_time, sender_address, target_address, custom_payload); |
| 81 | + } elseif (op == OP_PARSE_UNIQUE_PRICE_FEED_UPDATES) { |
| 82 | + ;; @notice Parses unique price feed updates (only the latest for each price ID) and returns the results to the caller |
| 83 | + ;; @param msg_val; @notice Parses unique price feed updates (only the latest for each price ID) and returns the results to the caller |
| 84 | + ;; @param msg_value - Amount of TON sent with the message (used for fee calculation) |
| 85 | + ;; @param data_slice - Slice containing the price feed update data |
| 86 | + ;; @param price_ids_slice - Slice containing the price IDs to filter for |
| 87 | + ;; @param publish_time - Target publish time for price updates |
| 88 | + ;; @param max_staleness - Maximum allowed staleness of price updates (in seconds) |
| 89 | + ;; @param sender_address - Address of the sender (for response) |
| 90 | + ;; @param target_address - Address to send the response to |
| 91 | + ;; @param custom_payload - Custom payload to include in the response |
| 92 | + cell price_ids_cell = in_msg_body~load_ref(); |
| 93 | + slice price_ids_slice = price_ids_cell.begin_parse(); |
| 94 | + int publish_time = in_msg_body~load_uint(64); |
| 95 | + int max_staleness = in_msg_body~load_uint(64); |
| 96 | + slice target_address = in_msg_body~load_msg_addr(); |
| 97 | + cell custom_payload_cell = in_msg_body~load_ref(); |
| 98 | + slice custom_payload = custom_payload_cell.begin_parse(); |
| 99 | + parse_unique_price_feed_updates(msg_value, data_slice, price_ids_slice, publish_time, max_staleness, sender_address, target_address, custom_payload); |
| 100 | + } else { |
| 101 | + throw(0xffff); ;; Throw exception for unknown op |
| 102 | + } |
| 103 | +} |
0 commit comments