Skip to content

Commit 7b30554

Browse files
committed
fix(near): match Price interface from pyth-sdk
1 parent e6d04f9 commit 7b30554

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

target_chains/near/receiver/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl Pyth {
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) > age {
472+
if u64::abs_diff(block_timestamp, price_timestamp.into()) > age {
473473
return None;
474474
}
475475

@@ -500,7 +500,7 @@ impl Pyth {
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) > age {
503+
if u64::abs_diff(block_timestamp, price_timestamp.into()) > age {
504504
return None;
505505
}
506506

target_chains/near/receiver/src/state.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use {
55
BorshDeserialize,
66
BorshSerialize,
77
},
8+
json_types::{
9+
I64,
10+
U64,
11+
},
812
serde::{
913
Deserialize,
1014
Serialize,
@@ -84,13 +88,13 @@ impl near_sdk::serde::Serialize for PriceIdentifier {
8488
#[derive(BorshDeserialize, BorshSerialize, Debug, Deserialize, Serialize, PartialEq, Eq)]
8589
#[serde(crate = "near_sdk::serde")]
8690
pub struct Price {
87-
pub price: i64,
91+
pub price: I64,
8892
/// Confidence interval around the price
89-
pub conf: u64,
93+
pub conf: U64,
9094
/// The exponent
9195
pub expo: i32,
9296
/// Unix timestamp of when this price was computed
93-
pub timestamp: u64,
97+
pub timestamp: U64,
9498
}
9599

96100
/// The PriceFeed structure is stored in the contract under a Price Feed Identifier.
@@ -113,16 +117,20 @@ impl From<&PriceAttestation> for PriceFeed {
113117
Self {
114118
id: PriceIdentifier(price_attestation.price_id.to_bytes()),
115119
price: Price {
116-
price: price_attestation.price,
117-
conf: price_attestation.conf,
120+
price: price_attestation.price.into(),
121+
conf: price_attestation.conf.into(),
118122
expo: price_attestation.expo,
119-
timestamp: price_attestation.publish_time.try_into().unwrap(),
123+
timestamp: TryInto::<u64>::try_into(price_attestation.publish_time)
124+
.unwrap()
125+
.into(),
120126
},
121127
ema_price: Price {
122-
price: price_attestation.ema_price,
123-
conf: price_attestation.ema_conf,
128+
price: price_attestation.ema_price.into(),
129+
conf: price_attestation.ema_conf.into(),
124130
expo: price_attestation.expo,
125-
timestamp: price_attestation.publish_time.try_into().unwrap(),
131+
timestamp: TryInto::<u64>::try_into(price_attestation.publish_time)
132+
.unwrap()
133+
.into(),
126134
},
127135
}
128136
}
@@ -133,16 +141,20 @@ impl From<&PriceFeedMessage> for PriceFeed {
133141
Self {
134142
id: PriceIdentifier(price_feed_message.feed_id),
135143
price: Price {
136-
price: price_feed_message.price,
137-
conf: price_feed_message.conf,
144+
price: price_feed_message.price.into(),
145+
conf: price_feed_message.conf.into(),
138146
expo: price_feed_message.exponent,
139-
timestamp: price_feed_message.publish_time.try_into().unwrap(),
147+
timestamp: TryInto::<u64>::try_into(price_feed_message.publish_time)
148+
.unwrap()
149+
.into(),
140150
},
141151
ema_price: Price {
142-
price: price_feed_message.ema_price,
143-
conf: price_feed_message.ema_conf,
152+
price: price_feed_message.ema_price.into(),
153+
conf: price_feed_message.ema_conf.into(),
144154
expo: price_feed_message.exponent,
145-
timestamp: price_feed_message.publish_time.try_into().unwrap(),
155+
timestamp: TryInto::<u64>::try_into(price_feed_message.publish_time)
156+
.unwrap()
157+
.into(),
146158
},
147159
}
148160
}

target_chains/near/receiver/tests/workspaces.rs

Lines changed: 9 additions & 9 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,
504-
conf: 1,
503+
price: 100.into(),
504+
conf: 1.into(),
505505
expo: 8,
506-
timestamp: now,
506+
timestamp: now.into(),
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,
564-
conf: 1,
563+
price: 100.into(),
564+
conf: 1.into(),
565565
expo: 8,
566-
timestamp: now,
566+
timestamp: now.into(),
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,
1132-
conf: 100,
1131+
price: 100.into(),
1132+
conf: 100.into(),
11331133
expo: 100,
1134-
timestamp: 100,
1134+
timestamp: 100.into(),
11351135
}),
11361136
serde_json::from_slice::<Option<Price>>(
11371137
&contract

0 commit comments

Comments
 (0)