@@ -17,6 +17,7 @@ pub struct EdgeState {
1717 // TODO: it may be much better to store this centrally, so it's cheap to take a snapshot
1818 pub cached_prices : Vec < ( u64 , f64 , f64 ) > ,
1919 is_valid : bool ,
20+ is_dirty : bool ,
2021 pub last_update : u64 ,
2122 pub last_update_slot : u64 ,
2223
@@ -153,7 +154,7 @@ impl Edge {
153154 } )
154155 . collect_vec ( ) ;
155156
156- debug ! ( input_mint = %self . input_mint, pool = %self . key( ) , multiplier = multiplier, price = price, amounts = amounts. iter( ) . join( ";" ) , "price_data" ) ;
157+ trace ! ( input_mint = %self . input_mint, pool = %self . key( ) , multiplier = multiplier, price = price, amounts = amounts. iter( ) . join( ";" ) , "price_data" ) ;
157158
158159 let overflow = amounts. iter ( ) . any ( |x| * x == u64:: MAX ) ;
159160 if overflow {
@@ -166,6 +167,7 @@ impl Edge {
166167 state. last_update_slot = chain_data. newest_processed_slot ( ) ;
167168 state. cached_prices . clear ( ) ;
168169 state. is_valid = false ;
170+ state. is_dirty = false ;
169171 return ;
170172 }
171173
@@ -196,6 +198,7 @@ impl Edge {
196198 state. last_update_slot = chain_data. newest_processed_slot ( ) ;
197199 state. cached_prices . clear ( ) ;
198200 state. is_valid = true ;
201+ state. is_dirty = false ;
199202
200203 if let Some ( timestamp) = state. cooldown_until {
201204 if timestamp < state. last_update {
@@ -229,6 +232,26 @@ impl Edge {
229232 }
230233 }
231234
235+ pub fn mark_as_dirty ( & self ) {
236+ let mut state = self . state . write ( ) . unwrap ( ) ;
237+ state. is_dirty = true ;
238+ }
239+
240+ pub fn update_if_needed (
241+ & self ,
242+ chain_data : & AccountProviderView ,
243+ token_cache : & TokenCache ,
244+ price_cache : & PriceCache ,
245+ path_warming_amounts : & Vec < u64 > ,
246+ ) {
247+ if !self . state . read ( ) . unwrap ( ) . is_dirty {
248+ return ;
249+ }
250+
251+ debug ! ( "Lazily updating {}->{}" , debug_tools:: name( & self . input_mint) , debug_tools:: name( & self . output_mint) ) ;
252+ self . update ( chain_data, token_cache, price_cache, path_warming_amounts)
253+ }
254+
232255 pub fn update (
233256 & self ,
234257 chain_data : & AccountProviderView ,
@@ -259,7 +282,7 @@ impl EdgeState {
259282 /// Returns the price (in native/native) and ln(price) most applicable for the in amount
260283 /// Returns None if invalid
261284 pub fn cached_price_for ( & self , in_amount : u64 ) -> Option < ( f64 , f64 ) > {
262- if !self . is_valid ( ) || self . cached_prices . is_empty ( ) {
285+ if !self . is_valid ( ) || self . cached_prices . is_empty ( ) || self . is_dirty {
263286 return None ;
264287 }
265288
@@ -272,7 +295,7 @@ impl EdgeState {
272295 }
273296
274297 pub fn cached_price_exact_out_for ( & self , out_amount : u64 ) -> Option < ( f64 , f64 ) > {
275- if !self . is_valid_out ( ) {
298+ if !self . is_valid_out ( ) || self . cached_prices . is_empty ( ) || self . is_dirty {
276299 return None ;
277300 }
278301
0 commit comments