-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sushiswap): sushiswap analytics package
- Loading branch information
sam
committed
Feb 22, 2021
1 parent
ed28437
commit ba65246
Showing
17 changed files
with
491 additions
and
55 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1 +1 @@ | ||
2021-02-10 | ||
2021-02-22 |
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,43 @@ | ||
WITH prices AS ( | ||
SELECT * | ||
FROM prices.usd | ||
where minute > now() - interval '6 months' | ||
), | ||
|
||
users AS ( | ||
SELECT block_time, a.trader_a AS trader | ||
FROM dex.trades a | ||
WHERE block_time > now() - interval '6 months' | ||
and project = 'Sushiswap' | ||
UNION | ||
SELECT block_time, a.trader_b AS trader | ||
FROM dex.trades a | ||
WHERE block_time > now() - interval '6 months' | ||
and project = 'Sushiswap' | ||
) | ||
|
||
SELECT date_trunc('week', t.block_time) as week, | ||
COUNT(DISTINCT u.trader) AS unique_traders, | ||
(SUM(COALESCE( | ||
usd_amount, | ||
token_a_amount * a.price, | ||
token_b_amount * b.price | ||
))) as usd_volume, | ||
SUM(COALESCE( | ||
usd_amount, | ||
token_a_amount * a.price, | ||
token_b_amount * b.price | ||
)*0.0025) as usd_protocol_fees, | ||
SUM(COALESCE( | ||
usd_amount, | ||
token_a_amount * a.price, | ||
token_b_amount * b.price | ||
)*0.0005) as usd_tokenholder_fees | ||
FROM dex."trades" t | ||
LEFT JOIN prices a ON date_trunc('minute', block_time) = a.minute AND token_a_address = a.contract_address | ||
LEFT JOIN prices b ON date_trunc('minute', block_time) = b.minute AND token_b_address = b.contract_address | ||
LEFT JOIN users u ON t.block_time = u.block_time | ||
WHERE t.block_time > now() - interval '6 months' | ||
and t.project = 'Sushiswap' | ||
GROUP BY 1 | ||
ORDER BY 1 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
WITH recet120 AS | ||
(SELECT date_trunc('day', block_time) as date, | ||
sum(usd_amount) AS daily_usd_amount | ||
FROM dex."trades" t | ||
WHERE project = 'Sushiswap' | ||
AND date_trunc('day', block_time) > now() - interval '240 days' | ||
GROUP BY 1 | ||
ORDER BY 1 DESC), | ||
|
||
F0_30 AS | ||
(SELECT daily_usd_amount,date | ||
FROM recet120 | ||
LIMIT 30), | ||
|
||
F90_120 AS | ||
(SELECT daily_usd_amount,date | ||
FROM recet120 | ||
LIMIT 30 OFFSET 90), | ||
|
||
-- SELECT sum(a.daily_usd_amount) as recent0_30 | ||
-- FROM F0_30 a, F90_120 b | ||
|
||
a as (SELECT sum(daily_usd_amount) as total_usd_amount from F0_30), | ||
b as (SELECT sum(daily_usd_amount) as total_usd_amount from F90_120) | ||
|
||
select a.total_usd_amount as total_usd_volume_0_30, | ||
b.total_usd_amount as total_usd_volume_90_120, | ||
POWER(a.total_usd_amount/b.total_usd_amount , 4) -1 as annualized_growth_rate | ||
from a a,b b |
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,50 @@ | ||
/* | ||
SELECT d.pid, d.sum as deposit, w.sum as withdraw, d.sum - w.sum as liquidity FROM | ||
( select pid, 0 as "type", sum(amount) / 1e18 as sum , count(*) as count from sushi."MasterChef_evt_Deposit" group by pid order by pid) as d | ||
LEFT JOIN | ||
(select pid, 1 as "type", sum(amount) / 1e18 as sum, count(*) as count from sushi."MasterChef_evt_Withdraw" group by pid order by pid) as w | ||
ON d.pid = w.pid | ||
*/ | ||
DROP TABLE IF EXISTS lp_names; | ||
|
||
CREATE TEMP TABLE lp_names( | ||
pid INT, | ||
name char(20), | ||
pair1 char(10), | ||
pair2 char(10) | ||
); | ||
INSERT INTO lp_names(pid, name) values(0, '0:WETH/USDT'); | ||
INSERT INTO lp_names(pid, name) values(1, '1:ETH/USDC'); | ||
INSERT INTO lp_names(pid, name) values(2, '2:DAI/WETH'); | ||
INSERT INTO lp_names(pid, name) values(3, '3:Synth/WETH'); | ||
INSERT INTO lp_names(pid, name) values(4, '4:COMP/WETH'); | ||
INSERT INTO lp_names(pid, name) values(5, '5:LEND/WETH'); | ||
INSERT INTO lp_names(pid, name) values(6, '6:Synthetix/WETH'); | ||
INSERT INTO lp_names(pid, name) values(7, '7:UMA/WETH'); | ||
INSERT INTO lp_names(pid, name) values(8, '8:LINK/WETH'); | ||
INSERT INTO lp_names(pid, name) values(9, '9:BAND/WETH'); | ||
INSERT INTO lp_names(pid, name) values(10, '10:WETH/AMPL'); | ||
INSERT INTO lp_names(pid, name) values(11, '11:FYI/WETH'); | ||
INSERT INTO lp_names(pid, name) values(12, '12:SUSHI/WETH'); | ||
INSERT INTO lp_names(pid, name) values(13, '13:REN/WETH'); | ||
INSERT INTO lp_names(pid, name) values(14, '14:Synth/$BASED'); | ||
INSERT INTO lp_names(pid, name) values(15, '15:SERUM/WETH'); | ||
INSERT INTO lp_names(pid, name) values(16, '16:YAM2/WETH'); | ||
INSERT INTO lp_names(pid, name) values(17, '17:WETH/CRV'); | ||
|
||
WITH data AS( | ||
SELECT day, name, s.pid, deposit, withdraw, liquidity, count FROM ( | ||
SELECT d.day, d.pid, d.sum as deposit, w.sum as withdraw, d.sum - w.sum as liquidity, w.count FROM | ||
( SELECT date_trunc('day', evt_block_time) as day, pid, sum(amount) / 1e18 as sum , count(*) as count from sushi."MasterChef_evt_Deposit" GROUP BY 1, 2) as d | ||
LEFT JOIN | ||
( SELECT date_trunc('day', evt_block_time) as day, pid, sum(amount) / 1e18 as sum , count(*) as count from sushi."MasterChef_evt_Withdraw" GROUP BY 1, 2) as w | ||
ON d.pid = w.pid AND d.day = w.day | ||
) as s | ||
INNER JOIN lp_names as l ON l.pid = s.pid | ||
) | ||
|
||
SELECT | ||
day, name, | ||
sum(liquidity) over (partition by name order by day asc rows between unbounded preceding and current row) as liquidity | ||
FROM data WHERE day <= current_date - interval '1 day'; |
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,50 @@ | ||
/* | ||
SELECT d.pid, d.sum as deposit, w.sum as withdraw, d.sum - w.sum as liquidity FROM | ||
( select pid, 0 as "type", sum(amount) / 1e18 as sum , count(*) as count from sushi."MasterChef_evt_Deposit" group by pid order by pid) as d | ||
LEFT JOIN | ||
(select pid, 1 as "type", sum(amount) / 1e18 as sum, count(*) as count from sushi."MasterChef_evt_Withdraw" group by pid order by pid) as w | ||
ON d.pid = w.pid | ||
*/ | ||
DROP TABLE IF EXISTS lp_names; | ||
|
||
CREATE TEMP TABLE lp_names( | ||
pid INT, | ||
name char(20), | ||
pair1 char(10), | ||
pair2 char(10) | ||
); | ||
INSERT INTO lp_names(pid, name) values(0, '0:WETH/USDT'); | ||
INSERT INTO lp_names(pid, name) values(1, '1:ETH/USDC'); | ||
INSERT INTO lp_names(pid, name) values(2, '2:DAI/WETH'); | ||
INSERT INTO lp_names(pid, name) values(3, '3:Synth/WETH'); | ||
INSERT INTO lp_names(pid, name) values(4, '4:COMP/WETH'); | ||
INSERT INTO lp_names(pid, name) values(5, '5:LEND/WETH'); | ||
INSERT INTO lp_names(pid, name) values(6, '6:Synthetix/WETH'); | ||
INSERT INTO lp_names(pid, name) values(7, '7:UMA/WETH'); | ||
INSERT INTO lp_names(pid, name) values(8, '8:LINK/WETH'); | ||
INSERT INTO lp_names(pid, name) values(9, '9:BAND/WETH'); | ||
INSERT INTO lp_names(pid, name) values(10, '10:WETH/AMPL'); | ||
INSERT INTO lp_names(pid, name) values(11, '11:FYI/WETH'); | ||
INSERT INTO lp_names(pid, name) values(12, '12:SUSHI/WETH'); | ||
INSERT INTO lp_names(pid, name) values(13, '13:REN/WETH'); | ||
INSERT INTO lp_names(pid, name) values(14, '14:Synth/$BASED'); | ||
INSERT INTO lp_names(pid, name) values(15, '15:SERUM/WETH'); | ||
INSERT INTO lp_names(pid, name) values(16, '16:YAM2/WETH'); | ||
INSERT INTO lp_names(pid, name) values(17, '17:WETH/CRV'); | ||
|
||
WITH data AS( | ||
SELECT day, name, s.pid, deposit, withdraw, liquidity, count FROM ( | ||
SELECT d.day, d.pid, d.sum as deposit, w.sum as withdraw, d.sum - w.sum as liquidity, w.count FROM | ||
( SELECT date_trunc('day', evt_block_time) as day, pid, sum(amount) / 1e18 as sum , count(*) as count from sushi."MasterChef_evt_Deposit" GROUP BY 1, 2) as d | ||
LEFT JOIN | ||
( SELECT date_trunc('day', evt_block_time) as day, pid, sum(amount) / 1e18 as sum , count(*) as count from sushi."MasterChef_evt_Withdraw" GROUP BY 1, 2) as w | ||
ON d.pid = w.pid AND d.day = w.day | ||
) as s | ||
INNER JOIN lp_names as l ON l.pid = s.pid | ||
) | ||
|
||
SELECT | ||
day, name, | ||
sum(liquidity) over (partition by name order by day asc rows between unbounded preceding and current row) as liquidity | ||
FROM data WHERE day <= current_date - interval '1 day'; |
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,47 @@ | ||
-- Pair, Uni, decimals, Sushi | ||
-- ETH/WBTC 0xbb2b8038a1640196fbe3e38816f3e67cba72d940 - 8 - 0xceff51756c56ceffca006cd410b03ffc46dd3a58 | ||
-- ETH/USDT 0x0d4a11d5eeaac28ec3f61d100daf4d40471f1852 - 6 - 0x06da0fd433c1a5d7a4faa01111c044910a184553 | ||
-- ETH/DAI 0xa478c2975ab1ea89e8196811f51a7b7ade33eb11 - 18 - 0xc3d03e4f041fd4cd388c549ee2a29a9e5075882f | ||
-- ETH/USDC 0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc - 6 - 0x397ff1542f962076d0bfe58ea045ffa2d347aca0 | ||
WITH prices AS ( | ||
SELECT date_trunc('hour', minute) as hour, | ||
AVG(price) as price | ||
FROM prices.layer1_usd | ||
WHERE symbol = 'ETH' | ||
GROUP BY 1 | ||
) | ||
|
||
select | ||
*, eth * 2 * a.price / 1e17 as val | ||
from ( | ||
select | ||
avg(CASE | ||
WHEN contract_address in ( | ||
'\x0d4a11d5eeaac28ec3f61d100daf4d40471f1852', '\x06da0fd433c1a5d7a4faa01111c044910a184553' | ||
) THEN reserve0 | ||
ELSE reserve1 | ||
END) / 1e1 as eth, | ||
date_trunc('hour', evt_block_time) as h, | ||
CASE | ||
WHEN contract_address = '\xbb2b8038a1640196fbe3e38816f3e67cba72d940' THEN 'eth-wbtc' | ||
WHEN contract_address = '\x0d4a11d5eeaac28ec3f61d100daf4d40471f1852' THEN 'eth-usdt' | ||
WHEN contract_address = '\xa478c2975ab1ea89e8196811f51a7b7ade33eb11' THEN 'eth-dai' | ||
WHEN contract_address = '\xb4e16d0168e52d35cacd2c6185b44281ec28c9dc' THEN 'eth-usdc' | ||
--Sushi Swap Contracts | ||
when contract_address = '\xceff51756c56ceffca006cd410b03ffc46dd3a58' THEN 'eth-wbtc' | ||
when contract_address = '\x06da0fd433c1a5d7a4faa01111c044910a184553' THEN 'eth-usdt' | ||
when contract_address = '\xc3d03e4f041fd4cd388c549ee2a29a9e5075882f' THEN 'eth-dai' | ||
when contract_address = '\x397ff1542f962076d0bfe58ea045ffa2d347aca0' THEN 'eth-usdc' | ||
END as pool, | ||
protocol | ||
from ( | ||
select *, 'uni' as protocol from uniswap_v2."Pair_evt_Sync" | ||
union | ||
select *, 'sushi' as protocol from sushi."Pair_evt_Sync" | ||
) y | ||
where contract_address in ('\xbb2b8038a1640196fbe3e38816f3e67cba72d940','\x0d4a11d5eeaac28ec3f61d100daf4d40471f1852','\xa478c2975ab1ea89e8196811f51a7b7ade33eb11','\xb4e16d0168e52d35cacd2c6185b44281ec28c9dc','\xceff51756c56ceffca006cd410b03ffc46dd3a58','\x06da0fd433c1a5d7a4faa01111c044910a184553','\xc3d03e4f041fd4cd388c549ee2a29a9e5075882f','\x397ff1542f962076d0bfe58ea045ffa2d347aca0') | ||
group by contract_address, h, protocol | ||
) x | ||
JOIN prices a | ||
ON x.h = a.hour | ||
where pool = {{ pools }} |
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,33 @@ | ||
WITH prices AS ( | ||
SELECT MINUTE, | ||
contract_address, | ||
price | ||
FROM prices.usd | ||
where date_trunc('day', MINUTE) > now() - interval '120 days' | ||
and date_trunc('day', MINUTE) < date_trunc('day', now()) | ||
) | ||
SELECT day, | ||
SUM(usd_volume) OVER (ORDER BY DAY) as cumulative_usd_volume | ||
FROM ( | ||
SELECT date_trunc('day', block_time) AS DAY, | ||
SUM(CASE | ||
WHEN token_a_address = a.contract_address | ||
THEN token_a_amount * a.price -- Use token A when there's USD price for it | ||
|
||
ELSE token_b_amount * b.price -- Else use token b | ||
|
||
END) AS usd_volume | ||
FROM ( | ||
SELECT * | ||
FROM dex."trades" t | ||
WHERE project = 'Sushiswap' | ||
and date_trunc('day', block_time) > now() - interval '120 days' | ||
and date_trunc('day', block_time) < date_trunc('day', now()) | ||
) AS trades | ||
LEFT JOIN prices a ON date_trunc('minute', block_time) = a.minute | ||
AND token_a_address = a.contract_address -- Joining with prices on time and token address for token A | ||
LEFT JOIN prices b ON date_trunc('minute', block_time) = b.minute | ||
AND token_b_address = b.contract_address -- Joining with prices on time and token address for token B | ||
GROUP BY DAY | ||
) AS days | ||
; |
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,22 @@ | ||
select | ||
count(DISTINCT(address)) as number_current_holders, | ||
t.day | ||
-- sushi_balance from (SELECT address, SUM(amount)/1e18 as sushi_balance | ||
FROM ( | ||
SELECT | ||
date_trunc('day',evt_block_time) as day, | ||
"from" AS address, -value AS amount | ||
FROM erc20."ERC20_evt_Transfer" e | ||
WHERE contract_address='\x6b3595068778dd592e39a122f4f5a5cf09c90fe2' | ||
UNION ALL | ||
SELECT | ||
date_trunc('day',evt_block_time) as day, | ||
"to" AS address, value AS amount | ||
FROM erc20."ERC20_evt_Transfer" e | ||
WHERE contract_address='\x6b3595068778dd592e39a122f4f5a5cf09c90fe2' | ||
) t | ||
WHERE address not in ('\xf942dba4159cb61f8ad88ca4a83f5204e8f4a6bd','\xce84867c3c02b05dc570d0135103d3fb9cc19433','\x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be','\xe93381fb4c4f14bda253907b18fad305d799241a','\xc2edad668740f1aa35e4d8f227fb8e17dca888cd','\x6cc5f688a315f3dc28a7781717a9a798a59fda7b','\x1c4b70a3968436b9a0a9cf5205c787eb81bb558c','\x4938960c507a4d7094c53a8cddcf925835393b8f','\xc8d02f2669ef9aabe6b3b75e2813695aed63748d','\x0211f3cedbef3143223d3acf0e589747933e8527','\x2faf487a4414fe77e2327f0bf4ae2a264a776ad2','\xd2e8827d4b1c44f64d1fa01bfbc14dc8545eca41') | ||
GROUP BY 2 | ||
LIMIT 1000000 | ||
-- GROUP BY 1 | ||
-- ORDER BY sushi_balance DESC |
Oops, something went wrong.