Skip to content

Commit

Permalink
Flip Dune Version Switch (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Dec 1, 2022
1 parent 7cfff4d commit 96bc4b5
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 11 deletions.
50 changes: 50 additions & 0 deletions queries/dune_v1/period_totals.sql
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions queries/dune_v2/period_totals.sql
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/fetch/dune.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/models/accounting_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="=&?")
10 changes: 4 additions & 6 deletions src/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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.
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down

0 comments on commit 96bc4b5

Please sign in to comment.