Skip to content

Commit f98d7cd

Browse files
committed
fix inequality, add remaining tests
1 parent 95a1991 commit f98d7cd

File tree

1 file changed

+98
-2
lines changed

1 file changed

+98
-2
lines changed

pyth-sdk-solana/src/state.rs

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl PriceAccount {
369369
})
370370
}
371371

372-
if self.prev_slot > clock.slot - slot_threshold {
372+
if self.prev_slot >= clock.slot - slot_threshold {
373373
return Some(Price {
374374
conf: self.prev_conf,
375375
expo: self.expo,
@@ -614,7 +614,7 @@ mod test {
614614
}
615615

616616
#[test]
617-
fn test_happy_price_no_older_than() {
617+
fn test_happy_use_latest_price_in_price_no_older_than() {
618618
let price_account = PriceAccount {
619619
expo: 5,
620620
agg: PriceInfo {
@@ -646,4 +646,100 @@ mod test {
646646
})
647647
);
648648
}
649+
650+
#[test]
651+
fn test_happy_use_prev_price_in_price_no_older_than() {
652+
let price_account = PriceAccount {
653+
expo: 5,
654+
agg: PriceInfo {
655+
price: 10,
656+
conf: 20,
657+
status: PriceStatus::Unknown,
658+
pub_slot: 3,
659+
..Default::default()
660+
},
661+
timestamp: 200,
662+
prev_timestamp: 100,
663+
prev_price: 60,
664+
prev_conf: 70,
665+
prev_slot: 1,
666+
..Default::default()
667+
};
668+
669+
let clock = Clock {
670+
slot: 5,
671+
..Default::default()
672+
};
673+
674+
assert_eq!(
675+
price_account.get_price_no_older_than(&clock, 4),
676+
Some(Price {
677+
conf: 70,
678+
expo: 5,
679+
price: 60,
680+
publish_time: 100,
681+
})
682+
);
683+
}
684+
685+
#[test]
686+
fn test_sad_cur_price_unknown_in_price_no_older_than() {
687+
let price_account = PriceAccount {
688+
expo: 5,
689+
agg: PriceInfo {
690+
price: 10,
691+
conf: 20,
692+
status: PriceStatus::Unknown,
693+
pub_slot: 3,
694+
..Default::default()
695+
},
696+
timestamp: 200,
697+
prev_timestamp: 100,
698+
prev_price: 60,
699+
prev_conf: 70,
700+
prev_slot: 1,
701+
..Default::default()
702+
};
703+
704+
let clock = Clock {
705+
slot: 5,
706+
..Default::default()
707+
};
708+
709+
// current price is unknown, prev price is too stale
710+
assert_eq!(
711+
price_account.get_price_no_older_than(&clock, 3),
712+
None
713+
);
714+
}
715+
716+
#[test]
717+
fn test_sad_cur_price_stale_in_price_no_older_than() {
718+
let price_account = PriceAccount {
719+
expo: 5,
720+
agg: PriceInfo {
721+
price: 10,
722+
conf: 20,
723+
status: PriceStatus::Unknown,
724+
pub_slot: 3,
725+
..Default::default()
726+
},
727+
timestamp: 200,
728+
prev_timestamp: 100,
729+
prev_price: 60,
730+
prev_conf: 70,
731+
prev_slot: 1,
732+
..Default::default()
733+
};
734+
735+
let clock = Clock {
736+
slot: 5,
737+
..Default::default()
738+
};
739+
740+
assert_eq!(
741+
price_account.get_price_no_older_than(&clock, 1),
742+
None
743+
);
744+
}
649745
}

0 commit comments

Comments
 (0)