Skip to content

Commit

Permalink
feat: add support for storing multiple batch denoms per retirement
Browse files Browse the repository at this point in the history
  • Loading branch information
blushi committed Oct 21, 2024
1 parent e580612 commit 232e02f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
20 changes: 15 additions & 5 deletions index_retires.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,30 @@ def _index_retires(pg_conn, _client, _chain_num):
normalize["chain_num"] = chain_num
normalize["timestamp"] = timestamp
normalize["tx_hash"] = tx_hash
normalize["batch_denoms"] = []
normalize["amount"] = 0

for entry in event:
(_, _, _, _, key, value, _, _, _) = entry
value = value.strip('"')
if "v1alpha1.EventRetire" in entry[0]:
if key == "amount":
normalize["amount"] = value
normalize["amount"] = normalize["amount"] + float(value)
elif key == "batch_denom":
normalize["batch_denom"] = value
normalize["batch_denoms"].append(value)
elif key == "location":
normalize["jurisdiction"] = value
elif key == "retirer":
normalize["owner"] = value
elif "v1.EventRetire" in entry[0]:
normalize[key] = value
if key == "amount":
normalize["amount"] = normalize["amount"] + float(value)
elif key == "batch_denom":
normalize["batch_denoms"].append(value)
elif key == "jurisdiction":
normalize["jurisdiction"] = value
elif key == "owner":
normalize["owner"] = value
with pg_conn.cursor() as _cur:
_cur.execute(
"""SELECT TRIM(BOTH '"' FROM (tx.data -> 'tx' -> 'body' -> 'memo')::text) AS memo FROM tx WHERE block_height=%s AND chain_num=%s AND tx_idx=%s""",
Expand All @@ -54,7 +64,7 @@ def _index_retires(pg_conn, _client, _chain_num):
retirement = (
normalize["type"],
normalize["amount"],
normalize["batch_denom"],
normalize["batch_denoms"],
normalize["jurisdiction"],
normalize["owner"],
normalize.get("reason", ""),
Expand All @@ -66,7 +76,7 @@ def _index_retires(pg_conn, _client, _chain_num):
normalize["tx_hash"],
)
_cur.execute(
"INSERT INTO retirements (type, amount, batch_denom, jurisdiction, owner, reason, block_height, chain_num, tx_idx, msg_idx, timestamp, tx_hash) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
"INSERT INTO retirements (type, amount, batch_denoms, jurisdiction, owner, reason, block_height, chain_num, tx_idx, msg_idx, timestamp, tx_hash) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
retirement,
)
pg_conn.commit()
Expand Down
10 changes: 5 additions & 5 deletions migrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ $ docker-compose up --build postgres

Then, you must initialize the database:

```
$ export DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer"
$ export SHADOW_DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer_shadow"
$ export ROOT_DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres"
$ yarn run graphile-migrate reset --erase
```sh
export DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer"
export SHADOW_DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer_shadow"
export ROOT_DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres"
yarn run graphile-migrate reset --erase
```

Now, we set up a watch process that will monitor `migrations/current.sql` for your changes as well as apply them to your local database:
Expand Down
12 changes: 12 additions & 0 deletions migrations/committed/000003.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--! Previous: sha1:80425329666e5d20b5fbb7b6179f3d19acceeb76
--! Hash: sha1:23379758e3a742625d5262c472693435196be95f

ALTER TABLE public.retirements
ADD COLUMN IF NOT EXISTS batch_denoms text[] DEFAULT ARRAY[]::text[] NOT NULL;

UPDATE public.retirements
SET batch_denoms = ARRAY[batch_denom];

-- TODO later once app fully migrated
-- ALTER TABLE public.retirements
-- DROP COLUMN IF EXISTS batch_denom;
6 changes: 4 additions & 2 deletions migrations/schema_snapshot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
--

-- Dumped from database version 14.9 (Debian 14.9-1.pgdg110+1)
-- Dumped by pg_dump version 15.4
-- Dumped by pg_dump version 17.0

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
Expand Down Expand Up @@ -405,7 +406,8 @@ CREATE TABLE public.retirements (
chain_num smallint NOT NULL,
tx_idx smallint NOT NULL,
msg_idx smallint NOT NULL,
tx_hash text NOT NULL
tx_hash text NOT NULL,
batch_denoms text[] DEFAULT ARRAY[]::text[] NOT NULL
);


Expand Down

0 comments on commit 232e02f

Please sign in to comment.