fix(near): sdk compat 2.0
This commit is contained in:
parent
bbd3d1add3
commit
5b7ada59bd
|
@ -464,12 +464,12 @@ impl Pyth {
|
||||||
pub fn get_price_no_older_than(&self, price_id: PriceIdentifier, age: u64) -> Option<Price> {
|
pub fn get_price_no_older_than(&self, price_id: PriceIdentifier, age: u64) -> Option<Price> {
|
||||||
self.prices.get(&price_id).and_then(|feed| {
|
self.prices.get(&price_id).and_then(|feed| {
|
||||||
let block_timestamp = env::block_timestamp() / 1_000_000_000;
|
let block_timestamp = env::block_timestamp() / 1_000_000_000;
|
||||||
let price_timestamp = feed.price.timestamp;
|
let price_timestamp = feed.price.publish_time;
|
||||||
|
|
||||||
// - If Price older than STALENESS_THRESHOLD, set status to Unknown.
|
// - If Price older than STALENESS_THRESHOLD, set status to Unknown.
|
||||||
// - If Price newer than now by more than STALENESS_THRESHOLD, set status to Unknown.
|
// - If Price newer than now by more than STALENESS_THRESHOLD, set status to Unknown.
|
||||||
// - Any other price around the current time is considered valid.
|
// - Any other price around the current time is considered valid.
|
||||||
if u64::abs_diff(block_timestamp, price_timestamp.into()) > age {
|
if u64::abs_diff(block_timestamp, price_timestamp.try_into().unwrap()) > age {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -495,12 +495,12 @@ impl Pyth {
|
||||||
) -> Option<Price> {
|
) -> Option<Price> {
|
||||||
self.prices.get(&price_id).and_then(|feed| {
|
self.prices.get(&price_id).and_then(|feed| {
|
||||||
let block_timestamp = env::block_timestamp();
|
let block_timestamp = env::block_timestamp();
|
||||||
let price_timestamp = feed.ema_price.timestamp;
|
let price_timestamp = feed.ema_price.publish_time;
|
||||||
|
|
||||||
// - If Price older than STALENESS_THRESHOLD, set status to Unknown.
|
// - If Price older than STALENESS_THRESHOLD, set status to Unknown.
|
||||||
// - If Price newer than now by more than STALENESS_THRESHOLD, set status to Unknown.
|
// - If Price newer than now by more than STALENESS_THRESHOLD, set status to Unknown.
|
||||||
// - Any other price around the current time is considered valid.
|
// - Any other price around the current time is considered valid.
|
||||||
if u64::abs_diff(block_timestamp, price_timestamp.into()) > age {
|
if u64::abs_diff(block_timestamp, price_timestamp.try_into().unwrap()) > age {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -538,7 +538,7 @@ impl Pyth {
|
||||||
fn update_price_feed_if_new(&mut self, price_feed: PriceFeed) -> bool {
|
fn update_price_feed_if_new(&mut self, price_feed: PriceFeed) -> bool {
|
||||||
match self.prices.get(&price_feed.id) {
|
match self.prices.get(&price_feed.id) {
|
||||||
Some(stored_price_feed) => {
|
Some(stored_price_feed) => {
|
||||||
let update = price_feed.price.timestamp > stored_price_feed.price.timestamp;
|
let update = price_feed.price.publish_time > stored_price_feed.price.publish_time;
|
||||||
update.then(|| self.prices.insert(&price_feed.id, &price_feed));
|
update.then(|| self.prices.insert(&price_feed.id, &price_feed));
|
||||||
update
|
update
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,7 +94,7 @@ pub struct Price {
|
||||||
/// The exponent
|
/// The exponent
|
||||||
pub expo: i32,
|
pub expo: i32,
|
||||||
/// Unix timestamp of when this price was computed
|
/// Unix timestamp of when this price was computed
|
||||||
pub timestamp: U64,
|
pub publish_time: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The PriceFeed structure is stored in the contract under a Price Feed Identifier.
|
/// The PriceFeed structure is stored in the contract under a Price Feed Identifier.
|
||||||
|
@ -120,17 +120,13 @@ impl From<&PriceAttestation> for PriceFeed {
|
||||||
price: price_attestation.price.into(),
|
price: price_attestation.price.into(),
|
||||||
conf: price_attestation.conf.into(),
|
conf: price_attestation.conf.into(),
|
||||||
expo: price_attestation.expo,
|
expo: price_attestation.expo,
|
||||||
timestamp: TryInto::<u64>::try_into(price_attestation.publish_time)
|
publish_time: price_attestation.publish_time,
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
},
|
},
|
||||||
ema_price: Price {
|
ema_price: Price {
|
||||||
price: price_attestation.ema_price.into(),
|
price: price_attestation.ema_price.into(),
|
||||||
conf: price_attestation.ema_conf.into(),
|
conf: price_attestation.ema_conf.into(),
|
||||||
expo: price_attestation.expo,
|
expo: price_attestation.expo,
|
||||||
timestamp: TryInto::<u64>::try_into(price_attestation.publish_time)
|
publish_time: price_attestation.publish_time,
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -144,17 +140,13 @@ impl From<&PriceFeedMessage> for PriceFeed {
|
||||||
price: price_feed_message.price.into(),
|
price: price_feed_message.price.into(),
|
||||||
conf: price_feed_message.conf.into(),
|
conf: price_feed_message.conf.into(),
|
||||||
expo: price_feed_message.exponent,
|
expo: price_feed_message.exponent,
|
||||||
timestamp: TryInto::<u64>::try_into(price_feed_message.publish_time)
|
publish_time: price_feed_message.publish_time,
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
},
|
},
|
||||||
ema_price: Price {
|
ema_price: Price {
|
||||||
price: price_feed_message.ema_price.into(),
|
price: price_feed_message.ema_price.into(),
|
||||||
conf: price_feed_message.ema_conf.into(),
|
conf: price_feed_message.ema_conf.into(),
|
||||||
expo: price_feed_message.exponent,
|
expo: price_feed_message.exponent,
|
||||||
timestamp: TryInto::<u64>::try_into(price_feed_message.publish_time)
|
publish_time: price_feed_message.publish_time,
|
||||||
.unwrap()
|
|
||||||
.into(),
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -503,7 +503,7 @@ async fn test_stale_threshold() {
|
||||||
price: 100.into(),
|
price: 100.into(),
|
||||||
conf: 1.into(),
|
conf: 1.into(),
|
||||||
expo: 8,
|
expo: 8,
|
||||||
timestamp: now.into(),
|
publish_time: now as i64,
|
||||||
},
|
},
|
||||||
serde_json::from_slice::<Price>(
|
serde_json::from_slice::<Price>(
|
||||||
&contract
|
&contract
|
||||||
|
@ -563,7 +563,7 @@ async fn test_stale_threshold() {
|
||||||
price: 100.into(),
|
price: 100.into(),
|
||||||
conf: 1.into(),
|
conf: 1.into(),
|
||||||
expo: 8,
|
expo: 8,
|
||||||
timestamp: now.into(),
|
publish_time: now as i64,
|
||||||
}),
|
}),
|
||||||
serde_json::from_slice::<Option<Price>>(
|
serde_json::from_slice::<Option<Price>>(
|
||||||
&contract
|
&contract
|
||||||
|
@ -1131,7 +1131,7 @@ async fn test_accumulator_updates() {
|
||||||
price: 100.into(),
|
price: 100.into(),
|
||||||
conf: 100.into(),
|
conf: 100.into(),
|
||||||
expo: 100,
|
expo: 100,
|
||||||
timestamp: 100.into(),
|
publish_time: 100,
|
||||||
}),
|
}),
|
||||||
serde_json::from_slice::<Option<Price>>(
|
serde_json::from_slice::<Option<Price>>(
|
||||||
&contract
|
&contract
|
||||||
|
@ -1144,3 +1144,64 @@ async fn test_accumulator_updates() {
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_sdk_compat() {
|
||||||
|
let price = pyth_sdk::Price {
|
||||||
|
price: i64::MAX,
|
||||||
|
conf: u64::MAX,
|
||||||
|
expo: 100,
|
||||||
|
publish_time: 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
let encoded = serde_json::to_string(&price).unwrap();
|
||||||
|
let decoded_price: Price = serde_json::from_str(&encoded).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
decoded_price,
|
||||||
|
Price {
|
||||||
|
price: i64::MAX.into(),
|
||||||
|
conf: u64::MAX.into(),
|
||||||
|
expo: 100,
|
||||||
|
publish_time: 100,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_borsh_field_cmopat() {
|
||||||
|
use near_sdk::borsh::{
|
||||||
|
self,
|
||||||
|
BorshDeserialize,
|
||||||
|
BorshSerialize,
|
||||||
|
};
|
||||||
|
|
||||||
|
let price = pyth_sdk::Price {
|
||||||
|
price: i64::MAX,
|
||||||
|
conf: u64::MAX,
|
||||||
|
expo: 100,
|
||||||
|
publish_time: 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Verify that we can still BorshDeserialize a struct with a different field name. Confirms
|
||||||
|
// we don't have to migrate the state.
|
||||||
|
#[derive(Eq, PartialEq, Debug, BorshSerialize, BorshDeserialize)]
|
||||||
|
struct PriceTester {
|
||||||
|
price: i64,
|
||||||
|
conf: u64,
|
||||||
|
expo: u32,
|
||||||
|
bad_field_name: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
let encoded = near_sdk::borsh::BorshSerialize::try_to_vec(&price).unwrap();
|
||||||
|
let decoded_price: PriceTester =
|
||||||
|
near_sdk::borsh::BorshDeserialize::try_from_slice(&encoded).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
decoded_price,
|
||||||
|
PriceTester {
|
||||||
|
price: i64::MAX.into(),
|
||||||
|
conf: u64::MAX.into(),
|
||||||
|
expo: 100,
|
||||||
|
bad_field_name: 100,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue