Query editor
WITH delegated_power_deltas_by_token AS (SELECT * FROM query_2988091)-- Two cases - when delegation increases counterparty is the previous evt_index & when it decreases it's the next, delegated_power_deltas_by_token_with_counterparty_plus AS ( -- First when it increasesSELECTdpd1.evt_block_time, dpd1.evt_index, dpd1.user, dpd1.token, dpd1.amount, dpd1.delta, dpd2.user AS counterparty, dpd1.evt_tx_hashFROM delegated_power_deltas_by_token dpd1 LEFT JOIN delegated_power_deltas_by_token dpd2ON dpd1.evt_tx_hash = dpd2.evt_tx_hashAND dpd1.token = dpd2.tokenAND dpd1.evt_index = dpd2.evt_index + 1), delegated_power_deltas_by_token_with_counterparty_minus AS ( -- First when it increasesSELECTdpd1.evt_block_time, dpd1.evt_index, dpd1.user, dpd1.token, dpd1.amount, dpd1.delta, CASEWHEN counterparty IS NOT NULL THEN counterpartyELSE dpd2.userEND AS counterparty, dpd1.evt_tx_hashFROM delegated_power_deltas_by_token_with_counterparty_plus dpd1 LEFT JOIN delegated_power_deltas_by_token dpd2ON dpd1.evt_tx_hash = dpd2.evt_tx_hashAND dpd1.token = dpd2.tokenAND dpd1.evt_index = dpd2.evt_index - 1)-- There is a NULL counterparty whenever the stkAAVE contract is involded in some way in the transaction-- Events emitted for this contract work differently and there are many ways of interacting with it-- Delegated power may change without a privous/next PowerChanged event being emitted-- In order to find the counterparty for each of this interactions each possible one should be decodedSELECT * FROM delegated_power_deltas_by_token_with_counterparty_minus