-
Notifications
You must be signed in to change notification settings - Fork 117
refactor: move upd_twap from upd_aggregate to rust upd_price #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,14 +134,6 @@ static inline void upd_twap( | |
| // update aggregate price | ||
| static inline bool upd_aggregate( pc_price_t *ptr, uint64_t slot, int64_t timestamp ) | ||
| { | ||
| // only re-compute aggregate in next slot | ||
| if ( slot <= ptr->agg_.pub_slot_ ) { | ||
| return false; | ||
| } | ||
|
|
||
| // get number of slots from last published valid price | ||
| int64_t agg_diff = ( int64_t )slot - ( int64_t )( ptr->last_slot_ ); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. something to watch out for going forward is the if statement below this comment which updates the previous price. There's a chance that we introduce a bug that updates this multiple times on a single slot, which would make prev_slot == agg_.pub_slot_ |
||
| // Update the value of the previous price, if it had TRADING status. | ||
| if ( ptr->agg_.status_ == PC_STATUS_TRADING ) { | ||
| ptr->prev_slot_ = ptr->agg_.pub_slot_; | ||
|
|
@@ -224,7 +216,6 @@ static inline bool upd_aggregate( pc_price_t *ptr, uint64_t slot, int64_t timest | |
| ptr->agg_.price_ = agg_price; | ||
| ptr->agg_.conf_ = (uint64_t)agg_conf; | ||
|
|
||
| upd_twap( ptr, agg_diff ); | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -179,6 +179,12 @@ pub fn upd_price( | |
| #[allow(unused_variables)] | ||
| let mut aggregate_updated = false; | ||
|
|
||
| let agg_diff = { | ||
| (clock.slot as i64) | ||
| - load_checked::<PriceAccount>(price_account, cmd_args.header.version)?.last_slot_ | ||
cctdaniel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| as i64 | ||
| }; | ||
|
|
||
|
||
| // NOTE: c_upd_aggregate must use a raw pointer to price | ||
| // data. Solana's `<account>.borrow_*` methods require exclusive | ||
| // access, i.e. no other borrow can exist for the account. | ||
|
|
@@ -190,6 +196,10 @@ pub fn upd_price( | |
| clock.slot, | ||
| clock.unix_timestamp, | ||
| ); | ||
| c_upd_twap( | ||
| price_account.try_borrow_mut_data()?.as_mut_ptr(), | ||
| agg_diff, // Ensure slots_since_last_update is cast to i64, as expected by the function signature | ||
cctdaniel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,8 +100,6 @@ fn test_upd_aggregate() { | |
|
|
||
| assert_eq!(price_data.agg_.price_, 100); | ||
| assert_eq!(price_data.agg_.conf_, 10); | ||
| assert_eq!(price_data.twap_.val_, 100); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe better to keep the asserts and run
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm we already have a |
||
| assert_eq!(price_data.twac_.val_, 10); | ||
| assert_eq!(price_data.num_qt_, 1); | ||
| assert_eq!(price_data.timestamp_, 1); | ||
| assert_eq!(price_data.prev_slot_, 0); | ||
|
|
@@ -134,8 +132,6 @@ fn test_upd_aggregate() { | |
|
|
||
| assert_eq!(price_data.agg_.price_, 145); | ||
| assert_eq!(price_data.agg_.conf_, 55); | ||
| assert_eq!(price_data.twap_.val_, 106); | ||
| assert_eq!(price_data.twac_.val_, 16); | ||
| assert_eq!(price_data.num_qt_, 2); | ||
| assert_eq!(price_data.timestamp_, 2); | ||
| assert_eq!(price_data.prev_slot_, 1000); | ||
|
|
@@ -169,8 +165,6 @@ fn test_upd_aggregate() { | |
|
|
||
| assert_eq!(price_data.agg_.price_, 200); | ||
| assert_eq!(price_data.agg_.conf_, 90); | ||
| assert_eq!(price_data.twap_.val_, 114); | ||
| assert_eq!(price_data.twac_.val_, 23); | ||
| assert_eq!(price_data.num_qt_, 3); | ||
| assert_eq!(price_data.timestamp_, 3); | ||
| assert_eq!(price_data.prev_slot_, 1000); | ||
|
|
@@ -205,8 +199,6 @@ fn test_upd_aggregate() { | |
|
|
||
| assert_eq!(price_data.agg_.price_, 245); | ||
| assert_eq!(price_data.agg_.conf_, 85); | ||
| assert_eq!(price_data.twap_.val_, 125); | ||
| assert_eq!(price_data.twac_.val_, 28); | ||
| assert_eq!(price_data.num_qt_, 4); | ||
| assert_eq!(price_data.timestamp_, 4); | ||
| assert_eq!(price_data.last_slot_, 1001); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.