6
6
extern crate alloc;
7
7
8
8
mod structs;
9
+ mod error;
9
10
10
11
use alloc:: vec:: Vec ;
11
12
use stylus_sdk:: { alloy_primitives:: { U256 , U64 , I32 , I64 , FixedBytes } ,
12
13
prelude:: * ,
13
14
storage:: { StorageAddress , StorageVec , StorageMap , StorageUint , StorageBool , StorageU256 } } ;
14
15
15
16
use structs:: { DataSourceStorage , PriceInfoReturn , PriceInfoStorage } ;
17
+ use error:: { PythReceiverError } ;
16
18
17
19
#[ storage]
18
20
#[ entrypoint]
@@ -31,12 +33,27 @@ pub struct PythReceiver {
31
33
32
34
#[ public]
33
35
impl PythReceiver {
34
- pub fn get_price_unsafe ( & self , _id : [ u8 ; 32 ] ) -> PriceInfoReturn {
35
- ( U64 :: ZERO , I32 :: ZERO , I64 :: ZERO , U64 :: ZERO , I64 :: ZERO , U64 :: ZERO )
36
+ pub fn get_price_unsafe ( & self , _id : [ u8 ; 32 ] ) -> Result < PriceInfoReturn , PythReceiverError > {
37
+ let id_fb = FixedBytes :: < 32 > :: from ( _id) ;
38
+
39
+ let price_info = self . latest_price_info . get ( id_fb) . unwrap_or ( PythReceiverError :: PriceUnavailable ) ?;
40
+
41
+ Ok ( (
42
+ price_info. publish_time ,
43
+ price_info. expo ,
44
+ price_info. price ,
45
+ price_info. conf ,
46
+ price_info. ema_price ,
47
+ price_info. ema_conf ,
48
+ ) )
36
49
}
37
50
38
- pub fn get_price_no_older_than ( & self , _id : [ u8 ; 32 ] , _age : u64 ) -> PriceInfoReturn {
39
- ( U64 :: ZERO , I32 :: ZERO , I64 :: ZERO , U64 :: ZERO , I64 :: ZERO , U64 :: ZERO )
51
+ pub fn get_price_no_older_than ( & self , _id : [ u8 ; 32 ] , _age : u64 ) -> Result < PriceInfoReturn , PythReceiverError > {
52
+ let price_info = self . get_price_unsafe ( _id) ?;
53
+ if !self . is_no_older_than ( price_info. 0 , _age) {
54
+ return Err ( PythReceiverError :: PriceUnavailable ) ;
55
+ }
56
+ Ok ( price_info)
40
57
}
41
58
42
59
pub fn get_ema_price_unsafe ( & self , _id : [ u8 ; 32 ] ) -> PriceInfoReturn {
@@ -108,4 +125,11 @@ impl PythReceiver {
108
125
) -> Vec < PriceInfoReturn > {
109
126
Vec :: new ( )
110
127
}
128
+
129
+ fn is_no_older_than ( & self , publish_time : U64 , max_age : u64 ) -> bool {
130
+ let current_u64: u64 = self . vm ( ) . block_timestamp ( ) ;
131
+ let publish_time_u64: u64 = publish_time. to :: < u64 > ( ) ;
132
+
133
+ current_u64. saturating_sub ( publish_time_u64) <= max_age
134
+ }
111
135
}
0 commit comments