Query editor
WITH borrow_events AS (SELECTevt_tx_hash AS borrow_tx_hash,user AS borrower,reserve AS borrowed_asset,amount AS borrowed_amount,evt_block_time AS borrow_timeFROM aave_v3_scroll.L2Pool_evt_Borrow),supply_events AS (SELECTevt_tx_hash AS supply_tx_hash,onBehalfOf AS supplier,reserve AS supplied_asset,amount AS supplied_amount,evt_block_time AS supply_timeFROM aave_v3_scroll.L2Pool_evt_Supply),closest_supply_per_borrow AS (SELECTb.borrow_tx_hash,b.borrow_time,b.borrower,b.borrowed_asset,b.borrowed_amount,s.supply_tx_hash,s.supply_time,s.supplied_amount,ROW_NUMBER() OVER (PARTITION BY b.borrow_tx_hash --partition by borrowORDER BY s.supply_time ASC -- get closest supply event after borrow) AS row_numFROM borrow_events bJOIN supply_events sON b.borrower = s.supplierAND b.borrowed_asset = s.supplied_asset -- same tokenAND s.supply_time > b.borrow_time --only when supply happens after borrowAND s.supplied_amount >= b.borrowed_amount * 0.9 -- +-10 % of valueAND s.supplied_amount <= b.borrowed_amount * 1.1)SELECTsupply_tx_hash AS recursive_deposit_tx_hash,borrower AS user,DATE(supply_time) AS block_dateFROM closest_supply_per_borrowWHERE row_num = 1 -- only keep 1st supplyORDER BY user, supply_time