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

R0.18.3 add stats fields #2

Open
wants to merge 4 commits into
base: R0.18.3_minor_update
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add rune entries total holders
  • Loading branch information
nhathuyle2002 committed Apr 26, 2024
commit d568b5c0d1a241d7639f77c67847fd81da1d71f8
4 changes: 3 additions & 1 deletion migrations/2024-04-24-105512_statistic_addition/down.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- This file should undo anything in `up.sql`
ALTER TABLE rune_stats DROP tx_count;
ALTER TABLE transaction_rune_entries DROP total_tx_count;
ALTER TABLE transaction_rune_entries DROP total_tx_count;

ALTER TABLE transaction_rune_entries DROP total_holders;
4 changes: 3 additions & 1 deletion migrations/2024-04-24-105512_statistic_addition/up.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- Your SQL goes here
ALTER TABLE rune_stats ADD tx_count Int8 NOT NULL DEFAULT 0;
ALTER TABLE transaction_rune_entries ADD total_tx_count Int8 NOT NULL DEFAULT 0;
ALTER TABLE transaction_rune_entries ADD total_tx_count Int8 NOT NULL DEFAULT 0;

ALTER TABLE transaction_rune_entries ADD total_holders Int8 NOT NULL DEFAULT 0;
25 changes: 13 additions & 12 deletions src/runebeta/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,8 @@ impl IndexExtension {
if balances.is_empty() {
continue;
}
if let Some(tx_out) = transaction.output.get(vout) {
for (rune_id, lot) in balances {
if let Some(_) = transaction.output.get(vout) {
for (rune_id, _) in balances {
index_block.add_rune_tx_count(block_height, rune_id);
}
}
Expand Down Expand Up @@ -812,9 +812,16 @@ impl IndexExtension {
{
handles.append(res);
}
if total_rune_stats.len() > 0 {
// update spent_outpoint_balance before update stats
if total_tx_ins.len() > 0 {
//move spent utxo to spent tx_output table
if let Ok(ref mut res) =
table_rune_stats.update(total_rune_stats, self.connection_pool.clone())
table_outpoint_balance.spends(total_tx_ins.clone(), self.connection_pool.clone())
{
handles.append(res);
}
if let Ok(ref mut res) =
table_transaction_out.spends(total_tx_ins, self.connection_pool.clone())
{
handles.append(res);
}
Expand All @@ -826,15 +833,9 @@ impl IndexExtension {
handles.append(res);
}
}
if total_tx_ins.len() > 0 {
//move spent utxo to spent tx_output table
if let Ok(ref mut res) =
table_outpoint_balance.spends(total_tx_ins.clone(), self.connection_pool.clone())
{
handles.append(res);
}
if total_rune_stats.len() > 0 {
if let Ok(ref mut res) =
table_transaction_out.spends(total_tx_ins, self.connection_pool.clone())
table_rune_stats.update(total_rune_stats, self.connection_pool.clone())
{
handles.append(res);
}
Expand Down
2 changes: 2 additions & 0 deletions src/runebeta/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ pub struct NewTxRuneEntry {
pub divisibility: i16,
pub etching: String,
pub parent: Option<String>,
pub total_tx_count: i64,
pub total_holders: i64,
pub terms: Option<RuneTerms>,
pub height_start: Option<i64>,
pub height_end: Option<i64>,
Expand Down
1 change: 1 addition & 0 deletions src/runebeta/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ diesel::table! {
turbo -> Bool,
timestamp -> Int4,
total_tx_count -> Int8,
total_holders -> Int8,
}
}

Expand Down
40 changes: 27 additions & 13 deletions src/runebeta/table_rune_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ use crate::schema::rune_stats::dsl::*;
use crate::{calculate_chunk_size, split_input, InsertRecords};

use super::models::NewRuneStats;
use super::table_transaction_rune_entry::create_update_rune_mintable;
pub const NUMBER_OF_FIELDS: u16 = 8;
use super::table_transaction_rune_entry::{create_update_rune_mintable, create_update_rune_total_holders};
pub const NUMBER_OF_FIELDS: u16 = 9;

pub fn create_update_rune_entry(height: &u64) -> SqlQuery {
let query = format!(
r#"
UPDATE transaction_rune_entries e SET
mints = e.mints + s.mints,
supply = e.supply + s.mint_amount * s.mints,
burned = e.burned + s.burned,
remaining = e.remaining - s.mints
total_tx_count = e.total_tx_count + s.tx_count
FROM rune_stats s
WHERE s.block_height = {}
AND s.aggregated = false
AND e.rune_id = s.rune_id;
"#,
UPDATE transaction_rune_entries e SET
mints = e.mints + s.mints,
supply = e.supply + s.mint_amount * s.mints,
burned = e.burned + s.burned,
remaining = e.remaining - s.mints,
total_tx_count = e.total_tx_count + s.tx_count
FROM rune_stats s
WHERE s.block_height = {}
AND s.aggregated = false
AND e.rune_id = s.rune_id;
"#,
height
);
diesel::sql_query(query)
Expand Down Expand Up @@ -136,6 +136,20 @@ impl RuneStatsTable {
}
};
}
//Update total holders for rune entries
let update_rune_total_holders = create_update_rune_total_holders();
let stat_res = update_rune_total_holders.execute(conn);
match &stat_res {
Ok(_) => log::info!(
"Updated rune entries total holders for blocks {:?} in {} ms",
heights,
start.elapsed().as_millis()
),
Err(err) => {
log::info!("Update rune entries total holders error {:?}", err);
return stat_res;
}
};

Ok(size)
});
Expand Down
20 changes: 19 additions & 1 deletion src/runebeta/table_transaction_rune_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::schema::transaction_rune_entries::dsl::*;
use crate::{InsertRecords, RuneEntry, RuneId};

use super::models::{NewTxRuneEntry, RuneTerms};
pub const NUMBER_OF_FIELDS: u16 = 28;
pub const NUMBER_OF_FIELDS: u16 = 30;
pub const RUNE_MINT_TYPE_FIXED_CAP: &str = "fixed-cap";
pub const RUNE_MINT_TYPE_FAIRMINT: &str = "fairmint";

Expand All @@ -24,6 +24,22 @@ pub fn create_update_rune_mintable(height: &u64) -> SqlQuery {
diesel::sql_query(query)
}

pub fn create_update_rune_total_holders() -> SqlQuery {
let query = r"
WITH rune_total_holders AS (
SELECT COUNT(DISTINCT address) AS total_holders, rune_id
FROM outpoint_rune_balances
WHERE balance_value > 0
GROUP BY rune_id
)
UPDATE transaction_rune_entries
SET total_holders = rtd.total_holders
FROM rune_total_holders rtd
WHERE transaction_rune_entries.rune_id = rtd.rune_id;
";
diesel::sql_query(query)
}

#[derive(Clone)]
pub struct TransactionRuneEntryTable {}

Expand Down Expand Up @@ -69,6 +85,8 @@ impl From<&RuneEntry> for NewTxRuneEntry {
divisibility: rune_entry.divisibility as i16,
etching: rune_entry.etching.to_string(),
parent: None,
total_tx_count: 0,
total_holders: 0,
mintable: rune_entry.mintable(rune_entry.block).is_ok(),
mint_type: rune_entry.terms.map_or_else(
|| String::from(RUNE_MINT_TYPE_FIXED_CAP),
Expand Down