From c4b5714a763af450cd6714de24368eb19c2e435b Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Tue, 23 Jul 2024 23:29:25 -0700 Subject: [PATCH 1/5] Implement SQL shims for pindexer --- crates/bin/pindexer/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bin/pindexer/src/lib.rs b/crates/bin/pindexer/src/lib.rs index 9c443724d8..ee30dc0139 100644 --- a/crates/bin/pindexer/src/lib.rs +++ b/crates/bin/pindexer/src/lib.rs @@ -7,3 +7,4 @@ pub mod dex; pub mod shielded_pool; mod sql; pub mod stake; +mod sql; From 637aeb9681005bed95b2c44681af8df795d07a91 Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Tue, 23 Jul 2024 23:57:22 -0700 Subject: [PATCH 2/5] Implement basic Dex component This is mainly just to have scaffolding for new dex events --- crates/bin/pindexer/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bin/pindexer/src/lib.rs b/crates/bin/pindexer/src/lib.rs index ee30dc0139..9c443724d8 100644 --- a/crates/bin/pindexer/src/lib.rs +++ b/crates/bin/pindexer/src/lib.rs @@ -7,4 +7,3 @@ pub mod dex; pub mod shielded_pool; mod sql; pub mod stake; -mod sql; From 70d2c0db29ed7e35d28473fa7ebcd2060051b6bf Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Wed, 24 Jul 2024 01:16:17 -0700 Subject: [PATCH 3/5] pindexer: Implement indexing of arb executions Co-authored-by: vacekj --- Cargo.lock | 1 + crates/bin/pindexer/Cargo.toml | 1 + crates/bin/pindexer/src/dex/dex.sql | 31 ++++++++++++- crates/bin/pindexer/src/dex/mod.rs | 72 ++++++++++++++++++++++++++--- 4 files changed, 98 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 65451b53d4..b2ce05e88f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5824,6 +5824,7 @@ dependencies = [ "num-bigint", "penumbra-app", "penumbra-asset", + "penumbra-dex", "penumbra-num", "penumbra-proto", "penumbra-shielded-pool", diff --git a/crates/bin/pindexer/Cargo.toml b/crates/bin/pindexer/Cargo.toml index 92a3d75445..a02dae2b84 100644 --- a/crates/bin/pindexer/Cargo.toml +++ b/crates/bin/pindexer/Cargo.toml @@ -18,6 +18,7 @@ num-bigint = { version = "0.4" } penumbra-shielded-pool = {workspace = true, default-features = false} penumbra-stake = {workspace = true, default-features = false} penumbra-app = {workspace = true, default-features = false} +penumbra-dex = {workspace = true, default-features = false} penumbra-num = {workspace = true, default-features = false} penumbra-asset = {workspace = true, default-features = false} penumbra-proto = {workspace = true, default-features = false} diff --git a/crates/bin/pindexer/src/dex/dex.sql b/crates/bin/pindexer/src/dex/dex.sql index e1e93b11ee..72ff858a6e 100644 --- a/crates/bin/pindexer/src/dex/dex.sql +++ b/crates/bin/pindexer/src/dex/dex.sql @@ -9,6 +9,13 @@ -- 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 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. @@ -16,5 +23,27 @@ CREATE TABLE IF NOT EXISTS dex_value_circuit_breaker_change ( -- 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 NUMERIC(39, 0) NOT NULL + flow Amount +); + +-- One step of an execution trace. +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), +); + +--- 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), ); diff --git a/crates/bin/pindexer/src/dex/mod.rs b/crates/bin/pindexer/src/dex/mod.rs index e854130375..1d1910c78a 100644 --- a/crates/bin/pindexer/src/dex/mod.rs +++ b/crates/bin/pindexer/src/dex/mod.rs @@ -3,6 +3,7 @@ use std::collections::HashSet; use anyhow::anyhow; use cometindex::async_trait; use penumbra_asset::asset::Id as AssetId; +use penumbra_dex::SwapExecution; use penumbra_num::Amount; use penumbra_proto::{event::ProtoEvent, penumbra::core::component::dex::v1 as pb}; use sqlx::{PgPool, Postgres, Transaction}; @@ -11,7 +12,7 @@ use crate::sql::Sql; use crate::{AppView, ContextualizedEvent, PgTransaction}; /// One of the possible events that we care about. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Debug)] enum Event { /// A parsed version of [pb::EventValueCircuitBreakerCredit]. CircuitBreakerCredit { @@ -25,17 +26,23 @@ enum Event { previous_balance: Amount, new_balance: Amount, }, + /// A parsed version of [pb::EventArbExecution] + ArbExecution { + height: u64, + execution: SwapExecution, + }, } impl Event { - const NAMES: [&'static str; 2] = [ + const NAMES: [&'static str; 3] = [ "penumbra.core.component.dex.v1.EventValueCircuitBreakerCredit", "penumbra.core.component.dex.v1.EventValueCircuitBreakerDebit", + "penumbra.core.component.dex.v1.EventArbExecution", ]; /// Index this event, using the handle to the postgres transaction. async fn index<'d>(&self, dbtx: &mut Transaction<'d, Postgres>) -> anyhow::Result<()> { - match *self { + match self { Event::CircuitBreakerCredit { asset_id, previous_balance, @@ -52,7 +59,7 @@ impl Event { VALUES ($1, $2); "#, ) - .bind(Sql::from(asset_id)) + .bind(Sql::from(*asset_id)) .bind(Sql::from(amount)) .execute(dbtx.as_mut()) .await?; @@ -74,12 +81,55 @@ impl Event { VALUES ($1, -$2); "#, ) - .bind(Sql::from(asset_id)) + .bind(Sql::from(*asset_id)) .bind(Sql::from(amount)) .execute(dbtx.as_mut()) .await?; Ok(()) } + Event::ArbExecution { height, execution } => { + let mut trace_start = None; + let mut trace_end = None; + for trace in &execution.traces { + let mut step_start = None; + let mut step_end = None; + for step in trace { + let (id,): (i64,) = sqlx::query_as( + r#"INSERT INTO trace_step VALUES (DEFAULT, ($1, $2)) RETURNING id;"#, + ) + .bind(Sql::from(step.amount)) + .bind(Sql::from(step.asset_id)) + .fetch_one(dbtx.as_mut()) + .await?; + if let None = step_start { + step_start = Some(id); + } + step_end = Some(id); + } + let (id,): (i64,) = sqlx::query_as( + r#"INSERT INTO trace VALUES (DEFAULT, $1, $2) RETURNING id;"#, + ) + .bind(step_start) + .bind(step_end) + .fetch_one(dbtx.as_mut()) + .await?; + if let None = trace_start { + trace_start = Some(id); + } + trace_end = Some(id); + } + sqlx::query(r#"INSERT INTO arb VALUES ($1, ($2, $3), ($4, $5), $6, $7);"#) + .bind(i64::try_from(*height)?) + .bind(Sql::from(execution.input.amount)) + .bind(Sql::from(execution.input.asset_id)) + .bind(Sql::from(execution.output.amount)) + .bind(Sql::from(execution.output.asset_id)) + .bind(trace_start) + .bind(trace_end) + .execute(dbtx.as_mut()) + .await?; + Ok(()) + } } } } @@ -123,6 +173,16 @@ impl<'a> TryFrom<&'a ContextualizedEvent> for Event { new_balance, }) } + // Arb + x if x == Event::NAMES[2] => { + let pe = pb::EventArbExecution::from_event(event.as_ref())?; + let height = pe.height; + let execution = pe + .swap_execution + .ok_or(anyhow!("missing swap execution"))? + .try_into()?; + Ok(Self::ArbExecution { height, execution }) + } x => Err(anyhow!(format!("unrecognized event kind: {x}"))), } } @@ -157,7 +217,7 @@ impl AppView for Component { self.event_strings.contains(type_str) } - #[tracing::instrument(skip_all, fields(height = event.block_height))] + #[tracing::instrument(skip_all, fields(height = event.block_height, name = event.event.kind.as_str()))] async fn index_event( &self, dbtx: &mut PgTransaction, From 2e5cd2a09a9ab5081ced252d11b727c929da8287 Mon Sep 17 00:00:00 2001 From: Atris Date: Wed, 24 Jul 2024 17:42:42 +0200 Subject: [PATCH 4/5] feat: add lp updates to indexer --- crates/bin/pindexer/src/dex/dex.sql | 63 ++++++----- crates/bin/pindexer/src/dex/mod.rs | 157 +++++++++++++++++++++++++++- 2 files changed, 195 insertions(+), 25 deletions(-) diff --git a/crates/bin/pindexer/src/dex/dex.sql b/crates/bin/pindexer/src/dex/dex.sql index 72ff858a6e..8e6b800dbf 100644 --- a/crates/bin/pindexer/src/dex/dex.sql +++ b/crates/bin/pindexer/src/dex/dex.sql @@ -9,41 +9,56 @@ -- 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), +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 ); diff --git a/crates/bin/pindexer/src/dex/mod.rs b/crates/bin/pindexer/src/dex/mod.rs index 1d1910c78a..0fe38a1b98 100644 --- a/crates/bin/pindexer/src/dex/mod.rs +++ b/crates/bin/pindexer/src/dex/mod.rs @@ -5,6 +5,7 @@ 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}; @@ -31,13 +32,39 @@ 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; 3] = [ + const NAMES: [&'static str; 6] = [ "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. @@ -130,6 +157,60 @@ 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(()) + } } } } @@ -183,6 +264,80 @@ 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}"))), } } From 8544be8d239a20533a0bb06a291a1fd6739e7a7c Mon Sep 17 00:00:00 2001 From: Lucas Meier Date: Wed, 24 Jul 2024 09:53:17 -0700 Subject: [PATCH 5/5] Revert "feat: add lp updates to indexer" This reverts commit 78f3077dfc2b7c27423f80215f2dc2fb5dbc5e1d. --- crates/bin/pindexer/src/dex/dex.sql | 63 +++++------ crates/bin/pindexer/src/dex/mod.rs | 157 +--------------------------- 2 files changed, 25 insertions(+), 195 deletions(-) diff --git a/crates/bin/pindexer/src/dex/dex.sql b/crates/bin/pindexer/src/dex/dex.sql index 8e6b800dbf..72ff858a6e 100644 --- a/crates/bin/pindexer/src/dex/dex.sql +++ b/crates/bin/pindexer/src/dex/dex.sql @@ -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), ); diff --git a/crates/bin/pindexer/src/dex/mod.rs b/crates/bin/pindexer/src/dex/mod.rs index 0fe38a1b98..1d1910c78a 100644 --- a/crates/bin/pindexer/src/dex/mod.rs +++ b/crates/bin/pindexer/src/dex/mod.rs @@ -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}; @@ -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. @@ -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(()) - } } } } @@ -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}"))), } }