4
4
Key ,
5
5
StorageData ,
6
6
} ,
7
- PriceFeedsWithUpdateData ,
8
7
RequestTime ,
9
8
State ,
10
9
UnixTimestamp ,
11
- UpdateData ,
12
10
} ,
13
11
anyhow:: {
14
12
anyhow,
@@ -24,38 +22,59 @@ use {
24
22
PriceAttestation ,
25
23
PriceStatus ,
26
24
} ,
27
- std:: collections:: {
28
- HashMap ,
29
- HashSet ,
25
+ std:: {
26
+ collections:: {
27
+ HashMap ,
28
+ HashSet ,
29
+ } ,
30
+ time:: {
31
+ SystemTime ,
32
+ UNIX_EPOCH ,
33
+ } ,
30
34
} ,
31
35
wormhole:: VAA ,
32
36
} ;
33
37
34
38
// TODO: We need to add more metadata to this struct.
35
39
#[ derive( Clone , Default , PartialEq , Debug ) ]
36
40
pub struct PriceInfo {
37
- pub price_feed : PriceFeed ,
38
- pub vaa_bytes : Vec < u8 > ,
39
- pub publish_time : UnixTimestamp ,
41
+ pub price_feed : PriceFeed ,
42
+ pub vaa_bytes : Vec < u8 > ,
43
+ pub publish_time : UnixTimestamp ,
44
+ pub emitter_chain : u16 ,
45
+ pub attestation_time : UnixTimestamp ,
46
+ pub receive_time : UnixTimestamp ,
47
+ pub sequence_number : u64 ,
40
48
}
41
49
50
+ #[ derive( Clone , Default ) ]
51
+ pub struct PriceInfosWithUpdateData {
52
+ pub price_infos : HashMap < PriceIdentifier , PriceInfo > ,
53
+ pub update_data : Vec < Vec < u8 > > ,
54
+ }
42
55
43
56
pub fn store_vaa_update ( state : State , vaa_bytes : Vec < u8 > ) -> Result < ( ) > {
44
57
// FIXME: Vaa bytes might not be a valid Pyth BatchUpdate message nor originate from Our emitter.
45
58
// We should check that.
59
+ // FIXME: We receive multiple vaas for the same update (due to different signedVAAs). We need
60
+ // to drop them.
46
61
let vaa = VAA :: from_bytes ( & vaa_bytes) ?;
47
62
let batch_price_attestation = BatchPriceAttestation :: deserialize ( vaa. payload . as_slice ( ) )
48
63
. map_err ( |_| anyhow ! ( "Failed to deserialize VAA" ) ) ?;
49
64
50
65
for price_attestation in batch_price_attestation. price_attestations {
51
- let price_feed = price_attestation_to_price_feed ( price_attestation) ;
66
+ let price_feed = price_attestation_to_price_feed ( price_attestation. clone ( ) ) ;
52
67
53
68
let publish_time = price_feed. get_price_unchecked ( ) . publish_time . try_into ( ) ?;
54
69
55
70
let price_info = PriceInfo {
56
71
price_feed,
57
72
vaa_bytes : vaa_bytes. clone ( ) ,
58
73
publish_time,
74
+ emitter_chain : vaa. emitter_chain . into ( ) ,
75
+ attestation_time : price_attestation. attestation_time . try_into ( ) ?,
76
+ receive_time : SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) ?. as_secs ( ) ,
77
+ sequence_number : vaa. sequence ,
59
78
} ;
60
79
61
80
let key = Key :: BatchVaa ( price_feed. id ) ;
@@ -65,32 +84,30 @@ pub fn store_vaa_update(state: State, vaa_bytes: Vec<u8>) -> Result<()> {
65
84
}
66
85
67
86
68
- pub fn get_price_feeds_with_update_data (
87
+ pub fn get_price_infos_with_update_data (
69
88
state : State ,
70
89
price_ids : Vec < PriceIdentifier > ,
71
90
request_time : RequestTime ,
72
- ) -> Result < PriceFeedsWithUpdateData > {
73
- let mut price_feeds = HashMap :: new ( ) ;
91
+ ) -> Result < PriceInfosWithUpdateData > {
92
+ let mut price_infos = HashMap :: new ( ) ;
74
93
let mut vaas: HashSet < Vec < u8 > > = HashSet :: new ( ) ;
75
94
for price_id in price_ids {
76
95
let key = Key :: BatchVaa ( price_id) ;
77
96
let maybe_data = state. get ( key, request_time. clone ( ) ) ?;
78
97
79
98
match maybe_data {
80
99
Some ( StorageData :: BatchVaa ( price_info) ) => {
81
- price_feeds . insert ( price_info. price_feed . id , price_info . price_feed ) ;
82
- vaas . insert ( price_info. vaa_bytes ) ;
100
+ vaas . insert ( price_info. vaa_bytes . clone ( ) ) ;
101
+ price_infos . insert ( price_id , price_info) ;
83
102
}
84
103
None => {
85
104
return Err ( anyhow ! ( "No price feed found for price id: {:?}" , price_id) ) ;
86
105
}
87
106
}
88
107
}
89
- let update_data = UpdateData {
90
- batch_vaa : vaas. into_iter ( ) . collect ( ) ,
91
- } ;
92
- Ok ( PriceFeedsWithUpdateData {
93
- price_feeds,
108
+ let update_data: Vec < Vec < u8 > > = vaas. into_iter ( ) . collect ( ) ;
109
+ Ok ( PriceInfosWithUpdateData {
110
+ price_infos,
94
111
update_data,
95
112
} )
96
113
}
0 commit comments