generated from KyberNetwork/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add exclusive order data * add tradelog mview * fix lint * add worker for refresh * add worker for refresh * add worker for refresh
- Loading branch information
1 parent
1d2e82c
commit 4fd460c
Showing
8 changed files
with
537 additions
and
10 deletions.
There are no files selected for viewing
311 changes: 311 additions & 0 deletions
311
v2/cmd/migrations/00017_add_exclusive_order_data.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,311 @@ | ||
ALTER TABLE tradelogs_uniswapx | ||
ADD COLUMN expiration BIGINT NOT NULL DEFAULT 0, | ||
ADD COLUMN decay_start_time BIGINT DEFAULT NULL, | ||
ADD COLUMN decay_end_time BIGINT DEFAULT NULL, | ||
ADD COLUMN exclusive_filler TEXT NOT NULL DEFAULT ''; | ||
|
||
ALTER TABLE tradelogs_pancakeswap | ||
ADD COLUMN expiration BIGINT NOT NULL DEFAULT 0; | ||
|
||
CREATE MATERIALIZED VIEW mview_tradelogs AS | ||
WITH all_trades AS ( | ||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'zerox' AS contract, | ||
'' AS type | ||
FROM public.tradelogs_zerox | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
|
||
|
||
UNION ALL | ||
|
||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'kyberswap' AS contract, | ||
'' AS type | ||
FROM public.tradelogs_kyberswap | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'kyberswap_rfq' AS contract, | ||
'' AS type | ||
FROM public.tradelogs_kyberswap_rfq | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'paraswap' AS contract, | ||
'' AS type | ||
FROM public.tradelogs_paraswap | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'hashflow_v3' AS contract, | ||
'' AS type | ||
FROM public.tradelogs_hashflow_v3 | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'oneinch_v6' AS contract, | ||
type | ||
FROM public.tradelogs_oneinch_v6 | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'uniswapx' AS contract, | ||
'' AS type | ||
FROM public.tradelogs_uniswapx | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'bebop' AS contract, | ||
'' AS type | ||
FROM public.tradelogs_bebop | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'zerox_v3' AS contract, | ||
'' AS type | ||
FROM public.tradelogs_zerox_v3 | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
|
||
UNION ALL | ||
|
||
SELECT | ||
maker, | ||
taker, | ||
maker_token, | ||
taker_token, | ||
CASE | ||
WHEN maker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(maker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS maker_token_amount, | ||
CASE | ||
WHEN taker_token_amount ~ '^[0-9]+(\.[0-9]+)?$' THEN REPLACE(taker_token_amount, ',', '.')::DECIMAL | ||
ELSE NULL | ||
END AS taker_token_amount, | ||
contract_address, | ||
block_number, | ||
tx_hash, | ||
timestamp, | ||
event_hash, | ||
maker_token_price, | ||
taker_token_price, | ||
maker_usd_amount, | ||
taker_usd_amount, | ||
'pancakeswap' AS contract, | ||
'' AS type | ||
FROM public.tradelogs_pancakeswap | ||
WHERE timestamp >= EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - INTERVAL '3 months') * 1000 | ||
AND maker_usd_amount > 0 AND taker_usd_amount > 0 | ||
) | ||
SELECT * FROM all_trades ORDER BY timestamp DESC; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package worker | ||
|
||
import ( | ||
"time" | ||
|
||
dashboardStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/dashboard" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Refresher struct { | ||
DashStorage *dashboardStorage.Storage | ||
l *zap.SugaredLogger | ||
} | ||
|
||
func NewRefresher(dashStorage *dashboardStorage.Storage, l *zap.SugaredLogger) *Refresher { | ||
return &Refresher{ | ||
DashStorage: dashStorage, | ||
l: l, | ||
} | ||
} | ||
|
||
func (r *Refresher) Run() { | ||
go r.ScheduleMViewRefresh() | ||
} | ||
|
||
func (r *Refresher) ScheduleMViewRefresh() { | ||
ticker := time.NewTicker(24 * time.Hour) | ||
defer ticker.Stop() | ||
for range ticker.C { | ||
err := r.DashStorage.RefreshTradelogsMView() | ||
if err != nil { | ||
r.l.Errorf("Error refreshing materialized view: %v", err) | ||
} else { | ||
r.l.Info("Materialized view refreshed successfully.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.