From 20ec8f069c832c0faa972692f8a55b4fabc7fc47 Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Wed, 28 Feb 2024 12:24:33 -0500 Subject: [PATCH] add delegated amount --- src/analytics/influx.rs | 5 +- src/analytics/ledger/ledger_outputs.rs | 85 ++++++++++++++++++++------ 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/src/analytics/influx.rs b/src/analytics/influx.rs index 0c4d18212..7c6759a1d 100644 --- a/src/analytics/influx.rs +++ b/src/analytics/influx.rs @@ -260,8 +260,8 @@ impl Measurement for LedgerOutputMeasurement { query .add_field("basic_count", self.basic.count as u64) .add_field("basic_amount", self.basic.amount) - .add_field("account_count", self.account.count_and_amount.count as u64) - .add_field("account_amount", self.account.count_and_amount.amount) + .add_field("account_count", self.account.count as u64) + .add_field("account_amount", self.account.amount) .add_field("block_issuer_accounts", self.account.block_issuers_count as u64) .add_field("anchor_count", self.anchor.count as u64) .add_field("anchor_amount", self.anchor.amount) @@ -271,6 +271,7 @@ impl Measurement for LedgerOutputMeasurement { .add_field("nft_amount", self.nft.amount) .add_field("delegation_count", self.delegation.count as u64) .add_field("delegation_amount", self.delegation.amount) + .add_field("delegated_amount", self.delegation.delegated_amount) } } diff --git a/src/analytics/ledger/ledger_outputs.rs b/src/analytics/ledger/ledger_outputs.rs index 077569650..9132e131b 100644 --- a/src/analytics/ledger/ledger_outputs.rs +++ b/src/analytics/ledger/ledger_outputs.rs @@ -5,9 +5,12 @@ use std::collections::HashSet; -use iota_sdk::types::block::{ - output::{AccountId, Output}, - payload::SignedTransactionPayload, +use iota_sdk::{ + types::block::{ + output::{AccountId, AccountOutput, DelegationOutput, Output}, + payload::SignedTransactionPayload, + }, + utils::serde::string, }; use serde::{Deserialize, Serialize}; @@ -19,12 +22,12 @@ use crate::{ #[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] pub(crate) struct LedgerOutputMeasurement { - pub(crate) account: AccountOutputMeasurement, + pub(crate) account: AccountCountAndAmount, pub(crate) basic: CountAndAmount, pub(crate) nft: CountAndAmount, pub(crate) foundry: CountAndAmount, pub(crate) anchor: CountAndAmount, - pub(crate) delegation: CountAndAmount, + pub(crate) delegation: DelegationCountAndAmount, } impl LedgerOutputMeasurement { @@ -33,17 +36,12 @@ impl LedgerOutputMeasurement { let mut measurement = Self::default(); for output in unspent_outputs { match output.output() { - Output::Account(account_output) => { - measurement.account.count_and_amount.add_output(output); - if account_output.is_block_issuer() { - measurement.account.block_issuers_count += 1; - } - } + Output::Account(o) => measurement.account.add_account_output(o), Output::Basic(_) => measurement.basic.add_output(output), Output::Nft(_) => measurement.nft.add_output(output), Output::Foundry(_) => measurement.foundry.add_output(output), Output::Anchor(_) => measurement.anchor.add_output(output), - Output::Delegation(_) => measurement.delegation.add_output(output), + Output::Delegation(o) => measurement.delegation.add_delegation_output(o), } } measurement @@ -116,19 +114,68 @@ impl Analytics for LedgerOutputMeasurement { } #[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] -pub(crate) struct AccountOutputMeasurement { - pub(crate) count_and_amount: CountAndAmount, +pub(crate) struct AccountCountAndAmount { + pub(crate) count: usize, + #[serde(with = "string")] + pub(crate) amount: u64, pub(crate) block_issuers_count: usize, } -impl AccountOutputMeasurement { +impl AccountCountAndAmount { fn wrapping_add(&mut self, rhs: Self) { - self.count_and_amount.wrapping_add(rhs.count_and_amount); - self.block_issuers_count = self.block_issuers_count.wrapping_add(rhs.block_issuers_count); + *self = Self { + count: self.count.wrapping_add(rhs.count), + amount: self.amount.wrapping_add(rhs.amount), + block_issuers_count: self.block_issuers_count.wrapping_add(rhs.block_issuers_count), + } } fn wrapping_sub(&mut self, rhs: Self) { - self.count_and_amount.wrapping_sub(rhs.count_and_amount); - self.block_issuers_count = self.block_issuers_count.wrapping_sub(rhs.block_issuers_count); + *self = Self { + count: self.count.wrapping_sub(rhs.count), + amount: self.amount.wrapping_sub(rhs.amount), + block_issuers_count: self.block_issuers_count.wrapping_sub(rhs.block_issuers_count), + } + } + + fn add_account_output(&mut self, account: &AccountOutput) { + self.count += 1; + self.amount += account.amount(); + if account.is_block_issuer() { + self.block_issuers_count += 1; + } + } +} + +#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] +pub(crate) struct DelegationCountAndAmount { + pub(crate) count: usize, + #[serde(with = "string")] + pub(crate) amount: u64, + #[serde(with = "string")] + pub(crate) delegated_amount: u64, +} + +impl DelegationCountAndAmount { + fn wrapping_add(&mut self, rhs: Self) { + *self = Self { + count: self.count.wrapping_add(rhs.count), + amount: self.amount.wrapping_add(rhs.amount), + delegated_amount: self.delegated_amount.wrapping_add(rhs.delegated_amount), + } + } + + fn wrapping_sub(&mut self, rhs: Self) { + *self = Self { + count: self.count.wrapping_sub(rhs.count), + amount: self.amount.wrapping_sub(rhs.amount), + delegated_amount: self.delegated_amount.wrapping_sub(rhs.delegated_amount), + } + } + + fn add_delegation_output(&mut self, delegation: &DelegationOutput) { + self.count += 1; + self.amount += delegation.amount(); + self.delegated_amount += delegation.delegated_amount(); } }