Query editor
-- Mirror of 8954865: same rolling-window Ethereum USDe/sUSDe -> USDC swap legs.-- Original query is untouched. Dune research only, never discoverability proof.-- USDe recovery at par is an explicit parameter, not a market-price observation.-- Historical sUSDe rate is the most recent prior-block observation, max 1 hour old.-- Rates are sampled, not exact historical eth_call reconstruction.-- Gas is an illustrative incremental cost PER SWAP LEG, not a measured benchmark.-- Split fills/batches are not end-user orders; no original transaction gas is charged again.-- Zero additional_cost_bps excludes acquisition fee/recovery/carry costs, so the-- resulting positive surplus is an upper-bound opportunity, not profit/APY.WITHsettings AS (SELECT CAST({{days}} AS integer) AS days,CAST({{amo_cost_bps}} AS double) AS amo_cost_bps,CAST({{additional_cost_bps}} AS double) AS additional_cost_bps,CAST({{usde_recovery_usdc}} AS double) AS usde_recovery_usdc,CAST({{extra_gas_units}} AS double) AS extra_gas_units),trades AS (SELECT t.block_time,t.block_number,t.tx_hash,t.evt_index,t.project AS dex,t.version,t.project_contract_address,t.tx_to AS transaction_entrypoint,CASE WHEN token_sold_address=0x9d39a5de30e57443bff2a8307a4256c8797a3497 THEN 'sUSDe' ELSE 'USDe' END AS token,t.token_sold_amount,t.token_bought_amount AS usdc_boughtFROM dex.trades t CROSS JOIN settings pWHERE blockchain='ethereum'AND block_month>=CAST(date_trunc('month',current_timestamp-p.days*interval '1' day) AS date)AND block_time>=current_timestamp-p.days*interval '1' day AND block_time<current_timestampAND token_bought_address=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48AND token_sold_address IN (0x4c9edd5852cd905f086c759e8383e09bff1e68b3,0x9d39a5de30e57443bff2a8307a4256c8797a3497)),rate_observations AS (SELECT call_block_time AS rate_time,call_block_number AS rate_block,call_tx_index AS rate_tx_index,BIGINT '0' AS rate_index,'convertToAssets' AS rate_source,CAST(output_0 AS double)/CAST(shares AS double) AS rateFROM ethena_labs_ethereum.stakedusdev2_call_converttoassets CROSS JOIN settings pWHERE call_block_date>=current_date-(p.days+2)*interval '1' dayAND contract_address=0x9d39a5de30e57443bff2a8307a4256c8797a3497 AND call_successAND shares>1000000000000 AND output_0>0UNION ALLSELECT evt_block_time,evt_block_number,evt_tx_index,evt_index,'Deposit assets/shares',CAST(assets AS double)/CAST(shares AS double)FROM ethena_labs_ethereum.stakedusdev2_evt_deposit CROSS JOIN settings pWHERE evt_block_date>=current_date-(p.days+2)*interval '1' dayAND contract_address=0x9d39a5de30e57443bff2a8307a4256c8797a3497 AND shares>1000000000000 AND assets>0UNION ALLSELECT evt_block_time,evt_block_number,evt_tx_index,evt_index,'Withdraw assets/shares',CAST(assets AS double)/CAST(shares AS double)FROM ethena_labs_ethereum.stakedusdev2_evt_withdraw CROSS JOIN settings pWHERE evt_block_date>=current_date-(p.days+2)*interval '1' dayAND contract_address=0x9d39a5de30e57443bff2a8307a4256c8797a3497 AND shares>1000000000000 AND assets>0),susde_ranked AS (SELECT t.*,r.rate,r.rate_time,r.rate_source,row_number() OVER (PARTITION BY t.tx_hash,t.evt_index,t.dex,t.version,t.project_contract_addressORDER BY r.rate_block DESC,r.rate_tx_index DESC,r.rate_index DESC,r.rate_source) AS rnFROM trades t LEFT JOIN rate_observations rON r.rate_block<t.block_number AND r.rate_time>=t.block_time-interval '1' hourWHERE t.token='sUSDe'),normalized AS (SELECT block_time,block_number,tx_hash,evt_index,dex,version,project_contract_address,transaction_entrypoint,