From fe0f7a7b6c6d327dcd3907178a8ef7a961a5c64f Mon Sep 17 00:00:00 2001 From: Ramesh Sachan <43104299+holps-7@users.noreply.github.com> Date: Tue, 18 Jun 2024 03:55:03 +0530 Subject: [PATCH 1/7] define parse_price_feed_updates --- target_chains/aptos/contracts/sources/pyth.move | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/target_chains/aptos/contracts/sources/pyth.move b/target_chains/aptos/contracts/sources/pyth.move index a831643061..7d80c612f2 100644 --- a/target_chains/aptos/contracts/sources/pyth.move +++ b/target_chains/aptos/contracts/sources/pyth.move @@ -520,6 +520,23 @@ module pyth::pyth { }; state::get_base_update_fee() * total_updates } + + public fun parse_price_feed_updates( + update_data: vector<vector<u8>>, + price_ids: vector<PriceIdentifier>, + min_publish_time: u64, + max_publish_time: u64, + fee: Coin<AptosCoin> + ): vector<ParsePriceFeed> { + parse_price_feed_updates_internal( + update_data, + price_ids, + min_publish_time, + max_publish_time, + fee + ) + } + } // ----------------------------------------------------------------------------- From dab1ffd156641571a49406dd3d819f1d507f7efb Mon Sep 17 00:00:00 2001 From: Ramesh Sachan <43104299+holps-7@users.noreply.github.com> Date: Tue, 18 Jun 2024 03:56:00 +0530 Subject: [PATCH 2/7] define struct ParsePriceFeed --- target_chains/aptos/contracts/sources/pyth.move | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/target_chains/aptos/contracts/sources/pyth.move b/target_chains/aptos/contracts/sources/pyth.move index 7d80c612f2..c4df794649 100644 --- a/target_chains/aptos/contracts/sources/pyth.move +++ b/target_chains/aptos/contracts/sources/pyth.move @@ -521,6 +521,14 @@ module pyth::pyth { state::get_base_update_fee() * total_updates } + struct ParsePriceFeed has copy, drop { + price_identifier: PriceIdentifier, + price: u64, + conf: u64, + expo: u64, + publish_time: u64 + } + public fun parse_price_feed_updates( update_data: vector<vector<u8>>, price_ids: vector<PriceIdentifier>, From 820a3f2e92cef1f40e331903a39fc18deb3e768c Mon Sep 17 00:00:00 2001 From: Ramesh Sachan <43104299+holps-7@users.noreply.github.com> Date: Tue, 18 Jun 2024 04:02:09 +0530 Subject: [PATCH 3/7] define parse_price_feed_updates_internal and check if gas fee is sufficient --- .../aptos/contracts/sources/pyth.move | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/target_chains/aptos/contracts/sources/pyth.move b/target_chains/aptos/contracts/sources/pyth.move index c4df794649..26c28e6fcd 100644 --- a/target_chains/aptos/contracts/sources/pyth.move +++ b/target_chains/aptos/contracts/sources/pyth.move @@ -529,6 +529,25 @@ module pyth::pyth { publish_time: u64 } + + fun parse_price_feed_updates_internal( + update_data: vector<vector<u8>>, + price_ids: vector<PriceIdentifier>, + min_publish_time: u64, + max_publish_time: u64, + fee: Coin<AptosCoin> + ): vector<ParsePriceFeed> { + //if fee provided is sufficient then proceed + //else give insufficient_fee error message + let required_fee = get_update_fee(&update_data); + assert!(coin::value(&fee) <= required_fee, error::insufficient_fee()); + + let price_feeds = vector::empty<ParsePriceFeed>(); + + coin::deposit(@pyth, fee); + price_feeds + } + public fun parse_price_feed_updates( update_data: vector<vector<u8>>, price_ids: vector<PriceIdentifier>, From 33f751087dc6b96df785ae92ac3a68d5b2c8b9da Mon Sep 17 00:00:00 2001 From: Ramesh Sachan <43104299+holps-7@users.noreply.github.com> Date: Tue, 18 Jun 2024 04:16:46 +0530 Subject: [PATCH 4/7] update fun parse_price_feed_updates_internal --- .../aptos/contracts/sources/pyth.move | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/target_chains/aptos/contracts/sources/pyth.move b/target_chains/aptos/contracts/sources/pyth.move index 26c28e6fcd..5bca5c2dfa 100644 --- a/target_chains/aptos/contracts/sources/pyth.move +++ b/target_chains/aptos/contracts/sources/pyth.move @@ -537,13 +537,36 @@ module pyth::pyth { max_publish_time: u64, fee: Coin<AptosCoin> ): vector<ParsePriceFeed> { - //if fee provided is sufficient then proceed - //else give insufficient_fee error message + // Check if the fee provided is sufficient + // If not, abort with an insufficient_fee error let required_fee = get_update_fee(&update_data); assert!(coin::value(&fee) <= required_fee, error::insufficient_fee()); + // Create an empty vector to store ParsePriceFeed structs let price_feeds = vector::empty<ParsePriceFeed>(); + let i = 0; + while (i < vector::length(&update_data)) { + let data = vector::borrow(&update_data, i); + let cur = cursor::init(*vector::borrow(&update_data, i)); + let header: u64 = deserialize::deserialize_u32(&mut cur); + + //Check if the provided data is valid (length > 4) && + //header matches the expected ACCUMULATOR_UPDATE_MAGIC value + if (vector::length(data) > 4 && header == PYTHNET_ACCUMULATOR_UPDATE_MAGIC) { + + let updates = parse_updates(*data, &price_ids, min_publish_time, max_publish_time); + let j = 0; + while (j < vector::length(&updates)) { + let update = vector::borrow(&updates, j); + vector::push_back(&mut updates, *update); + j = j + 1; + }; + }; + cursor::rest(cur); + i = i + 1; + }; + coin::deposit(@pyth, fee); price_feeds } From 8411e5574e5644ac9a45e2b6fdeb4cbcf2d134ea Mon Sep 17 00:00:00 2001 From: Ramesh Sachan <43104299+holps-7@users.noreply.github.com> Date: Tue, 18 Jun 2024 04:21:04 +0530 Subject: [PATCH 5/7] define fun parse_updates --- target_chains/aptos/contracts/sources/pyth.move | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/target_chains/aptos/contracts/sources/pyth.move b/target_chains/aptos/contracts/sources/pyth.move index 5bca5c2dfa..0112fee850 100644 --- a/target_chains/aptos/contracts/sources/pyth.move +++ b/target_chains/aptos/contracts/sources/pyth.move @@ -529,6 +529,17 @@ module pyth::pyth { publish_time: u64 } + fun parse_updates( + data: vector<u8>, + price_ids: &vector<PriceIdentifier>, + min_publish_time: u64, + max_publish_time: u64 + ): vector<ParsePriceFeed> { + // Create an empty vector to store ParsePriceFeed structs + let updates: vector<ParsePriceFeed> = vector::empty<ParsePriceFeed>(); + + updates + } fun parse_price_feed_updates_internal( update_data: vector<vector<u8>>, From c78c7854294fe5d8d0ea9dc1bb232821a7af01d2 Mon Sep 17 00:00:00 2001 From: Ramesh Sachan <43104299+holps-7@users.noreply.github.com> Date: Tue, 18 Jun 2024 04:27:14 +0530 Subject: [PATCH 6/7] update fun parse_updates --- .../aptos/contracts/sources/pyth.move | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/target_chains/aptos/contracts/sources/pyth.move b/target_chains/aptos/contracts/sources/pyth.move index 0112fee850..043c2948a9 100644 --- a/target_chains/aptos/contracts/sources/pyth.move +++ b/target_chains/aptos/contracts/sources/pyth.move @@ -538,6 +538,41 @@ module pyth::pyth { // Create an empty vector to store ParsePriceFeed structs let updates: vector<ParsePriceFeed> = vector::empty<ParsePriceFeed>(); + let i = 0; + while (i < vector::length(&data)) { + let message_cur = cursor::init(data); + let message_type = deserialize::deserialize_u8(&mut message_cur); + + // Deserialize the price identifier, price, conf, expo, publish_time, and previous_publish_time + let price_identifier = price_identifier::from_byte_vec(deserialize::deserialize_vector(&mut message_cur, 32)); + let price = deserialize::deserialize_u64(&mut message_cur); + let conf = deserialize::deserialize_u64(&mut message_cur); + let expo = deserialize::deserialize_u32(&mut message_cur); + let publish_time = deserialize::deserialize_u64(&mut message_cur); + let prev_publish_time = deserialize::deserialize_u64(&mut message_cur); + // let ema_price = deserialize::deserialize_i64(&mut message_cur); + // let ema_conf = deserialize::deserialize_u64(&mut message_cur); + + //check if caller has requested for this data + if (vector::contains(price_ids, &price_identifier)) { + // Check the publish_time of the price is within the given range + // and the min_publish_time is greater than the previous_publish_time + if (publish_time >= min_publish_time && publish_time <= max_publish_time && min_publish_time > prev_publish_time) { + let update = ParsePriceFeed { + price_identifier: price_identifier, + price: price, + conf: conf, + expo: expo, + publish_time: publish_time + }; + vector::push_back(&mut updates, update); + }; + + }; + cursor::rest(message_cur); + i = i + 1; + }; + updates } From 85850e4e5f95dee12b196dde8d460abeccd8b34a Mon Sep 17 00:00:00 2001 From: Ramesh Sachan <43104299+holps-7@users.noreply.github.com> Date: Tue, 18 Jun 2024 04:38:54 +0530 Subject: [PATCH 7/7] define code block comment --- target_chains/aptos/contracts/sources/pyth.move | 3 +++ 1 file changed, 3 insertions(+) diff --git a/target_chains/aptos/contracts/sources/pyth.move b/target_chains/aptos/contracts/sources/pyth.move index 043c2948a9..c971840a60 100644 --- a/target_chains/aptos/contracts/sources/pyth.move +++ b/target_chains/aptos/contracts/sources/pyth.move @@ -521,6 +521,9 @@ module pyth::pyth { state::get_base_update_fee() * total_updates } + /////////////////////////////////////////////////////// + //Parse Pyth Benchmark Prices + /////////////////////////////////////////////////////// struct ParsePriceFeed has copy, drop { price_identifier: PriceIdentifier, price: u64,