From 96bc4b5d41cd3ae6cf78c33a097fe49e6f194b6c Mon Sep 17 00:00:00 2001 From: Benjamin Smith Date: Thu, 1 Dec 2022 21:01:30 +0100 Subject: [PATCH] Flip Dune Version Switch (#153) --- queries/dune_v1/period_totals.sql | 50 +++++++++++++++++++++++++++++++ queries/dune_v2/period_totals.sql | 46 ++++++++++++++++++++++++++++ src/fetch/dune.py | 2 +- src/models/accounting_period.py | 6 ++-- src/queries.py | 10 +++---- tests/unit/test_data_utils.py | 2 +- 6 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 queries/dune_v1/period_totals.sql create mode 100644 queries/dune_v2/period_totals.sql diff --git a/queries/dune_v1/period_totals.sql b/queries/dune_v1/period_totals.sql new file mode 100644 index 00000000..dbb3594c --- /dev/null +++ b/queries/dune_v1/period_totals.sql @@ -0,0 +1,50 @@ +with +-- V2 Query: https://dune.com/queries/1687870 +execution_costs as ( + SELECT + success, + sum(gas_used * gas_price) / pow(10, 18) as gas_cost_eth + FROM ethereum.transactions as tx + where block_time between '{{StartTime}}' and '{{EndTime}}' + AND position('\x13d79a0b' in data) > 0 --! settle method ID + AND "to" = '\x9008D19f58AAbD9eD0D60971565AA8510560ab41' + group by success +), +solver_rewards as ( + select + sum(value) / pow(10, 18) as cow_rewards + from cow_protocol."CowProtocolToken_evt_Transfer" + where "from" = '\xA03be496e67Ec29bC62F01a428683D7F9c204930' + and evt_block_time between '{{StartTime}}' and '{{EndTime}}' +), +batch_details as ( + select + count(*) as batches_settled, + sum(num_trades) as num_trades + from gnosis_protocol_v2.batches + where block_time >= '{{StartTime}}' and block_time < '{{EndTime}}' +), +realized_fees as ( + select + sum(atoms_bought) / 10^18 as realized_fees_eth + from gnosis_protocol_v2."trades" + where trader in ( + select address + from gnosis_protocol_v2."view_solvers" + where name = 'Withdraw' + ) + and block_time between '{{StartTime}}' and '{{EndTime}}' +) + +select + concat( + to_char('{{StartTime}}'::timestamptz, 'YYYY-MM-DD'), + ' to ', + to_char('{{EndTime}}'::timestamptz - interval '1 day', 'YYYY-MM-DD') + ) as accounting_period, + (select gas_cost_eth from execution_costs where success = true) as execution_cost_eth, + (select gas_cost_eth from execution_costs where success = false) as failure_cost_eth, + (select realized_fees_eth from realized_fees) as realized_fees_eth, + (select cow_rewards from solver_rewards) as cow_reward, + (select batches_settled from batch_details) as batches_settled, + (select num_trades from batch_details) as num_trades diff --git a/queries/dune_v2/period_totals.sql b/queries/dune_v2/period_totals.sql new file mode 100644 index 00000000..3572c0fa --- /dev/null +++ b/queries/dune_v2/period_totals.sql @@ -0,0 +1,46 @@ +with +-- V1 Query: https://dune.com/queries/1687752 +execution_costs as ( + SELECT + success, + sum((gas_used * gas_price)/ pow(10, 18)) as gas_cost_eth + FROM ethereum.transactions as tx + where block_time between '{{StartTime}}' and '{{EndTime}}' + AND position('0x13d79a0b' in data) > 0 --! settle method ID + AND to = lower('0x9008D19f58AAbD9eD0D60971565AA8510560ab41') + group by success +), +solver_rewards as ( + select + sum(value) / pow(10, 18) as cow_rewards + from cow_protocol_ethereum.CowProtocolToken_evt_Transfer + where from = lower('0xA03be496e67Ec29bC62F01a428683D7F9c204930') + and evt_block_time between '{{StartTime}}' and '{{EndTime}}' +), +batch_details as ( + select + count(*) as batches_settled, + sum(num_trades) as num_trades + from cow_protocol_ethereum.batches + where block_time >= '{{StartTime}}' and block_time < '{{EndTime}}' +), +realized_fees as ( + select + sum(atoms_bought) / pow(10, 18) as realized_fees_eth + from cow_protocol_ethereum.trades + where trader in ( + select address + from cow_protocol_ethereum.solvers + where name = 'Withdraw' + ) + and block_time between '{{StartTime}}' and '{{EndTime}}' +) + +select + concat('{{StartTime}}'::date, ' to ', '{{EndTime}}'::date - interval '1 day') as accounting_period, + (select gas_cost_eth from execution_costs where success = true) as execution_cost_eth, + (select gas_cost_eth from execution_costs where success = false) as failure_cost_eth, + (select realized_fees_eth from realized_fees) as realized_fees_eth, + (select cow_rewards from solver_rewards) as cow_reward, + (select batches_settled from batch_details) as batches_settled, + (select num_trades from batch_details) as num_trades diff --git a/src/fetch/dune.py b/src/fetch/dune.py index 67af9481..3687b2a6 100644 --- a/src/fetch/dune.py +++ b/src/fetch/dune.py @@ -37,7 +37,7 @@ def __init__( self, dune: DuneClient, period: AccountingPeriod, - dune_version: DuneVersion = DuneVersion.V1, + dune_version: DuneVersion = DuneVersion.V2, ): self.dune = dune self.period = period diff --git a/src/models/accounting_period.py b/src/models/accounting_period.py index ebf3a0d3..2fdd8481 100644 --- a/src/models/accounting_period.py +++ b/src/models/accounting_period.py @@ -36,13 +36,13 @@ def as_query_params(self) -> list[QueryParameter]: def dashboard_url(self) -> str: """Constructs Solver Accounting Dashboard URL for Period""" - base = "https://dune.com/gnosis.protocol/" - slug = "solver-rewards-accounting-v2" + base = "https://dune.com/bh2smith/" + slug = "cow-solver-rewards" query = f"?StartTime={self.start}&EndTime={self.end}" return base + urllib.parse.quote_plus(slug + query, safe="=&?") def unusual_slippage_url(self) -> str: """Returns a link to unusual slippage query for period""" - base = "https://dune.com/queries/645559" + base = "https://dune.com/queries/1688044" query = f"?StartTime={self.start}&EndTime={self.end}" return base + urllib.parse.quote_plus(query, safe="=&?") diff --git a/src/queries.py b/src/queries.py index 0a680a1e..a56ca22b 100644 --- a/src/queries.py +++ b/src/queries.py @@ -10,8 +10,6 @@ from dune_client.query import Query from dune_client.types import QueryParameter -from src.utils.query_file import dashboard_file - class DuneVersion(Enum): """Dune Version Identifier""" @@ -35,7 +33,7 @@ def __init__(self, name: str, v1_id: int, v2_id: int, filepath: str) -> None: self.v2_query = Query(v2_id, name) def with_params( - self, params: list[QueryParameter], dune_version: DuneVersion = DuneVersion.V1 + self, params: list[QueryParameter], dune_version: DuneVersion = DuneVersion.V2 ) -> Query: """ Copies the query and adds parameters to it, returning the copy. @@ -82,9 +80,9 @@ def with_params( ), "PERIOD_TOTALS": QueryData( name="Accounting Period Totals", - filepath=dashboard_file("period-totals.sql"), - v1_id=448457, - v2_id=-1, # Not implemented + filepath="period_totals.sql", + v1_id=1687752, + v2_id=1687870, ), "PERIOD_SLIPPAGE": QueryData( name="Solver Slippage for Period", diff --git a/tests/unit/test_data_utils.py b/tests/unit/test_data_utils.py index f6d2b936..c6866927 100644 --- a/tests/unit/test_data_utils.py +++ b/tests/unit/test_data_utils.py @@ -14,7 +14,7 @@ class DummyDataClass: class TestDataUtils(unittest.TestCase): def test_dashboard_url(self): expected = ( - "https://dune.com/gnosis.protocol/solver-rewards-accounting-v2?" + "https://dune.com/bh2smith/cow-solver-rewards?" "StartTime=2022-05-31+00%3A00%3A00&" "EndTime=2022-06-07+00%3A00%3A00" )