Bläddra i källkod

Abehjati/update p2w sdk to pyth sdk (#83)

* Make p2w-sdk use pyth-sdk
Ali Behjati 3 år sedan
förälder
incheckning
3199015c7d
3 ändrade filer med 43 tillägg och 30 borttagningar
  1. 21 8
      terra/Cargo.lock
  2. 1 1
      third_party/pyth/p2w-sdk/rust/Cargo.toml
  3. 21 21
      third_party/pyth/p2w-sdk/rust/src/lib.rs

+ 21 - 8
terra/Cargo.lock

@@ -1322,7 +1322,7 @@ checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
 name = "p2w-sdk"
 version = "0.1.0"
 dependencies = [
- "pyth-client",
+ "pyth-sdk-solana",
  "serde",
  "solana-program",
 ]
@@ -1445,16 +1445,29 @@ dependencies = [
 ]
 
 [[package]]
-name = "pyth-client"
-version = "0.5.0"
+name = "pyth-sdk"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "446ff07d7ef3bd98214f9b4fe6a611a69e36b5aad74b18cdbad5150193c1f204"
+dependencies = [
+ "borsh",
+ "borsh-derive",
+ "schemars",
+ "serde",
+]
+
+[[package]]
+name = "pyth-sdk-solana"
+version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f779e98b8c8016d0c1409247a204bd4fcdea8b67ceeef545f04e324d66c49e52"
+checksum = "27a648739aa69cab94edd900a0d7ca37d8a789e9c88741b23deec11fab418d16"
 dependencies = [
  "borsh",
  "borsh-derive",
  "bytemuck",
  "num-derive",
  "num-traits",
+ "pyth-sdk",
  "serde",
  "solana-program",
  "thiserror",
@@ -1691,9 +1704,9 @@ checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
 
 [[package]]
 name = "schemars"
-version = "0.8.7"
+version = "0.8.8"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "271ac0c667b8229adf70f0f957697c96fafd7486ab7481e15dc5e45e3e6a4368"
+checksum = "c6b5a3c80cea1ab61f4260238409510e814e38b4b563c06044edf91e7dc070e3"
 dependencies = [
  "dyn-clone",
  "schemars_derive",
@@ -1703,9 +1716,9 @@ dependencies = [
 
 [[package]]
 name = "schemars_derive"
-version = "0.8.7"
+version = "0.8.8"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "6ebda811090b257411540779860bc09bf321bc587f58d2c5864309d1566214e7"
+checksum = "41ae4dce13e8614c46ac3c38ef1c0d668b101df6ac39817aebdaa26642ddae9b"
 dependencies = [
  "proc-macro2",
  "quote",

+ 1 - 1
third_party/pyth/p2w-sdk/rust/Cargo.toml

@@ -15,7 +15,7 @@ wasm = ["wasm-bindgen", "solana"]
 
 [dependencies]
 serde = { version = "1.0.103", default-features = false, features = ["derive"] }
-pyth-client = { version = "0.5.0", features = ["no-entrypoint"] }
+pyth-sdk-solana = { version = "0.1.0" }
 wasm-bindgen = { version = "0.2.74", features = ["serde-serialize"], optional = true}
 solitaire = { path = "../../../../solana/solitaire/program", optional = true }
 solana-program = "1.8.16"

+ 21 - 21
third_party/pyth/p2w-sdk/rust/src/lib.rs

@@ -12,9 +12,9 @@ use std::io::Read;
 use std::iter::Iterator;
 use std::mem;
 
-use pyth_client::{
+use pyth_sdk_solana::state::{
     CorpAction,
-    Ema,
+    Rational,
     PriceStatus,
     PriceType,
 };
@@ -68,8 +68,8 @@ pub struct PriceAttestation {
     pub price_type:          PriceType,
     pub price:               i64,
     pub expo:                i32,
-    pub twap:                Ema,
-    pub twac:                Ema,
+    pub twap:                Rational,
+    pub twac:                Rational,
     pub confidence_interval: u64,
     pub status:              PriceStatus,
     pub corp_act:            CorpAction,
@@ -199,21 +199,21 @@ impl BatchPriceAttestation {
     }
 }
 
-pub fn serialize_ema(ema: &Ema) -> Vec<u8> {
+pub fn serialize_rational(rational: &Rational) -> Vec<u8> {
     let mut v = vec![];
     // val
-    v.extend(&ema.val.to_be_bytes()[..]);
+    v.extend(&rational.val.to_be_bytes()[..]);
 
     // numer
-    v.extend(&ema.numer.to_be_bytes()[..]);
+    v.extend(&rational.numer.to_be_bytes()[..]);
 
     // denom
-    v.extend(&ema.denom.to_be_bytes()[..]);
+    v.extend(&rational.denom.to_be_bytes()[..]);
 
     v
 }
 
-pub fn deserialize_ema(mut bytes: impl Read) -> Result<Ema, ErrBox> {
+pub fn deserialize_rational(mut bytes: impl Read) -> Result<Rational, ErrBox> {
     let mut val_vec = vec![0u8; mem::size_of::<i64>()];
     bytes.read_exact(val_vec.as_mut_slice())?;
     let val = i64::from_be_bytes(val_vec.as_slice().try_into()?);
@@ -226,7 +226,7 @@ pub fn deserialize_ema(mut bytes: impl Read) -> Result<Ema, ErrBox> {
     bytes.read_exact(denom_vec.as_mut_slice())?;
     let denom = i64::from_be_bytes(denom_vec.as_slice().try_into()?);
 
-    Ok(Ema { val, numer, denom })
+    Ok(Rational { val, numer, denom })
 }
 
 // On-chain data types
@@ -237,15 +237,15 @@ impl PriceAttestation {
         timestamp: UnixTimestamp,
         value: &[u8],
     ) -> Result<Self, ErrBox> {
-        let price = pyth_client::load_price(value)?;
+        let price = pyth_sdk_solana::state::load_price_account(value)?;
 
         Ok(PriceAttestation {
             product_id: Pubkey::new(&price.prod.val[..]),
             price_id,
             price_type: price.ptype,
             price: price.agg.price,
-            twap: price.twap,
-            twac: price.twac,
+            twap: price.ema_price,
+            twac: price.ema_conf,
             expo: price.expo,
             confidence_interval: price.agg.conf,
             status: price.agg.status,
@@ -297,10 +297,10 @@ impl PriceAttestation {
         buf.extend_from_slice(&expo.to_be_bytes()[..]);
 
         // twap
-        buf.append(&mut serialize_ema(&twap));
+        buf.append(&mut serialize_rational(&twap));
 
         // twac
-        buf.append(&mut serialize_ema(&twac));
+        buf.append(&mut serialize_rational(&twac));
 
         // confidence_interval
         buf.extend_from_slice(&confidence_interval.to_be_bytes()[..]);
@@ -379,8 +379,8 @@ impl PriceAttestation {
         bytes.read_exact(expo_vec.as_mut_slice())?;
         let expo = i32::from_be_bytes(expo_vec.as_slice().try_into()?);
 
-        let twap = deserialize_ema(&mut bytes)?;
-        let twac = deserialize_ema(&mut bytes)?;
+        let twap = deserialize_rational(&mut bytes)?;
+        let twac = deserialize_rational(&mut bytes)?;
 
         println!("twac OK");
         let mut confidence_interval_vec = vec![0u8; mem::size_of::<u64>()];
@@ -432,8 +432,8 @@ impl PriceAttestation {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use pyth_client::{
-        Ema,
+    use pyth_sdk_solana::state::{
+        Rational,
         PriceStatus,
         PriceType,
     };
@@ -446,12 +446,12 @@ mod tests {
             price_id:            Pubkey::new_from_array(price_id_bytes),
             price:               (0xdeadbeefdeadbabe as u64) as i64,
             price_type:          PriceType::Price,
-            twap:                Ema {
+            twap:                Rational {
                 val:   -42,
                 numer: 15,
                 denom: 37,
             },
-            twac:                Ema {
+            twac:                Rational {
                 val:   42,
                 numer: 1111,
                 denom: 2222,