Skip to content
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

dex: Generate candlestick data and provide RPCs for access #4398

Merged
merged 27 commits into from
May 24, 2024

Conversation

hdevalence
Copy link
Member

@hdevalence hdevalence commented May 15, 2024

Describe your changes

This adds support for tracking candlestick data to the dex component via a new Chandelier trait.

  • Whenever a position execution or swap execution occurs, methods in the Chandelier are called which will extract and accumulate data into an ordered Vec stored in the object store
  • at the end of the block, the chandelier collapses the block's collected data points into a CandlestickData and saves it in non-verifiable storage under the key dex/candlesticks/{pair}/{height:020} so that they can be iterated over in order
  • Two RPCs have been added, one for retrieving non-streaming historical data, and another which allows you to watch realtime candlestick data for a given trading pair

Issue ticket number and link

#4399

Checklist before requesting a review

  • If this code contains consensus-breaking changes, I have added the "consensus-breaking" label. Otherwise, I declare my belief that there are not consensus-breaking changes, for the following reason:

    Only affects non-verifiable storage

let mut direct_volume = 0.0;

// Create summary data on a per-trading pair basis.
for execution in block_executions {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: do we know that every block_executions that appears here will be nonempty? In other words, do we have to worry about whether our sentinel values like f64::INFINITY would leak through into the data we produce?

My assumption is that we wouldn't end up with an empty block_executions but wanted to check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe it should be possible but I can add some checks to the record_position_execution and record_swap_execution to make sure that none are included.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// We have already short-circuited no-op execution updates, so we can emit an execution
// event and not worry about duplicates.
-- should be impossible for empty position executions to be recorded

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does look like it's possible for no-op swap executions to be recorded, so an additional check there would be useful.

Comment on lines 395 to 401
// Update the candlestick tracking
self.record_position_execution(&prev_state, &new_state, &context)
.await?;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: would it make sense to combine the candlestick recording with the event emission, either by moving this up to 377 where the event is emitted, or by moving the event emission inside the candlestick call, so that we always record the candlestick data and the event together?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered this however the event emission and candlestick creation are different concerns and the position_execution method is supposed to be the sole method tracking position executions and tying the various subsystems together so this seemed most design-legible to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, the way I had thought about it is that the candlestick RPC is basically a very special-purpose embedded indexer (and the reason we want to put it in the fullnode by default is so that any frontend can assume it will be there). Either way, though, I think it's fine.

@zbuc zbuc marked this pull request as ready for review May 23, 2024 20:29
@zbuc zbuc changed the title dex: draft candlestick data RPC dex: Generate candlestick data and provide RPCs for access May 23, 2024
@erwanor erwanor self-requested a review May 23, 2024 21:42
@erwanor
Copy link
Member

erwanor commented May 24, 2024

Some suggestions, will take it for a spin trying to generate price data

@zbuc zbuc force-pushed the candlestick-rpc branch from d116fb4 to 09ffd4c Compare May 24, 2024 15:45
@erwanor
Copy link
Member

erwanor commented May 24, 2024

I am rebasing this now fyalli

};
let mut block_executions = self.block_executions_by_pair(&trading_pair).clone();

let execution_price = U128x128::ratio(swap.output.amount, swap.input.amount)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK this looks correct to me

@conorsch conorsch merged commit e49f1aa into main May 24, 2024
13 checks passed
@conorsch conorsch deleted the candlestick-rpc branch May 24, 2024 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dex Area: Relates to the dex C-enhancement Category: an enhancement to the codebase protobuf-changes Makes changes to the protobuf definitions.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants