-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Staking Models and Create Parquet Stake Processor (#647)
- Loading branch information
1 parent
3064a07
commit 91a9826
Showing
31 changed files
with
2,063 additions
and
920 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
rust/processor/src/db/common/models/stake_models/delegator_activities.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright © Aptos Foundation | ||
|
||
// This is required because a diesel macro makes clippy sad | ||
#![allow(clippy::extra_unused_lifetimes)] | ||
|
||
use crate::{ | ||
db::common::models::stake_models::stake_utils::StakeEvent, | ||
utils::{ | ||
counters::PROCESSOR_UNKNOWN_TYPE_COUNT, | ||
util::{parse_timestamp, standardize_address, u64_to_bigdecimal}, | ||
}, | ||
}; | ||
use aptos_protos::transaction::v1::{transaction::TxnData, Transaction}; | ||
use bigdecimal::BigDecimal; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Deserialize, Serialize)] | ||
pub struct RawDelegatedStakingActivity { | ||
pub transaction_version: i64, | ||
pub event_index: i64, | ||
pub delegator_address: String, | ||
pub pool_address: String, | ||
pub event_type: String, | ||
pub amount: BigDecimal, | ||
pub block_timestamp: chrono::NaiveDateTime, | ||
} | ||
pub trait RawDelegatedStakingActivityConvertible { | ||
fn from_raw(raw: RawDelegatedStakingActivity) -> Self; | ||
} | ||
|
||
impl RawDelegatedStakingActivity { | ||
/// Pretty straightforward parsing from known delegated staking events | ||
pub fn from_transaction(transaction: &Transaction) -> anyhow::Result<Vec<Self>> { | ||
let mut delegator_activities = vec![]; | ||
let txn_data = match transaction.txn_data.as_ref() { | ||
Some(data) => data, | ||
None => { | ||
PROCESSOR_UNKNOWN_TYPE_COUNT | ||
.with_label_values(&["DelegatedStakingActivity"]) | ||
.inc(); | ||
tracing::warn!( | ||
transaction_version = transaction.version, | ||
"Transaction data doesn't exist", | ||
); | ||
return Ok(delegator_activities); | ||
}, | ||
}; | ||
|
||
let txn_version = transaction.version as i64; | ||
let events = match txn_data { | ||
TxnData::User(txn) => &txn.events, | ||
TxnData::BlockMetadata(txn) => &txn.events, | ||
TxnData::Validator(txn) => &txn.events, | ||
_ => return Ok(delegator_activities), | ||
}; | ||
let block_timestamp = parse_timestamp(transaction.timestamp.as_ref().unwrap(), txn_version); | ||
for (index, event) in events.iter().enumerate() { | ||
let event_index = index as i64; | ||
if let Some(staking_event) = | ||
StakeEvent::from_event(event.type_str.as_str(), &event.data, txn_version)? | ||
{ | ||
let activity = match staking_event { | ||
StakeEvent::AddStakeEvent(inner) => RawDelegatedStakingActivity { | ||
transaction_version: txn_version, | ||
event_index, | ||
delegator_address: standardize_address(&inner.delegator_address), | ||
pool_address: standardize_address(&inner.pool_address), | ||
event_type: event.type_str.clone(), | ||
amount: u64_to_bigdecimal(inner.amount_added), | ||
block_timestamp, | ||
}, | ||
StakeEvent::UnlockStakeEvent(inner) => RawDelegatedStakingActivity { | ||
transaction_version: txn_version, | ||
event_index, | ||
delegator_address: standardize_address(&inner.delegator_address), | ||
pool_address: standardize_address(&inner.pool_address), | ||
event_type: event.type_str.clone(), | ||
amount: u64_to_bigdecimal(inner.amount_unlocked), | ||
block_timestamp, | ||
}, | ||
StakeEvent::WithdrawStakeEvent(inner) => RawDelegatedStakingActivity { | ||
transaction_version: txn_version, | ||
event_index, | ||
delegator_address: standardize_address(&inner.delegator_address), | ||
pool_address: standardize_address(&inner.pool_address), | ||
event_type: event.type_str.clone(), | ||
amount: u64_to_bigdecimal(inner.amount_withdrawn), | ||
block_timestamp, | ||
}, | ||
StakeEvent::ReactivateStakeEvent(inner) => RawDelegatedStakingActivity { | ||
transaction_version: txn_version, | ||
event_index, | ||
delegator_address: standardize_address(&inner.delegator_address), | ||
pool_address: standardize_address(&inner.pool_address), | ||
event_type: event.type_str.clone(), | ||
amount: u64_to_bigdecimal(inner.amount_reactivated), | ||
block_timestamp, | ||
}, | ||
StakeEvent::DistributeRewardsEvent(inner) => RawDelegatedStakingActivity { | ||
transaction_version: txn_version, | ||
event_index, | ||
delegator_address: "".to_string(), | ||
pool_address: standardize_address(&inner.pool_address), | ||
event_type: event.type_str.clone(), | ||
amount: u64_to_bigdecimal(inner.rewards_amount), | ||
block_timestamp, | ||
}, | ||
_ => continue, | ||
}; | ||
delegator_activities.push(activity); | ||
} | ||
} | ||
Ok(delegator_activities) | ||
} | ||
} |
Oops, something went wrong.