Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add raw order data wrapper query #80

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cowprotocol/accounting/raw_data/.sqlfluff
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[sqlfluff:templater:jinja:context]
start_time='2024-08-01 12:00'
end_time='2024-08-02 12:00'
blockchain='ethereum'
195 changes: 195 additions & 0 deletions cowprotocol/accounting/raw_data/order_data_4364122.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
-- This query provides data related to rewards/payouts on a per batch auction level
-- for all auctions that had at least one winner.
-- Parameters:
-- {{blockchain}}: the chain for which we want to retrieve batch data

-- The output has the following columns:
-- environment: varchar
-- auction_id: integer
-- block_number: integer
-- order_uid: varbinary
-- solver: varbinary
-- quote_solver: varbinary
-- tx_hash: varbinary
-- surplus_fee: decimal(38, 0)
-- protocol_fee: decimal(38, 0)
-- protocol_fee_token: varbinary
-- protocol_fee_native_price: decimal(38, 0)
-- quote_sell_amount: decimal(38, 0)
-- quote_buy_amount: decimal(38, 0)
-- quote_gas_cost: decimal(38, 0)
-- quote_sell_token_price: decimal(38, 0)
-- partner_fee: decimal(38, 0)
-- partner_fee_recipient: varbinary
-- protocol_fee_kind: varchar

with
past_data_ethereum as (
select
s.environment,
-1 as auction_id,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this encoded as -1 and not just Null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i see your point.... Will update it.

d.block_number,
from_hex(d.order_uid) as order_uid,
from_hex(d.solver) as solver,
from_hex(d.data.quote_solver) as quote_solver, --noqa: RF01
from_hex(d.tx_hash) as tx_hash,
cast(d.data.surplus_fee as decimal(38, 0)) as surplus_fee, --noqa: RF01
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there precedence for this number of decimals?
Our database uses decimal(78, 0) for most things, which can hold a (u)int256.

Copy link
Contributor Author

@harisang harisang Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I run

select cast(10 as decimal(39,0))

I get an error Error: DECIMAL precision must be in range [1, 38]: 39

So i decided to max it out, but didn't think this through much tbh

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dune seems to only support 38 digits of precision.
An alternative would be to use integers, which is compatible with data types used by the smart contract.

cast(d.data.protocol_fee as decimal(38, 0)) as protocol_fee, --noqa: RF01
from_hex(d.data.protocol_fee_token) as protocol_fee_token, --noqa: RF01
cast(d.data.protocol_fee_native_price as decimal(38, 0)) as protocol_fee_native_price, --noqa: RF01
case
when d.data.quote_sell_amount = 'None' then null --noqa: RF01
else cast(d.data.quote_sell_amount as decimal(38, 0)) --noqa: RF01
end as quote_sell_amount,
case
when d.data.quote_buy_amount = 'None' then null --noqa: RF01
else cast(d.data.quote_buy_amount as decimal(38, 0)) --noqa: RF01
end as quote_buy_amount,
case
when cast(d.data.quote_gas_cost as varchar) = 'NaN' then null --noqa: RF01
else cast(d.data.quote_gas_cost as decimal(38, 0)) --noqa: RF01
end as quote_gas_cost,
case
when cast(d.data.quote_sell_token_price as varchar) = 'NaN' then null --noqa: RF01
else cast(d.data.quote_sell_token_price as decimal(38, 0)) --noqa: RF01
end as quote_sell_token_price,
cast(d.data.partner_fee as decimal(38, 0)) as partner_fee, --noqa: RF01
from_hex(d.data.partner_fee_recipient) as partner_fee_recipient, --noqa: RF01
d.data.protocol_fee_kind --noqa: RF01
from cowswap.raw_order_rewards as d inner join cow_protocol_ethereum.solvers as s on from_hex(d.solver) = s.address where d.block_number < 20866925
),

past_data_gnosis as ( --noqa: ST03
-- data from Jan 23, 2024 till Sept 23, 2024, are present here
select --noqa: ST06
s.environment,
-1 as auction_id,
d.block_number,
d.order_uid,
d.solver,
d.quote_solver,
d.tx_hash,
cast(d.surplus_fee as decimal(38, 0)) as surplus_fee,
cast(d.protocol_fee as decimal(38, 0)) as protocol_fee,
d.protocol_fee_token,
cast(d.protocol_fee_native_price as decimal(38, 0)) as protocol_fee_native_price,
cast(d.quote_sell_amount as decimal(38, 0)) as quote_sell_amount,
cast(d.quote_buy_amount as decimal(38, 0)) as quote_buy_amount,
case
when cast(d.quote_gas_cost as varchar) = 'NaN' then null
else cast(d.quote_gas_cost as decimal(38, 0))
end as quote_gas_cost,
case
when cast(d.quote_sell_token_price as varchar) = 'NaN' then null
else cast(d.quote_sell_token_price as decimal(38, 0))
end as quote_sell_token_price,
cast(d.partner_fee as decimal(38, 0)) as partner_fee,
d.partner_fee_recipient,
d.protocol_fee_kind
from dune.cowprotocol.dataset_cowswap_gnosis_raw_order_rewards as d inner join cow_protocol_gnosis.solvers as s on cast(d.solver as varchar) = cast(s.address as varchar)
),

