Skip to content

Commit

Permalink
Store batch swap summaries as json (#5100)
Browse files Browse the repository at this point in the history
## Describe your changes

As discussed in the slack thread, parsing a custom type in typescript
turns out to require a lot of hoops. This converts the
batch_swap_summaries to a json so that it can be parsed easily.

## Issue ticket number and link

Part of penumbra-zone/dex-explorer#338

## Checklist before requesting a review

- [x] I have added guiding text to explain how a reviewer should test
these changes.

- [x] 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:

  > REPLACE THIS TEXT WITH RATIONALE (CAN BE BRIEF)

---------

Co-authored-by: Conor Schaefer <conor@penumbralabs.xyz>
  • Loading branch information
JasonMHasperhoven and conorsch authored Feb 21, 2025
1 parent 144543b commit f0b0db5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
31 changes: 15 additions & 16 deletions crates/bin/pindexer/src/dex_ex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use penumbra_sdk_proto::DomainType;
use penumbra_sdk_sct::event::EventBlockRoot;
use penumbra_sdk_transaction::Transaction;
use sqlx::types::BigDecimal;
use sqlx::{prelude::Type, Row};
use sqlx::Row;
use std::collections::{BTreeMap, HashMap, HashSet};

type DateTime = sqlx::types::chrono::DateTime<sqlx::types::chrono::Utc>;
Expand Down Expand Up @@ -673,13 +673,12 @@ struct PairMetrics {
liquidity_change: f64,
}

#[derive(Debug, Clone, Type)]
#[sqlx(type_name = "batch_swap_summary")]
#[derive(Debug, Clone, serde::Serialize)]
struct BatchSwapSummary {
asset_start: Vec<u8>,
asset_end: Vec<u8>,
input: BigDecimal,
output: BigDecimal,
asset_start: asset::Id,
asset_end: asset::Id,
input: Amount,
output: Amount,
num_swaps: i32,
price_float: f64,
}
Expand Down Expand Up @@ -1150,10 +1149,10 @@ impl Component {
let num_swaps = filtered_swaps.len() as i32;

batch_swap_summaries.push(BatchSwapSummary {
asset_start: asset_start.to_bytes().to_vec(),
asset_end: asset_end.to_bytes().to_vec(),
input: BigDecimal::from(input.value()),
output: BigDecimal::from(output.value()),
asset_start,
asset_end,
input,
output,
num_swaps,
price_float,
});
Expand All @@ -1175,10 +1174,10 @@ impl Component {
let num_swaps = filtered_swaps.len() as i32;

batch_swap_summaries.push(BatchSwapSummary {
asset_start: asset_start.to_bytes().to_vec(),
asset_end: asset_end.to_bytes().to_vec(),
input: BigDecimal::from(input.value()),
output: BigDecimal::from(output.value()),
asset_start,
asset_end,
input,
output,
num_swaps,
price_float,
});
Expand All @@ -1200,7 +1199,7 @@ impl Component {
)
.bind(height)
.bind(time)
.bind(&batch_swap_summaries)
.bind(serde_json::to_value(&batch_swap_summaries)?)
.bind(num_opened_lps)
.bind(num_closed_lps)
.bind(num_withdrawn_lps)
Expand Down
18 changes: 1 addition & 17 deletions crates/bin/pindexer/src/dex_ex/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,6 @@ CREATE INDEX ON dex_ex_batch_swap_traces (time, height);
CREATE INDEX ON dex_ex_batch_swap_traces (asset_start, asset_end);
-- TODO(erwan): We can add a GIN index on the position id later.

-- A high-level view of a batch swap.
CREATE TYPE batch_swap_summary AS (
-- The directed start asset of the batch swap.
asset_start BYTEA,
-- The directed end asset of the batch swap.
asset_end BYTEA,
-- The amount of asset was consumed by the batch swap.
input NUMERIC(39),
-- The amount of asset was produced by the batch swap.
output NUMERIC(39),
-- The number of swaps in the batch swap.
num_swaps INTEGER,
-- The price with `asset_end` as the quote asset.
price_float DOUBLE PRECISION
);

-- A summary of block data with a bias for DEX data.
CREATE TABLE IF NOT EXISTS dex_ex_block_summary (
-- Primary key
Expand All @@ -234,7 +218,7 @@ CREATE TABLE IF NOT EXISTS dex_ex_block_summary (
-- The timestamp for the block.
time TIMESTAMPTZ NOT NULL,
-- A list of batch swap summaries that occurred in this block.
batch_swaps batch_swap_summary[] NOT NULL,
batch_swaps jsonb NOT NULL,
-- The number of opened LPs in this block.
num_open_lps INTEGER NOT NULL,
-- The number of closed LPs in this block.
Expand Down

0 comments on commit f0b0db5

Please sign in to comment.