Query editor
WITH markets AS (SELECT m.*FROM dune.xvslove_team.dataset_markets_all_chains mWHERE vToken != 'VAI'),exchange_rates AS (SELECT*,LEAD(day) OVER (PARTITION BY chain, contract_address ORDER BY day) AS next_update_dayFROM (SELECT chain, contract_address, call_block_date AS day, AVG(output_0) AS exchange_rate_rawFROM (SELECT 'bnb' AS chain, contract_address, call_block_date, output_0 FROM venus_bnb.vbnb_v2_call_exchangeratestored WHERE call_successUNION ALLSELECT 'bnb' AS chain, contract_address, call_block_date, output_0 FROM venus_bnb.vbep20_bnb_core_call_exchangeratestored WHERE call_successUNION ALLSELECT 'bnb' AS chain, contract_address, call_block_date, output_0 FROM venus_bnb.vbep20delegate_v2_call_exchangeratestored WHERE call_success AND contract_address IN (0x0c1da220d301155b87318b90692da8dc43b67340, 0xcc1db43a06d97f736c7b045aedd03c6707c09bdf) --two contracts that could not be carried over to the new tableUNION ALLSELECT chain, contract_address, call_block_date, output_0 FROM venus_multichain.VToken_call_exchangeRateStored WHERE call_success)GROUP BY 1,2,3)),reserve_factor AS ( --reserve factor changes over timeSELECT*,LEAD(day) OVER (PARTITION BY chain, contract_address ORDER BY day) AS next_update_dayFROM (SELECT chain, contract_address, evt_block_date AS day, AVG(newReserveFactorMantissa / POWER(10, 18)) AS reserve_factorFROM (SELECT 'bnb' AS chain, evt_block_date, contract_address, newReserveFactorMantissa FROM venus_bnb.vbnb_v2_evt_newreservefactorUNION ALLSELECT 'bnb' AS chain, evt_block_date, contract_address, newReserveFactorMantissa FROM venus_bnb.vbep20_bnb_core_evt_newreservefactorUNION ALLSELECT 'bnb' AS chain, evt_block_date, contract_address, newReserveFactorMantissa FROM venus_bnb.vbep20delegate_v2_evt_newreservefactor WHERE contract_address IN (0x0c1da220d301155b87318b90692da8dc43b67340, 0xcc1db43a06d97f736c7b045aedd03c6707c09bdf) --two contracts that could not be carried over to the new tableUNION ALLSELECT chain, evt_block_date, contract_address, newReserveFactorMantissa FROM venus_multichain.VToken_evt_NewReserveFactor)GROUP BY 1,2,3)),comptroller AS ( -- used to get share of protocol liquidation feeSELECT*,LEAD(day) OVER (PARTITION BY chain, contract_address ORDER BY day) AS next_update_dayFROM (SELECT 'bnb' AS chain, contract_address, evt_block_date AS day, newComptroller AS comptroller, ROW_NUMBER() OVER(PARTITION BY contract_address ORDER BY evt_block_time DESC, evt_index DESC) AS rn FROM venus_bnb.vbnb_v2_evt_newcomptrollerUNION ALLSELECT 'bnb' AS chain, contract_address, evt_block_date AS day, newComptroller AS comptroller, ROW_NUMBER() OVER(PARTITION BY contract_address ORDER BY evt_block_time DESC, evt_index DESC) AS rn FROM venus_bnb.vbep20_bnb_core_evt_newcomptrollerUNION ALLSELECT 'bnb' AS chain, contract_address, evt_block_date AS day, newComptroller AS comptroller, ROW_NUMBER() OVER(PARTITION BY contract_address ORDER BY evt_block_time DESC, evt_index DESC) AS rn FROM venus_bnb.vbep20delegate_v2_evt_newcomptroller WHERE contract_address IN (0x0c1da220d301155b87318b90692da8dc43b67340, 0xcc1db43a06d97f736c7b045aedd03c6707c09bdf) --two contracts that could not be carried over to the new tableUNION ALLSELECT chain, contract_address, evt_block_date AS day, newComptroller AS comptroller, ROW_NUMBER() OVER(PARTITION BY contract_address ORDER BY evt_block_time DESC, evt_index DESC) AS rn FROM venus_multichain.vtoken_evt_newcomptroller)WHERE rn = 1),