Skip to content

Commit 5b7ada5

Browse files
committed
fix(near): sdk compat 2.0
1 parent bbd3d1a commit 5b7ada5

File tree

3 files changed

+98
-45
lines changed

3 files changed

+98
-45
lines changed

target_chains/near/receiver/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,12 @@ impl Pyth {
464464
pub fn get_price_no_older_than(&self, price_id: PriceIdentifier, age: u64) -> Option<Price> {
465465
self.prices.get(&price_id).and_then(|feed| {
466466
let block_timestamp = env::block_timestamp() / 1_000_000_000;
467-
let price_timestamp = feed.price.timestamp;
467+
let price_timestamp = feed.price.publish_time;
468468

469469
// - If Price older than STALENESS_THRESHOLD, set status to Unknown.
470470
// - If Price newer than now by more than STALENESS_THRESHOLD, set status to Unknown.
471471
// - Any other price around the current time is considered valid.
472-
if u64::abs_diff(block_timestamp, price_timestamp.into()) > age {
472+
if u64::abs_diff(block_timestamp, price_timestamp.try_into().unwrap()) > age {
473473
return None;
474474
}
475475

@@ -495,12 +495,12 @@ impl Pyth {
495495
) -> Option<Price> {
496496
self.prices.get(&price_id).and_then(|feed| {
497497
let block_timestamp = env::block_timestamp();
498-
let price_timestamp = feed.ema_price.timestamp;
498+
let price_timestamp = feed.ema_price.publish_time;
499499

500500
// - If Price older than STALENESS_THRESHOLD, set status to Unknown.
501501
// - If Price newer than now by more than STALENESS_THRESHOLD, set status to Unknown.
502502
// - Any other price around the current time is considered valid.
503-
if u64::abs_diff(block_timestamp, price_timestamp.into()) > age {
503+
if u64::abs_diff(block_timestamp, price_timestamp.try_into().unwrap()) > age {
504504
return None;
505505
}
506506

@@ -538,7 +538,7 @@ impl Pyth {
538538
fn update_price_feed_if_new(&mut self, price_feed: PriceFeed) -> bool {
539539
match self.prices.get(&price_feed.id) {
540540
Some(stored_price_feed) => {
541-
let update = price_feed.price.timestamp > stored_price_feed.price.timestamp;
541+
let update = price_feed.price.publish_time > stored_price_feed.price.publish_time;
542542
update.then(|| self.prices.insert(&price_feed.id, &price_feed));
543543
update
544544
}

target_chains/near/receiver/src/state.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ impl near_sdk::serde::Serialize for PriceIdentifier {
8888
#[derive(BorshDeserialize, BorshSerialize, Debug, Deserialize, Serialize, PartialEq, Eq)]
8989
#[serde(crate = "near_sdk::serde")]
9090
pub struct Price {
91-
pub price: I64,
91+
pub price: I64,
9292
/// Confidence interval around the price
93-
pub conf: U64,
93+
pub conf: U64,
9494
/// The exponent
95-
pub expo: i32,
95+
pub expo: i32,
9696
/// Unix timestamp of when this price was computed
97-
pub timestamp: U64,
97+
pub publish_time: i64,
9898
}
9999

100100
/// The PriceFeed structure is stored in the contract under a Price Feed Identifier.
@@ -117,20 +117,16 @@ impl From<&PriceAttestation> for PriceFeed {
117117
Self {
118118
id: PriceIdentifier(price_attestation.price_id.to_bytes()),
119119
price: Price {
120-
price: price_attestation.price.into(),
121-
conf: price_attestation.conf.into(),
122-
expo: price_attestation.expo,
123-
timestamp: TryInto::<u64>::try_into(price_attestation.publish_time)
124-
.unwrap()
125-
.into(),
120+
price: price_attestation.price.into(),
121+
conf: price_attestation.conf.into(),
122+
expo: price_attestation.expo,
123+
publish_time: price_attestation.publish_time,
126124
},
127125
ema_price: Price {
128-
price: price_attestation.ema_price.into(),
129-
conf: price_attestation.ema_conf.into(),
130-
expo: price_attestation.expo,
131-
timestamp: TryInto::<u64>::try_into(price_attestation.publish_time)
132-
.unwrap()
133-
.into(),
126+
price: price_attestation.ema_price.into(),
127+
conf: price_attestation.ema_conf.into(),
128+
expo: price_attestation.expo,
129+
publish_time: price_attestation.publish_time,
134130
},
135131
}
136132
}
@@ -141,20 +137,16 @@ impl From<&PriceFeedMessage> for PriceFeed {
141137
Self {
142138
id: PriceIdentifier(price_feed_message.feed_id),
143139
price: Price {
144-
price: price_feed_message.price.into(),
145-
conf: price_feed_message.conf.into(),
146-
expo: price_feed_message.exponent,
147-
timestamp: TryInto::<u64>::try_into(price_feed_message.publish_time)
148-
.unwrap()
149-
.into(),
140+
price: price_feed_message.price.into(),
141+
conf: price_feed_message.conf.into(),
142+
expo: price_feed_message.exponent,
143+
publish_time: price_feed_message.publish_time,
150144
},
151145
ema_price: Price {
152-
price: price_feed_message.ema_price.into(),
153-
conf: price_feed_message.ema_conf.into(),
154-
expo: price_feed_message.exponent,
155-
timestamp: TryInto::<u64>::try_into(price_feed_message.publish_time)
156-
.unwrap()
157-
.into(),
146+
price: price_feed_message.ema_price.into(),
147+
conf: price_feed_message.ema_conf.into(),
148+
expo: price_feed_message.exponent,
149+
publish_time: price_feed_message.publish_time,
158150
},
159151
}
160152
}

target_chains/near/receiver/tests/workspaces.rs

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,10 @@ async fn test_stale_threshold() {
500500
// timestamp and price should be unchanged.
501501
assert_eq!(
502502
Price {
503-
price: 100.into(),
504-
conf: 1.into(),
505-
expo: 8,
506-
timestamp: now.into(),
503+
price: 100.into(),
504+
conf: 1.into(),
505+
expo: 8,
506+
publish_time: now as i64,
507507
},
508508
serde_json::from_slice::<Price>(
509509
&contract
@@ -560,10 +560,10 @@ async fn test_stale_threshold() {
560560
// [ref:failed_price_check]
561561
assert_eq!(
562562
Some(Price {
563-
price: 100.into(),
564-
conf: 1.into(),
565-
expo: 8,
566-
timestamp: now.into(),
563+
price: 100.into(),
564+
conf: 1.into(),
565+
expo: 8,
566+
publish_time: now as i64,
567567
}),
568568
serde_json::from_slice::<Option<Price>>(
569569
&contract
@@ -1128,10 +1128,10 @@ async fn test_accumulator_updates() {
11281128

11291129
assert_eq!(
11301130
Some(Price {
1131-
price: 100.into(),
1132-
conf: 100.into(),
1133-
expo: 100,
1134-
timestamp: 100.into(),
1131+
price: 100.into(),
1132+
conf: 100.into(),
1133+
expo: 100,
1134+
publish_time: 100,
11351135
}),
11361136
serde_json::from_slice::<Option<Price>>(
11371137
&contract
@@ -1144,3 +1144,64 @@ async fn test_accumulator_updates() {
11441144
.unwrap(),
11451145
);
11461146
}
1147+
1148+
#[tokio::test]
1149+
async fn test_sdk_compat() {
1150+
let price = pyth_sdk::Price {
1151+
price: i64::MAX,
1152+
conf: u64::MAX,
1153+
expo: 100,
1154+
publish_time: 100,
1155+
};
1156+
1157+
let encoded = serde_json::to_string(&price).unwrap();
1158+
let decoded_price: Price = serde_json::from_str(&encoded).unwrap();
1159+
assert_eq!(
1160+
decoded_price,
1161+
Price {
1162+
price: i64::MAX.into(),
1163+
conf: u64::MAX.into(),
1164+
expo: 100,
1165+
publish_time: 100,
1166+
}
1167+
);
1168+
}
1169+
1170+
#[tokio::test]
1171+
async fn test_borsh_field_cmopat() {
1172+
use near_sdk::borsh::{
1173+
self,
1174+
BorshDeserialize,
1175+
BorshSerialize,
1176+
};
1177+
1178+
let price = pyth_sdk::Price {
1179+
price: i64::MAX,
1180+
conf: u64::MAX,
1181+
expo: 100,
1182+
publish_time: 100,
1183+
};
1184+
1185+
// Verify that we can still BorshDeserialize a struct with a different field name. Confirms
1186+
// we don't have to migrate the state.
1187+
#[derive(Eq, PartialEq, Debug, BorshSerialize, BorshDeserialize)]
1188+
struct PriceTester {
1189+
price: i64,
1190+
conf: u64,
1191+
expo: u32,
1192+
bad_field_name: u64,
1193+
}
1194+
1195+
let encoded = near_sdk::borsh::BorshSerialize::try_to_vec(&price).unwrap();
1196+
let decoded_price: PriceTester =
1197+
near_sdk::borsh::BorshDeserialize::try_from_slice(&encoded).unwrap();
1198+
assert_eq!(
1199+
decoded_price,
1200+
PriceTester {
1201+
price: i64::MAX.into(),
1202+
conf: u64::MAX.into(),
1203+
expo: 100,
1204+
bad_field_name: 100,
1205+
}
1206+
);
1207+
}

0 commit comments

Comments
 (0)