Skip to content

Commit

Permalink
address some review comments
Browse files Browse the repository at this point in the history
- rename transfer_type to type
- rename fee_data to raw_fee_data
- reorder columns for consistency
- fix some comments
- rename price to price_unit
- rename slippage_native_atom to slippage_wei
  • Loading branch information
fhenneke committed Sep 19, 2024
1 parent cf754bf commit 76ec325
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
14 changes: 7 additions & 7 deletions cowprotocol/accounting/fees/classified_fees_4058574.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
-- - token_address: address of token with a balance change. contract address for erc20 tokens,
-- 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee for native token
-- - amount: value of fee in atoms of the token
-- - transfer_type: 'protocol_fee' for the total protocol fee (including partner fee), 'network_fee' for network fees
-- - type: 'protocol_fee' for the total protocol fee (including partner fee), 'network_fee' for network fees

with fee_data as (
with raw_fee_data as (
select
block_time,
t.tx_hash,
Expand All @@ -38,21 +38,21 @@ protocol_fee_balance_changes as (
tx_hash,
protocol_fee_token_address as token_address,
protocol_fee as amount,
'protocol_fee' as transfer_type
from fee_data
'protocol_fee' as type
from raw_fee_data
),

network_fee_balance_changes as (
select
block_time,
tx_hash,
sell_token_address as token_address,
'network_fee' as transfer_type,
case
when sell_token_address = protocol_fee_token_address then surplus_fee - protocol_fee
else surplus_fee - cast(1.0 * protocol_fee * (atoms_sold - surplus_fee) / atoms_bought as int256)
end as amount
from fee_data
end as amount,
'network_fee' as type
from raw_fee_data
)

select * from protocol_fee_balance_changes
Expand Down
18 changes: 9 additions & 9 deletions cowprotocol/accounting/slippage/slippage_breakdown_4059683.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- This query a breakdown of slippage on CoW Protocol
-- This query gives a breakdown of slippage on CoW Protocol
--
-- Parameters:
-- {{start_time}} - the timestamp for which the analysis should start (inclusively)
Expand All @@ -12,20 +12,20 @@
-- 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee for native token
-- - amount: signed value of slippage in atoms of the token; fees are represented as negative
-- integers since they will be removed from imbalances
-- - transfer_type: 'raw_imbalance' for imbalance observable on chain, 'protocol_fee' for the total
-- - type: 'raw_imbalance' for imbalance observable on chain, 'protocol_fee' for the total
-- protocol fee (including partner fee), 'network_fee' for network fees
-- - price: USD price of one unit (i.e. pow(10, decimals) atoms) of a token
-- - price_unit: USD price of one unit (i.e. pow(10, decimals) atoms) of a token
-- - price_atom: USD price of one atom (i.e. 1. / pow(10, decimals) units) of a token
-- - slippage_usd: USD value of slippage
-- - slippage_native_atom: value of slippage in atoms of native token
-- - slippage_wei: value of slippage in atoms of native token

with raw_token_imbalances as (
select
block_time,
tx_hash,
token_address,
amount,
'raw_imbalance' as transfer_type,
'raw_imbalance' as type,
date_trunc('hour', block_time) as hour --noqa: RF04
from "query_4021644(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')"
),
Expand All @@ -35,8 +35,8 @@ fee_balance_changes as (
block_time,
tx_hash,
token_address,
transfer_type,
-amount as amount,
type,
date_trunc('hour', block_time) as hour --noqa: RF04
from "query_4058574(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')"
),
Expand All @@ -56,10 +56,10 @@ select
tx_hash,
rs.token_address,
amount as slippage_atoms,
transfer_type,
p.price,
type,
p.price_unit,
p.price_atom,
cast(amount * p.price_atom / np.price_atom as int256) as slippage_native_atom,
cast(amount * p.price_atom / np.price_atom as int256) as slippage_wei,
amount * p.price_atom as slippage_usd
from
raw_slippage as rs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
-- This query return slippage per transaction
-- This query returns slippage per solver over a period of time,
-- evaluated in both usd and the native token of the chain.
--
-- Parameters:
-- {{start_time}} - the timestamp for which the analysis should start (inclusively)
-- {{end_time}} - the timestamp for which the analysis should end (exclusively)
-- {{blockchain}} - network to run the analysis on
--
-- The columns of the result are
-- - block_time: time of settlement transaction
-- - tx_hash: settlement transaction hash
-- - solver_address: address of the solver executing the settlement
-- - slippage_usd: USD value of slippage
-- - slippage_native_atom: value of slippage in atoms of native token
-- - slippage_wei: value of slippage in atoms of native token

select
solver_address,
sum(slippage_usd) as slippage_usd,
sum(slippage_native_atom) as slippage_native_atom
sum(slippage_wei) as slippage_wei
from "query_4070059(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')"
group by solver_address
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
-- - tx_hash: settlement transaction hash
-- - solver_address: address of the solver executing the settlement
-- - slippage_usd: USD value of slippage
-- - slippage_native: value of slippage in native token
-- - slippage_wei: value of slippage in native token
--
-- Results of the query are filtered to not include batches from excluded_batches.
-- Batches are also excluded if there is a non-zero imbalance and no value (in native atomes).
-- Batches are also excluded if there is a non-zero imbalance and no value (in native atoms).

with excluded_batches as (
select tx_hash from query_3490353
Expand All @@ -24,10 +24,10 @@ select
s.tx_hash,
solver_address,
sum(slippage_usd) as slippage_usd,
sum(slippage_native_atom) as slippage_native_atom
sum(slippage_native_atom) as slippage_wei
from "query_4059683(blockchain='{{blockchain}}',start_time='{{start_time}}',end_time='{{end_time}}')" as s
inner join cow_protocol_{{blockchain}}.batches as b
on s.tx_hash = b.tx_hash
where s.tx_hash not in (select tx_hash from excluded_batches)
group by 1, 2, 3
having bool_and(slippage_native_atom is not null or slippage_atoms = 0)
having bool_and(slippage_wei is not null or slippage_atoms = 0)
18 changes: 9 additions & 9 deletions cowprotocol/accounting/slippage/slippage_prices_4064601.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-- - token_address: address of token with a price. contract address for erc20 tokens,
-- 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee for native token
-- - decimals: decimals of the token, can be null
-- - price: USD price of one unit (i.e. pow(10, decimals) atoms) of a token
-- - price_usd: USD price of one unit (i.e. pow(10, decimals) atoms) of a token
-- - price_atom: USD price of one atom (i.e. 1. / pow(10, decimals) units) of a token

with token_times as (
Expand All @@ -29,7 +29,7 @@ precise_prices as (
token_address,
decimals,
date_trunc('hour', minute) as hour, --noqa: RF04
avg(price) as price,
avg(price) as price_usd,
avg(price) / pow(10, decimals) as price_atom
from
prices.usd
Expand All @@ -46,14 +46,14 @@ intrinsic_prices as (
token_address,
decimals,
hour,
avg(price) as price,
avg(price_usd) as price_usd,
avg(price_atom) as price_atom
from (
select
buy_token_address as token_address,
round(log(10, atoms_bought / units_bought)) as decimals,
date_trunc('hour', block_time) as hour, --noqa: RF04
usd_value / units_bought as price,
usd_value / units_bought as price_usd,
usd_value / atoms_bought as price_atom
from cow_protocol_{{blockchain}}.trades
where
Expand All @@ -64,7 +64,7 @@ intrinsic_prices as (
sell_token_address as token_address,
round(log(10, atoms_sold / units_sold)) as decimals,
date_trunc('hour', block_time) as hour, --noqa: RF04
usd_value / units_sold as price,
usd_value / units_sold as price_usd,
usd_value / atoms_sold as price_atom
from cow_protocol_{{blockchain}}.trades
where
Expand All @@ -84,9 +84,9 @@ prices as (
intrinsic.decimals
) as decimals,
coalesce(
precise.price,
intrinsic.price
) as price,
precise.price_usd,
intrinsic.price_usd
) as price_usd,
coalesce(
precise.price_atom,
intrinsic.price_atom
Expand Down Expand Up @@ -116,7 +116,7 @@ native_token_prices as (
0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee as token_address,
18 as decimals,
date_trunc('hour', minute) as hour, --noqa: RF04
avg(price) as price,
avg(price) as price_usd,
avg(price) / pow(10, 18) as price_atom
from prices.usd
where
Expand Down

0 comments on commit 76ec325

Please sign in to comment.