past_data_arbitrum as ( --noqa: ST03
-- data till Sept 23, 2024, are present here
select --noqa: ST06
s.environment,
-1 as auction_id,
d.block_number,
d.order_uid,
d.solver,
d.quote_solver,
d.tx_hash,
cast(d.surplus_fee as decimal(38, 0)) as surplus_fee,
cast(d.protocol_fee as decimal(38, 0)) as protocol_fee,
d.protocol_fee_token,
case
when d.protocol_fee_native_price = 'inf' then null
else cast(d.protocol_fee_native_price as decimal(38, 0))
end as protocol_fee_native_price,
cast(d.quote_sell_amount as decimal(38, 0)) as quote_sell_amount,
cast(d.quote_buy_amount as decimal(38, 0)) as quote_buy_amount,
case
when cast(d.quote_gas_cost as varchar) = 'NaN' then null
else cast(d.quote_gas_cost as decimal(38, 0))
end as quote_gas_cost,
case
when cast(d.quote_sell_token_price as varchar) = 'NaN' then null
else cast(d.quote_sell_token_price as decimal(38, 0))
end as quote_sell_token_price,
cast(d.partner_fee as decimal(38, 0)) as partner_fee,
d.partner_fee_recipient,
d.protocol_fee_kind
from dune.cowprotocol.dataset_cowswap_arbitrum_raw_order_rewards as d inner join cow_protocol_arbitrum.solvers as s on cast(d.solver as varchar) = cast(s.address as varchar)
)

select *
from past_data_{{blockchain}}
union all
select
environment,
auction_id,
block_number,
order_uid,
solver,
quote_solver,
tx_hash,
cast(surplus_fee as decimal(38, 0)) as surplus_fee,
cast(protocol_fee as decimal(38, 0)) as protocol_fee,
protocol_fee_token,
cast(protocol_fee_native_price as decimal(38, 0)) as protocol_fee_native_price,
cast(quote_sell_amount as decimal(38, 0)) as quote_sell_amount,
cast(quote_buy_amount as decimal(38, 0)) as quote_buy_amount,
cast(quote_gas_cost as decimal(38, 0)) as quote_gas_cost,
case
when quote_sell_token_price = 'inf' then null
else cast(quote_sell_token_price as decimal(38, 0))
end as quote_sell_token_price,
cast(partner_fee as decimal(38, 0)) as partner_fee,
partner_fee_recipient,
protocol_fee_kind
from dune.cowprotocol.dataset_order_data_{{blockchain}}_2024_10
union all
select
environment,
auction_id,
block_number,
order_uid,
solver,
quote_solver,
tx_hash,
cast(surplus_fee as decimal(38, 0)) as surplus_fee,
cast(protocol_fee as decimal(38, 0)) as protocol_fee,
protocol_fee_token,
cast(protocol_fee_native_price as decimal(38, 0)) as protocol_fee_native_price,
cast(quote_sell_amount as decimal(38, 0)) as quote_sell_amount,
cast(quote_buy_amount as decimal(38, 0)) as quote_buy_amount,
cast(quote_gas_cost as decimal(38, 0)) as quote_gas_cost,
case
when quote_sell_token_price = 'inf' then null
else cast(quote_sell_token_price as decimal(38, 0))
end as quote_sell_token_price,
cast(partner_fee as decimal(38, 0)) as partner_fee,
partner_fee_recipient,
protocol_fee_kind
from dune.cowprotocol.dataset_order_data_{{blockchain}}_2024_11
union all
select
environment,
auction_id,
block_number,
order_uid,
solver,
quote_solver,
tx_hash,
cast(surplus_fee as decimal(38, 0)) as surplus_fee,
cast(protocol_fee as decimal(38, 0)) as protocol_fee,
protocol_fee_token,
cast(protocol_fee_native_price as decimal(38, 0)) as protocol_fee_native_price,
cast(quote_sell_amount as decimal(38, 0)) as quote_sell_amount,
cast(quote_buy_amount as decimal(38, 0)) as quote_buy_amount,
cast(quote_gas_cost as decimal(38, 0)) as quote_gas_cost,
cast(quote_sell_token_price as decimal(38, 0)) as quote_sell_token_price,
cast(partner_fee as decimal(38, 0)) as partner_fee,
partner_fee_recipient,
protocol_fee_kind
from dune.cowprotocol.dataset_order_data_{{blockchain}}_2024_12
Loading