Query editor
WITH liquidation_tx AS(SELECT evt_tx_hash as tx_hashFROM aave_v2_ethereum.LendingPool_evt_LiquidationCallWHERE evt_block_time >= NOW() - INTERVAL '1' YEAR),transfers AS(SELECT block_number, tx_hash, "from" as caller, "to" as destination, valueFROM ethereum.tracesWHERE value > 0 AND tx_success AND call_type <> 'delegatecall'AND tx_hash IN (SELECT tx_hash FROM liquidation_tx)),flashbot_transfer AS(SELECT block_number, tx_hash, caller, destination, valueFROM ethereum.blocks bJOIN transfers tON destination = minerAND t.block_number = b."number"),gas_consumption AS(SELECT hash as tx_hash, gas_used, gas_price, priority_fee_per_gasFROM ethereum.transactionsWHERE hash IN (SELECT tx_hash FROM liquidation_tx)),tokens AS(SELECT t.contract_address, symbol, decimalsFROM aave_v2_ethereum.LendingPoolConfigurator_evt_ReserveInitialized riJOIN tokens.erc20 tON asset = t.contract_address),skim_prices AS(SELECT p.contract_address, median_price as price, decimals, symbol, hour as tFROM dex.prices pJOIN tokens tON blockchain='ethereum' and p.contract_address = t.contract_address),prices AS(SELECT contract_address, price, decimals, symbol,t, LEAD(t, 1, now()) OVER (PARTITION BY contract_address ORDER BY t) next_tFROM skim_prices),liquidation_data AS(SELECT evt_block_time, pc.symbol as collateral_symbol, pl.symbol as loan_symbol,debtToCover * pl.price / pow(10, pl.decimals) as repaid_usd,liquidatedCollateralAmount * pc.price / pow(10, pc.decimals) as seized_usd,evt_tx_hash, user as liquidated, liquidatorFROM aave_v2_ethereum.LendingPool_evt_LiquidationCall lJOIN prices pcON pc.contract_address = l.collateralAsset AND pc.t < l.evt_block_time AND l.evt_block_time < pc.next_tJOIN prices plON pl.contract_address = l.debtAsset AND pl.t < l.evt_block_time AND l.evt_block_time < pl.next_t),res AS(SELECT evt_block_time, collateral_symbol, loan_symbol, seized_usd - repaid_usd - COALESCE(gas_used*(gas_price-priority_fee_per_gas)*price/pow(10, decimals), 0.) as profit_usd,COALESCE(value*price/pow(10, decimals), 0.) coinbase_payment_usd, COALESCE(value/pow(10, decimals), 0.) coinbase_payment_eth,(COALESCE(value*price/pow(10,decimals), 0.) + COALESCE(gas_used*priority_fee_per_gas*price/pow(10, decimals), 0.)) / (seized_usd - repaid_usd - COALESCE(gas_used*(gas_price-priority_fee_per_gas)*price/pow(10, decimals), 0.)) as flashbot_proportion,COALESCE(gas_used*priority_fee_per_gas*price/pow(10, decimals), 0.) as builder_gas_payment_usd,COALESCE(gas_used*gas_price*price/pow(10, decimals), 0.) as gas_payment_usd, evt_tx_hashFROM liquidation_dataLEFT JOIN flashbot_transfer ftON evt_tx_hash = ft.tx_hashLEFT JOIN gas_consumption gcON evt_tx_hash = gc.tx_hashJOIN prices pON p.contract_address = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2AND p.t < evt_block_time AND evt_block_time < p.next_t)SELECT evt_block_time, collateral_symbol, loan_symbol, profit_usd - coinbase_payment_usd - builder_gas_payment_usd as net_profit_usd,