From 55a914cb62d078f15632d23fda9f5a887733aca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erdem=20De=C4=9Fer?= Date: Tue, 7 Dec 2021 22:20:20 +0300 Subject: [PATCH 1/4] Add trades from DEX DefiPlaza to Ethereum dex.trades table --- ethereum/dex/trades/insert_defiplaza.sql | 129 +++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 ethereum/dex/trades/insert_defiplaza.sql diff --git a/ethereum/dex/trades/insert_defiplaza.sql b/ethereum/dex/trades/insert_defiplaza.sql new file mode 100644 index 00000000000..ec269a84fe5 --- /dev/null +++ b/ethereum/dex/trades/insert_defiplaza.sql @@ -0,0 +1,129 @@ +CREATE OR REPLACE FUNCTION dex.insert_defiplaza(start_ts timestamptz, end_ts timestamptz=now(), start_block numeric=0, end_block numeric=9e18) RETURNS integer +LANGUAGE plpgsql AS $function$ +DECLARE r integer; +BEGIN +WITH rows AS ( + INSERT INTO dex.trades ( + block_time, + token_a_symbol, + token_b_symbol, + token_a_amount, + token_b_amount, + project, + version, + category, + trader_a, + trader_b, + token_a_amount_raw, + token_b_amount_raw, + usd_amount, + token_a_address, + token_b_address, + exchange_contract_address, + tx_hash, + tx_from, + tx_to, + trace_address, + evt_index, + trade_id + ) + SELECT + dexs.block_time, + erc20a.symbol AS token_a_symbol, + erc20b.symbol AS token_b_symbol, + token_a_amount_raw / 10 ^ erc20a.decimals AS token_a_amount, + token_b_amount_raw / 10 ^ erc20b.decimals AS token_b_amount, + project, + version, + category, + coalesce(trader_a, tx."from") AS trader_a, + trader_b, + token_a_amount_raw, + token_b_amount_raw, + coalesce( + usd_amount, + token_a_amount_raw / 10 ^ pa.decimals * pa.price, + token_b_amount_raw / 10 ^ pb.decimals * pb.price + ) AS usd_amount, + token_a_address, + token_b_address, + exchange_contract_address, + tx_hash, + tx."from" AS tx_from, + tx."to" AS tx_to, + trace_address, + evt_index, + row_number() OVER (PARTITION BY project, tx_hash, evt_index, trace_address ORDER BY version, category) AS trade_id + FROM ( + SELECT + evt_block_time AS block_time, + 'DefiPlaza' AS project, + NULL AS version, + 'DEX' AS category, + sender AS trader_a, + NULL::bytea AS trader_b, + "inputAmount" AS token_a_amount_raw, + "outputAmount" AS token_b_amount_raw, + NULL::numeric AS usd_amount, + CASE WHEN "inputToken" = '\x0000000000000000000000000000000000000000' THEN + '\xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'::bytea ELSE "inputToken" + END AS token_a_address, + CASE WHEN "outputToken" = '\x0000000000000000000000000000000000000000' THEN + '\xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'::bytea ELSE "outputToken" + END AS token_b_address, + contract_address AS exchange_contract_address, + evt_tx_hash AS tx_hash, + NULL::integer[] AS trace_address, + evt_index + FROM defiplaza."DeFiPlaza_evt_Swapped" + ) dexs + INNER JOIN ethereum.transactions tx + ON dexs.tx_hash = tx.hash + AND tx.block_time >= start_ts + AND tx.block_time < end_ts + AND tx.block_number >= start_block + AND tx.block_number < end_block + LEFT JOIN erc20.tokens erc20a ON erc20a.contract_address = dexs.token_a_address + LEFT JOIN erc20.tokens erc20b ON erc20b.contract_address = dexs.token_b_address + LEFT JOIN prices.usd pa ON pa.minute = date_trunc('minute', dexs.block_time) + AND pa.contract_address = dexs.token_a_address + AND pa.minute >= start_ts + AND pa.minute < end_ts + LEFT JOIN prices.usd pb ON pb.minute = date_trunc('minute', dexs.block_time) + AND pb.contract_address = dexs.token_b_address + AND pb.minute >= start_ts + AND pb.minute < end_ts + WHERE dexs.block_time >= start_ts + AND dexs.block_time < end_ts + ON CONFLICT DO NOTHING + RETURNING 1 +) +SELECT count(*) INTO r from rows; +RETURN r; +END +$function$; + +-- fill 2021 +SELECT dex.insert_defiplaza( + '2021-01-01', + now(), + (SELECT max(number) FROM ethereum.blocks WHERE time < '2021-01-01'), + (SELECT MAX(number) FROM ethereum.blocks where time < now() - interval '20 minutes') +) +WHERE NOT EXISTS ( + SELECT * + FROM dex.trades + WHERE block_time > '2021-01-01' + AND block_time <= now() - interval '20 minutes' + AND project = 'DefiPlaza' +); + +INSERT INTO cron.job (schedule, command) +VALUES ('*/12 * * * *', $$ + SELECT dex.insert_defiplaza( + (SELECT max(block_time) - interval '1 days' FROM dex.trades WHERE project='DefiPlaza'), + (SELECT now() - interval '20 minutes'), + (SELECT max(number) FROM ethereum.blocks WHERE time < (SELECT max(block_time) - interval '1 days' FROM dex.trades WHERE project='DefiPlaza')), + (SELECT MAX(number) FROM ethereum.blocks where time < now() - interval '20 minutes')); +$$) +ON CONFLICT (command) DO UPDATE SET schedule=EXCLUDED.schedule; \ No newline at end of file From 348a78493555cf947cbe188a4ae0ea819c605636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erdem=20De=C4=9Fer?= Date: Wed, 8 Dec 2021 00:19:08 +0300 Subject: [PATCH 2/4] Swap token_a and token_b (a=output, b=input) --- ethereum/dex/trades/insert_defiplaza.sql | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ethereum/dex/trades/insert_defiplaza.sql b/ethereum/dex/trades/insert_defiplaza.sql index ec269a84fe5..5616af0a990 100644 --- a/ethereum/dex/trades/insert_defiplaza.sql +++ b/ethereum/dex/trades/insert_defiplaza.sql @@ -62,14 +62,14 @@ WITH rows AS ( 'DEX' AS category, sender AS trader_a, NULL::bytea AS trader_b, - "inputAmount" AS token_a_amount_raw, - "outputAmount" AS token_b_amount_raw, + "outputAmount" AS token_a_amount_raw, + "inputAmount" AS token_b_amount_raw, NULL::numeric AS usd_amount, - CASE WHEN "inputToken" = '\x0000000000000000000000000000000000000000' THEN - '\xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'::bytea ELSE "inputToken" + CASE WHEN "outputToken" = '\x0000000000000000000000000000000000000000' THEN + '\xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'::bytea ELSE "outputToken" END AS token_a_address, - CASE WHEN "outputToken" = '\x0000000000000000000000000000000000000000' THEN - '\xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'::bytea ELSE "outputToken" + CASE WHEN "inputToken" = '\x0000000000000000000000000000000000000000' THEN + '\xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'::bytea ELSE "inputToken" END AS token_b_address, contract_address AS exchange_contract_address, evt_tx_hash AS tx_hash, From ae1469a2c939fc3112522c824f52039013da4c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erdem=20De=C4=9Fer?= Date: Thu, 9 Dec 2021 15:17:51 +0300 Subject: [PATCH 3/4] Add usd_amount for ETH trades --- ethereum/dex/trades/insert_defiplaza.sql | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ethereum/dex/trades/insert_defiplaza.sql b/ethereum/dex/trades/insert_defiplaza.sql index 5616af0a990..10ba0238ba2 100644 --- a/ethereum/dex/trades/insert_defiplaza.sql +++ b/ethereum/dex/trades/insert_defiplaza.sql @@ -42,8 +42,8 @@ WITH rows AS ( token_b_amount_raw, coalesce( usd_amount, - token_a_amount_raw / 10 ^ pa.decimals * pa.price, - token_b_amount_raw / 10 ^ pb.decimals * pb.price + token_a_amount_raw / 10 ^ (CASE token_a_address WHEN '\xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' THEN 18 ELSE pa.decimals END) * (CASE token_a_address WHEN '\xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' THEN pe.price ELSE pa.price END), + token_b_amount_raw / 10 ^ (CASE token_b_address WHEN '\xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' THEN 18 ELSE pb.decimals END) * (CASE token_b_address WHEN '\xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' THEN pe.price ELSE pb.price END) ) AS usd_amount, token_a_address, token_b_address, @@ -93,6 +93,10 @@ WITH rows AS ( AND pb.contract_address = dexs.token_b_address AND pb.minute >= start_ts AND pb.minute < end_ts + LEFT JOIN prices.layer1_usd pe ON pe.minute = date_trunc('minute', dexs.block_time) + AND pe.symbol = 'ETH' + AND pe.minute >= start_ts + AND pe.minute < end_ts WHERE dexs.block_time >= start_ts AND dexs.block_time < end_ts ON CONFLICT DO NOTHING From 5e2b79f26a23c7c09f519ff4933cc36077d5bf7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erdem=20De=C4=9Fer?= Date: Thu, 9 Dec 2021 15:22:37 +0300 Subject: [PATCH 4/4] Change version to '1' --- ethereum/dex/trades/insert_defiplaza.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/dex/trades/insert_defiplaza.sql b/ethereum/dex/trades/insert_defiplaza.sql index 10ba0238ba2..61044dc3a15 100644 --- a/ethereum/dex/trades/insert_defiplaza.sql +++ b/ethereum/dex/trades/insert_defiplaza.sql @@ -58,7 +58,7 @@ WITH rows AS ( SELECT evt_block_time AS block_time, 'DefiPlaza' AS project, - NULL AS version, + '1' AS version, 'DEX' AS category, sender AS trader_a, NULL::bytea AS trader_b,