This commit is contained in:
2026-05-14 07:52:36 +02:00
parent cfa1ff2289
commit 348ae7f153
29 changed files with 1956 additions and 35 deletions

View File

@@ -2,9 +2,9 @@
//! Materialization of useful non-trade DEX events.
//!
//! This service persists liquidity and pool lifecycle events from already
//! decoded DEX events. It deliberately does not feed trade, metric or candle
//! materialization.
//! This service persists liquidity, pool lifecycle, fee, reward and pool
//! administration events from already decoded DEX events. It deliberately does
//! not feed trade, metric or candle materialization.
/// Result of non-trade event materialization for one transaction.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
@@ -13,6 +13,12 @@ pub struct NonTradeEventMaterializationResult {
pub liquidity_event_count: usize,
/// Number of pool lifecycle events inserted or refreshed.
pub pool_lifecycle_event_count: usize,
/// Number of fee events inserted or refreshed.
pub fee_event_count: usize,
/// Number of reward events inserted or refreshed.
pub reward_event_count: usize,
/// Number of pool administration events inserted or refreshed.
pub pool_admin_event_count: usize,
}
/// Materializes useful non-trade decoded DEX events.
@@ -127,6 +133,50 @@ impl NonTradeEventMaterializationService {
Err(error) => return Err(error),
}
}
if crate::is_dex_fee_event_kind(decoded_event.event_kind.as_str()) {
let materialized = self
.materialize_fee_event(&transaction, transaction_id, decoded_event, &payload)
.await;
match materialized {
Ok(was_materialized) => {
if was_materialized {
result.fee_event_count += 1;
}
},
Err(error) => return Err(error),
}
}
if crate::is_dex_reward_event_kind(decoded_event.event_kind.as_str()) {
let materialized = self
.materialize_reward_event(&transaction, transaction_id, decoded_event, &payload)
.await;
match materialized {
Ok(was_materialized) => {
if was_materialized {
result.reward_event_count += 1;
}
},
Err(error) => return Err(error),
}
}
if crate::is_dex_admin_event_kind(decoded_event.event_kind.as_str()) {
let materialized = self
.materialize_pool_admin_event(
&transaction,
transaction_id,
decoded_event,
&payload,
)
.await;
match materialized {
Ok(was_materialized) => {
if was_materialized {
result.pool_admin_event_count += 1;
}
},
Err(error) => return Err(error),
}
}
}
return Ok(result);
}
@@ -170,6 +220,207 @@ impl NonTradeEventMaterializationService {
}
}
async fn materialize_fee_event(
&self,
transaction: &crate::ChainTransactionDto,
transaction_id: i64,
decoded_event: &crate::DexDecodedEventDto,
payload: &serde_json::Value,
) -> Result<bool, crate::Error> {
let decoded_event_id = match decoded_event.id {
Some(decoded_event_id) => decoded_event_id,
None => return Ok(false),
};
let context = self.resolve_decoded_event_context(decoded_event).await;
let context = match context {
Ok(context) => context,
Err(error) => return Err(error),
};
let actor_wallet = extract_first_string(
payload,
&[
"actorWallet",
"actor_wallet",
"receiver",
"recipient",
"owner",
"payer",
"authority",
"user",
],
);
let fee_token_mint = extract_first_string(
payload,
&[
"feeTokenMint",
"fee_token_mint",
"tokenMint",
"token_mint",
"mint",
"quoteMint",
"quote_mint",
],
);
let fee_amount_raw = extract_first_amount_string(
payload,
&[
"feeAmountRaw",
"fee_amount_raw",
"feeAmount",
"fee_amount",
"protocolFeeAmount",
"protocol_fee_amount",
"fundFeeAmount",
"fund_fee_amount",
"creatorFeeAmount",
"creator_fee_amount",
"amount",
],
);
let dto = crate::FeeEventDto::new(
transaction_id,
Some(decoded_event_id),
context.dex_id,
context.pool_id,
context.pair_id,
transaction.signature.clone(),
transaction.slot,
decoded_event.protocol_name.clone(),
decoded_event.program_id.clone(),
decoded_event.event_kind.clone(),
decoded_event.pool_account.clone(),
actor_wallet,
fee_token_mint,
fee_amount_raw,
decoded_event.payload_json.clone(),
);
let upsert_result = crate::query_fee_events_upsert(self.database.as_ref(), &dto).await;
match upsert_result {
Ok(_) => return Ok(true),
Err(error) => return Err(error),
}
}
async fn materialize_reward_event(
&self,
transaction: &crate::ChainTransactionDto,
transaction_id: i64,
decoded_event: &crate::DexDecodedEventDto,
payload: &serde_json::Value,
) -> Result<bool, crate::Error> {
let decoded_event_id = match decoded_event.id {
Some(decoded_event_id) => decoded_event_id,
None => return Ok(false),
};
let context = self.resolve_decoded_event_context(decoded_event).await;
let context = match context {
Ok(context) => context,
Err(error) => return Err(error),
};
let actor_wallet = extract_first_string(
payload,
&[
"actorWallet",
"actor_wallet",
"receiver",
"recipient",
"owner",
"payer",
"authority",
"user",
],
);
let reward_token_mint = extract_first_string(
payload,
&["rewardTokenMint", "reward_token_mint", "tokenMint", "token_mint", "mint"],
);
let reward_amount_raw = extract_first_amount_string(
payload,
&[
"rewardAmountRaw",
"reward_amount_raw",
"rewardAmount",
"reward_amount",
"emissionAmount",
"emission_amount",
"amount",
],
);
let dto = crate::RewardEventDto::new(
transaction_id,
Some(decoded_event_id),
context.dex_id,
context.pool_id,
context.pair_id,
transaction.signature.clone(),
transaction.slot,
decoded_event.protocol_name.clone(),
decoded_event.program_id.clone(),
decoded_event.event_kind.clone(),
decoded_event.pool_account.clone(),
actor_wallet,
reward_token_mint,
reward_amount_raw,
decoded_event.payload_json.clone(),
);
let upsert_result = crate::query_reward_events_upsert(self.database.as_ref(), &dto).await;
match upsert_result {
Ok(_) => return Ok(true),
Err(error) => return Err(error),
}
}
async fn materialize_pool_admin_event(
&self,
transaction: &crate::ChainTransactionDto,
transaction_id: i64,
decoded_event: &crate::DexDecodedEventDto,
payload: &serde_json::Value,
) -> Result<bool, crate::Error> {
let decoded_event_id = match decoded_event.id {
Some(decoded_event_id) => decoded_event_id,
None => return Ok(false),
};
let context = self.resolve_decoded_event_context(decoded_event).await;
let context = match context {
Ok(context) => context,
Err(error) => return Err(error),
};
let actor_wallet = extract_first_string(
payload,
&["actorWallet", "actor_wallet", "authority", "admin", "owner", "payer", "user"],
);
let admin_action = match extract_first_string(
payload,
&["adminAction", "admin_action", "action", "configAction", "config_action"],
) {
Some(admin_action) => Some(admin_action),
None => Some(decoded_event.event_kind.clone()),
};
let dto = crate::PoolAdminEventDto::new(
transaction_id,
Some(decoded_event_id),
context.dex_id,
context.pool_id,
context.pair_id,
transaction.signature.clone(),
transaction.slot,
decoded_event.protocol_name.clone(),
decoded_event.program_id.clone(),
decoded_event.event_kind.clone(),
decoded_event.pool_account.clone(),
actor_wallet,
admin_action,
decoded_event.payload_json.clone(),
);
let upsert_result =
crate::query_pool_admin_events_upsert(self.database.as_ref(), &dto).await;
match upsert_result {
Ok(_) => return Ok(true),
Err(error) => return Err(error),
}
}
async fn materialize_liquidity_event(
&self,
transaction: &crate::ChainTransactionDto,
@@ -202,15 +453,16 @@ impl NonTradeEventMaterializationService {
Some(pair_id) => Some(pair_id),
None => None,
};
let event_kind = if crate::is_dex_position_open_event_kind(decoded_event.event_kind.as_str()) {
crate::LiquidityEventKind::PositionOpen
} else if crate::is_dex_position_close_event_kind(decoded_event.event_kind.as_str()) {
crate::LiquidityEventKind::PositionClose
} else if crate::is_dex_liquidity_remove_event_kind(decoded_event.event_kind.as_str()) {
crate::LiquidityEventKind::Remove
} else {
crate::LiquidityEventKind::Add
};
let event_kind =
if crate::is_dex_position_open_event_kind(decoded_event.event_kind.as_str()) {
crate::LiquidityEventKind::PositionOpen
} else if crate::is_dex_position_close_event_kind(decoded_event.event_kind.as_str()) {
crate::LiquidityEventKind::PositionClose
} else if crate::is_dex_liquidity_remove_event_kind(decoded_event.event_kind.as_str()) {
crate::LiquidityEventKind::Remove
} else {
crate::LiquidityEventKind::Add
};
let actor_wallet = extract_first_string(
payload,
&[