diff --git a/dbt_subprojects/dex/models/_projects/paraswap/paraswap_fees.sql b/dbt_subprojects/dex/models/_projects/paraswap/paraswap_fees.sql index b552eae3a79..0ee500e8b2b 100644 --- a/dbt_subprojects/dex/models/_projects/paraswap/paraswap_fees.sql +++ b/dbt_subprojects/dex/models/_projects/paraswap/paraswap_fees.sql @@ -1,641 +1,443 @@ --para-fee-details-with-epoch-v3 https://dune.com/queries/4257927 -with fee_claim_detail as ( - select 'arbitrum' as blockchain, - date_trunc('day', call_block_time) as block_date, - call_block_time as block_time, - call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 - when _token = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH - else _token end) as token_address, - _fee as fee_raw - from paraswap_arbitrum.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - - union all - - -- Transfer to SmartVault directly - select 'arbitrum' as blockchain, - date_trunc('day', erc.evt_block_time) as block_date, - erc.evt_block_time as block_time, - erc.evt_block_number as call_block_number, - erc.evt_tx_hash as call_tx_hash, - erc.to as user_address, - (case when erc.contract_address = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH - else erc.contract_address end) as token_address, - erc.value as fee_raw - from arbitrum.transactions t - join erc20_arbitrum.evt_Transfer erc on t.hash = erc.evt_tx_hash - and t.block_number = erc.evt_block_number - and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join erc20_arbitrum.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash - and t.block_number = erc2.evt_block_number - and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc2.evt_index > erc.evt_index - left join erc20_arbitrum.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash - and t.block_number = erc3.evt_block_number - and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc3.evt_index > erc.evt_index - where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) - and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_arbitrum.FeeClaimer_call_registerFee - where call_tx_hash = erc.evt_tx_hash - and call_block_number = erc.evt_block_number - ) +{{ config( + schema='paraswap', + alias = 'fees', + post_hook='{{ expose_spells(\'["avalanche_c","fantom","arbitrum","bnb","ethereum","optimism","polygon","base"]\', + "project", + "paraswap", + \'["eptighte"]\') }}' + ) +}} +-- old date: '2024-07-08 12:00' -- start of epoch 20 +{% - union all - select 'arbitrum' as blockchain, - date_trunc('day', t.block_time) as block_date, - t.block_time as block_time, - t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, - t.value as fee_raw - from arbitrum.transactions tr - join arbitrum.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join arbitrum.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number - and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t2.trace_address > t.trace_address - and t2.type = 'call' - and t2.call_type = 'call' - and t2.value > cast(0 as uint256) - left join arbitrum.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number - and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t3.trace_address > t.trace_address - and t3.type = 'call' - and t3.call_type = 'call' - and t3.value > cast(0 as uint256) - where (t2.tx_hash is not null or t3.tx_hash is null) - and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_arbitrum.FeeClaimer_call_registerFee - where call_tx_hash = t.tx_hash - and call_block_number = t.block_number - ) - - union all - - select 'avalanche_c' as blockchain, - date_trunc('day', call_block_time) as block_date, - call_block_time as block_time, - call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7 else _token end) as token_address, - _fee as fee_raw - from paraswap_avalanche_c.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - - union all - - -- Transfer to SmartVault directly - select 'avalanche_c' as blockchain, - date_trunc('day', erc.evt_block_time) as block_date, - erc.evt_block_time as block_time, - erc.evt_block_number as call_block_number, - erc.evt_tx_hash as call_tx_hash, - erc.to as user_address, - erc.contract_address as token_address, - erc.value as fee_raw - from avalanche_c.transactions t - join erc20_avalanche_c.evt_Transfer erc on t.hash = erc.evt_tx_hash - and t.block_number = erc.evt_block_number - and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join erc20_avalanche_c.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash - and t.block_number = erc2.evt_block_number - and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc2.evt_index > erc.evt_index - left join erc20_avalanche_c.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash - and t.block_number = erc3.evt_block_number - and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc3.evt_index > erc.evt_index - where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) - and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_avalanche_c.FeeClaimer_call_registerFee - where call_tx_hash = erc.evt_tx_hash - and call_block_number = erc.evt_block_number - ) +set cutoff_date = '2024-07-08 12:00' - union all +%}{% - select 'avalanche_c' as blockchain, - date_trunc('day', t.block_time) as block_date, - t.block_time as block_time, - t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7 as token_address, - t.value as fee_raw - from avalanche_c.transactions tr - join avalanche_c.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join avalanche_c.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number - and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t2.trace_address > t.trace_address - and t2.type = 'call' - and t2.call_type = 'call' - and t2.value > cast(0 as uint256) - left join avalanche_c.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number - and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t3.trace_address > t.trace_address - and t3.type = 'call' - and t3.call_type = 'call' - and t3.value > cast(0 as uint256) - where (t2.tx_hash is not null or t3.tx_hash is null) - and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_avalanche_c.FeeClaimer_call_registerFee - where call_tx_hash = t.tx_hash - and call_block_number = t.block_number - ) - - union all - - select 'bnb' as blockchain, - date_trunc('day', call_block_time) as block_date, - call_block_time as block_time, - call_block_number, +set blockchains = [ + 'arbitrum', + 'avalanche_c', + 'bnb', + 'ethereum', + 'fantom', + 'optimism', + 'polygon', +] +%}{% +set blockchain_dependencies = { + 'nativeToken': '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', + 'ethereum': { + 'registerFeesV6': 'paraswap_v6_ethereum.AugustusFeeVault_call_registerFees', + 'registerFeesV5': 'paraswap_ethereum.FeeClaimer_call_registerFee', + 'transactions': 'ethereum.transactions', + 'traces': 'ethereum.traces', + 'erc20EvtTransfer': 'erc20_ethereum.evt_Transfer', + 'tokensToReplace': [], + 'wrappedNative': '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', + 'delta_v2': '0x0000000000bbF5c5Fd284e657F01Bd000933C96D' + }, + 'arbitrum': { + 'registerFeesV6': 'paraswap_v6_arbitrum.AugustusFeeVault_call_registerFees', + 'registerFeesV5': 'paraswap_arbitrum.FeeClaimer_call_registerFee', + 'transactions': 'arbitrum.transactions', + 'traces': 'arbitrum.traces', + 'erc20EvtTransfer': 'erc20_arbitrum.evt_Transfer', + 'tokensToReplace': [ + ['0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8', '-- aArbWETH'] + ], + 'wrappedNative': '0x82af49447d8a07e3bd95bd0d56f35241523fbab1' + }, + 'avalanche_c': { + 'registerFeesV6': 'paraswap_v6_avalanche_c.AugustusFeeVault_call_registerFees', + 'registerFeesV5': 'paraswap_avalanche_c.FeeClaimer_call_registerFee', + 'transactions': 'avalanche_c.transactions', + 'traces': 'avalanche_c.traces', + 'erc20EvtTransfer': 'erc20_avalanche_c.evt_Transfer', + 'tokensToReplace': [], + 'wrappedNative': '0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7' + }, + 'bnb': { + 'registerFeesV6': 'paraswap_v6_bnb.AugustusFeeVault_call_registerFees', + 'registerFeesV5': 'paraswap_bnb.FeeClaimer_call_registerFee', + 'transactions': 'bnb.transactions', + 'traces': 'bnb.traces', + 'erc20EvtTransfer': 'erc20_bnb.evt_Transfer', + 'tokensToReplace': [], + 'wrappedNative': '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c' + }, + 'fantom': { + 'registerFeesV6': 'paraswap_v6_fantom.AugustusFeeVault_call_registerFees', + 'registerFeesV5': 'paraswap_fantom.FeeClaimer_call_registerFee', + 'transactions': 'fantom.transactions', + 'traces': 'fantom.traces', + 'erc20EvtTransfer': 'erc20_fantom.evt_Transfer', + 'tokensToReplace': [], + 'wrappedNative': '0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83' + }, + 'optimism': { + 'registerFeesV6': 'paraswap_v6_optimism.AugustusFeeVault_call_registerFees', + 'registerFeesV5': 'paraswap_optimism.FeeClaimer_call_registerFee', + 'transactions': 'optimism.transactions', + 'traces': 'optimism.traces', + 'erc20EvtTransfer': 'erc20_optimism.evt_Transfer', + 'tokensToReplace': [], + 'wrappedNative': '0x4200000000000000000000000000000000000006' + }, + 'polygon': { + 'registerFeesV6': 'paraswap_v6_polygon.AugustusFeeVault_call_registerFees', + 'registerFeesV5': 'paraswap_polygon.FeeClaimer_call_registerFee', + 'transactions': 'polygon.transactions', + 'traces': 'polygon.traces', + 'erc20EvtTransfer': 'erc20_polygon.evt_Transfer', + 'tokensToReplace': [], + 'wrappedNative': '0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270' + }, + 'base': { + 'registerFeesV6': 'paraswap_v6_base.AugustusFeeVault_call_registerFees', + 'transactions': 'base.transactions', + 'traces': 'base.traces', + 'erc20EvtTransfer': 'erc20_base.evt_Transfer', + 'tokensToReplace': [], + 'wrappedNative': '0x4200000000000000000000000000000000000006', + 'delta_v2': '0x0000000000bbF5c5Fd284e657F01Bd000933C96D' + }, + 'zkevm': { + 'registerFeesV6': 'paraswap_v6_zkevm.AugustusFeeVault_call_registerFees', + 'transactions': 'zkevm.transactions', + 'traces': 'zkevm.traces', + 'erc20EvtTransfer': 'erc20_zkevm.evt_Transfer', + 'tokensToReplace': [], + 'wrappedNative': '0x4F9A0e7FD2Bf6067db6994CF12E4495Df938E6e9' + }, +} +%} +with +{% for blockchain in blockchains %}{% + if blockchain_dependencies[blockchain].get('delta_v2') %} + -- delta v2 protocol's and partners revenue src data + deltav2_fees_balances_raw_{{ blockchain }} as ( + select + evt_block_time, + evt_block_number, + evt_tx_hash, + destToken as fee_token, + protocolFee, + partnerFee + from + paraswapdelta_{{ blockchain }}.ParaswapDeltav2_evt_OrderSettled as evt + where + {% if is_incremental() %} + {{ incremental_predicate('evt_block_time') }} AND + {% endif %} + evt_block_time >= TIMESTAMP '{{ cutoff_date }}' + ),{% + endif %} + -- all registerFee calls on v6 Fee Claimer + parsed_fee_data_{{ blockchain }} AS ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(json_parse(feeData) AS MAP) AS fee_json + FROM + paraswap_v6_{{ blockchain }}.AugustusFeeVault_call_registerFees + WHERE + call_success = true + and call_block_time >= TIMESTAMP '{{ cutoff_date }}' + ), + unpacked_fee_data_{{ blockchain }} as ( + SELECT + contract_address, + call_success, call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c else _token end) as token_address, - _fee as fee_raw - from paraswap_bnb.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - + call_trace_address, + call_block_time, + call_block_number, + CAST(fee_json['addresses'] AS ARRAY) AS addresses, + from_hex(CAST(fee_json['token'] AS VARCHAR)) AS _token, + CAST(fee_json['fees'] AS ARRAY) AS fees + FROM + parsed_fee_data_{{ blockchain }} + ), + exploded_data_{{ blockchain }} AS ( + SELECT + call_block_time, + call_block_number, + call_tx_hash, + from_hex(address) as "address", + _token, + fee + FROM + unpacked_fee_data_{{ blockchain }} + CROSS JOIN UNNEST(addresses, fees) AS t(address, fee) + ), +{% endfor %} +fee_claim_detail as ( + {% for blockchain in blockchains %}{% + if blockchain_dependencies[blockchain].get('delta_v2') %} + -- delta v2 protocol's revenue + select '{{ blockchain }}' as blockchain, + 'delta-v2' as source, + date_trunc('day', evt_block_time) as block_date, + evt_block_time as block_time, + evt_block_number as call_block_number, + evt_tx_hash as call_tx_hash, + {{ blockchain_dependencies[blockchain].get('delta_v2') }} as user_address, + (case when fee_token = {{ blockchain_dependencies.get('nativeToken') }} then {{ blockchain_dependencies[blockchain].get('wrappedNative') }} else fee_token end) as token_address, + protocolFee as fee_raw + from deltav2_fees_balances_raw_{{ blockchain }} + where evt_block_time >= TIMESTAMP '{{ cutoff_date }}' union all - - -- Transfer to SmartVault directly - select 'bnb' as blockchain, - date_trunc('day', erc.evt_block_time) as block_date, - erc.evt_block_time as block_time, - erc.evt_block_number as call_block_number, - erc.evt_tx_hash as call_tx_hash, - erc.to as user_address, - erc.contract_address as token_address, - erc.value as fee_raw - from bnb.transactions t - join erc20_bnb.evt_Transfer erc on t.hash = erc.evt_tx_hash - and t.block_number = erc.evt_block_number - and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join erc20_bnb.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash - and t.block_number = erc2.evt_block_number - and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc2.evt_index > erc.evt_index - left join erc20_bnb.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash - and t.block_number = erc3.evt_block_number - and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc3.evt_index > erc.evt_index - where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) - and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_bnb.FeeClaimer_call_registerFee - where call_tx_hash = erc.evt_tx_hash - and call_block_number = erc.evt_block_number - ) - + -- delta v2 partners revenue + select '{{ blockchain }}' as blockchain, + 'delta-v2' as source, + date_trunc('day', evt_block_time) as block_date, + evt_block_time as block_time, + evt_block_number as call_block_number, + evt_tx_hash as call_tx_hash, + -- TODO: not easy to extract partner address here, may need to find a way + 0x0000000000000000000000000000000000000000 as user_address, + (case when fee_token = {{ blockchain_dependencies.get('nativeToken') }} then {{ blockchain_dependencies[blockchain].get('wrappedNative') }} else fee_token end) as token_address, + partnerFee as fee_raw + from deltav2_fees_balances_raw_{{ blockchain }} + where evt_block_time >= TIMESTAMP '{{ cutoff_date }}' union all - - select 'bnb' as blockchain, - date_trunc('day', t.block_time) as block_date, - t.block_time as block_time, - t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c as token_address, - t.value as fee_raw - from bnb.transactions tr - join bnb.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join bnb.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number - and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t2.trace_address > t.trace_address - and t2.type = 'call' - and t2.call_type = 'call' - and t2.value > cast(0 as uint256) - left join bnb.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number - and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t3.trace_address > t.trace_address - and t3.type = 'call' - and t3.call_type = 'call' - and t3.value > cast(0 as uint256) - where (t2.tx_hash is not null or t3.tx_hash is null) - and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_bnb.FeeClaimer_call_registerFee - where call_tx_hash = t.tx_hash - and call_block_number = t.block_number - ) - - union all - - select 'ethereum' as blockchain, + {% endif %} + -- + -- all registerFee calls on v6 Fee Claimer + select '{{ blockchain }}' as blockchain, + 'registerFee-v6' as source, date_trunc('day', call_block_time) as block_date, call_block_time as block_time, call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 else _token end) as token_address, - _fee as fee_raw - from paraswap_ethereum.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - + call_tx_hash, + address as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 else _token end) as token_address, + fee as fee_raw + FROM + exploded_data_{{ blockchain }} + where call_block_time >= TIMESTAMP '{{ cutoff_date }}' + union all - - -- Transfer to SmartVault directly - select 'ethereum' as blockchain, + -- ERC20 transfer to v6 Depositor + select '{{ blockchain }}' as blockchain, + 'erc20-v6' as source, date_trunc('day', erc.evt_block_time) as block_date, erc.evt_block_time as block_time, erc.evt_block_number as call_block_number, erc.evt_tx_hash as call_tx_hash, erc.to as user_address, - erc.contract_address as token_address, + (case when erc.contract_address = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH + else erc.contract_address end) as token_address, erc.value as fee_raw - from ethereum.transactions t - join erc20_ethereum.evt_Transfer erc on t.hash = erc.evt_tx_hash + from {{ blockchain }}.transactions t + join erc20_{{ blockchain }}.evt_Transfer erc on t.hash = erc.evt_tx_hash and t.block_number = erc.evt_block_number - and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3, ParaSwapRepayAdapter -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc.evt_block_time >= TIMESTAMP '{{ cutoff_date }}' -- If following transfers have outgoing only, exclude this revenue. - left join erc20_ethereum.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + left join erc20_{{ blockchain }}.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash and t.block_number = erc2.evt_block_number - and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3 -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 and erc2.evt_index > erc.evt_index - left join erc20_ethereum.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and erc2.evt_block_time >= TIMESTAMP '{{ cutoff_date }}' + left join erc20_{{ blockchain }}.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash and t.block_number = erc3.evt_block_number - and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + and erc3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '{{ cutoff_date }}' + -- i don't understand this conditional. Don't count swaps? But then should omit txs that have ANY outgoing transfer of WETH / ETH, no? where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_ethereum.FeeClaimer_call_registerFee - where call_tx_hash = erc.evt_tx_hash - and call_block_number = erc.evt_block_number - ) + and block_time >= TIMESTAMP '{{ cutoff_date }}' union all - - select 'ethereum' as blockchain, + -- v6: ETH Transfer to SmartVault directly + select '{{ blockchain }}' as blockchain, + 'eth-v6' as source, date_trunc('day', t.block_time) as block_date, t.block_time as block_time, t.block_number as call_block_number, t.tx_hash as call_tx_hash, t.to as user_address, - 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 as token_address, + 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, t.value as fee_raw - from ethereum.transactions tr - join ethereum.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + from {{ blockchain }}.transactions tr + join {{ blockchain }}.traces t on + t.block_time >= TIMESTAMP '{{ cutoff_date }}' + and tr.hash = t.tx_hash + and tr.block_number = t.block_number + -- and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 -- If following transfers have outgoing only, exclude this revenue. - left join ethereum.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number - and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + left join {{ blockchain }}.traces t2 on + t2.block_time >= TIMESTAMP '{{ cutoff_date }}' + and tr.hash = t2.tx_hash -- Other income for Depositor v6 + and tr.block_number = t2.block_number + -- and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 and t2.trace_address > t.trace_address and t2.type = 'call' and t2.call_type = 'call' and t2.value > cast(0 as uint256) - left join ethereum.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number - and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + left join {{ blockchain }}.traces t3 on + t3.block_time >= TIMESTAMP '{{ cutoff_date }}' + and tr.hash = t3.tx_hash -- Outgoing + and tr.block_number = t3.block_number + and t3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 and t3.trace_address > t.trace_address and t3.type = 'call' and t3.call_type = 'call' - and t3.value > cast(0 as uint256) + and t3.value > cast(0 as uint256) where (t2.tx_hash is not null or t3.tx_hash is null) and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_ethereum.FeeClaimer_call_registerFee - where call_tx_hash = t.tx_hash - and call_block_number = t.block_number - ) - - union all - - select 'fantom' as blockchain, - date_trunc('day', call_block_time) as block_date, - call_block_time as block_time, - call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83 else _token end) as token_address, - _fee as fee_raw - from paraswap_fantom.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - - union all - - -- Transfer to SmartVault directly - select 'fantom' as blockchain, - date_trunc('day', erc.evt_block_time) as block_date, - erc.evt_block_time as block_time, - erc.evt_block_number as call_block_number, - erc.evt_tx_hash as call_tx_hash, - erc.to as user_address, - erc.contract_address as token_address, - erc.value as fee_raw - from fantom.transactions t - join erc20_fantom.evt_Transfer erc on t.hash = erc.evt_tx_hash - and t.block_number = erc.evt_block_number - and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join erc20_fantom.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash - and t.block_number = erc2.evt_block_number - and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc2.evt_index > erc.evt_index - left join erc20_fantom.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash - and t.block_number = erc3.evt_block_number - and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc3.evt_index > erc.evt_index - where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) - and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_fantom.FeeClaimer_call_registerFee - where call_tx_hash = erc.evt_tx_hash - and call_block_number = erc.evt_block_number - ) + and tr.block_time >= TIMESTAMP '{{ cutoff_date }}' + -- union all - select 'fantom' as blockchain, - date_trunc('day', t.block_time) as block_date, - t.block_time as block_time, - t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83 as token_address, - t.value as fee_raw - from fantom.transactions tr - join fantom.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join fantom.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number - and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t2.trace_address > t.trace_address - and t2.type = 'call' - and t2.call_type = 'call' - and t2.value > cast(0 as uint256) - left join fantom.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number - and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t3.trace_address > t.trace_address - and t3.type = 'call' - and t3.call_type = 'call' - and t3.value > cast(0 as uint256) - where (t2.tx_hash is not null or t3.tx_hash is null) - and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_fantom.FeeClaimer_call_registerFee - where call_tx_hash = t.tx_hash - and call_block_number = t.block_number - ) - - union all - - select 'optimism' as blockchain, + -- v5 fee claimer.registerFee + select '{{ blockchain }}' as blockchain, + 'registerFee-v5' as source, date_trunc('day', call_block_time) as block_date, call_block_time as block_time, call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x4200000000000000000000000000000000000006 else _token end) as token_address, - _fee as fee_raw - from paraswap_optimism.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - - union all - - -- Transfer to SmartVault directly - select 'optimism' as blockchain, - date_trunc('day', erc.evt_block_time) as block_date, - erc.evt_block_time as block_time, - erc.evt_block_number as call_block_number, - erc.evt_tx_hash as call_tx_hash, - erc.to as user_address, - erc.contract_address as token_address, - erc.value as fee_raw - from optimism.transactions t - join erc20_optimism.evt_Transfer erc on t.hash = erc.evt_tx_hash - and t.block_number = erc.evt_block_number - and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join erc20_optimism.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash - and t.block_number = erc2.evt_block_number - and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc2.evt_index > erc.evt_index - left join erc20_optimism.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash - and t.block_number = erc3.evt_block_number - and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and erc3.evt_index > erc.evt_index - where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) - and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_optimism.FeeClaimer_call_registerFee - where call_tx_hash = erc.evt_tx_hash - and call_block_number = erc.evt_block_number - ) + call_tx_hash, + _account as user_address, + (case when _token = {{ blockchain_dependencies.get('nativeToken') }} then {{ blockchain_dependencies[blockchain].get('wrappedNative') }}{% + if blockchain_dependencies[blockchain].get('tokensToReplace')|length != 0 %}{% + for tokenToReplace in blockchain_dependencies[blockchain].get('tokensToReplace') %} + when _token = {{ tokenToReplace[0] }} then {{ blockchain_dependencies[blockchain].get('wrappedNative') }} {{ tokenToReplace[1] }}{% + endfor %} + {% + endif %} else _token end) as token_address, + _fee as fee_raw + from {{ blockchain_dependencies[blockchain].get('registerFeesV5') }} + where call_success = true + and call_block_time >= TIMESTAMP '{{ cutoff_date }}' union all - select 'optimism' as blockchain, - date_trunc('day', t.block_time) as block_date, - t.block_time as block_time, - t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0x4200000000000000000000000000000000000006 as token_address, - t.value as fee_raw - from optimism.transactions tr - join optimism.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - -- If following transfers have outgoing only, exclude this revenue. - left join optimism.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number - and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t2.trace_address > t.trace_address - and t2.type = 'call' - and t2.call_type = 'call' - and t2.value > cast(0 as uint256) - left join optimism.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number - and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault - and t3.trace_address > t.trace_address - and t3.type = 'call' - and t3.call_type = 'call' - and t3.value > cast(0 as uint256) - where (t2.tx_hash is not null or t3.tx_hash is null) - and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - and not exists ( - select 1 from paraswap_optimism.FeeClaimer_call_registerFee - where call_tx_hash = t.tx_hash - and call_block_number = t.block_number - ) - - union all - - select 'polygon' as blockchain, - date_trunc('day', call_block_time) as block_date, - call_block_time as block_time, - call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270 else _token end) as token_address, - _fee as fee_raw - from paraswap_polygon.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - - union all - -- Transfer to SmartVault directly - select 'polygon' as blockchain, + -- v5: ERC20 Transfer to SmartVault directly + select '{{ blockchain }}' as blockchain, + 'erc20-v5' as source, date_trunc('day', erc.evt_block_time) as block_date, erc.evt_block_time as block_time, erc.evt_block_number as call_block_number, - erc.evt_tx_hash as call_tx_hash, - erc.to as user_address, - erc.contract_address as token_address, - erc.value as fee_raw - from polygon.transactions t - join erc20_polygon.evt_Transfer erc on t.hash = erc.evt_tx_hash + erc.evt_tx_hash as call_tx_hash, + erc.to as user_address,{% + if blockchain_dependencies[blockchain].get('tokensToReplace')|length == 0 %} + erc.contract_address as token_address,{% + else + %} + (case {% + for + tokenToReplace in blockchain_dependencies[blockchain].get('tokensToReplace') + %}when erc.contract_address = {{ tokenToReplace[0] }} then {{ blockchain_dependencies[blockchain].get('wrappedNative') }} {{ tokenToReplace[1] }}{% + endfor %} + else erc.contract_address end) as token_address,{% + endif %} + erc.value as fee_raw + from {{ blockchain_dependencies[blockchain].get('transactions') }} t + join {{ blockchain_dependencies[blockchain].get('erc20EvtTransfer') }} erc on t.hash = erc.evt_tx_hash and t.block_number = erc.evt_block_number and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + and erc.evt_block_time >= TIMESTAMP '{{ cutoff_date }}' -- If following transfers have outgoing only, exclude this revenue. - left join erc20_polygon.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + left join {{ blockchain_dependencies[blockchain].get('erc20EvtTransfer') }} erc2 on t.hash = erc2.evt_tx_hash and t.block_number = erc2.evt_block_number and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc2.evt_index > erc.evt_index - left join erc20_polygon.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and erc2.evt_block_time >= TIMESTAMP '{{ cutoff_date }}' + left join {{ blockchain_dependencies[blockchain].get('erc20EvtTransfer') }} erc3 on t.hash = erc3.evt_tx_hash and t.block_number = erc3.evt_block_number and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '{{ cutoff_date }}' where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and block_time >= TIMESTAMP '{{ cutoff_date }}' and not exists ( - select 1 from paraswap_polygon.FeeClaimer_call_registerFee + select 1 from paraswap_{{blockchain}}.FeeClaimer_call_registerFee where call_tx_hash = erc.evt_tx_hash and call_block_number = erc.evt_block_number - ) + ) union all - - select 'polygon' as blockchain, + -- v5: ETH Transfer to SmartVault directly + select '{{ blockchain }}' as blockchain, + 'eth-v5' AS source, date_trunc('day', t.block_time) as block_date, t.block_time as block_time, t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270 as token_address, + t.tx_hash as call_tx_hash, + t.to as user_address, + {{ blockchain_dependencies[blockchain].get('wrappedNative')}} as token_address, t.value as fee_raw - from polygon.transactions tr - join polygon.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + from {{ blockchain_dependencies[blockchain].get('transactions') }} tr + join {{ blockchain_dependencies[blockchain].get('traces') }} t on tr.hash = t.tx_hash + and t.block_time >= TIMESTAMP '{{ cutoff_date }}' + and tr.block_number = t.block_number + and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault -- If following transfers have outgoing only, exclude this revenue. - left join polygon.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number + left join {{ blockchain_dependencies[blockchain].get('traces') }} t2 on tr.hash = t2.tx_hash -- Other income for SmartVault + and t2.block_time >= TIMESTAMP '{{ cutoff_date }}' + and tr.block_number = t2.block_number and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t2.trace_address > t.trace_address and t2.type = 'call' and t2.call_type = 'call' and t2.value > cast(0 as uint256) - left join polygon.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number + left join {{ blockchain_dependencies[blockchain].get('traces') }} t3 on tr.hash = t3.tx_hash -- Outgoing + and t3.block_time >= TIMESTAMP '{{ cutoff_date }}' + and tr.block_number = t3.block_number and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t3.trace_address > t.trace_address and t3.type = 'call' and t3.call_type = 'call' - and t3.value > cast(0 as uint256) + and t3.value > cast(0 as uint256) where (t2.tx_hash is not null or t3.tx_hash is null) and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and tr.block_time >= TIMESTAMP '{{ cutoff_date }}' and not exists ( - select 1 from paraswap_polygon.FeeClaimer_call_registerFee + select 1 from paraswap_{{blockchain}}.FeeClaimer_call_registerFee where call_tx_hash = t.tx_hash and call_block_number = t.block_number ) + {% + if not loop.last %} + union all{% + endif %} + {% endfor %} ), - + price_list as ( select date_trunc('day', minute) as block_date, blockchain, contract_address, avg(price) as price from prices.usd - where minute >= date('2021-04-01') + where minute >= TIMESTAMP '{{ cutoff_date }}' group by 1, 2, 3 ) -select e.epoch_num as en, +select + f.source as source, + e.epoch_num as en, f.blockchain as bc, f.block_date as bd, f.block_time as bt, diff --git a/dbt_subprojects/dex/models/_projects/paraswap/paraswap_fees_by_epoch.sql b/dbt_subprojects/dex/models/_projects/paraswap/paraswap_fees_by_epoch.sql index 3596b7ac9c5..4db13a74e21 100644 --- a/dbt_subprojects/dex/models/_projects/paraswap/paraswap_fees_by_epoch.sql +++ b/dbt_subprojects/dex/models/_projects/paraswap/paraswap_fees_by_epoch.sql @@ -2,28 +2,36 @@ -- para-fee-detail-v4-materialized-view with paraswap_fee_address(blockchain, address) as ( - values - ('ethereum',0xb1720612d0131839dc489fcf20398ea925282fca ), - ('ethereum',0xd5b927956057075377263aab7f8afc12f85100db ), - ('ethereum',0x619bbf92fd6ba59893327676b2685a3762a49a33 ), - ('arbitrum',0xd5b927956057075377263aab7f8afc12f85100db ), + values + ('ethereum',0xb1720612d0131839dc489fcf20398ea925282fca ), -- no hits since epoch 20 + ('ethereum',0xd5b927956057075377263aab7f8afc12f85100db ), -- v5 smart vault + ('ethereum',0x619bbf92fd6ba59893327676b2685a3762a49a33 ), -- no hits since epoch 20 + ('ethereum',0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f ), -- depositor v6 augustus -- accumulates fees from augustus v6 + ('ethereum',0x0000000000bbf5c5fd284e657f01bd000933c96d ), -- delta v2, accumulates all fees on itself + ('arbitrum',0xd5b927956057075377263aab7f8afc12f85100db ), -- v5 smart vault ('arbitrum',0xb9eeb869d6dbfc5988d5adf3f63cd6b77ac9d9fd ), ('arbitrum',0x7dA82E75BE36Ab9625B1dd40A5aE5181b43473f3 ), - ('bnb',0xd5b927956057075377263aab7f8afc12f85100db ), + ('arbitrum',0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f ), -- depositor v6 augustus -- accumulates fees from augustus v6 + ('bnb',0xd5b927956057075377263aab7f8afc12f85100db ), -- v5 smart vault ('bnb',0xcbb65ad3e64f404b5411486e15561bfb645ce642 ), ('bnb',0x8c1a1D0b6286F35d47a676aB78482f1cf3D749dC ), - ('avalanche_c',0xd5b927956057075377263aab7f8afc12f85100db ), + ('bnb',0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f ), -- depositor v6 augustus -- accumulates fees from augustus v6 + ('avalanche_c',0xd5b927956057075377263aab7f8afc12f85100db ), -- v5 smart vault ('avalanche_c',0x1bf4c97384e7bdc609017305edb23fd28c13e76a ), ('avalanche_c',0xAFFdeC0FE0B5BBfd725642D87D14c465d25F8dE8 ), - ('fantom',0xd5b927956057075377263aab7f8afc12f85100db ), + ('avalanche_c',0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f ), -- depositor v6 augustus -- accumulates fees from augustus v6 + ('fantom',0xd5b927956057075377263aab7f8afc12f85100db ), -- v5 smart vault ('fantom',0xced122f9b99d224675eeda13f0c14639c5633f91 ), ('fantom',0x5487683dc3216655D0C8AA31255e2e313b99B477 ), - ('optimism',0xd5b927956057075377263aab7f8afc12f85100db ), + ('fantom',0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f ), -- depositor v6 augustus -- accumulates fees from augustus v6 + ('optimism',0xd5b927956057075377263aab7f8afc12f85100db ), -- v5 smart vault ('optimism',0x3b28a6f6291f7e8277751f2911ac49c585d049f6 ), ('optimism',0xf93A7F819F83DBfDbC307d4D4f0FE5a208C50318 ), - ('polygon',0xd5b927956057075377263aab7f8afc12f85100db ), + ('optimism',0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f ), -- depositor v6 augustus -- accumulates fees from augustus v6 + ('polygon',0xd5b927956057075377263aab7f8afc12f85100db ), -- v5 smart vault ('polygon',0x0c84cd406b8a4e07df9a1b15ef348023a1dcd075 ), - ('polygon',0xABF832105D7D19E5DEC28D014d5a12579dfa1097 ) + ('polygon',0xABF832105D7D19E5DEC28D014d5a12579dfa1097 ), + ('polygon',0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f ) -- depositor v6 augustus -- accumulates fees from augustus v6 ), revenue_detail as ( @@ -49,7 +57,7 @@ revenue_detail as ( --sum(fa) filter (where bd >= current_date - interval '1' day and bd < current_date) as fee_amount_24_hour, --sum(fa) filter (where bd >= current_date - interval '90' day and bd < current_date) as fee_amount_90_day, --sum(fa) filter (where bd >= date_trunc('year', now())) as fee_amount_ytd - from dune.paraswap.result_para_fee_details_with_epoch_v_3 f + from dune.paraswap.result_para_fee_details_with_epoch_v_4 f left join paraswap_fee_address a on f.bc = a.blockchain and f.ua = a.address union all diff --git a/dbt_subprojects/dex/target/compiled/dex/models/_projects/paraswap/paraswap_fees.sql b/dbt_subprojects/dex/target/compiled/dex/models/_projects/paraswap/paraswap_fees.sql index 553f852e96a..0af9b741e68 100644 --- a/dbt_subprojects/dex/target/compiled/dex/models/_projects/paraswap/paraswap_fees.sql +++ b/dbt_subprojects/dex/target/compiled/dex/models/_projects/paraswap/paraswap_fees.sql @@ -1,23 +1,346 @@ --para-fee-details-with-epoch-v3 https://dune.com/queries/4257927 -with fee_claim_detail as ( + +-- old date: '2024-07-08 12:00' -- start of epoch 20 + +with + + -- all registerFee calls on v6 Fee Claimer + parsed_fee_data_arbitrum AS ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(json_parse(feeData) AS MAP) AS fee_json + FROM + paraswap_v6_arbitrum.AugustusFeeVault_call_registerFees + WHERE + call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + ), + unpacked_fee_data_arbitrum as ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(fee_json['addresses'] AS ARRAY) AS addresses, + from_hex(CAST(fee_json['token'] AS VARCHAR)) AS _token, + CAST(fee_json['fees'] AS ARRAY) AS fees + FROM + parsed_fee_data_arbitrum + ), + exploded_data_arbitrum AS ( + SELECT + call_block_time, + call_block_number, + call_tx_hash, + from_hex(address) as "address", + _token, + fee + FROM + unpacked_fee_data_arbitrum + CROSS JOIN UNNEST(addresses, fees) AS t(address, fee) + ), + + -- all registerFee calls on v6 Fee Claimer + parsed_fee_data_avalanche_c AS ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(json_parse(feeData) AS MAP) AS fee_json + FROM + paraswap_v6_avalanche_c.AugustusFeeVault_call_registerFees + WHERE + call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + ), + unpacked_fee_data_avalanche_c as ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(fee_json['addresses'] AS ARRAY) AS addresses, + from_hex(CAST(fee_json['token'] AS VARCHAR)) AS _token, + CAST(fee_json['fees'] AS ARRAY) AS fees + FROM + parsed_fee_data_avalanche_c + ), + exploded_data_avalanche_c AS ( + SELECT + call_block_time, + call_block_number, + call_tx_hash, + from_hex(address) as "address", + _token, + fee + FROM + unpacked_fee_data_avalanche_c + CROSS JOIN UNNEST(addresses, fees) AS t(address, fee) + ), + + -- all registerFee calls on v6 Fee Claimer + parsed_fee_data_bnb AS ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(json_parse(feeData) AS MAP) AS fee_json + FROM + paraswap_v6_bnb.AugustusFeeVault_call_registerFees + WHERE + call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + ), + unpacked_fee_data_bnb as ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(fee_json['addresses'] AS ARRAY) AS addresses, + from_hex(CAST(fee_json['token'] AS VARCHAR)) AS _token, + CAST(fee_json['fees'] AS ARRAY) AS fees + FROM + parsed_fee_data_bnb + ), + exploded_data_bnb AS ( + SELECT + call_block_time, + call_block_number, + call_tx_hash, + from_hex(address) as "address", + _token, + fee + FROM + unpacked_fee_data_bnb + CROSS JOIN UNNEST(addresses, fees) AS t(address, fee) + ), + + -- delta v2 protocol's and partners revenue src data + deltav2_fees_balances_raw_ethereum as ( + select + evt_block_time, + evt_block_number, + evt_tx_hash, + destToken as fee_token, + protocolFee, + partnerFee + from + paraswapdelta_ethereum.ParaswapDeltav2_evt_OrderSettled as evt + where + + evt_block_time >= TIMESTAMP '2024-07-08 12:00' + ), + -- all registerFee calls on v6 Fee Claimer + parsed_fee_data_ethereum AS ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(json_parse(feeData) AS MAP) AS fee_json + FROM + paraswap_v6_ethereum.AugustusFeeVault_call_registerFees + WHERE + call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + ), + unpacked_fee_data_ethereum as ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(fee_json['addresses'] AS ARRAY) AS addresses, + from_hex(CAST(fee_json['token'] AS VARCHAR)) AS _token, + CAST(fee_json['fees'] AS ARRAY) AS fees + FROM + parsed_fee_data_ethereum + ), + exploded_data_ethereum AS ( + SELECT + call_block_time, + call_block_number, + call_tx_hash, + from_hex(address) as "address", + _token, + fee + FROM + unpacked_fee_data_ethereum + CROSS JOIN UNNEST(addresses, fees) AS t(address, fee) + ), + + -- all registerFee calls on v6 Fee Claimer + parsed_fee_data_fantom AS ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(json_parse(feeData) AS MAP) AS fee_json + FROM + paraswap_v6_fantom.AugustusFeeVault_call_registerFees + WHERE + call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + ), + unpacked_fee_data_fantom as ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(fee_json['addresses'] AS ARRAY) AS addresses, + from_hex(CAST(fee_json['token'] AS VARCHAR)) AS _token, + CAST(fee_json['fees'] AS ARRAY) AS fees + FROM + parsed_fee_data_fantom + ), + exploded_data_fantom AS ( + SELECT + call_block_time, + call_block_number, + call_tx_hash, + from_hex(address) as "address", + _token, + fee + FROM + unpacked_fee_data_fantom + CROSS JOIN UNNEST(addresses, fees) AS t(address, fee) + ), + + -- all registerFee calls on v6 Fee Claimer + parsed_fee_data_optimism AS ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(json_parse(feeData) AS MAP) AS fee_json + FROM + paraswap_v6_optimism.AugustusFeeVault_call_registerFees + WHERE + call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + ), + unpacked_fee_data_optimism as ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(fee_json['addresses'] AS ARRAY) AS addresses, + from_hex(CAST(fee_json['token'] AS VARCHAR)) AS _token, + CAST(fee_json['fees'] AS ARRAY) AS fees + FROM + parsed_fee_data_optimism + ), + exploded_data_optimism AS ( + SELECT + call_block_time, + call_block_number, + call_tx_hash, + from_hex(address) as "address", + _token, + fee + FROM + unpacked_fee_data_optimism + CROSS JOIN UNNEST(addresses, fees) AS t(address, fee) + ), + + -- all registerFee calls on v6 Fee Claimer + parsed_fee_data_polygon AS ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(json_parse(feeData) AS MAP) AS fee_json + FROM + paraswap_v6_polygon.AugustusFeeVault_call_registerFees + WHERE + call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + ), + unpacked_fee_data_polygon as ( + SELECT + contract_address, + call_success, + call_tx_hash, + call_trace_address, + call_block_time, + call_block_number, + CAST(fee_json['addresses'] AS ARRAY) AS addresses, + from_hex(CAST(fee_json['token'] AS VARCHAR)) AS _token, + CAST(fee_json['fees'] AS ARRAY) AS fees + FROM + parsed_fee_data_polygon + ), + exploded_data_polygon AS ( + SELECT + call_block_time, + call_block_number, + call_tx_hash, + from_hex(address) as "address", + _token, + fee + FROM + unpacked_fee_data_polygon + CROSS JOIN UNNEST(addresses, fees) AS t(address, fee) + ), + +fee_claim_detail as ( + + -- + -- all registerFee calls on v6 Fee Claimer select 'arbitrum' as blockchain, + 'registerFee-v6' as source, date_trunc('day', call_block_time) as block_date, call_block_time as block_time, call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 - when _token = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH - else _token end) as token_address, - _fee as fee_raw - from paraswap_arbitrum.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - + call_tx_hash, + address as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 else _token end) as token_address, + fee as fee_raw + FROM + exploded_data_arbitrum + where call_block_time >= TIMESTAMP '2024-07-08 12:00' + union all - - -- Transfer to SmartVault directly + -- ERC20 transfer to v6 Depositor select 'arbitrum' as blockchain, + 'erc20-v6' as source, date_trunc('day', erc.evt_block_time) as block_date, erc.evt_block_time as block_time, erc.evt_block_number as call_block_number, @@ -28,614 +351,1401 @@ with fee_claim_detail as ( erc.value as fee_raw from arbitrum.transactions t join erc20_arbitrum.evt_Transfer erc on t.hash = erc.evt_tx_hash + and t.block_number = erc.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3, ParaSwapRepayAdapter -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- If following transfers have outgoing only, exclude this revenue. + left join erc20_arbitrum.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + and t.block_number = erc2.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3 -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc2.evt_index > erc.evt_index + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_arbitrum.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and t.block_number = erc3.evt_block_number + and erc3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- i don't understand this conditional. Don't count swaps? But then should omit txs that have ANY outgoing transfer of WETH / ETH, no? + where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) + and t.success + and block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + -- v6: ETH Transfer to SmartVault directly + select 'arbitrum' as blockchain, + 'eth-v6' as source, + date_trunc('day', t.block_time) as block_date, + t.block_time as block_time, + t.block_number as call_block_number, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, + t.value as fee_raw + from arbitrum.transactions tr + join arbitrum.traces t on + t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t.tx_hash + and tr.block_number = t.block_number + -- and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + -- If following transfers have outgoing only, exclude this revenue. + left join arbitrum.traces t2 on + t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t2.tx_hash -- Other income for Depositor v6 + and tr.block_number = t2.block_number + -- and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t2.trace_address > t.trace_address + and t2.type = 'call' + and t2.call_type = 'call' + and t2.value > cast(0 as uint256) + left join arbitrum.traces t3 on + t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t3.tx_hash -- Outgoing + and tr.block_number = t3.block_number + and t3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t3.trace_address > t.trace_address + and t3.type = 'call' + and t3.call_type = 'call' + and t3.value > cast(0 as uint256) + where (t2.tx_hash is not null or t3.tx_hash is null) + and tr.success + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' + + -- + union all + + -- v5 fee claimer.registerFee + select 'arbitrum' as blockchain, + 'registerFee-v5' as source, + date_trunc('day', call_block_time) as block_date, + call_block_time as block_time, + call_block_number, + call_tx_hash, + _account as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 + when _token = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH + else _token end) as token_address, + _fee as fee_raw + from paraswap_arbitrum.FeeClaimer_call_registerFee + where call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + + -- Transfer to SmartVault directly + -- v5: ERC20 Transfer to SmartVault directly + select 'arbitrum' as blockchain, + 'erc20-v5' as source, + date_trunc('day', erc.evt_block_time) as block_date, + erc.evt_block_time as block_time, + erc.evt_block_number as call_block_number, + erc.evt_tx_hash as call_tx_hash, + erc.to as user_address, + (case when erc.contract_address = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH + else erc.contract_address end) as token_address, + erc.value as fee_raw + from arbitrum.transactions t + join erc20_arbitrum.evt_Transfer erc on t.hash = erc.evt_tx_hash and t.block_number = erc.evt_block_number and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' -- If following transfers have outgoing only, exclude this revenue. - left join erc20_arbitrum.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + left join erc20_arbitrum.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash and t.block_number = erc2.evt_block_number and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc2.evt_index > erc.evt_index - left join erc20_arbitrum.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_arbitrum.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash and t.block_number = erc3.evt_block_number and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_arbitrum.FeeClaimer_call_registerFee + select 1 from paraswap_arbitrum.FeeClaimer_call_registerFee where call_tx_hash = erc.evt_tx_hash and call_block_number = erc.evt_block_number - ) + ) union all - - select 'arbitrum' as blockchain, + -- v5: ETH Transfer to SmartVault directly + select 'arbitrum' as blockchain, + 'eth-v5' AS source, date_trunc('day', t.block_time) as block_date, t.block_time as block_time, t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, t.value as fee_raw - from arbitrum.transactions tr - join arbitrum.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + from arbitrum.transactions tr + join arbitrum.traces t on tr.hash = t.tx_hash + and t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t.block_number + and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault -- If following transfers have outgoing only, exclude this revenue. left join arbitrum.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number + and t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t2.block_number and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t2.trace_address > t.trace_address and t2.type = 'call' and t2.call_type = 'call' and t2.value > cast(0 as uint256) - left join arbitrum.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number + left join arbitrum.traces t3 on tr.hash = t3.tx_hash -- Outgoing + and t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t3.block_number and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t3.trace_address > t.trace_address and t3.type = 'call' and t3.call_type = 'call' - and t3.value > cast(0 as uint256) + and t3.value > cast(0 as uint256) where (t2.tx_hash is not null or t3.tx_hash is null) and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_arbitrum.FeeClaimer_call_registerFee + select 1 from paraswap_arbitrum.FeeClaimer_call_registerFee where call_tx_hash = t.tx_hash and call_block_number = t.block_number ) + + union all - union all - + -- + -- all registerFee calls on v6 Fee Claimer select 'avalanche_c' as blockchain, + 'registerFee-v6' as source, date_trunc('day', call_block_time) as block_date, call_block_time as block_time, call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7 else _token end) as token_address, - _fee as fee_raw - from paraswap_avalanche_c.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - + call_tx_hash, + address as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 else _token end) as token_address, + fee as fee_raw + FROM + exploded_data_avalanche_c + where call_block_time >= TIMESTAMP '2024-07-08 12:00' + union all - - -- Transfer to SmartVault directly + -- ERC20 transfer to v6 Depositor select 'avalanche_c' as blockchain, + 'erc20-v6' as source, date_trunc('day', erc.evt_block_time) as block_date, erc.evt_block_time as block_time, erc.evt_block_number as call_block_number, erc.evt_tx_hash as call_tx_hash, erc.to as user_address, - erc.contract_address as token_address, + (case when erc.contract_address = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH + else erc.contract_address end) as token_address, erc.value as fee_raw from avalanche_c.transactions t join erc20_avalanche_c.evt_Transfer erc on t.hash = erc.evt_tx_hash + and t.block_number = erc.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3, ParaSwapRepayAdapter -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- If following transfers have outgoing only, exclude this revenue. + left join erc20_avalanche_c.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + and t.block_number = erc2.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3 -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc2.evt_index > erc.evt_index + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_avalanche_c.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and t.block_number = erc3.evt_block_number + and erc3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- i don't understand this conditional. Don't count swaps? But then should omit txs that have ANY outgoing transfer of WETH / ETH, no? + where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) + and t.success + and block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + -- v6: ETH Transfer to SmartVault directly + select 'avalanche_c' as blockchain, + 'eth-v6' as source, + date_trunc('day', t.block_time) as block_date, + t.block_time as block_time, + t.block_number as call_block_number, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, + t.value as fee_raw + from avalanche_c.transactions tr + join avalanche_c.traces t on + t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t.tx_hash + and tr.block_number = t.block_number + -- and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + -- If following transfers have outgoing only, exclude this revenue. + left join avalanche_c.traces t2 on + t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t2.tx_hash -- Other income for Depositor v6 + and tr.block_number = t2.block_number + -- and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t2.trace_address > t.trace_address + and t2.type = 'call' + and t2.call_type = 'call' + and t2.value > cast(0 as uint256) + left join avalanche_c.traces t3 on + t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t3.tx_hash -- Outgoing + and tr.block_number = t3.block_number + and t3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t3.trace_address > t.trace_address + and t3.type = 'call' + and t3.call_type = 'call' + and t3.value > cast(0 as uint256) + where (t2.tx_hash is not null or t3.tx_hash is null) + and tr.success + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' + + -- + union all + + -- v5 fee claimer.registerFee + select 'avalanche_c' as blockchain, + 'registerFee-v5' as source, + date_trunc('day', call_block_time) as block_date, + call_block_time as block_time, + call_block_number, + call_tx_hash, + _account as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7 else _token end) as token_address, + _fee as fee_raw + from paraswap_avalanche_c.FeeClaimer_call_registerFee + where call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + + -- Transfer to SmartVault directly + -- v5: ERC20 Transfer to SmartVault directly + select 'avalanche_c' as blockchain, + 'erc20-v5' as source, + date_trunc('day', erc.evt_block_time) as block_date, + erc.evt_block_time as block_time, + erc.evt_block_number as call_block_number, + erc.evt_tx_hash as call_tx_hash, + erc.to as user_address, + erc.contract_address as token_address, + erc.value as fee_raw + from avalanche_c.transactions t + join erc20_avalanche_c.evt_Transfer erc on t.hash = erc.evt_tx_hash and t.block_number = erc.evt_block_number and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' -- If following transfers have outgoing only, exclude this revenue. - left join erc20_avalanche_c.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + left join erc20_avalanche_c.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash and t.block_number = erc2.evt_block_number and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc2.evt_index > erc.evt_index - left join erc20_avalanche_c.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_avalanche_c.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash and t.block_number = erc3.evt_block_number and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_avalanche_c.FeeClaimer_call_registerFee + select 1 from paraswap_avalanche_c.FeeClaimer_call_registerFee where call_tx_hash = erc.evt_tx_hash and call_block_number = erc.evt_block_number - ) + ) union all - - select 'avalanche_c' as blockchain, + -- v5: ETH Transfer to SmartVault directly + select 'avalanche_c' as blockchain, + 'eth-v5' AS source, date_trunc('day', t.block_time) as block_date, t.block_time as block_time, t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7 as token_address, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7 as token_address, t.value as fee_raw - from avalanche_c.transactions tr - join avalanche_c.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + from avalanche_c.transactions tr + join avalanche_c.traces t on tr.hash = t.tx_hash + and t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t.block_number + and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault -- If following transfers have outgoing only, exclude this revenue. left join avalanche_c.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number + and t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t2.block_number and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t2.trace_address > t.trace_address and t2.type = 'call' and t2.call_type = 'call' and t2.value > cast(0 as uint256) - left join avalanche_c.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number + left join avalanche_c.traces t3 on tr.hash = t3.tx_hash -- Outgoing + and t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t3.block_number and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t3.trace_address > t.trace_address and t3.type = 'call' and t3.call_type = 'call' - and t3.value > cast(0 as uint256) + and t3.value > cast(0 as uint256) where (t2.tx_hash is not null or t3.tx_hash is null) and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_avalanche_c.FeeClaimer_call_registerFee + select 1 from paraswap_avalanche_c.FeeClaimer_call_registerFee where call_tx_hash = t.tx_hash and call_block_number = t.block_number ) + + union all - union all - + -- + -- all registerFee calls on v6 Fee Claimer select 'bnb' as blockchain, + 'registerFee-v6' as source, date_trunc('day', call_block_time) as block_date, call_block_time as block_time, call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c else _token end) as token_address, - _fee as fee_raw - from paraswap_bnb.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - + call_tx_hash, + address as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 else _token end) as token_address, + fee as fee_raw + FROM + exploded_data_bnb + where call_block_time >= TIMESTAMP '2024-07-08 12:00' + union all - - -- Transfer to SmartVault directly + -- ERC20 transfer to v6 Depositor select 'bnb' as blockchain, + 'erc20-v6' as source, date_trunc('day', erc.evt_block_time) as block_date, erc.evt_block_time as block_time, erc.evt_block_number as call_block_number, erc.evt_tx_hash as call_tx_hash, erc.to as user_address, - erc.contract_address as token_address, + (case when erc.contract_address = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH + else erc.contract_address end) as token_address, erc.value as fee_raw from bnb.transactions t join erc20_bnb.evt_Transfer erc on t.hash = erc.evt_tx_hash + and t.block_number = erc.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3, ParaSwapRepayAdapter -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- If following transfers have outgoing only, exclude this revenue. + left join erc20_bnb.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + and t.block_number = erc2.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3 -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc2.evt_index > erc.evt_index + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_bnb.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and t.block_number = erc3.evt_block_number + and erc3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- i don't understand this conditional. Don't count swaps? But then should omit txs that have ANY outgoing transfer of WETH / ETH, no? + where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) + and t.success + and block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + -- v6: ETH Transfer to SmartVault directly + select 'bnb' as blockchain, + 'eth-v6' as source, + date_trunc('day', t.block_time) as block_date, + t.block_time as block_time, + t.block_number as call_block_number, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, + t.value as fee_raw + from bnb.transactions tr + join bnb.traces t on + t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t.tx_hash + and tr.block_number = t.block_number + -- and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + -- If following transfers have outgoing only, exclude this revenue. + left join bnb.traces t2 on + t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t2.tx_hash -- Other income for Depositor v6 + and tr.block_number = t2.block_number + -- and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t2.trace_address > t.trace_address + and t2.type = 'call' + and t2.call_type = 'call' + and t2.value > cast(0 as uint256) + left join bnb.traces t3 on + t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t3.tx_hash -- Outgoing + and tr.block_number = t3.block_number + and t3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t3.trace_address > t.trace_address + and t3.type = 'call' + and t3.call_type = 'call' + and t3.value > cast(0 as uint256) + where (t2.tx_hash is not null or t3.tx_hash is null) + and tr.success + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' + + -- + union all + + -- v5 fee claimer.registerFee + select 'bnb' as blockchain, + 'registerFee-v5' as source, + date_trunc('day', call_block_time) as block_date, + call_block_time as block_time, + call_block_number, + call_tx_hash, + _account as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c else _token end) as token_address, + _fee as fee_raw + from paraswap_bnb.FeeClaimer_call_registerFee + where call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + + -- Transfer to SmartVault directly + -- v5: ERC20 Transfer to SmartVault directly + select 'bnb' as blockchain, + 'erc20-v5' as source, + date_trunc('day', erc.evt_block_time) as block_date, + erc.evt_block_time as block_time, + erc.evt_block_number as call_block_number, + erc.evt_tx_hash as call_tx_hash, + erc.to as user_address, + erc.contract_address as token_address, + erc.value as fee_raw + from bnb.transactions t + join erc20_bnb.evt_Transfer erc on t.hash = erc.evt_tx_hash and t.block_number = erc.evt_block_number and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' -- If following transfers have outgoing only, exclude this revenue. - left join erc20_bnb.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + left join erc20_bnb.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash and t.block_number = erc2.evt_block_number and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc2.evt_index > erc.evt_index - left join erc20_bnb.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_bnb.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash and t.block_number = erc3.evt_block_number and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_bnb.FeeClaimer_call_registerFee + select 1 from paraswap_bnb.FeeClaimer_call_registerFee where call_tx_hash = erc.evt_tx_hash and call_block_number = erc.evt_block_number - ) + ) union all - - select 'bnb' as blockchain, + -- v5: ETH Transfer to SmartVault directly + select 'bnb' as blockchain, + 'eth-v5' AS source, date_trunc('day', t.block_time) as block_date, t.block_time as block_time, t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c as token_address, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c as token_address, t.value as fee_raw - from bnb.transactions tr - join bnb.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + from bnb.transactions tr + join bnb.traces t on tr.hash = t.tx_hash + and t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t.block_number + and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault -- If following transfers have outgoing only, exclude this revenue. left join bnb.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number + and t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t2.block_number and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t2.trace_address > t.trace_address and t2.type = 'call' and t2.call_type = 'call' and t2.value > cast(0 as uint256) - left join bnb.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number + left join bnb.traces t3 on tr.hash = t3.tx_hash -- Outgoing + and t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t3.block_number and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t3.trace_address > t.trace_address and t3.type = 'call' and t3.call_type = 'call' - and t3.value > cast(0 as uint256) + and t3.value > cast(0 as uint256) where (t2.tx_hash is not null or t3.tx_hash is null) and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_bnb.FeeClaimer_call_registerFee + select 1 from paraswap_bnb.FeeClaimer_call_registerFee where call_tx_hash = t.tx_hash and call_block_number = t.block_number ) + + union all + -- delta v2 protocol's revenue + select 'ethereum' as blockchain, + 'delta-v2' as source, + date_trunc('day', evt_block_time) as block_date, + evt_block_time as block_time, + evt_block_number as call_block_number, + evt_tx_hash as call_tx_hash, + 0x0000000000bbF5c5Fd284e657F01Bd000933C96D as user_address, + (case when fee_token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 else fee_token end) as token_address, + protocolFee as fee_raw + from deltav2_fees_balances_raw_ethereum + where evt_block_time >= TIMESTAMP '2024-07-08 12:00' + union all + -- delta v2 partners revenue + select 'ethereum' as blockchain, + 'delta-v2' as source, + date_trunc('day', evt_block_time) as block_date, + evt_block_time as block_time, + evt_block_number as call_block_number, + evt_tx_hash as call_tx_hash, + -- TODO: not easy to extract partner address here, may need to find a way + 0x0000000000000000000000000000000000000000 as user_address, + (case when fee_token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 else fee_token end) as token_address, + partnerFee as fee_raw + from deltav2_fees_balances_raw_ethereum + where evt_block_time >= TIMESTAMP '2024-07-08 12:00' union all + -- + -- all registerFee calls on v6 Fee Claimer select 'ethereum' as blockchain, + 'registerFee-v6' as source, date_trunc('day', call_block_time) as block_date, call_block_time as block_time, call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 else _token end) as token_address, - _fee as fee_raw - from paraswap_ethereum.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - + call_tx_hash, + address as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 else _token end) as token_address, + fee as fee_raw + FROM + exploded_data_ethereum + where call_block_time >= TIMESTAMP '2024-07-08 12:00' + union all - - -- Transfer to SmartVault directly + -- ERC20 transfer to v6 Depositor select 'ethereum' as blockchain, + 'erc20-v6' as source, date_trunc('day', erc.evt_block_time) as block_date, erc.evt_block_time as block_time, erc.evt_block_number as call_block_number, erc.evt_tx_hash as call_tx_hash, erc.to as user_address, - erc.contract_address as token_address, + (case when erc.contract_address = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH + else erc.contract_address end) as token_address, erc.value as fee_raw from ethereum.transactions t join erc20_ethereum.evt_Transfer erc on t.hash = erc.evt_tx_hash + and t.block_number = erc.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3, ParaSwapRepayAdapter -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- If following transfers have outgoing only, exclude this revenue. + left join erc20_ethereum.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + and t.block_number = erc2.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3 -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc2.evt_index > erc.evt_index + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_ethereum.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and t.block_number = erc3.evt_block_number + and erc3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- i don't understand this conditional. Don't count swaps? But then should omit txs that have ANY outgoing transfer of WETH / ETH, no? + where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) + and t.success + and block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + -- v6: ETH Transfer to SmartVault directly + select 'ethereum' as blockchain, + 'eth-v6' as source, + date_trunc('day', t.block_time) as block_date, + t.block_time as block_time, + t.block_number as call_block_number, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, + t.value as fee_raw + from ethereum.transactions tr + join ethereum.traces t on + t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t.tx_hash + and tr.block_number = t.block_number + -- and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + -- If following transfers have outgoing only, exclude this revenue. + left join ethereum.traces t2 on + t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t2.tx_hash -- Other income for Depositor v6 + and tr.block_number = t2.block_number + -- and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t2.trace_address > t.trace_address + and t2.type = 'call' + and t2.call_type = 'call' + and t2.value > cast(0 as uint256) + left join ethereum.traces t3 on + t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t3.tx_hash -- Outgoing + and tr.block_number = t3.block_number + and t3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t3.trace_address > t.trace_address + and t3.type = 'call' + and t3.call_type = 'call' + and t3.value > cast(0 as uint256) + where (t2.tx_hash is not null or t3.tx_hash is null) + and tr.success + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' + + -- + union all + + -- v5 fee claimer.registerFee + select 'ethereum' as blockchain, + 'registerFee-v5' as source, + date_trunc('day', call_block_time) as block_date, + call_block_time as block_time, + call_block_number, + call_tx_hash, + _account as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 else _token end) as token_address, + _fee as fee_raw + from paraswap_ethereum.FeeClaimer_call_registerFee + where call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + + -- Transfer to SmartVault directly + -- v5: ERC20 Transfer to SmartVault directly + select 'ethereum' as blockchain, + 'erc20-v5' as source, + date_trunc('day', erc.evt_block_time) as block_date, + erc.evt_block_time as block_time, + erc.evt_block_number as call_block_number, + erc.evt_tx_hash as call_tx_hash, + erc.to as user_address, + erc.contract_address as token_address, + erc.value as fee_raw + from ethereum.transactions t + join erc20_ethereum.evt_Transfer erc on t.hash = erc.evt_tx_hash and t.block_number = erc.evt_block_number and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' -- If following transfers have outgoing only, exclude this revenue. - left join erc20_ethereum.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + left join erc20_ethereum.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash and t.block_number = erc2.evt_block_number and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc2.evt_index > erc.evt_index - left join erc20_ethereum.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_ethereum.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash and t.block_number = erc3.evt_block_number and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_ethereum.FeeClaimer_call_registerFee + select 1 from paraswap_ethereum.FeeClaimer_call_registerFee where call_tx_hash = erc.evt_tx_hash and call_block_number = erc.evt_block_number - ) + ) union all - - select 'ethereum' as blockchain, + -- v5: ETH Transfer to SmartVault directly + select 'ethereum' as blockchain, + 'eth-v5' AS source, date_trunc('day', t.block_time) as block_date, t.block_time as block_time, t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 as token_address, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 as token_address, t.value as fee_raw - from ethereum.transactions tr - join ethereum.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + from ethereum.transactions tr + join ethereum.traces t on tr.hash = t.tx_hash + and t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t.block_number + and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault -- If following transfers have outgoing only, exclude this revenue. left join ethereum.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number + and t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t2.block_number and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t2.trace_address > t.trace_address and t2.type = 'call' and t2.call_type = 'call' and t2.value > cast(0 as uint256) - left join ethereum.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number + left join ethereum.traces t3 on tr.hash = t3.tx_hash -- Outgoing + and t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t3.block_number and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t3.trace_address > t.trace_address and t3.type = 'call' and t3.call_type = 'call' - and t3.value > cast(0 as uint256) + and t3.value > cast(0 as uint256) where (t2.tx_hash is not null or t3.tx_hash is null) and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_ethereum.FeeClaimer_call_registerFee + select 1 from paraswap_ethereum.FeeClaimer_call_registerFee where call_tx_hash = t.tx_hash and call_block_number = t.block_number ) + + union all - union all - + -- + -- all registerFee calls on v6 Fee Claimer select 'fantom' as blockchain, + 'registerFee-v6' as source, date_trunc('day', call_block_time) as block_date, call_block_time as block_time, call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83 else _token end) as token_address, - _fee as fee_raw - from paraswap_fantom.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - + call_tx_hash, + address as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 else _token end) as token_address, + fee as fee_raw + FROM + exploded_data_fantom + where call_block_time >= TIMESTAMP '2024-07-08 12:00' + union all - - -- Transfer to SmartVault directly + -- ERC20 transfer to v6 Depositor select 'fantom' as blockchain, + 'erc20-v6' as source, date_trunc('day', erc.evt_block_time) as block_date, erc.evt_block_time as block_time, erc.evt_block_number as call_block_number, erc.evt_tx_hash as call_tx_hash, erc.to as user_address, - erc.contract_address as token_address, + (case when erc.contract_address = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH + else erc.contract_address end) as token_address, erc.value as fee_raw from fantom.transactions t join erc20_fantom.evt_Transfer erc on t.hash = erc.evt_tx_hash + and t.block_number = erc.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3, ParaSwapRepayAdapter -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- If following transfers have outgoing only, exclude this revenue. + left join erc20_fantom.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + and t.block_number = erc2.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3 -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc2.evt_index > erc.evt_index + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_fantom.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and t.block_number = erc3.evt_block_number + and erc3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- i don't understand this conditional. Don't count swaps? But then should omit txs that have ANY outgoing transfer of WETH / ETH, no? + where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) + and t.success + and block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + -- v6: ETH Transfer to SmartVault directly + select 'fantom' as blockchain, + 'eth-v6' as source, + date_trunc('day', t.block_time) as block_date, + t.block_time as block_time, + t.block_number as call_block_number, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, + t.value as fee_raw + from fantom.transactions tr + join fantom.traces t on + t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t.tx_hash + and tr.block_number = t.block_number + -- and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + -- If following transfers have outgoing only, exclude this revenue. + left join fantom.traces t2 on + t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t2.tx_hash -- Other income for Depositor v6 + and tr.block_number = t2.block_number + -- and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t2.trace_address > t.trace_address + and t2.type = 'call' + and t2.call_type = 'call' + and t2.value > cast(0 as uint256) + left join fantom.traces t3 on + t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t3.tx_hash -- Outgoing + and tr.block_number = t3.block_number + and t3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t3.trace_address > t.trace_address + and t3.type = 'call' + and t3.call_type = 'call' + and t3.value > cast(0 as uint256) + where (t2.tx_hash is not null or t3.tx_hash is null) + and tr.success + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' + + -- + union all + + -- v5 fee claimer.registerFee + select 'fantom' as blockchain, + 'registerFee-v5' as source, + date_trunc('day', call_block_time) as block_date, + call_block_time as block_time, + call_block_number, + call_tx_hash, + _account as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83 else _token end) as token_address, + _fee as fee_raw + from paraswap_fantom.FeeClaimer_call_registerFee + where call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + + -- Transfer to SmartVault directly + -- v5: ERC20 Transfer to SmartVault directly + select 'fantom' as blockchain, + 'erc20-v5' as source, + date_trunc('day', erc.evt_block_time) as block_date, + erc.evt_block_time as block_time, + erc.evt_block_number as call_block_number, + erc.evt_tx_hash as call_tx_hash, + erc.to as user_address, + erc.contract_address as token_address, + erc.value as fee_raw + from fantom.transactions t + join erc20_fantom.evt_Transfer erc on t.hash = erc.evt_tx_hash and t.block_number = erc.evt_block_number and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' -- If following transfers have outgoing only, exclude this revenue. - left join erc20_fantom.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + left join erc20_fantom.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash and t.block_number = erc2.evt_block_number and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc2.evt_index > erc.evt_index - left join erc20_fantom.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_fantom.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash and t.block_number = erc3.evt_block_number and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_fantom.FeeClaimer_call_registerFee + select 1 from paraswap_fantom.FeeClaimer_call_registerFee where call_tx_hash = erc.evt_tx_hash and call_block_number = erc.evt_block_number - ) + ) union all - - select 'fantom' as blockchain, + -- v5: ETH Transfer to SmartVault directly + select 'fantom' as blockchain, + 'eth-v5' AS source, date_trunc('day', t.block_time) as block_date, t.block_time as block_time, t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83 as token_address, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x21be370d5312f44cb42ce377bc9b8a0cef1a4c83 as token_address, t.value as fee_raw - from fantom.transactions tr - join fantom.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + from fantom.transactions tr + join fantom.traces t on tr.hash = t.tx_hash + and t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t.block_number + and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault -- If following transfers have outgoing only, exclude this revenue. left join fantom.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number + and t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t2.block_number and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t2.trace_address > t.trace_address and t2.type = 'call' and t2.call_type = 'call' and t2.value > cast(0 as uint256) - left join fantom.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number + left join fantom.traces t3 on tr.hash = t3.tx_hash -- Outgoing + and t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t3.block_number and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t3.trace_address > t.trace_address and t3.type = 'call' and t3.call_type = 'call' - and t3.value > cast(0 as uint256) + and t3.value > cast(0 as uint256) where (t2.tx_hash is not null or t3.tx_hash is null) and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_fantom.FeeClaimer_call_registerFee + select 1 from paraswap_fantom.FeeClaimer_call_registerFee where call_tx_hash = t.tx_hash and call_block_number = t.block_number ) + + union all - union all - + -- + -- all registerFee calls on v6 Fee Claimer select 'optimism' as blockchain, + 'registerFee-v6' as source, date_trunc('day', call_block_time) as block_date, call_block_time as block_time, call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x4200000000000000000000000000000000000006 else _token end) as token_address, - _fee as fee_raw - from paraswap_optimism.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - + call_tx_hash, + address as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 else _token end) as token_address, + fee as fee_raw + FROM + exploded_data_optimism + where call_block_time >= TIMESTAMP '2024-07-08 12:00' + union all - - -- Transfer to SmartVault directly + -- ERC20 transfer to v6 Depositor select 'optimism' as blockchain, + 'erc20-v6' as source, date_trunc('day', erc.evt_block_time) as block_date, erc.evt_block_time as block_time, erc.evt_block_number as call_block_number, erc.evt_tx_hash as call_tx_hash, erc.to as user_address, - erc.contract_address as token_address, + (case when erc.contract_address = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH + else erc.contract_address end) as token_address, erc.value as fee_raw from optimism.transactions t join erc20_optimism.evt_Transfer erc on t.hash = erc.evt_tx_hash + and t.block_number = erc.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3, ParaSwapRepayAdapter -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- If following transfers have outgoing only, exclude this revenue. + left join erc20_optimism.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + and t.block_number = erc2.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3 -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc2.evt_index > erc.evt_index + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_optimism.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and t.block_number = erc3.evt_block_number + and erc3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- i don't understand this conditional. Don't count swaps? But then should omit txs that have ANY outgoing transfer of WETH / ETH, no? + where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) + and t.success + and block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + -- v6: ETH Transfer to SmartVault directly + select 'optimism' as blockchain, + 'eth-v6' as source, + date_trunc('day', t.block_time) as block_date, + t.block_time as block_time, + t.block_number as call_block_number, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, + t.value as fee_raw + from optimism.transactions tr + join optimism.traces t on + t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t.tx_hash + and tr.block_number = t.block_number + -- and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + -- If following transfers have outgoing only, exclude this revenue. + left join optimism.traces t2 on + t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t2.tx_hash -- Other income for Depositor v6 + and tr.block_number = t2.block_number + -- and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t2.trace_address > t.trace_address + and t2.type = 'call' + and t2.call_type = 'call' + and t2.value > cast(0 as uint256) + left join optimism.traces t3 on + t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t3.tx_hash -- Outgoing + and tr.block_number = t3.block_number + and t3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t3.trace_address > t.trace_address + and t3.type = 'call' + and t3.call_type = 'call' + and t3.value > cast(0 as uint256) + where (t2.tx_hash is not null or t3.tx_hash is null) + and tr.success + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' + + -- + union all + + -- v5 fee claimer.registerFee + select 'optimism' as blockchain, + 'registerFee-v5' as source, + date_trunc('day', call_block_time) as block_date, + call_block_time as block_time, + call_block_number, + call_tx_hash, + _account as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x4200000000000000000000000000000000000006 else _token end) as token_address, + _fee as fee_raw + from paraswap_optimism.FeeClaimer_call_registerFee + where call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + + -- Transfer to SmartVault directly + -- v5: ERC20 Transfer to SmartVault directly + select 'optimism' as blockchain, + 'erc20-v5' as source, + date_trunc('day', erc.evt_block_time) as block_date, + erc.evt_block_time as block_time, + erc.evt_block_number as call_block_number, + erc.evt_tx_hash as call_tx_hash, + erc.to as user_address, + erc.contract_address as token_address, + erc.value as fee_raw + from optimism.transactions t + join erc20_optimism.evt_Transfer erc on t.hash = erc.evt_tx_hash and t.block_number = erc.evt_block_number and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' -- If following transfers have outgoing only, exclude this revenue. - left join erc20_optimism.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + left join erc20_optimism.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash and t.block_number = erc2.evt_block_number and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc2.evt_index > erc.evt_index - left join erc20_optimism.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_optimism.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash and t.block_number = erc3.evt_block_number and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_optimism.FeeClaimer_call_registerFee + select 1 from paraswap_optimism.FeeClaimer_call_registerFee where call_tx_hash = erc.evt_tx_hash and call_block_number = erc.evt_block_number - ) + ) union all - - select 'optimism' as blockchain, + -- v5: ETH Transfer to SmartVault directly + select 'optimism' as blockchain, + 'eth-v5' AS source, date_trunc('day', t.block_time) as block_date, t.block_time as block_time, t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0x4200000000000000000000000000000000000006 as token_address, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x4200000000000000000000000000000000000006 as token_address, t.value as fee_raw - from optimism.transactions tr - join optimism.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + from optimism.transactions tr + join optimism.traces t on tr.hash = t.tx_hash + and t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t.block_number + and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault -- If following transfers have outgoing only, exclude this revenue. left join optimism.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number + and t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t2.block_number and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t2.trace_address > t.trace_address and t2.type = 'call' and t2.call_type = 'call' and t2.value > cast(0 as uint256) - left join optimism.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number + left join optimism.traces t3 on tr.hash = t3.tx_hash -- Outgoing + and t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t3.block_number and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t3.trace_address > t.trace_address and t3.type = 'call' and t3.call_type = 'call' - and t3.value > cast(0 as uint256) + and t3.value > cast(0 as uint256) where (t2.tx_hash is not null or t3.tx_hash is null) and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_optimism.FeeClaimer_call_registerFee + select 1 from paraswap_optimism.FeeClaimer_call_registerFee where call_tx_hash = t.tx_hash and call_block_number = t.block_number ) + + union all - union all - + -- + -- all registerFee calls on v6 Fee Claimer select 'polygon' as blockchain, + 'registerFee-v6' as source, date_trunc('day', call_block_time) as block_date, call_block_time as block_time, call_block_number, - call_tx_hash, - _account as user_address, - (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270 else _token end) as token_address, - _fee as fee_raw - from paraswap_polygon.FeeClaimer_call_registerFee - where call_success = true - and call_block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 - + call_tx_hash, + address as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 else _token end) as token_address, + fee as fee_raw + FROM + exploded_data_polygon + where call_block_time >= TIMESTAMP '2024-07-08 12:00' + union all - - -- Transfer to SmartVault directly + -- ERC20 transfer to v6 Depositor select 'polygon' as blockchain, + 'erc20-v6' as source, date_trunc('day', erc.evt_block_time) as block_date, erc.evt_block_time as block_time, erc.evt_block_number as call_block_number, erc.evt_tx_hash as call_tx_hash, erc.to as user_address, - erc.contract_address as token_address, + (case when erc.contract_address = 0xe50fa9b3c56ffb159cb0fca61f5c9d750e8128c8 then 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 -- aArbWETH + else erc.contract_address end) as token_address, erc.value as fee_raw from polygon.transactions t join erc20_polygon.evt_Transfer erc on t.hash = erc.evt_tx_hash + and t.block_number = erc.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3, ParaSwapRepayAdapter -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- If following transfers have outgoing only, exclude this revenue. + left join erc20_polygon.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + and t.block_number = erc2.evt_block_number + -- fees come from Augustus v6 but also from ParaSwapDebtSwapAdapterV3 -- no need to restrict then + -- and erc."from" = 0x6a000f20005980200259b80c5102003040001068 -- Augustus v6 + and erc2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc2.evt_index > erc.evt_index + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_polygon.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and t.block_number = erc3.evt_block_number + and erc3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + -- i don't understand this conditional. Don't count swaps? But then should omit txs that have ANY outgoing transfer of WETH / ETH, no? + where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) + and t.success + and block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + -- v6: ETH Transfer to SmartVault directly + select 'polygon' as blockchain, + 'eth-v6' as source, + date_trunc('day', t.block_time) as block_date, + t.block_time as block_time, + t.block_number as call_block_number, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x82af49447d8a07e3bd95bd0d56f35241523fbab1 as token_address, + t.value as fee_raw + from polygon.transactions tr + join polygon.traces t on + t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t.tx_hash + and tr.block_number = t.block_number + -- and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + -- If following transfers have outgoing only, exclude this revenue. + left join polygon.traces t2 on + t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t2.tx_hash -- Other income for Depositor v6 + and tr.block_number = t2.block_number + -- and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t2.to = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t2.trace_address > t.trace_address + and t2.type = 'call' + and t2.call_type = 'call' + and t2.value > cast(0 as uint256) + left join polygon.traces t3 on + t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.hash = t3.tx_hash -- Outgoing + and tr.block_number = t3.block_number + and t3."from" = 0x4d5401b9e9dcd7c9097e1df036c3afafc35d604f -- Depositor v6 + and t3.trace_address > t.trace_address + and t3.type = 'call' + and t3.call_type = 'call' + and t3.value > cast(0 as uint256) + where (t2.tx_hash is not null or t3.tx_hash is null) + and tr.success + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' + + -- + union all + + -- v5 fee claimer.registerFee + select 'polygon' as blockchain, + 'registerFee-v5' as source, + date_trunc('day', call_block_time) as block_date, + call_block_time as block_time, + call_block_number, + call_tx_hash, + _account as user_address, + (case when _token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee then 0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270 else _token end) as token_address, + _fee as fee_raw + from paraswap_polygon.FeeClaimer_call_registerFee + where call_success = true + and call_block_time >= TIMESTAMP '2024-07-08 12:00' + + union all + + -- Transfer to SmartVault directly + -- v5: ERC20 Transfer to SmartVault directly + select 'polygon' as blockchain, + 'erc20-v5' as source, + date_trunc('day', erc.evt_block_time) as block_date, + erc.evt_block_time as block_time, + erc.evt_block_number as call_block_number, + erc.evt_tx_hash as call_tx_hash, + erc.to as user_address, + erc.contract_address as token_address, + erc.value as fee_raw + from polygon.transactions t + join erc20_polygon.evt_Transfer erc on t.hash = erc.evt_tx_hash and t.block_number = erc.evt_block_number and erc."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + and erc.evt_block_time >= TIMESTAMP '2024-07-08 12:00' -- If following transfers have outgoing only, exclude this revenue. - left join erc20_polygon.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash + left join erc20_polygon.evt_Transfer erc2 on t.hash = erc2.evt_tx_hash and t.block_number = erc2.evt_block_number and erc2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and erc2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc2.evt_index > erc.evt_index - left join erc20_polygon.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash + and erc2.evt_block_time >= TIMESTAMP '2024-07-08 12:00' + left join erc20_polygon.evt_Transfer erc3 on t.hash = erc3.evt_tx_hash and t.block_number = erc3.evt_block_number and erc3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and erc3.evt_index > erc.evt_index + and erc3.evt_block_time >= TIMESTAMP '2024-07-08 12:00' where (erc2.evt_tx_hash is not null or erc3.evt_tx_hash is null) and t.success - and block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_polygon.FeeClaimer_call_registerFee + select 1 from paraswap_polygon.FeeClaimer_call_registerFee where call_tx_hash = erc.evt_tx_hash and call_block_number = erc.evt_block_number - ) + ) union all - - select 'polygon' as blockchain, + -- v5: ETH Transfer to SmartVault directly + select 'polygon' as blockchain, + 'eth-v5' AS source, date_trunc('day', t.block_time) as block_date, t.block_time as block_time, t.block_number as call_block_number, - t.tx_hash as call_tx_hash, - t.to as user_address, - 0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270 as token_address, + t.tx_hash as call_tx_hash, + t.to as user_address, + 0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270 as token_address, t.value as fee_raw - from polygon.transactions tr - join polygon.traces t on tr.hash = t.tx_hash - and tr.block_number = t.block_number - and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router - and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault + from polygon.transactions tr + join polygon.traces t on tr.hash = t.tx_hash + and t.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t.block_number + and t."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router + and t.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault -- If following transfers have outgoing only, exclude this revenue. left join polygon.traces t2 on tr.hash = t2.tx_hash -- Other income for SmartVault - and tr.block_number = t2.block_number + and t2.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t2.block_number and t2."from" = 0xdef171fe48cf0115b1d80b88dc8eab59176fee57 -- Router and t2.to = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t2.trace_address > t.trace_address and t2.type = 'call' and t2.call_type = 'call' and t2.value > cast(0 as uint256) - left join polygon.traces t3 on tr.hash = t3.tx_hash -- Outgoing - and tr.block_number = t3.block_number + left join polygon.traces t3 on tr.hash = t3.tx_hash -- Outgoing + and t3.block_time >= TIMESTAMP '2024-07-08 12:00' + and tr.block_number = t3.block_number and t3."from" = 0xd5b927956057075377263aab7f8afc12f85100db -- SmartVault and t3.trace_address > t.trace_address and t3.type = 'call' and t3.call_type = 'call' - and t3.value > cast(0 as uint256) + and t3.value > cast(0 as uint256) where (t2.tx_hash is not null or t3.tx_hash is null) and tr.success - and tr.block_time >= TIMESTAMP '2024-07-08 12:00' -- Start from Epoch 20 + and tr.block_time >= TIMESTAMP '2024-07-08 12:00' and not exists ( - select 1 from paraswap_polygon.FeeClaimer_call_registerFee + select 1 from paraswap_polygon.FeeClaimer_call_registerFee where call_tx_hash = t.tx_hash and call_block_number = t.block_number ) + + ), - + price_list as ( select date_trunc('day', minute) as block_date, blockchain, contract_address, avg(price) as price from prices.usd - where minute >= date('2021-04-01') + where minute >= TIMESTAMP '2024-07-08 12:00' group by 1, 2, 3 ) -select e.epoch_num as en, +select + f.source as source, + e.epoch_num as en, f.blockchain as bc, f.block_date as bd, f.block_time as bt,