1
- WITH decimals AS (
1
+ WITH ohlc AS
2
+ (
2
3
SELECT
3
- pool,
4
- if(decimals0 = 0 , 9 , decimals0) AS decimals0,
5
- if(decimals1 = 0 , 9 , decimals1) AS decimals1,
6
- pow(10 , - abs(decimals0 - decimals1)) AS decimals_factor
7
- FROM ohlc_prices
8
- WHERE pool = {pool: String}
4
+ if(
5
+ toTime(toStartOfInterval(o .timestamp , INTERVAL {interval:UInt64} MINUTE)) = toDateTime(' 1970-01-02 00:00:00' ),
6
+ toDate(toStartOfInterval(o .timestamp , INTERVAL {interval:UInt64} MINUTE)),
7
+ toStartOfInterval(o .timestamp , INTERVAL {interval:UInt64} MINUTE)
8
+ ) AS datetime,
9
+ toString(o .amm ) AS amm,
10
+ toString(o .amm_pool ) AS pool,
11
+ toString(o .mint0 ) AS token0,
12
+ toString(o .mint1 ) AS token1,
13
+ argMinMerge(open0) AS open_raw,
14
+ quantileDeterministicMerge({high_quantile:Float64})(quantile0) AS high_raw,
15
+ quantileDeterministicMerge({low_quantile:Float64})(quantile0) AS low_raw,
16
+ argMaxMerge(close0) AS close_raw,
17
+ sum (gross_volume1) AS volume,
18
+ uniqMerge(uaw) AS uaw,
19
+ sum (transactions) AS transactions,
20
+ token0 IN {stablecoin_contracts: Array(String)} AS is_stablecoin
21
+ FROM ohlc_prices AS o
22
+ WHERE pool = {pool:String}
23
+ AND timestamp >= today() - toIntervalMinute(({offset:UInt64} + {limit :UInt64} - 1 ) * {interval:UInt64})
24
+ AND timestamp <= today() - toIntervalMinute({offset:UInt64} * {interval:UInt64})
25
+ GROUP BY token0, token1, amm, amm_pool, datetime
9
26
)
10
27
SELECT
11
- if(
12
- toTime(toStartOfInterval(o .timestamp , INTERVAL {interval: UInt64} MINUTE)) = toDateTime(' 1970-01-02 00:00:00' ),
13
- toDate(toStartOfInterval(o .timestamp , INTERVAL {interval: UInt64} MINUTE)),
14
- toStartOfInterval(o .timestamp , INTERVAL {interval: UInt64} MINUTE)
15
- ) AS datetime,
16
- toString(pool) AS pool,
17
- toString(token0) AS token0,
18
- toString(token1) AS token1,
19
- decimals_factor * argMinMerge(o .open0 ) AS open,
20
- decimals_factor * quantileDeterministicMerge({high_quantile: Float32})(o .quantile0 ) AS high,
21
- decimals_factor * quantileDeterministicMerge({low_quantile: Float32})(o .quantile0 ) AS low,
22
- decimals_factor * argMaxMerge(o .close0 ) AS close,
23
- sum (o .gross_volume0 ) AS volume,
24
- uniqMerge(o .uaw ) AS uaw,
25
- sum (o .transactions ) AS transactions,
26
- {network_id: String} AS network_id
27
- FROM ohlc_prices AS o
28
- JOIN decimals USING pool
29
- WHERE pool = {pool: String} AND timestamp BETWEEN {startTime: UInt64} AND {endTime: UInt64}
30
- GROUP BY token0, token1, decimals_factor, pool, datetime
31
- ORDER BY datetime DESC
32
- LIMIT {limit :UInt64}
33
- OFFSET {offset:UInt64}
28
+ datetime,
29
+ amm,
30
+ pool,
31
+ token0,
32
+ coalesce(
33
+ (SELECT decimals FROM ` {svm_metadata_db}` .mints WHERE mint IN (SELECT token0 FROM ohlc) AND decimals != 0 ),
34
+ 9
35
+ ) AS token0_decimals,
36
+ token1,
37
+ coalesce(
38
+ (SELECT decimals FROM ` {svm_metadata_db}` .mints WHERE mint IN (SELECT token1 FROM ohlc) AND decimals != 0 ),
39
+ 9
40
+ ) AS token1_decimals,
41
+ pow(10 , - (token1_decimals - token0_decimals)) * if(is_stablecoin, 1 / open_raw, open_raw) AS open,
42
+ pow(10 , - (token1_decimals - token0_decimals)) * if(is_stablecoin, 1 / low_raw, high_raw) AS high,
43
+ pow(10 , - (token1_decimals - token0_decimals)) * if(is_stablecoin, 1 / high_raw, low_raw) AS low,
44
+ pow(10 , - (token1_decimals - token0_decimals)) * if(is_stablecoin, 1 / close_raw, close_raw) AS close,
45
+ pow(10 , - (token1_decimals)) * toFloat64(volume) * if(is_stablecoin, close, 1 ) AS volume,
46
+ uaw,
47
+ transactions
48
+ FROM ohlc AS o
49
+ ORDER BY datetime DESC ;
0 commit comments