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

Competitor analysis #143

Merged
merged 4 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions cowprotocol/competitor_analysis/.sqlfluff
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[sqlfluff:templater:jinja:context]
start_time='2024-08-01 12:00'
end_time='2024-08-02 12:00'
chain_of_interest='ethereum'
show_competitors_on_target_chain_only=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
-- Get all users who are using COW Protocol on any chain but {{chain_of_interest}}
-- Get aggregates (tx_count, usd_volume, user_count) of other dexes/aggregators they use on all chains supported by cow
-- or limit the results to only competitor usage on {{chain_of_interest}} if {{show_competitors_on_target_chain_only}} is set to 1
--
-- Parameters:
-- {{start_time}} - the trade timestamp for which the analysis should start (inclusive)
-- {{end_time}} - the trade timestamp for which the analysis should end (inclusive)
-- {{chain_of_interest}} - which chain is not used by cohort of users we want to investigate
-- {{show_competitors_on_target_chain_only}} - filters competitor's usage to only {{chain_of_interest}}

with all_cow_users as (
select
tx_from as address,
array_agg(distinct blockchain) as uses_on_chains
from
dex_aggregator.trades
where
project = 'cow_protocol'
and
block_time between timestamp '{{start_time}}' and timestamp '{{end_time}}'
group by
1
),

cow_users_filtered as (
select *
from
all_cow_users
where
not contains(uses_on_chains, '{{chain_of_interest}}')
),

chains_supported_by_cow as (
select distinct blockchain
from
dex_aggregator.trades
where
project = 'cow_protocol'
),

trades_dex_and_aggregators as (
select
tx_from,
blockchain,
project,
amount_usd
from
dex.trades
where
blockchain in (select blockchain from chains_supported_by_cow)
and
block_time between timestamp '{{start_time}}' and timestamp '{{end_time}}'

union all

select
tx_from,
blockchain,
project,
amount_usd
from
dex_aggregator.trades
where
project != 'cow_protocol'
and
blockchain in (select blockchain from chains_supported_by_cow)
and
block_time between timestamp '{{start_time}}' and timestamp '{{end_time}}'
),

all_competitor_transactions as (
select
tx_from as address,
blockchain as chain_used_for_competitor,
project as competitor_project,
sum(amount_usd) as competitor_total_volume_usd,
count(*) as competitor_total_transactions
from
trades_dex_and_aggregators
group by 1, 2, 3
)

select
chain_used_for_competitor,
competitor_project,
sum(competitor_total_volume_usd) as competitor_total_volume_usd,
sum(competitor_total_transactions) as competitor_total_transactions,
count(distinct address) as distinct_users_count
from
cow_users_filtered
left join all_competitor_transactions using (address) -- noqa: disable=L032
where
{{show_competitors_on_target_chain_only}} = 0 or chain_used_for_competitor = '{{chain_of_interest}}'
group by 1, 2
order by 3 desc
118 changes: 118 additions & 0 deletions cowprotocol/competitor_analysis/cow_vs_competition_4807448.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
-- Get all users who are using COW Protocol on any chain.
-- Get aggregates (tx_count, usd_volume, user_count) of other [dexes/aggregators, chain] pairs that are used by our users.
-- Join competitor usage and cow protocol usage on the same chain (using user address).
-- Form a cohorts that will be compared using the aggregates (tx_count, usd_volume, user_count) .
--
-- Parameters:
-- {{start_time}} - the trade timestamp for which the analysis should start (inclusive)
-- {{end_time}} - the trade timestamp for which the analysis should end (inclusive)

with cow_protocol_target_users as (
select
tx_from as address,
blockchain as chain_used_for_cow,
count(*) as total_transactions_on_cow,
sum(amount_usd) as total_volume_usd_on_cow
from
dex_aggregator.trades
where
project = 'cow_protocol'
and
block_time between timestamp '{{start_time}}' and timestamp '{{end_time}}'
group by
1, 2
),

users_per_chain_cow as (
select
blockchain as chain_used_for_cow,
count(distinct tx_from) as distinct_users_cow
from
dex_aggregator.trades
where
project = 'cow_protocol'
and
block_time between timestamp '{{start_time}}' and timestamp '{{end_time}}'
group by
1
),

chains_supported_by_cow as (
select distinct blockchain
from
dex_aggregator.trades
where
project = 'cow_protocol'
),

trades_dex_and_aggregators as (
select
tx_from,
blockchain,
project,
amount_usd
from
dex.trades
where
blockchain in (select blockchain from chains_supported_by_cow)
and
block_time between timestamp '{{start_time}}' and timestamp '{{end_time}}'

union all

select
tx_from,
blockchain,
project,
amount_usd
from
dex_aggregator.trades
where
project != 'cow_protocol'
and
blockchain in (select blockchain from chains_supported_by_cow)
and
block_time between timestamp '{{start_time}}' and timestamp '{{end_time}}'
),

all_competitor_transactions as (
select
tx_from as address,
blockchain as chain_used_for_competitor,
project as competitor_project,
sum(amount_usd) as competitor_total_volume_usd,
count(*) as competitor_total_transactions
from
trades_dex_and_aggregators
group by 1, 2, 3
),

agg as (
select
chain_used_for_cow,
chain_used_for_competitor,
competitor_project,
count(distinct address) as distinct_users_competitor,
sum(total_transactions_on_cow) as transactions_made_on_cow,
sum(total_volume_usd_on_cow) as total_volume_usd_on_cow,
sum(competitor_total_transactions) as transactions_made_on_competitor,
sum(competitor_total_volume_usd) as total_volume_usd_on_competitor
from
cow_protocol_target_users
Comment on lines +67 to +95
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it better to do a seperate join for just one additional line, or group it one, with 2 joins

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you are talking about joining users_per_chain_cow - I keep it out for readability

left join all_competitor_transactions using (address) -- noqa: disable=L032
group by 1, 2, 3
)

select
chain_used_for_cow,
chain_used_for_competitor,
competitor_project,
distinct_users_cow,
distinct_users_competitor,
transactions_made_on_cow,
total_volume_usd_on_cow,
transactions_made_on_competitor,
total_volume_usd_on_competitor
from
agg
left join users_per_chain_cow using (chain_used_for_cow) -- noqa: disable=L032