Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

flusher: added temporary delegations and validator votes view tables #2186

Merged
merged 4 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

### Emitter & Flusher

- (impv) [\#2186](https://github.com/bandprotocol/bandchain/pull/2142) Add temporary view tables.
- (impv) [\#2142](https://github.com/bandprotocol/bandchain/pull/2142) Add account transactions table.
- (impv) [\#2170](https://github.com/bandprotocol/bandchain/pull/2170) Add validator status field in table.
- (impv) [\#2169](https://github.com/bandprotocol/bandchain/pull/2169) Add unbonding and redelegation table.
Expand Down
31 changes: 28 additions & 3 deletions flusher/flusher/init.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import click
import json
from sqlalchemy import create_engine

from .cli import cli
import click

from .db import metadata, tracking
from .cli import cli

from sqlalchemy import create_engine


@cli.command()
Expand All @@ -20,4 +22,27 @@ def init(chain_id, topic, db):
engine = create_engine("postgresql+psycopg2://" + db, echo=True)
metadata.create_all(engine)
engine.execute(tracking.insert(), {"chain_id": chain_id, "topic": topic, "kafka_offset": -1})
engine.execute('''CREATE VIEW delegations_view AS
SELECT CAST(shares AS DECIMAL) * CAST(tokens AS DECIMAL) / CAST(delegator_shares AS DECIMAL) as amount,
CAST(shares AS DECIMAL) / CAST(delegator_shares AS DECIMAL) * 100 as share_percentage,
CAST(shares AS DECIMAL) * CAST(current_reward AS DECIMAL) / CAST(delegator_shares AS DECIMAL) + (CAST(current_ratio AS DECIMAL) - CAST(last_ratio AS DECIMAL)) * CAST(shares AS DECIMAL) as reward,
delegations.operator_address,
moniker,
delegator_address
FROM delegations JOIN validators ON delegations.operator_address=validators.operator_address; ''')
engine.execute('''CREATE VIEW validator_last_250_votes AS
SELECT COUNT(*), consensus_address, voted
FROM validator_votes
WHERE block_height > (SELECT MAX(height) from blocks) - 251
GROUP BY consensus_address, voted; ''')
engine.execute('''CREATE VIEW validator_last_1000_votes AS
SELECT COUNT(*), consensus_address, voted
FROM validator_votes
WHERE block_height > (SELECT MAX(height) from blocks) - 1001
GROUP BY consensus_address, voted; ''')
engine.execute('''CREATE VIEW validator_last_10000_votes AS
SELECT COUNT(*), consensus_address, voted
FROM validator_votes
WHERE block_height > (SELECT MAX(height) from blocks) - 10000
GROUP BY consensus_address, voted;''')