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

[DRAFT] Implement account rewards #335

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions backend-rust/migrations/0001_initialize.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ CREATE TYPE pool_open_status AS ENUM (
'ClosedForAll'
);

CREATE TYPE account_statement_entry_type AS ENUM (
'TransferIn',
'TransferOut',
'AmountDecrypted',
'AmountEncrypted',
'TransactionFee',
'FinalizationReward',
'FoundationReward',
'BakerReward',
'TransactionFeeReward'
);

CREATE TYPE account_statement_reward_type AS ENUM (
'FinalizationReward',
'FoundationReward',
'BakerReward',
'TransactionFeeReward'
);

-- Every block on chain.
CREATE TABLE blocks(
-- The absolute height of the block.
Expand Down Expand Up @@ -224,6 +243,7 @@ CREATE TABLE accounts(
CREATE INDEX accounts_amount_idx ON accounts (amount);
CREATE INDEX accounts_delegated_stake_idx ON accounts (delegated_stake);
CREATE INDEX accounts_num_txs_idx ON accounts (num_txs);
CREATE INDEX accounts_address_index ON accounts (address);

-- Add foreign key constraint now that the account table is created.
ALTER TABLE transactions
Expand Down Expand Up @@ -453,3 +473,19 @@ $trigger$ LANGUAGE plpgsql;
CREATE TRIGGER account_updated_notify_trigger AFTER INSERT
ON affected_accounts
FOR EACH ROW EXECUTE PROCEDURE account_updated_notify_trigger_function();

CREATE TABLE account_statements (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
account_id BIGINT REFERENCES accounts(index) NOT NULL,
timestamp TIMESTAMPTZ NOT NULL,
entry_type account_statement_entry_type NOT NULL,
amount BIGINT NOT NULL,
block_height BIGINT REFERENCES blocks(height) NOT NULL
);

CREATE INDEX account_statements_entry_type_idx ON account_statements (entry_type);

CREATE VIEW account_rewards AS
SELECT id, account_id, timestamp, entry_type::TEXT::account_statement_reward_type AS reward_type, amount, block_height
FROM account_statements
WHERE entry_type::TEXT IN (SELECT unnest(enum_range(NULL::account_statement_reward_type))::TEXT);
Loading