Skip to content

Commit

Permalink
Revert "feat: add lp updates to indexer"
Browse files Browse the repository at this point in the history
This reverts commit 78f3077.
  • Loading branch information
cronokirby committed Jul 24, 2024
1 parent 2e5cd2a commit 8544be8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 195 deletions.
63 changes: 24 additions & 39 deletions crates/bin/pindexer/src/dex/dex.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,41 @@
-- that given an `penumbra_asset::asset::Id`, we always know exactly how to filter
-- tables, rather than needing to do a join with another table.

CREATE DOMAIN IF NOT EXISTS Amount AS NUMERIC (39, 0) NOT NULL;
CREATE DOMAIN IF NOT EXISTS Amount AS NUMERIC(39, 0) NOT NULL;

CREATE TYPE Value AS
(
amount Amount,
asset BYTEA NOT NULL
CREATE TYPE Value AS (
amount Amount,
asset BYTEA NOT NULL
);

-- Keeps track of changes to the dex's value circuit breaker.
CREATE TABLE IF NOT EXISTS dex_value_circuit_breaker_change
(
-- The asset being moved into or out of the dex.
asset_id BYTEA NOT NULL,
-- The flow, either positive, or negative, into the dex via this particular asset.
--
-- Because we're dealing with arbitrary assets, we need to use something which can store u128
flow Amount
CREATE TABLE IF NOT EXISTS dex_value_circuit_breaker_change (
-- The asset being moved into or out of the dex.
asset_id BYTEA NOT NULL,
-- The flow, either positive, or negative, into the dex via this particular asset.
--
-- Because we're dealing with arbitrary assets, we need to use something which can store u128
flow Amount
);

-- One step of an execution trace.
CREATE TABLE IF NOT EXISTS trace_step
(
id SERIAL PRIMARY KEY,
value Value,
CREATE TABLE IF NOT EXISTS trace_step (
id SERIAL PRIMARY KEY,
value Value,
);

-- A single trace, showing what a small amount of an input asset was exchanged for.
CREATE TABLE IF NOT EXISTS trace
(
id SERIAL PRIMARY KEY,
step_start INTEGER REFERENCES trace_step (id),
step_end INTEGER REFERENCES trace_step (id),
CREATE TABLE IF NOT EXISTS trace (
id SERIAL PRIMARY KEY,
step_start INTEGER REFERENCES trace_step(id),
step_end INTEGER REFERENCES trace_step(id),
);

--- Represents instances where arb executions happened.
CREATE TABLE IF NOT EXISTS arb
(
height BIGINT PRIMARY KEY,
input Value,
output Value,
trace_start INTEGER REFERENCES arb_traces (id),
trace_end INTEGER REFERENCES arb_traces (id),
);

--- Represents LP updates
CREATE TABLE IF NOT EXISTS lp_updates
(
id SERIAL PRIMARY KEY,
height INT8 NOT NULL,
type integer NOT NULL,
position_id BYTEA NOT NULL,
trading_pair BYTEA
CREATE TABLE IF NOT EXISTS arb (
height BIGINT PRIMARY KEY,
input Value,
output Value,
trace_start INTEGER REFERENCES arb_traces(id),
trace_end INTEGER REFERENCES arb_traces(id),
);
157 changes: 1 addition & 156 deletions crates/bin/pindexer/src/dex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use cometindex::async_trait;
use penumbra_asset::asset::Id as AssetId;
use penumbra_dex::SwapExecution;
use penumbra_num::Amount;
use penumbra_proto::core::component::dex::v1::{PositionId, TradingPair};
use penumbra_proto::{event::ProtoEvent, penumbra::core::component::dex::v1 as pb};
use sqlx::{PgPool, Postgres, Transaction};

Expand All @@ -32,39 +31,13 @@ enum Event {
height: u64,
execution: SwapExecution,
},
/// A parsed version of [pb::EventPositionOpen]
PositionOpen {
height: u64,
position_id: PositionId,
trading_pair: TradingPair,
reserves_1: Amount,
reserves_2: Amount,
trading_fee: u32,
},
/// A parsed version of [pb::EventPositionWithdraw]
PositionWithdraw {
height: u64,
position_id: PositionId,
trading_pair: TradingPair,
reserves_1: Amount,
reserves_2: Amount,
sequence: u32,
},
/// A parsed version of [pb::EventPositionClose]
PositionClose {
height: u64,
position_id: PositionId,
},
}

impl Event {
const NAMES: [&'static str; 6] = [
const NAMES: [&'static str; 3] = [
"penumbra.core.component.dex.v1.EventValueCircuitBreakerCredit",
"penumbra.core.component.dex.v1.EventValueCircuitBreakerDebit",
"penumbra.core.component.dex.v1.EventArbExecution",
"penumbra.core.component.dex.v1.EventPositionWithdraw",
"penumbra.core.component.dex.v1.EventPositionOpen",
"penumbra.core.component.dex.v1.EventPositionClose",
];

/// Index this event, using the handle to the postgres transaction.
Expand Down Expand Up @@ -157,60 +130,6 @@ impl Event {
.await?;
Ok(())
}
Event::PositionOpen {
height,
position_id,
..
} => {
sqlx::query(
"
INSERT INTO lp_updates (height, type, position_id)
VALUES ($1, $2, $3)
",
)
.bind(*height as i64)
.bind(0)
.bind(&position_id.inner)
.execute(dbtx.as_mut())
.await?;
Ok(())
}
Event::PositionWithdraw {
height,
position_id,
..
} => {
sqlx::query(
"
INSERT INTO lp_updates (height, type, position_id)
VALUES ($1, $2, $3)
",
)
.bind(*height as i64)
.bind(2)
.bind(&position_id.inner)
.execute(dbtx.as_mut())
.await?;
Ok(())
}
Event::PositionClose {
height,
position_id,
..
} => {
sqlx::query(
"
INSERT INTO lp_updates (height, type, position_id)
VALUES ($1, $2, $3)
",
)
.bind(*height as i64)
.bind(1)
.bind(&position_id.inner)
.execute(dbtx.as_mut())
.await?;
Ok(())
}
}
}
}
Expand Down Expand Up @@ -264,80 +183,6 @@ impl<'a> TryFrom<&'a ContextualizedEvent> for Event {
.try_into()?;
Ok(Self::ArbExecution { height, execution })
}
// LP Withdraw
x if x == Event::NAMES[3] => {
let pe = pb::EventPositionWithdraw::from_event(event.as_ref())?;
let height = event.block_height;
let position_id = pe
.position_id
.ok_or(anyhow!("missing position id"))?
.try_into()?;
let trading_pair = pe
.trading_pair
.ok_or(anyhow!("missing trading pair"))?
.try_into()?;
let reserves_1 = pe
.reserves_1
.ok_or(anyhow!("missing reserves_1"))?
.try_into()?;
let reserves_2 = pe
.reserves_2
.ok_or(anyhow!("missing reserves_2"))?
.try_into()?;
let sequence = pe.sequence.try_into()?;
Ok(Self::PositionWithdraw {
height,
position_id,
trading_pair,
reserves_1,
reserves_2,
sequence,
})
}
// LP Open
x if x == Event::NAMES[4] => {
let pe = pb::EventPositionOpen::from_event(event.as_ref())?;
let height = event.block_height;
let position_id = pe
.position_id
.ok_or(anyhow!("missing position id"))?
.try_into()?;
let trading_pair = pe
.trading_pair
.ok_or(anyhow!("missing trading pair"))?
.try_into()?;
let reserves_1 = pe
.reserves_1
.ok_or(anyhow!("missing reserves_1"))?
.try_into()?;
let reserves_2 = pe
.reserves_2
.ok_or(anyhow!("missing reserves_2"))?
.try_into()?;
let trading_fee = pe.trading_fee.try_into()?;
Ok(Self::PositionOpen {
height,
position_id,
trading_pair,
reserves_1,
reserves_2,
trading_fee,
})
}
// LP Close
x if x == Event::NAMES[5] => {
let pe = pb::EventPositionClose::from_event(event.as_ref())?;
let height = event.block_height;
let position_id = pe
.position_id
.ok_or(anyhow!("missing position id"))?
.try_into()?;

Ok(Self::PositionClose {
height,
position_id,
})
}
x => Err(anyhow!(format!("unrecognized event kind: {x}"))),
}
}
Expand Down

0 comments on commit 8544be8

Please sign in to comment.