Query editor
WITH redemptions AS (SELECTevt_tx_hash,evt_block_time,evt_block_date,evt_tx_to,_actualBoldAmountFROM liquity_v2_ethereum.trovemanager_weth_evt_redemptionWHERE evt_block_date > TIMESTAMP '{{start_date}}'AND evt_tx_to = {{bot_execution_contract}}),weth_received AS (SELECTevt_tx_hash,SUM(CAST(value AS DOUBLE)) / 1e18 as weth_received_amountFROM erc20_ethereum.evt_TransferWHERE contract_address = 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2AND "to" = {{bot_execution_contract}}AND evt_block_date > TIMESTAMP '{{start_date}}'GROUP BY evt_tx_hash),weth_sent AS (SELECTevt_tx_hash,SUM(CAST(value AS DOUBLE)) / 1e18 as weth_sent_amountFROM erc20_ethereum.evt_TransferWHERE contract_address = 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2AND "from" = {{bot_execution_contract}}AND evt_block_date > TIMESTAMP '{{start_date}}'GROUP BY evt_tx_hash),eth_sent AS (SELECThash as evt_tx_hash,SUM(CAST("value" AS DOUBLE)) / 1e18 as eth_sent_amountFROM ethereum.transactionsWHERE "from" = {{bot_execution_contract}}AND block_date > TIMESTAMP '{{start_date}}'GROUP BY hash),joint_table as (SELECTr.evt_tx_hash,r.evt_block_time,r.evt_block_date,r._actualBoldAmount,COALESCE(wr.weth_received_amount, 0) as weth_received,COALESCE(ws.weth_sent_amount, 0) as weth_sent,COALESCE(es.eth_sent_amount, 0) as eth_sent,COALESCE(wr.weth_received_amount, 0) - COALESCE(ws.weth_sent_amount, 0) - COALESCE(es.eth_sent_amount, 0) as deltaFROM redemptions rLEFT JOIN weth_received wr ON r.evt_tx_hash = wr.evt_tx_hashLEFT JOIN weth_sent ws ON r.evt_tx_hash = ws.evt_tx_hashLEFT JOIN eth_sent es ON r.evt_tx_hash = es.evt_tx_hash),aggregated AS (SELECTCASEWHEN '{{time_granularity}}' = 'hourly' THEN date_trunc('hour', evt_block_time)WHEN '{{time_granularity}}' = 'weekly' THEN date_trunc('week', evt_block_time)