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

[ECO-484] Add order book state #518

Merged
merged 28 commits into from
Oct 9, 2023
Merged

[ECO-484] Add order book state #518

merged 28 commits into from
Oct 9, 2023

Conversation

CRBl69
Copy link
Collaborator

@CRBl69 CRBl69 commented Oct 4, 2023

  • add a prev column to limit orders
  • updated views (when updating the underlying tables, views need
    to be updated)
  • fix bigdecimal dependency
  • fix sdk (broke by aptos update)
  • fix fills (orders would not go into 'closed' mode)

Order is implemented with a field named last_increase_stamp. This is a number where the order with the lowest last_increase_stamp is the highest priority one. It is computed as (txn_version << 64) | event_idx.

The limit_orders endpoint is basically the order book state endpoint.

Example query: `http://0.0.0.0:3001/limit_orders?order=price.desc,last_increase_stamp.asc&market_id=eq.3&side=eq.ask&order_status=eq.open&limit=3

Example result:

[
  {
    "market_id": 3,
    "order_id": 866997112223312137760,
    "user": "0x9538c839fe490ccfaf32ad9f7491b5e84e610ff6edc110ff883f06ebde82463d",
    "custodian_id": 0,
    "self_matching_behavior": 3,
    "restriction": 0,
    "created_at": "2023-09-14T04:39:15.732539+00:00",
    "last_updated_at": null,
    "integrator": "0xd718181a753f5b759518d9b896018dd7eb3d77d80bf90ba77fffaf678f781929",
    "total_filled": 0,
    "remaining_size": 10000,
    "order_status": "open",
    "order_type": "limit",
    "price": 20000,
    "last_increase_stamp": 12466419366246665406505811969,
    "side": "ask"
  },
  {
    "market_id": 3,
    "order_id": 1272825481853512207904,
    "user": "0x9538c839fe490ccfaf32ad9f7491b5e84e610ff6edc110ff883f06ebde82463d",
    "custodian_id": 0,
    "self_matching_behavior": 3,
    "restriction": 0,
    "created_at": "2023-09-16T07:45:30.393582+00:00",
    "last_updated_at": null,
    "integrator": "0xd718181a753f5b759518d9b896018dd7eb3d77d80bf90ba77fffaf678f781929",
    "total_filled": 0,
    "remaining_size": 10000,
    "order_status": "open",
    "order_type": "limit",
    "price": 20000,
    "last_increase_stamp": 12514843600519911097390596097,
    "side": "ask"
  },
  {
    "market_id": 3,
    "order_id": 1291272225935811694112,
    "user": "0x9538c839fe490ccfaf32ad9f7491b5e84e610ff6edc110ff883f06ebde82463d",
    "custodian_id": 0,
    "self_matching_behavior": 3,
    "restriction": 0,
    "created_at": "2023-09-16T07:48:19.409595+00:00",
    "last_updated_at": null,
    "integrator": "0xd718181a753f5b759518d9b896018dd7eb3d77d80bf90ba77fffaf678f781929",
    "total_filled": 0,
    "remaining_size": 10000,
    "order_status": "open",
    "order_type": "limit",
    "price": 20000,
    "last_increase_stamp": 12514871842485087946714120193,
    "side": "ask"
  }
]

Config used:

health_check_port: 8085
server_config:
  processor_config:
    type: econia_transaction_processor
    econia_address: 0xc0de11113b427d35ece1d8991865a941c0578b0f349acabbe9753863c24109ff
  postgres_connection_string: postgres://econia:econia@postgres:5432/econia
  indexer_grpc_data_service_address: https://grpc.testnet.aptoslabs.com:443
  indexer_grpc_http2_ping_interval_in_secs: 60
  indexer_grpc_http2_ping_timeout_in_secs: 10
  auth_token: TOKEN
  starting_version: 649555969

CRBl69 added 2 commits October 3, 2023 09:26
- add a prev column to limit orders
- updated views (when updating the underlying tables, views need
  to be updated)
- fix bigdecimal dependency
- fix sdk (broke by aptos update)
- fix fills (orders would not go into 'closed' mode)
@vercel
Copy link

vercel bot commented Oct 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
econia ❌ Failed (Inspect) Oct 5, 2023 4:25pm

@CRBl69 CRBl69 changed the title [ECO-484] [ECO-484] Add order book state Oct 4, 2023
@vercel vercel bot temporarily deployed to Preview October 4, 2023 18:40 Inactive
@vercel vercel bot temporarily deployed to Preview October 4, 2023 19:05 Inactive
@vercel vercel bot temporarily deployed to Preview October 4, 2023 19:50 Inactive
@alnoki alnoki requested review from elliottdehn and alnoki October 4, 2023 20:15
Copy link
Member

@alnoki alnoki left a comment

Choose a reason for hiding this comment

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

Additional TODOs that I could not attach directly to comments

  1. Take out the aptos-indexer-processors submodule update
  2. When ready to merge and all comments addressed, add in the description a printout like the one in [ECO-486][ECO-538] Add balance update support  #511
    a. Show the config used for global testing and some of the printouts (e.g. screenshots like in [ECO-486][ECO-538] Add balance update support aptos-indexer-processors#9 (comment))
    b. Potentially use a more recent version number to hit relevant functionality
    c. Also run the local DSS per the docker readme to verify it works, since that docker compose has changed

src/rust/aggregator/src/main.rs Outdated Show resolved Hide resolved
src/rust/aggregator/src/data/user_history.rs Outdated Show resolved Hide resolved
src/rust/aggregator/src/data/user_history.rs Outdated Show resolved Hide resolved
src/rust/aggregator/src/data/user_history.rs Outdated Show resolved Hide resolved
src/docker/compose.dss.yaml Outdated Show resolved Hide resolved
src/rust/dbv2/src/schema.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@elliottdehn elliottdehn left a comment

Choose a reason for hiding this comment

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

Need to account for change size (increase) event, reconsider use of persistent data structure.

@elliottdehn
Copy link
Contributor

How does this handle bids/asks having separate books? I think the data structure assumes all markets have just one book which isn't true.

@elliottdehn
Copy link
Contributor

elliottdehn commented Oct 4, 2023

You should be able to provide two views: order_book_asks and order_book_bids which are simply a select of all orders where side is (given, constant) and market id is (given, optional) and status is OPEN, ordered by price (asc/desc depends on side), transaction version ascending. Version is updated on order change size increases. No need for the persistent linked list or any next/previous logic.

@vercel vercel bot temporarily deployed to Preview October 5, 2023 07:33 Inactive
- [uncomment accidentally commented line](#518 (comment))
- [use `last_increase_time` instead of `prev`](#518 (comment))
- [update JWT secret](#518 (comment))
- [add documentation on how to generate `.sqlx/*` files](#518 (comment))
- [no, it's not](#518 (comment))
- [compile aggregator in build step](#518 (comment))
- [renamed migration to reduce confusion](#518 (comment))
- [consolidate migrations](#518 (comment))
@vercel vercel bot temporarily deployed to Preview October 5, 2023 07:37 Inactive
@vercel vercel bot temporarily deployed to Preview October 5, 2023 13:18 Inactive
@@ -288,6 +293,8 @@ impl Data for UserHistory {
&change.order_id,
&change.market_id,
&change.time,
&fill.txn_version,
&fill.event_idx,
Copy link
Contributor

Choose a reason for hiding this comment

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

@CRBl69 It doesn't look like you de-duplicate fill events in this code here. There two fill events emitted for every fill, one for the maker and one for the taker. You can de-duplicated them using the order id field (I believe).

@@ -137,10 +137,13 @@ impl Data for UserHistory {
.await
.map_err(|e| DataAggregationError::ProcessingError(anyhow!(e)))?;
for x in &limit_events {
let txn = x.txn_version.to_bigint().ok_or(DataAggregationError::ProcessingError(anyhow!("txn_version not integer")))? << 64;
let event = x.event_idx.to_bigint().ok_or(DataAggregationError::ProcessingError(anyhow!("event_idx not integer")))?;
let txn_event: BigDecimal = BigDecimal::from(txn & event);
Copy link
Contributor

Choose a reason for hiding this comment

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

@CRBl69 You mean (txn << 64) | (event)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The << 64 is done at line 140.

CRBl69 and others added 5 commits October 7, 2023 17:22
- fix user history not registering anymore because use of fetch_one
  instead of execute
- add a better error handling system in the aggregator
- fix limit_orders view
[ECO-616] Add assorted order book PR tweaks
@elliottdehn elliottdehn merged commit 1658ee1 into main Oct 9, 2023
@elliottdehn elliottdehn deleted the ECO-484 branch October 9, 2023 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants