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 partner fees verification query #29

Merged
merged 9 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions cowprotocol/accounting/rewards/mainnet/.sqlfluff
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[sqlfluff:templater:jinja:context]
result=per_settlement,aggregate_per_recipient
cte_name = results,results_per_tx
start_time='2024-08-20 00:00:00'
end_time='2024-08-27 00:00:00'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
-- This query computes the partner fees associated with each partner in a given time interval
--
-- Parameters:
-- {{start_time}} - the timestamp for which the accounting should start (inclusively)
-- {{end_time}} - the timestamp for which the accounting should end (exclusively)
-- {{result}} - two views of the result, one aggregated and one on a per tx basis

with
per_settlement_prelim as (
Copy link
Contributor

Choose a reason for hiding this comment

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

this is probably not per settlement but per trade

Suggested change
per_settlement_prelim as (
per_trade as (

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I did rename some of the tables. Let me know what you think.

select
t.block_time,
t.block_number,
t.order_uid,
t.tx_hash,
r.data.partner_fee_recipient as partner_recipient, -- noqa: RF01
t.app_data,
usd_value,
cast(cast(r.data.protocol_fee as varchar) as int256) as protocol_fee, -- noqa: RF01
r.data.protocol_fee_token, -- noqa: RF01
json_extract(a.encode, '$.metadata.partnerFee.bps') as patnerFeeBps,
json_extract(a.encode, '$.metadata.widget.appCode') as app_code,
cast(
usd_value * cast(
json_extract(a.encode, '$.metadata.partnerFee.bps') as double
) as double
) / 10000 as est_partner_revenue,
harisang marked this conversation as resolved.
Show resolved Hide resolved
cast(
usd_value * cast(
json_extract(a.encode, '$.metadata.partnerFee.bps') as double
) as double
) / 10000 * 0.15 as est_cow_revenue,
harisang marked this conversation as resolved.
Show resolved Hide resolved
cast(
cast(
coalesce(r.data.partner_fee, r.data.protocol_fee) as varchar -- noqa: RF01
) as int256
) * r.data.protocol_fee_native_price / pow(10, 18) as raw_integrator_fee_in_eth -- noqa: RF01
from
cow_protocol_ethereum.trades as t
left join dune.cowprotocol.dataset_app_data_mainnet as a on t.app_data = a.contract_app_data
left join cowswap.raw_order_rewards as r
on
r.order_uid = cast(t.order_uid as varchar)
and t.tx_hash = from_hex(r.tx_hash)
where
json_extract(a.encode, '$.metadata.partnerFee.recipient') is not null
and t.block_number >= (select start_block from "query_3333356(start_time='{{start_time}}',end_time='{{end_time}}')")
and t.block_number < (select end_block from "query_3333356(start_time='{{start_time}}',end_time='{{end_time}}')")
harisang marked this conversation as resolved.
Show resolved Hide resolved
order by
t.block_time desc
),

per_settlement as (
select *
from
per_settlement_prelim
where
raw_integrator_fee_in_eth > 0
),
Copy link
Contributor

Choose a reason for hiding this comment

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

To get a per settlement value you would probably need

Suggested change
per_settlement as (
select *
from
per_settlement_prelim
where
raw_integrator_fee_in_eth > 0
),
per_settlement as (
select
block_time,
tx_hash,
partner_recipient,
sum(est_partner_revenue) as est_partner_revenue,
sum(est_cow_revenue) as est_cow_revenue,
sum(raw_integrator_fee_in_eth) as raw_integrator_fee_in_eth
from
per_trade
where
raw_integrator_fee_in_eth > 0
),


aggregate_per_recipient as (
fhenneke marked this conversation as resolved.
Show resolved Hide resolved
select
partner_recipient,
case
when partner_recipient = '0x63695Eee2c3141BDE314C5a6f89B98E62808d716' then sum(0.9 * raw_integrator_fee_in_eth) -- this is for the Safe integration
harisang marked this conversation as resolved.
Show resolved Hide resolved
else sum(0.85 * raw_integrator_fee_in_eth)
end as partner_fee_part,
case
when partner_recipient = '0x63695Eee2c3141BDE314C5a6f89B98E62808d716' then sum(0.1 * raw_integrator_fee_in_eth) -- this is for the Safe integratio
else sum(0.15 * raw_integrator_fee_in_eth)
end as cow_dao_partner_fee_part
from
per_settlement
group by
partner_recipient
)

select * from {{result}}
Loading