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

dex(events): use ProtoEvent to record events #3513

Merged
merged 7 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified crates/cnidarium/src/gen/proto_descriptor.bin.no_lfs
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use async_trait::async_trait;
use cnidarium::{StateRead, StateWrite};
use cnidarium_component::ActionHandler;
use penumbra_proto::StateWriteProto as _;

use crate::{component::PositionManager, event, lp::action::PositionClose};

Expand All @@ -29,7 +30,7 @@ impl ActionHandler for PositionClose {
// during that block's batch swap execution.
state.queue_close_position(self.position_id);

state.record(event::position_close(self));
state.record_proto(event::position_close(self));

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use async_trait::async_trait;
use cnidarium::{StateRead, StateWrite};
use cnidarium_component::ActionHandler;
use penumbra_proto::StateWriteProto as _;

use crate::{
component::{PositionManager, PositionRead},
Expand Down Expand Up @@ -37,7 +38,7 @@ impl ActionHandler for PositionOpen {

async fn execute<S: StateWrite>(&self, mut state: S) -> Result<()> {
state.put_position(self.position.clone()).await?;
state.record(event::position_open(self));
state.record_proto(event::position_open(self));
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use async_trait::async_trait;
use cnidarium::{StateRead, StateWrite};
use cnidarium_component::ActionHandler;
use decaf377::Fr;
use penumbra_proto::StateWriteProto;

use crate::{
component::{PositionManager, PositionRead},
Expand Down Expand Up @@ -67,7 +68,7 @@ impl ActionHandler for PositionWithdraw {
);
}

state.record(event::position_withdraw(self, &metadata));
state.record_proto(event::position_withdraw(self, &metadata));

metadata.state = position::State::Withdrawn;
state.put_position(metadata).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use async_trait::async_trait;
use cnidarium::{StateRead, StateWrite};
use cnidarium_component::ActionHandler;
use penumbra_proof_params::SWAP_PROOF_VERIFICATION_KEY;
use penumbra_proto::StateWriteProto;

use crate::{
component::{metrics, StateReadExt, StateWriteExt, SwapManager},
Expand Down Expand Up @@ -61,7 +62,7 @@ impl ActionHandler for Swap {
crate::component::metrics::DEX_SWAP_DURATION,
swap_start.elapsed()
);
state.record(event::swap(self));
state.record_proto(event::swap(self));

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use penumbra_chain::TransactionContext;

use cnidarium::{StateRead, StateWrite};
use penumbra_proof_params::SWAPCLAIM_PROOF_VERIFICATION_KEY;
use penumbra_proto::StateWriteProto;
use penumbra_shielded_pool::component::{NoteManager, StateReadExt as _};

use crate::{component::StateReadExt, event, swap_claim::SwapClaim};
Expand Down Expand Up @@ -77,7 +78,7 @@ impl ActionHandler for SwapClaim {

state.spend_nullifier(self.body.nullifier, source).await;

state.record(event::swap_claim(self));
state.record_proto(event::swap_claim(self));

Ok(())
}
Expand Down
102 changes: 38 additions & 64 deletions crates/core/component/dex/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use tendermint::abci::{Event, EventAttributeIndexExt};

use crate::{
lp::{
action::{PositionClose, PositionOpen, PositionWithdraw},
Expand All @@ -9,76 +7,52 @@ use crate::{
swap_claim::SwapClaim,
};

pub fn swap(swap: &Swap) -> Event {
Event::new(
"action_swap",
[
("trading_pair", swap.body.trading_pair.to_string()).index(),
("delta_1_i", swap.body.delta_1_i.to_string()).index(),
("delta_2_i", swap.body.delta_2_i.to_string()).index(),
("swap_commitment", swap.body.payload.commitment.to_string()).index(),
],
)
use penumbra_proto::penumbra::core::component::dex::v1alpha1 as pb;

pub fn swap(swap: &Swap) -> pb::EventSwap {
pb::EventSwap {
trading_pair: Some(swap.body.trading_pair.into()),
delta_1_i: Some(swap.body.delta_1_i.into()),
delta_2_i: Some(swap.body.delta_2_i.into()),
commitment: Some(swap.body.fee_commitment.into()),
}
}

pub fn swap_claim(swap_claim: &SwapClaim) -> Event {
Event::new(
"action_swap_claim",
[
(
"trading_pair",
swap_claim.body.output_data.trading_pair.to_string(),
)
.index(),
(
"output_1_commitment",
swap_claim.body.output_1_commitment.to_string(),
)
.index(),
(
"output_2_commitment",
swap_claim.body.output_2_commitment.to_string(),
)
.index(),
("nullifier", swap_claim.body.nullifier.to_string()).index(),
],
)
pub fn swap_claim(swap_claim: &SwapClaim) -> pb::EventSwapClaim {
pb::EventSwapClaim {
trading_pair: Some(swap_claim.body.output_data.trading_pair.into()),
output_1_commitment: Some(swap_claim.body.output_1_commitment.into()),
output_2_commitment: Some(swap_claim.body.output_2_commitment.into()),
nullifier: Some(swap_claim.body.nullifier.into()),
}
}

pub fn position_open(action: &PositionOpen) -> Event {
Event::new(
"action_position_open",
[
("position_id", action.position.id().to_string()).index(),
("trading_pair", action.position.phi.pair.to_string()).index(),
// TODO: move into position manager and include in a "position updated" event?
("reserves_1", action.position.reserves.r1.to_string()).index(),
("reserves_2", action.position.reserves.r2.to_string()).index(),
("trading_fee", action.position.phi.component.fee.to_string()).index(),
("trading_p1", action.position.phi.component.p.to_string()).index(),
("trading_p2", action.position.phi.component.q.to_string()).index(),
],
)
pub fn position_open(position_open: &PositionOpen) -> pb::EventPositionOpen {
pb::EventPositionOpen {
position_id: Some(position_open.position.id().into()),
trading_pair: Some(position_open.position.phi.pair.into()),
reserves_1: Some(position_open.position.reserves.r1.into()),
reserves_2: Some(position_open.position.reserves.r2.into()),
trading_fee: position_open.position.phi.component.fee.into(),
}
}

pub fn position_close(action: &PositionClose) -> Event {
pub fn position_close(action: &PositionClose) -> pb::EventPositionClose {
// TODO: should we have another event triggered by the position manager for when
// the position is actually closed?
Event::new(
"action_position_close",
[("position_id", action.position_id.to_string()).index()],
)
pb::EventPositionClose {
position_id: Some(action.position_id.into()),
}
}

pub fn position_withdraw(action: &PositionWithdraw, final_position_state: &Position) -> Event {
Event::new(
"action_position_withdraw",
[
("position_id", action.position_id.to_string()).index(),
// reserves not included in action so need to be passed in separately
("trading_pair", final_position_state.phi.pair.to_string()).index(),
("reserves_1", final_position_state.reserves.r1.to_string()).index(),
("reserves_2", final_position_state.reserves.r2.to_string()).index(),
],
)
pub fn position_withdraw(
position_withdraw: &PositionWithdraw,
final_position_state: &Position,
) -> pb::EventPositionWithdraw {
pb::EventPositionWithdraw {
position_id: Some(position_withdraw.position_id.into()),
trading_pair: Some(final_position_state.phi.pair.into()),
reserves_1: Some(final_position_state.reserves.r1.into()),
reserves_2: Some(final_position_state.reserves.r2.into()),
}
}
116 changes: 116 additions & 0 deletions crates/proto/src/gen/penumbra.core.component.dex.v1alpha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,122 @@ impl ::prost::Name for SimulateTradeResponse {
::prost::alloc::format!("penumbra.core.component.dex.v1alpha1.{}", Self::NAME)
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct EventSwap {
/// The trading pair to swap.
#[prost(message, optional, tag = "1")]
pub trading_pair: ::core::option::Option<TradingPair>,
/// The amount for asset 1.
#[prost(message, optional, tag = "2")]
pub delta_1_i: ::core::option::Option<super::super::super::num::v1alpha1::Amount>,
/// The amount for asset 2.
#[prost(message, optional, tag = "3")]
pub delta_2_i: ::core::option::Option<super::super::super::num::v1alpha1::Amount>,
/// A commitment to a prepaid fee for the future SwapClaim.
#[prost(message, optional, tag = "4")]
pub commitment: ::core::option::Option<
super::super::super::asset::v1alpha1::BalanceCommitment,
>,
}
impl ::prost::Name for EventSwap {
const NAME: &'static str = "EventSwap";
const PACKAGE: &'static str = "penumbra.core.component.dex.v1alpha1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!("penumbra.core.component.dex.v1alpha1.{}", Self::NAME)
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct EventSwapClaim {
/// The trading pair that is subject of the swap claim.
#[prost(message, optional, tag = "1")]
pub trading_pair: ::core::option::Option<TradingPair>,
/// Note commitment for the first asset.
#[prost(message, optional, tag = "2")]
pub output_1_commitment: ::core::option::Option<
super::super::super::super::crypto::tct::v1alpha1::StateCommitment,
>,
/// Note commitment for the second asset.
#[prost(message, optional, tag = "3")]
pub output_2_commitment: ::core::option::Option<
super::super::super::super::crypto::tct::v1alpha1::StateCommitment,
>,
/// The nullifier for the swap commitment.
#[prost(message, optional, tag = "4")]
pub nullifier: ::core::option::Option<super::super::sct::v1alpha1::Nullifier>,
}
impl ::prost::Name for EventSwapClaim {
const NAME: &'static str = "EventSwapClaim";
const PACKAGE: &'static str = "penumbra.core.component.dex.v1alpha1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!("penumbra.core.component.dex.v1alpha1.{}", Self::NAME)
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct EventPositionOpen {
/// Position ID.
#[prost(message, optional, tag = "1")]
pub position_id: ::core::option::Option<PositionId>,
/// The trading pair to open.
#[prost(message, optional, tag = "2")]
pub trading_pair: ::core::option::Option<TradingPair>,
/// The amount for asset 1.
#[prost(message, optional, tag = "3")]
pub reserves_1: ::core::option::Option<super::super::super::num::v1alpha1::Amount>,
/// The amount for asset 2.
#[prost(message, optional, tag = "4")]
pub reserves_2: ::core::option::Option<super::super::super::num::v1alpha1::Amount>,
/// The trading fee for the position, expressed in basis points.
/// e.g. 2% fee is expressed as 200, 100% fee is expressed as 10000;
#[prost(uint32, tag = "5")]
pub trading_fee: u32,
}
impl ::prost::Name for EventPositionOpen {
const NAME: &'static str = "EventPositionOpen";
const PACKAGE: &'static str = "penumbra.core.component.dex.v1alpha1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!("penumbra.core.component.dex.v1alpha1.{}", Self::NAME)
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct EventPositionClose {
/// The ID of the closed position
#[prost(message, optional, tag = "1")]
pub position_id: ::core::option::Option<PositionId>,
}
impl ::prost::Name for EventPositionClose {
const NAME: &'static str = "EventPositionClose";
const PACKAGE: &'static str = "penumbra.core.component.dex.v1alpha1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!("penumbra.core.component.dex.v1alpha1.{}", Self::NAME)
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct EventPositionWithdraw {
/// The ID of the withdrawn position.
#[prost(message, optional, tag = "1")]
pub position_id: ::core::option::Option<PositionId>,
/// The trading pair of the withdrawn position.
#[prost(message, optional, tag = "2")]
pub trading_pair: ::core::option::Option<TradingPair>,
/// The reserves of asset 1 of the withdrawn position.
#[prost(message, optional, tag = "3")]
pub reserves_1: ::core::option::Option<super::super::super::num::v1alpha1::Amount>,
/// The reserves of asset 2 of the withdrawn position.
#[prost(message, optional, tag = "4")]
pub reserves_2: ::core::option::Option<super::super::super::num::v1alpha1::Amount>,
}
impl ::prost::Name for EventPositionWithdraw {
const NAME: &'static str = "EventPositionWithdraw";
const PACKAGE: &'static str = "penumbra.core.component.dex.v1alpha1";
fn full_name() -> ::prost::alloc::string::String {
::prost::alloc::format!("penumbra.core.component.dex.v1alpha1.{}", Self::NAME)
}
}
/// Generated client implementations.
#[cfg(feature = "rpc")]
pub mod query_service_client {
Expand Down
Loading
Loading