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

4869(ish) dex rich events #4893

Merged
merged 9 commits into from
Oct 11, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Result;
use async_trait::async_trait;
use cnidarium::StateWrite;
use cnidarium_component::ActionHandler;
use penumbra_proto::StateWriteProto as _;
use penumbra_proto::{DomainType as _, StateWriteProto as _};

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

Expand All @@ -25,7 +25,12 @@ impl ActionHandler for PositionClose {
state.queue_close_position(self.position_id);

// queue position close you will...
state.record_proto(event::queue_position_close(self));
state.record_proto(
event::EventQueuePositionClose {
position_id: self.position_id,
}
.to_proto(),
);

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use async_trait::async_trait;
use cnidarium::StateWrite;
use cnidarium_component::ActionHandler;
use penumbra_proof_params::SWAP_PROOF_VERIFICATION_KEY;
use penumbra_proto::StateWriteProto;
use penumbra_proto::{DomainType as _, StateWriteProto};
use penumbra_sct::component::source::SourceContext;

use crate::{
Expand Down Expand Up @@ -66,7 +66,7 @@ impl ActionHandler for Swap {
);
state.add_recently_accessed_asset(swap.body.trading_pair.asset_2(), fixed_candidates);

state.record_proto(event::swap(self));
state.record_proto(event::EventSwap::from(self).to_proto());

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

use cnidarium::{StateRead, StateWrite};
use penumbra_proof_params::SWAPCLAIM_PROOF_VERIFICATION_KEY;
use penumbra_proto::StateWriteProto;
use penumbra_proto::{DomainType as _, StateWriteProto};
use penumbra_sct::component::{
source::SourceContext,
tree::{SctManager, VerificationExt},
Expand Down Expand Up @@ -95,7 +95,7 @@ impl ActionHandler for SwapClaim {

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

state.record_proto(event::swap_claim(self));
state.record_proto(event::EventSwapClaim::from(self).to_proto());

Ok(())
}
Expand Down
10 changes: 8 additions & 2 deletions crates/core/component/dex/src/component/arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::Result;
use async_trait::async_trait;
use cnidarium::{StateDelta, StateWrite};
use penumbra_asset::{asset, Value};
use penumbra_proto::StateWriteProto as _;
use penumbra_proto::{DomainType as _, StateWriteProto as _};
use penumbra_sct::component::clock::EpochRead;
use tracing::instrument;

Expand Down Expand Up @@ -134,7 +134,13 @@ pub trait Arbitrage: StateWrite + Sized {
.await?;

// Emit an ABCI event detailing the arb execution.
self_mut.record_proto(event::arb_execution(height, se));
self_mut.record_proto(
event::EventArbExecution {
height,
swap_execution: se,
}
.to_proto(),
);
return Ok(Some(Value {
amount: arb_profit,
asset_id: arb_token,
Expand Down
20 changes: 17 additions & 3 deletions crates/core/component/dex/src/component/circuit_breaker/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use cnidarium::{StateRead, StateWrite};
use penumbra_asset::{asset, Value};
use penumbra_num::Amount;
use penumbra_proto::{StateReadProto, StateWriteProto};
use penumbra_proto::{DomainType, StateReadProto, StateWriteProto};
use tonic::async_trait;
use tracing::instrument;

Expand Down Expand Up @@ -39,7 +39,14 @@ pub(crate) trait ValueCircuitBreaker: StateWrite {
tracing::debug!(?prev_balance, ?new_balance, "crediting the dex VCB");
self.put(state_key::value_balance(&value.asset_id), new_balance);

self.record_proto(event::vcb_credit(value.asset_id, prev_balance, new_balance));
self.record_proto(
event::EventValueCircuitBreakerCredit {
asset_id: value.asset_id,
previous_balance: prev_balance,
new_balance,
}
.to_proto(),
);
Ok(())
}

Expand All @@ -61,7 +68,14 @@ pub(crate) trait ValueCircuitBreaker: StateWrite {
tracing::debug!(?prev_balance, ?new_balance, "crediting the dex VCB");
self.put(state_key::value_balance(&value.asset_id), new_balance);

self.record_proto(event::vcb_debit(value.asset_id, prev_balance, new_balance));
self.record_proto(
event::EventValueCircuitBreakerDebit {
asset_id: value.asset_id,
previous_balance: prev_balance,
new_balance,
}
.to_proto(),
);
Ok(())
}
}
Expand Down
15 changes: 9 additions & 6 deletions crates/core/component/dex/src/component/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use penumbra_asset::{Value, STAKING_TOKEN_ASSET_ID};
use penumbra_fee::component::StateWriteExt as _;
use penumbra_fee::Fee;
use penumbra_num::Amount;
use penumbra_proto::{StateReadProto, StateWriteProto};
use penumbra_proto::{DomainType as _, StateReadProto, StateWriteProto};
use tendermint::v0_37::abci;
use tracing::instrument;

Expand Down Expand Up @@ -399,11 +399,14 @@ pub(crate) trait InternalDexWrite: StateWrite {
self.object_put(state_key::pending_outputs(), outputs);

// Also generate an ABCI event for indexing:
self.record_proto(event::batch_swap(
output_data,
swap_execution_1_for_2,
swap_execution_2_for_1,
));
self.record_proto(
event::EventBatchSwap {
batch_swap_output_data: output_data,
swap_execution_1_for_2,
swap_execution_2_for_1,
}
.to_proto(),
);

Ok(())
}
Expand Down
14 changes: 9 additions & 5 deletions crates/core/component/dex/src/component/position_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ pub trait PositionManager: StateWrite + PositionRead {

self.update_position(id, Some(prev_state), new_state)
.await?;
self.record_proto(event::position_close_by_id(*id));
self.record_proto(event::EventPositionClose { position_id: *id }.to_proto());

Ok(())
}
Expand Down Expand Up @@ -279,7 +279,7 @@ pub trait PositionManager: StateWrite + PositionRead {
self.mark_trading_pair_as_active(position.phi.pair);

// Finally, record the new position state.
self.record_proto(event::position_open(&position));
self.record_proto(event::EventPositionOpen::from(position.clone()).to_proto());
self.update_position(&id, None, position).await?;

Ok(())
Expand Down Expand Up @@ -349,7 +349,9 @@ pub trait PositionManager: StateWrite + PositionRead {

// We have already short-circuited no-op execution updates, so we can emit an execution
// event and not worry about duplicates.
self.record_proto(event::position_execution(&prev_state, &new_state, context));
self.record_proto(
event::EventPositionExecution::in_context(&prev_state, &new_state, context).to_proto(),
);

// Handle "close-on-fill": automatically flip the position state to "closed" if
// either of the reserves are zero.
Expand All @@ -363,7 +365,7 @@ pub trait PositionManager: StateWrite + PositionRead {
);

new_state.state = position::State::Closed;
self.record_proto(event::position_close_by_id(position_id));
self.record_proto(event::EventPositionClose { position_id }.to_proto());
}
}

Expand Down Expand Up @@ -431,7 +433,9 @@ pub trait PositionManager: StateWrite + PositionRead {

// Record an event prior to updating the position state, so we have access to
// the current reserves.
self.record_proto(event::position_withdraw(position_id, &prev_state));
self.record_proto(
event::EventPositionWithdraw::in_context(position_id, &prev_state).to_proto(),
);

// Grab a copy of the final reserves of the position to return to the caller.
let reserves = prev_state.reserves.balance(&prev_state.phi.pair);
Expand Down
Loading
Loading