Skip to content

Commit

Permalink
Alter Swap model to make Candlestick views with timescaledb.continuous
Browse files Browse the repository at this point in the history
  • Loading branch information
igorsereda committed Oct 24, 2023
1 parent 4e3b8cc commit 2eb0b29
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 134 deletions.
4 changes: 2 additions & 2 deletions src/demo_uniswap/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ class Swap(Model):
# pointer to transaction
transaction_hash = fields.TextField()
# timestamp of transaction
timestamp = fields.BigIntField()
timestamp = fields.DatetimeField(index=True)
# pool swap occured within
pool: fields.ForeignKeyRelation[Pool] = fields.ForeignKeyField('models.Pool', related_name='swaps')
# allow indexing by tokens
Expand Down Expand Up @@ -410,4 +410,4 @@ class Flash(Model):
# amount token1 paid for flash
amount1_paid = fields.DecimalField(decimal_places=18, max_digits=96, default=0)
# index within the txn
log_index = fields.BigIntField()
log_index = fields.BigIntField()
32 changes: 0 additions & 32 deletions src/demo_uniswap/sql/on_reindex/01_create_mv_token_price.sql

This file was deleted.

25 changes: 0 additions & 25 deletions src/demo_uniswap/sql/on_reindex/10_create_mv_quotes_1m.sql

This file was deleted.

25 changes: 0 additions & 25 deletions src/demo_uniswap/sql/on_reindex/11_create_mv_quotes_1d.sql

This file was deleted.

50 changes: 25 additions & 25 deletions src/demo_uniswap/sql/on_reindex/20_create_ca_quotes_1m.sql
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
-- CREATE MATERIALIZED VIEW
-- candlestick_1m
-- WITH (timescaledb.continuous) AS
--
-- SELECT
-- time_bucket('1 minute'::INTERVAL, to_timestamp(timestamp)) AS bucket,
-- token0_id as token_id,
-- candlestick_agg(
-- to_timestamp(timestamp),
-- abs(amount_usd/amount0),
-- amount_usd
-- ) as candlestick
-- FROM swap
-- WHERE
-- amount_usd!=0
-- AND
-- amount0!=0
--
-- GROUP BY
-- bucket,
-- token0_id
-- ORDER BY
-- bucket,
-- token0_id
-- WITH NO DATA;
CREATE MATERIALIZED VIEW
candlestick_1m
WITH (timescaledb.continuous) AS

SELECT
time_bucket('1 minute'::INTERVAL, timestamp) AS bucket,
token0_id as token_id,
candlestick_agg(
timestamp,
abs(amount_usd/amount0),
amount_usd
) as candlestick
FROM swap
WHERE
amount_usd!=0
AND
amount0!=0

GROUP BY
bucket,
token0_id
ORDER BY
bucket,
token0_id
WITH NO DATA;
25 changes: 0 additions & 25 deletions src/demo_uniswap/sql/on_reindex/21_create_ca_quotes_1d.sql

This file was deleted.

25 changes: 25 additions & 0 deletions src/demo_uniswap/sql/on_reindex/21_create_ca_quotes_1h.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE MATERIALIZED VIEW
candlestick_1h
WITH (timescaledb.continuous) AS

SELECT
time_bucket('1 hour'::INTERVAL, timestamp) AS bucket,
token0_id as token_id,
candlestick_agg(
timestamp,
abs(amount_usd/amount0),
amount_usd
) as candlestick
FROM swap
WHERE
amount_usd!=0
AND
amount0!=0

GROUP BY
bucket,
token0_id
ORDER BY
bucket,
token0_id
WITH NO DATA;
25 changes: 25 additions & 0 deletions src/demo_uniswap/sql/on_reindex/22_create_ca_quotes_1d.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE MATERIALIZED VIEW
candlestick_1d
WITH (timescaledb.continuous) AS

SELECT
time_bucket('1 day'::INTERVAL, timestamp) AS bucket,
token0_id as token_id,
candlestick_agg(
timestamp,
abs(amount_usd/amount0),
amount_usd
) as candlestick
FROM swap
WHERE
amount_usd!=0
AND
amount0!=0

GROUP BY
bucket,
token0_id
ORDER BY
bucket,
token0_id
WITH NO DATA;

0 comments on commit 2eb0b29

Please sign in to comment.