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

Add timestamp on historical tables #1221

Merged
merged 5 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
117 changes: 73 additions & 44 deletions indexers/db/docker-entrypoint-initdb.d/init-db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,8 @@ CREATE TABLE staking.accounts (
total_deposits numeric NOT NULL,
total_estimated_withdrawals numeric NOT NULL,
total_withdrawals numeric NOT NULL,
total_deposits_count numeric NOT NULL,
total_withdrawals_count numeric NOT NULL,
total_tax_collected numeric NOT NULL,
current_total_stake numeric NOT NULL,
current_storage_fee_deposit numeric NOT NULL,
Expand Down Expand Up @@ -1179,6 +1181,8 @@ CREATE TABLE staking.bundle_submissions (
consensus_storage_fee numeric NOT NULL,
domain_execution_fee numeric NOT NULL,
burned_balance numeric NOT NULL,
block_height numeric NOT NULL,
extrinsic_id text NOT NULL,
event_id text NOT NULL,
_id uuid NOT NULL,
_block_range int8range NOT NULL
Expand Down Expand Up @@ -1218,6 +1222,7 @@ CREATE TABLE staking.deposit_histories (
effective_domain_epoch_pending integer NOT NULL,
amount_pending numeric NOT NULL,
storage_fee_deposit_pending numeric NOT NULL,
timestamp timestamp with time zone NOT NULL,
block_height numeric NOT NULL,
_id uuid NOT NULL,
_block_range int8range NOT NULL
Expand Down Expand Up @@ -1312,6 +1317,8 @@ CREATE TABLE staking.domains (
total_deposits numeric NOT NULL,
total_estimated_withdrawals numeric NOT NULL,
total_withdrawals numeric NOT NULL,
total_deposits_count numeric NOT NULL,
total_withdrawals_count numeric NOT NULL,
total_tax_collected numeric NOT NULL,
total_rewards_collected numeric NOT NULL,
total_transfers_in numeric NOT NULL,
Expand Down Expand Up @@ -1437,6 +1444,7 @@ CREATE TABLE staking.operator_staking_histories (
total_storage_fee_deposit numeric NOT NULL,
share_price numeric NOT NULL,
partial_status text NOT NULL,
timestamp timestamp with time zone NOT NULL,
block_height numeric NOT NULL,
_id uuid NOT NULL,
_block_range int8range NOT NULL
Expand Down Expand Up @@ -1473,6 +1481,8 @@ CREATE TABLE staking.operators (
total_deposits numeric NOT NULL,
total_estimated_withdrawals numeric NOT NULL,
total_withdrawals numeric NOT NULL,
total_deposits_count numeric NOT NULL,
total_withdrawals_count numeric NOT NULL,
total_tax_collected numeric NOT NULL,
total_rewards_collected numeric NOT NULL,
total_transfers_in numeric NOT NULL,
Expand Down Expand Up @@ -1563,6 +1573,7 @@ CREATE TABLE staking.withdrawal_histories (
unlock_at_confirmed_domain_block_number numeric NOT NULL,
shares numeric NOT NULL,
storage_fee_refund numeric NOT NULL,
timestamp timestamp with time zone NOT NULL,
block_height numeric NOT NULL,
_id uuid NOT NULL,
_block_range int8range NOT NULL
Expand Down Expand Up @@ -2469,6 +2480,8 @@ BEGIN
total_deposits,
total_estimated_withdrawals,
total_withdrawals,
total_deposits_count,
total_withdrawals_count,
total_tax_collected,
total_rewards_collected,
total_transfers_in,
Expand Down Expand Up @@ -2514,6 +2527,8 @@ BEGIN
0, -- total_deposits
0, -- total_estimated_withdrawals
0, -- total_withdrawals
0, -- total_deposits_count
0, -- total_withdrawals_count
0, -- total_tax_collected
0, -- total_rewards_collected
0, -- total_transfers_in
Expand Down Expand Up @@ -2579,6 +2594,8 @@ BEGIN
total_deposits,
total_estimated_withdrawals,
total_withdrawals,
total_deposits_count,
total_withdrawals_count,
total_tax_collected,
total_rewards_collected,
total_transfers_in,
Expand Down Expand Up @@ -2622,6 +2639,8 @@ BEGIN
0, -- total_deposits
0, -- total_estimated_withdrawals
0, -- total_withdrawals
0, -- total_deposits_count
0, -- total_withdrawals_count
0, -- total_tax_collected
0, -- total_rewards_collected
0, -- total_transfers_in
Expand Down Expand Up @@ -2666,12 +2685,14 @@ CREATE OR REPLACE FUNCTION staking.handle_deposit_events() RETURNS TRIGGER
BEGIN
UPDATE staking.domains
SET
total_deposits = staking.domains.total_deposits::NUMERIC(78) + NEW.amount::NUMERIC(78)
total_deposits = staking.domains.total_deposits + NEW.amount,
total_deposits_count = staking.domains.total_deposits_count + 1
WHERE id = NEW.domain_id;

UPDATE staking.operators
SET
total_deposits = staking.operators.total_deposits::NUMERIC(78) + NEW.amount::NUMERIC(78)
total_deposits = staking.operators.total_deposits + NEW.amount,
total_deposits_count = staking.operators.total_deposits_count + 1
WHERE id = NEW.operator_id;

IF NOT EXISTS (
Expand Down Expand Up @@ -2747,8 +2768,8 @@ BEGIN
ELSE
UPDATE staking.nominators
SET
total_deposits = staking.nominators.total_deposits::NUMERIC(78) + NEW.amount::NUMERIC(78),
total_deposits_count = staking.nominators.total_deposits_count::NUMERIC(78) + 1,
total_deposits = staking.nominators.total_deposits + NEW.amount,
total_deposits_count = staking.nominators.total_deposits_count + 1,
updated_at = NEW.block_height
WHERE id = NEW.nominator_id;
END IF;
Expand Down Expand Up @@ -2790,6 +2811,8 @@ BEGIN
total_deposits,
total_estimated_withdrawals,
total_withdrawals,
total_deposits_count,
total_withdrawals_count,
total_tax_collected,
current_total_stake,
current_storage_fee_deposit,
Expand All @@ -2805,6 +2828,8 @@ BEGIN
NEW.total_amount, -- total_deposits (start with the new deposit amount)
0, -- total_estimated_withdrawals
0, -- total_withdrawals
0, -- total_deposits_count
0, -- total_withdrawals_count
0, -- total_tax_collected
NEW.amount, -- current_total_stake
NEW.storage_fee_deposit, -- current_storage_fee_deposit
Expand All @@ -2817,9 +2842,10 @@ BEGIN
NEW.block_height -- updated_at
)
ON CONFLICT (id) DO UPDATE SET
total_deposits = staking.accounts.total_deposits::NUMERIC(78) + NEW.total_amount::NUMERIC(78),
current_total_stake = staking.accounts.current_total_stake::NUMERIC(78) + NEW.amount::NUMERIC(78),
current_storage_fee_deposit = staking.accounts.current_storage_fee_deposit::NUMERIC(78) + NEW.storage_fee_deposit::NUMERIC(78),
total_deposits = staking.accounts.total_deposits + NEW.total_amount,
total_deposits_count = staking.accounts.total_deposits_count + 1,
current_total_stake = staking.accounts.current_total_stake + NEW.amount,
current_storage_fee_deposit = staking.accounts.current_storage_fee_deposit + NEW.storage_fee_deposit,
updated_at = NEW.block_height;

RETURN NEW;
Expand Down Expand Up @@ -2903,17 +2929,17 @@ BEGIN

UPDATE staking.domains
SET
total_tax_collected = staking.domains.total_tax_collected::NUMERIC(78) + NEW.amount::NUMERIC(78)
total_tax_collected = staking.domains.total_tax_collected + NEW.amount
WHERE id = NEW.domain_id;

UPDATE staking.operators
SET
total_tax_collected = staking.operators.total_tax_collected::NUMERIC(78) + NEW.amount::NUMERIC(78)
total_tax_collected = staking.operators.total_tax_collected + NEW.amount
WHERE id = NEW.operator_id;

UPDATE staking.accounts
SET
total_tax_collected = staking.accounts.total_tax_collected::NUMERIC(78) + NEW.amount::NUMERIC(78)
total_tax_collected = staking.accounts.total_tax_collected + NEW.amount
WHERE id = operator_account_id;

RETURN NEW;
Expand All @@ -2932,12 +2958,12 @@ CREATE OR REPLACE FUNCTION staking.handle_operator_rewards_events() RETURNS TRIG
BEGIN
UPDATE staking.domains
SET
total_rewards_collected = staking.domains.total_rewards_collected::NUMERIC(78) + NEW.amount::NUMERIC(78)
total_rewards_collected = staking.domains.total_rewards_collected + NEW.amount
WHERE id = NEW.domain_id;

UPDATE staking.operators
SET
total_rewards_collected = staking.operators.total_rewards_collected::NUMERIC(78) + NEW.amount::NUMERIC(78)
total_rewards_collected = staking.operators.total_rewards_collected + NEW.amount
WHERE id = NEW.operator_id;

RETURN NEW;
Expand All @@ -2956,37 +2982,37 @@ CREATE OR REPLACE FUNCTION staking.handle_bundle_submissions_events() RETURNS TR
BEGIN
UPDATE staking.domains
SET
total_transfers_in = staking.domains.total_transfers_in::NUMERIC(78) + NEW.total_transfers_in::NUMERIC(78),
transfers_in_count = staking.domains.transfers_in_count::NUMERIC(78) + NEW.transfers_in_count::NUMERIC(78),
total_transfers_out = staking.domains.total_transfers_out::NUMERIC(78) + NEW.total_transfers_out::NUMERIC(78),
transfers_out_count = staking.domains.transfers_out_count::NUMERIC(78) + NEW.transfers_out_count::NUMERIC(78),
total_rejected_transfers_claimed = staking.domains.total_rejected_transfers_claimed::NUMERIC(78) + NEW.total_rejected_transfers_claimed::NUMERIC(78),
rejected_transfers_claimed_count = staking.domains.rejected_transfers_claimed_count::NUMERIC(78) + NEW.rejected_transfers_claimed_count::NUMERIC(78),
total_transfers_rejected = staking.domains.total_transfers_rejected::NUMERIC(78) + NEW.total_transfers_rejected::NUMERIC(78),
transfers_rejected_count = staking.domains.transfers_rejected_count::NUMERIC(78) + NEW.transfers_rejected_count::NUMERIC(78),
total_volume = staking.domains.total_volume::NUMERIC(78) + NEW.total_volume::NUMERIC(78),
total_consensus_storage_fee = staking.domains.total_consensus_storage_fee::NUMERIC(78) + NEW.consensus_storage_fee::NUMERIC(78),
total_domain_execution_fee = staking.domains.total_domain_execution_fee::NUMERIC(78) + NEW.domain_execution_fee::NUMERIC(78),
total_burned_balance = staking.domains.total_burned_balance::NUMERIC(78) + NEW.burned_balance::NUMERIC(78) ,
total_transfers_in = staking.domains.total_transfers_in + NEW.total_transfers_in,
transfers_in_count = staking.domains.transfers_in_count + NEW.transfers_in_count,
total_transfers_out = staking.domains.total_transfers_out + NEW.total_transfers_out,
transfers_out_count = staking.domains.transfers_out_count + NEW.transfers_out_count,
total_rejected_transfers_claimed = staking.domains.total_rejected_transfers_claimed + NEW.total_rejected_transfers_claimed,
rejected_transfers_claimed_count = staking.domains.rejected_transfers_claimed_count + NEW.rejected_transfers_claimed_count,
total_transfers_rejected = staking.domains.total_transfers_rejected + NEW.total_transfers_rejected,
transfers_rejected_count = staking.domains.transfers_rejected_count + NEW.transfers_rejected_count,
total_volume = staking.domains.total_volume + NEW.total_volume,
total_consensus_storage_fee = staking.domains.total_consensus_storage_fee + NEW.consensus_storage_fee,
total_domain_execution_fee = staking.domains.total_domain_execution_fee + NEW.domain_execution_fee,
total_burned_balance = staking.domains.total_burned_balance + NEW.burned_balance ,
bundle_count = staking.domains.bundle_count + 1,
last_bundle_at = NEW.consensus_block_number
WHERE id = NEW.domain_id;

UPDATE staking.operators
SET
total_transfers_in = staking.operators.total_transfers_in::NUMERIC(78) + NEW.total_transfers_in::NUMERIC(78),
transfers_in_count = staking.operators.transfers_in_count::NUMERIC(78) + NEW.transfers_in_count::NUMERIC(78),
total_transfers_out = staking.operators.total_transfers_out::NUMERIC(78) + NEW.total_transfers_out::NUMERIC(78),
transfers_out_count = staking.operators.transfers_out_count::NUMERIC(78) + NEW.transfers_out_count::NUMERIC(78),
total_rejected_transfers_claimed = staking.operators.total_rejected_transfers_claimed::NUMERIC(78) + NEW.total_rejected_transfers_claimed::NUMERIC(78),
rejected_transfers_claimed_count = staking.operators.rejected_transfers_claimed_count::NUMERIC(78) + NEW.rejected_transfers_claimed_count::NUMERIC(78),
total_transfers_rejected = staking.operators.total_transfers_rejected::NUMERIC(78) + NEW.total_transfers_rejected::NUMERIC(78),
transfers_rejected_count = staking.operators.transfers_rejected_count::NUMERIC(78) + NEW.transfers_rejected_count::NUMERIC(78),
total_volume = staking.operators.total_volume::NUMERIC(78) + NEW.total_volume::NUMERIC(78),
total_consensus_storage_fee = staking.operators.total_consensus_storage_fee::NUMERIC(78) + NEW.consensus_storage_fee::NUMERIC(78),
total_domain_execution_fee = staking.operators.total_domain_execution_fee::NUMERIC(78) + NEW.domain_execution_fee::NUMERIC(78),
total_burned_balance = staking.operators.total_burned_balance::NUMERIC(78) + NEW.burned_balance::NUMERIC(78),
bundle_count = staking.operators.bundle_count::NUMERIC(78) + 1,
total_transfers_in = staking.operators.total_transfers_in + NEW.total_transfers_in,
transfers_in_count = staking.operators.transfers_in_count + NEW.transfers_in_count,
total_transfers_out = staking.operators.total_transfers_out + NEW.total_transfers_out,
transfers_out_count = staking.operators.transfers_out_count + NEW.transfers_out_count,
total_rejected_transfers_claimed = staking.operators.total_rejected_transfers_claimed + NEW.total_rejected_transfers_claimed,
rejected_transfers_claimed_count = staking.operators.rejected_transfers_claimed_count + NEW.rejected_transfers_claimed_count,
total_transfers_rejected = staking.operators.total_transfers_rejected + NEW.total_transfers_rejected,
transfers_rejected_count = staking.operators.transfers_rejected_count + NEW.transfers_rejected_count,
total_volume = staking.operators.total_volume + NEW.total_volume,
total_consensus_storage_fee = staking.operators.total_consensus_storage_fee + NEW.consensus_storage_fee,
total_domain_execution_fee = staking.operators.total_domain_execution_fee + NEW.domain_execution_fee,
total_burned_balance = staking.operators.total_burned_balance + NEW.burned_balance,
bundle_count = staking.operators.bundle_count + 1,
last_bundle_at = NEW.consensus_block_number
WHERE id = NEW.operator_id;

Expand Down Expand Up @@ -3106,28 +3132,31 @@ CREATE OR REPLACE FUNCTION staking.handle_unlocked_events() RETURNS TRIGGER
BEGIN
UPDATE staking.domains
SET
total_withdrawals = staking.domains.total_withdrawals::NUMERIC(78) + NEW.amount::NUMERIC(78),
total_withdrawals = staking.domains.total_withdrawals + NEW.amount,
total_withdrawals_count = staking.domains.total_withdrawals_count + 1,
updated_at = NEW.block_height
WHERE id = NEW.domain_id;

UPDATE staking.operators
SET
total_withdrawals = staking.operators.total_withdrawals::NUMERIC(78) + NEW.amount::NUMERIC(78),
total_withdrawals = staking.operators.total_withdrawals + NEW.amount,
total_withdrawals_count = staking.operators.total_withdrawals_count + 1,
updated_at = NEW.block_height
WHERE id = NEW.operator_id;

UPDATE staking.nominators
SET
total_withdrawal_amounts = staking.nominators.total_withdrawal_amounts::NUMERIC(78) + NEW.amount::NUMERIC(78),
total_storage_fee_refund = staking.nominators.total_storage_fee_refund::NUMERIC(78) + NEW.storage_fee::NUMERIC(78),
total_withdrawals = staking.nominators.total_withdrawals::NUMERIC(78) + NEW.amount::NUMERIC(78),
total_withdrawals_count = staking.nominators.total_withdrawal_amounts::NUMERIC(78) + 1,
total_withdrawal_amounts = staking.nominators.total_withdrawal_amounts + NEW.amount,
total_storage_fee_refund = staking.nominators.total_storage_fee_refund + NEW.storage_fee,
total_withdrawals = staking.nominators.total_withdrawals + NEW.amount,
total_withdrawals_count = staking.nominators.total_withdrawals_count + 1,
updated_at = NEW.block_height
WHERE id = NEW.nominator_id;

UPDATE staking.accounts
SET
total_withdrawals = staking.accounts.total_withdrawals::NUMERIC(78) + NEW.amount::NUMERIC(78),
total_withdrawals = staking.accounts.total_withdrawals + NEW.amount,
total_withdrawals_count = staking.accounts.total_withdrawals_count + 1,
updated_at = NEW.block_height
WHERE id = NEW.account_id;

Expand Down
5 changes: 5 additions & 0 deletions indexers/staking/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ type BundleSubmission @entity {
consensusStorageFee: BigInt!
domainExecutionFee: BigInt!
burnedBalance: BigInt!
blockHeight: BigInt!
extrinsicId: String!
eventId: String!
}

Expand Down Expand Up @@ -177,6 +179,7 @@ type OperatorStakingHistory @entity {
totalStorageFeeDeposit: BigInt!
sharePrice: BigInt!
partialStatus: String!
timestamp: Date!
blockHeight: BigInt!
}

Expand All @@ -194,6 +197,7 @@ type DepositHistory @entity {
effectiveDomainEpochPending: Int!
amountPending: BigInt!
storageFeeDepositPending: BigInt!
timestamp: Date!
blockHeight: BigInt!
}

Expand All @@ -208,5 +212,6 @@ type WithdrawalHistory @entity {
unlockAtConfirmedDomainBlockNumber: BigInt!
shares: BigInt!
storageFeeRefund: BigInt!
timestamp: Date!
blockHeight: BigInt!
}
10 changes: 10 additions & 0 deletions indexers/staking/src/mappings/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ export function createBundleSubmission(
consensusStorageFee: bigint,
domainExecutionFee: bigint,
burnedBalance: bigint,
blockHeight: bigint,
extrinsicId: string,
eventId: string
): BundleSubmission {
return BundleSubmission.create({
Expand Down Expand Up @@ -342,6 +344,8 @@ export function createBundleSubmission(
consensusStorageFee,
domainExecutionFee,
burnedBalance,
blockHeight,
extrinsicId,
eventId,
});
}
Expand Down Expand Up @@ -393,6 +397,7 @@ export function createOperatorStakingHistory(
totalStorageFeeDeposit: bigint,
sharePrice: bigint,
partialStatus: string,
timestamp: Date,
blockHeight: bigint
): OperatorStakingHistory {
return OperatorStakingHistory.create({
Expand All @@ -408,6 +413,7 @@ export function createOperatorStakingHistory(
totalStorageFeeDeposit,
sharePrice,
partialStatus,
timestamp,
blockHeight,
});
}
Expand All @@ -425,6 +431,7 @@ export function createDepositHistory(
effectiveDomainEpochPending: number,
amountPending: bigint,
storageFeeDepositPending: bigint,
timestamp: Date,
blockHeight: bigint
): DepositHistory {
return DepositHistory.create({
Expand All @@ -441,6 +448,7 @@ export function createDepositHistory(
effectiveDomainEpochPending,
amountPending,
storageFeeDepositPending,
timestamp,
blockHeight,
});
}
Expand All @@ -455,6 +463,7 @@ export function createWithdrawalHistory(
unlockAtConfirmedDomainBlockNumber: bigint,
shares: bigint,
storageFeeRefund: bigint,
timestamp: Date,
blockHeight: bigint
): WithdrawalHistory {
return WithdrawalHistory.create({
Expand All @@ -468,6 +477,7 @@ export function createWithdrawalHistory(
unlockAtConfirmedDomainBlockNumber,
shares,
storageFeeRefund,
timestamp,
blockHeight,
});
}
Loading