Skip to content

Commit

Permalink
pindexer: auction: add auction component
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Sep 30, 2024
1 parent c661b2d commit ebfed00
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 3 deletions.
4 changes: 2 additions & 2 deletions crates/bin/pindexer/src/auction/auction.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CREATE TABLE IF NOT EXISTS auction_dutch_update (
-- Serves only as a primary key.
id SERIAL PRIMARY KEY,
-- Points to the description of the auction.
auction_id BYTE NOT NULL REFERENCES auction_dutch_description (id),
auction_id BYTEA NOT NULL REFERENCES auction_dutch_description (id),
-- The height of this update.
height BIGINT NOT NULL,
-- 0 -> opened, 1 -> closed, >= 2 -> withdrawn.
Expand All @@ -37,7 +37,7 @@ CREATE TABLE IF NOT EXISTS auction_dutch_update (
-- If present, the current position being managed by the auction.
position_id BYTEA,
-- The next height at which a change to the positions will happen.
next_trigger INT,
next_trigger BIGINT,
-- The current reserves of the input asset.
in_reserves NUMERIC(39, 0) NOT NULL,
-- The current reserves of the output asset.
Expand Down
141 changes: 140 additions & 1 deletion crates/bin/pindexer/src/auction/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use anyhow::anyhow;
use cometindex::ContextualizedEvent;
use cometindex::{async_trait, AppView, ContextualizedEvent, PgTransaction};
use penumbra_auction::auction::{
dutch::{DutchAuctionDescription, DutchAuctionState},
AuctionId,
};
use penumbra_proto::{core::component::auction::v1 as pb, event::ProtoEvent};
use sqlx::PgPool;

#[derive(Clone, Copy)]
enum EventKind {
Expand Down Expand Up @@ -120,3 +121,141 @@ impl TryFrom<&ContextualizedEvent> for Event {
}
}
}

async fn create_dutch_auction_description(
dbtx: &mut PgTransaction<'_>,
id: AuctionId,
description: &DutchAuctionDescription,
) -> anyhow::Result<()> {
sqlx::query(
"
INSERT INTO
auction_dutch_description
VALUES (
$1,
$2,
$3::NUMERIC(39, 0),
$4,
$5::NUMERIC(39, 0),
$6::NUMERIC(39, 0),
$7,
$8,
$9,
$10
)",
)
.bind(id.0)
.bind(&description.input.asset_id.to_bytes())
.bind(&description.input.amount.to_string())
.bind(&description.output_id.to_bytes())
.bind(&description.max_output.to_string())
.bind(&description.min_output.to_string())
.bind(i64::try_from(description.start_height)?)
.bind(i64::try_from(description.end_height)?)
.bind(i64::try_from(description.step_count)?)
.bind(&description.nonce)
.execute(dbtx.as_mut())
.await?;
Ok(())
}

async fn create_dutch_auction_update(
dbtx: &mut PgTransaction<'_>,
height: u64,
id: AuctionId,
state: &DutchAuctionState,
reason: Option<pb::event_dutch_auction_ended::Reason>,
) -> anyhow::Result<()> {
sqlx::query(
"
INSERT INTO
auction_dutch_update
VALUES (
DEFAULT,
$1,
$2,
$3,
$4,
$5,
$6,
$7::NUMERIC(39, 0),
$8::NUMERIC(39, 0)
)",
)
.bind(&id.0)
.bind(i64::try_from(height)?)
.bind(i32::try_from(state.sequence)?)
.bind(reason.map(|x| {
use pb::event_dutch_auction_ended::Reason::*;
match x {
Unspecified => 0i32,
Expired => 1,
Filled => 2,
ClosedByOwner => 3,
}
}))
.bind(state.current_position.map(|x| x.0))
.bind(
state
.next_trigger
.map(|x| i64::try_from(u64::from(x)))
.transpose()?,
)
.bind(&state.input_reserves.to_string())
.bind(&state.output_reserves.to_string())
.execute(dbtx.as_mut())
.await?;
Ok(())
}

#[derive(Debug)]
pub struct Component {}

impl Component {
pub fn new() -> Self {
Self {}
}
}

#[async_trait]
impl AppView for Component {
async fn init_chain(
&self,
dbtx: &mut PgTransaction,
_app_state: &serde_json::Value,
) -> anyhow::Result<()> {
for statement in include_str!("auction.sql").split(";") {
sqlx::query(statement).execute(dbtx.as_mut()).await?;
}
Ok(())
}

fn is_relevant(&self, type_str: &str) -> bool {
EventKind::try_from(type_str).is_ok()
}

#[tracing::instrument(skip_all, fields(height = event.block_height, name = event.event.kind.as_str()))]
async fn index_event(
&self,
dbtx: &mut PgTransaction,
event: &ContextualizedEvent,
_src_db: &PgPool,
) -> anyhow::Result<()> {
let height = event.block_height;
match Event::try_from(event)? {
Event::DutchScheduled { id, description } => {
create_dutch_auction_description(dbtx, id, &description).await?;
}
Event::DutchUpdated { id, state } => {
create_dutch_auction_update(dbtx, height, id, &state, None).await?;
}
Event::DutchEnded { id, state, reason } => {
create_dutch_auction_update(dbtx, height, id, &state, Some(reason)).await?;
}
Event::DutchWithdrawn { id, state } => {
create_dutch_auction_update(dbtx, height, id, &state, None).await?;
}
};
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/bin/pindexer/src/indexer_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ impl IndexerExt for cometindex::Indexer {
.with_index(crate::governance::GovernanceProposals {})
.with_index(crate::dex::Component::new())
.with_index(crate::supply::Component::new())
.with_index(crate::auction::Component::new())
}
}

0 comments on commit ebfed00

Please sign in to comment.