Selaa lähdekoodia

Use publish time instead of attest time (#194)

* Use publish time instead of attest time

This is for the logic of price freshness.
Ali Behjati 3 vuotta sitten
vanhempi
sitoutus
8635668417
1 muutettua tiedostoa jossa 33 lisäystä ja 8 poistoa
  1. 33 8
      terra/contracts/pyth-bridge/src/contract.rs

+ 33 - 8
terra/contracts/pyth-bridge/src/contract.rs

@@ -210,7 +210,9 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
 pub fn query_price_info(deps: Deps, env: Env, address: &[u8]) -> StdResult<PriceFeedResponse> {
     match price_info_read(deps.storage).load(address) {
         Ok(mut terra_price_info) => {
-            // Attestation time is very close to the actual price time (maybe a few seconds older).
+            let env_time_sec = env.block.time.seconds();
+            let price_pub_time_sec = terra_price_info.price_feed.publish_time as u64;
+
             // Cases that it will cover:
             // - This will ensure to set status unknown if the price has become very old and hasn't updated yet.
             // - If a price has arrived very late to terra it will set the status to unknown.
@@ -219,10 +221,10 @@ pub fn query_price_info(deps: Deps, env: Env, address: &[u8]) -> StdResult<Price
             //   problem in a either Terra or Solana blockchain and if it is Solana we don't want to propagate
             //   Solana internal problems to Terra
             let time_abs_diff =
-                if env.block.time.seconds() > terra_price_info.attestation_time.seconds() {
-                    env.block.time.seconds() - terra_price_info.attestation_time.seconds()
+                if env_time_sec > price_pub_time_sec {
+                    env_time_sec - price_pub_time_sec
                 } else {
-                    terra_price_info.attestation_time.seconds() - env.block.time.seconds()
+                    price_pub_time_sec - env_time_sec
                 };
 
             if time_abs_diff > VALID_TIME_PERIOD.as_secs() {
@@ -427,14 +429,14 @@ mod test {
         let address = b"123".as_ref();
 
         let mut dummy_price_info = PriceInfo::default();
-        dummy_price_info.attestation_time = Timestamp::from_seconds(80);
+        dummy_price_info.price_feed.publish_time = 80;
         dummy_price_info.price_feed.status = PriceStatus::Trading;
 
         price_info(&mut deps.storage)
             .save(address, &dummy_price_info)
             .unwrap();
 
-        env.block.time = Timestamp::from_seconds(100);
+        env.block.time = Timestamp::from_seconds(80 + VALID_TIME_PERIOD.as_secs());
 
         let price_feed = query_price_info(deps.as_ref(), env, address)
             .unwrap()
@@ -449,7 +451,7 @@ mod test {
         let address = b"123".as_ref();
 
         let mut dummy_price_info = PriceInfo::default();
-        dummy_price_info.attestation_time = Timestamp::from_seconds(500);
+        dummy_price_info.price_feed.publish_time = 500;
         dummy_price_info.price_feed.status = PriceStatus::Trading;
 
         price_info(&mut deps.storage)
@@ -465,6 +467,29 @@ mod test {
         assert_eq!(price_feed.status, PriceStatus::Unknown);
     }
 
+    #[test]
+    fn test_query_price_info_ok_trading_future() {
+        let (mut deps, mut env) = setup_test();
+
+        let address = b"123".as_ref();
+
+        let mut dummy_price_info = PriceInfo::default();
+        dummy_price_info.price_feed.publish_time = 500;
+        dummy_price_info.price_feed.status = PriceStatus::Trading;
+
+        price_info(&mut deps.storage)
+            .save(address, &dummy_price_info)
+            .unwrap();
+
+        env.block.time = Timestamp::from_seconds(500 - VALID_TIME_PERIOD.as_secs());
+
+        let price_feed = query_price_info(deps.as_ref(), env, address)
+            .unwrap()
+            .price_feed;
+
+        assert_eq!(price_feed.status, PriceStatus::Trading);
+    }
+
     #[test]
     fn test_query_price_info_ok_stale_future() {
         let (mut deps, mut env) = setup_test();
@@ -472,7 +497,7 @@ mod test {
         let address = b"123".as_ref();
 
         let mut dummy_price_info = PriceInfo::default();
-        dummy_price_info.attestation_time = Timestamp::from_seconds(500);
+        dummy_price_info.price_feed.publish_time = 500;
         dummy_price_info.price_feed.status = PriceStatus::Trading;
 
         price_info(&mut deps.storage)