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

Coingecko router #25

Draft
wants to merge 2 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
98 changes: 62 additions & 36 deletions queries/coingecko/v1/pairs.sql
Original file line number Diff line number Diff line change
@@ -1,39 +1,65 @@
-- getPairs
WITH listed AS (
SELECT
DISTINCT asset_id
FROM
omnipool_asset
WHERE
asset_id NOT BETWEEN 100 AND 199
UNION SELECT 10
UNION SELECT 22
with router_24h as (
select
cast(args ->> 'assetIn' as integer) as asset_in,
cast(args ->> 'assetOut' as integer) as asset_out,
row_number() over (partition by args ->> 'assetIn', args ->> 'assetOut' order by timestamp desc) rn_from_latest,
row_number() over (partition by args ->> 'assetIn', args ->> 'assetOut'
order by cast(args ->> 'amountIn' as numeric) / cast(args ->> 'amountOut' as numeric) desc) rn_high,
row_number() over (partition by args ->> 'assetIn', args ->> 'assetOut'
order by cast(args ->> 'amountIn' as numeric) / cast(args ->> 'amountOut' as numeric)) rn_low,
cast(args ->> 'amountIn' as numeric) as amount_in,
cast(args ->> 'amountOut' as numeric) as amount_out
from
event
join block on event.block_id = block.id
where
name = 'Router.RouteExecuted'
and timestamp > now() - interval '1d'

),
combos AS (
SELECT
la.asset_id AS asset_1_id,
lb.asset_id AS asset_2_id,
tm.symbol AS asset_1_symbol,
tme.symbol AS asset_2_symbol
FROM
listed la CROSS
JOIN listed lb
JOIN token_metadata tm ON la.asset_id = tm.id
JOIN token_metadata tme ON lb.asset_id = tme.id
real_pairs as (
select
tm_in.symbol as base_currency,
tm_out.symbol as target_currency,
r.asset_in,
r.asset_out
from router_24h r
join token_metadata tm_in on r.asset_in = tm_in.id
join token_metadata tm_out on r.asset_out = tm_out.id
group by r.asset_in, r.asset_out, tm_in.symbol, tm_out.symbol
),
hide_stablepools as (
select
CASE WHEN asset_in = 100 THEN REPLACE(base_currency, '4-Pool', CASE WHEN asset_out = 10 THEN 'USDC' ELSE 'USDT' END)
WHEN asset_in = 101 THEN REPLACE(base_currency, '2-Pool', CASE WHEN asset_out = 11 THEN 'WBTC' ELSE 'iBTC' END)
WHEN asset_in = 102 THEN REPLACE(base_currency, '2-Pool-Stbl', CASE WHEN asset_out = 22 THEN 'USDT' ELSE 'USDC' END)
WHEN asset_out = 100 THEN REPLACE(base_currency, '4-Pool', CASE WHEN asset_in = 10 THEN 'USDC' ELSE 'USDT' END)
WHEN asset_out = 101 THEN REPLACE(base_currency, '2-Pool', CASE WHEN asset_in = 11 THEN 'WBTC' ELSE 'iBTC' END)
WHEN asset_out = 102 THEN REPLACE(base_currency, '2-Pool-Stbl', CASE WHEN asset_in = 22 THEN 'USDT' ELSE 'USDC' END)
ELSE base_currency
END as base_currency,
CASE WHEN asset_in = 100 THEN REPLACE(target_currency, '4-Pool', CASE WHEN asset_out = 10 THEN 'USDC' ELSE 'USDT' END)
WHEN asset_in = 101 THEN REPLACE(target_currency, '2-Pool', CASE WHEN asset_out = 11 THEN 'WBTC' ELSE 'iBTC' END)
WHEN asset_in = 102 THEN REPLACE(target_currency, '2-Pool-Stbl', CASE WHEN asset_out = 22 THEN 'USDT' ELSE 'USDC' END)
WHEN asset_out = 100 THEN REPLACE(target_currency, '4-Pool', CASE WHEN asset_in = 10 THEN 'USDC' ELSE 'USDT' END)
WHEN asset_out = 101 THEN REPLACE(target_currency, '2-Pool', CASE WHEN asset_in = 11 THEN 'WBTC' ELSE 'iBTC' END)
WHEN asset_out = 102 THEN REPLACE(target_currency, '2-Pool-Stbl', CASE WHEN asset_in = 22 THEN 'USDT' ELSE 'USDC' END)
ELSE target_currency
END as target_currency
from real_pairs
),
dedup as (
select
case when base_currency > target_currency then base_currency else target_currency end as base_currency,
case when base_currency > target_currency then target_currency else base_currency end as target_currency
from hide_stablepools
)
SELECT
DISTINCT
CASE WHEN asset_1_id > asset_2_id THEN CONCAT(
asset_1_symbol, '_', asset_2_symbol
) ELSE CONCAT(
asset_2_symbol, '_', asset_1_symbol
) END AS ticker_id,
CASE WHEN asset_1_id > asset_2_id THEN asset_1_symbol ELSE asset_2_symbol END AS base,
CASE WHEN asset_1_id > asset_2_id THEN asset_2_symbol ELSE asset_1_symbol END AS target
FROM
combos c
WHERE
asset_1_symbol <> asset_2_symbol
ORDER BY
ticker_id,
base
select
CONCAT(base_currency, '_', target_currency) as ticker_id,
base_currency as base,
target_currency as target
from
dedup
group by
ticker_id, base_currency, target_currency
Loading