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

Competitor analysis #143

merged 4 commits into from
Mar 5, 2025

Conversation

alex-zadorozhnyi
Copy link
Contributor

Added competitor analysis queries to the dune queries repo

@alex-zadorozhnyi alex-zadorozhnyi self-assigned this Mar 5, 2025
@fleupold fleupold requested a review from PoloX2021 March 5, 2025 16:03
Copy link
Contributor

@fleupold fleupold left a comment

Choose a reason for hiding this comment

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

Can you document what exactly the purpose of the queries is (it's not clear to me) and think if they can somehow be made dependent on one another (or a third base query) to reduce code duplication?

Comment on lines 31 to 71
chains_supported_by_cow as (
select distinct blockchain
from
dex_aggregator.trades
where
project = 'cow_protocol'
),

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
dex.trades
where
blockchain in (select blockchain from chains_supported_by_cow)
and
block_time between timestamp '{{start_time}}' and timestamp '{{end_time}}'
group by 1, 2, 3

union all

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
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}}'
group by 1, 2, 3
),
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of duplicating this query, can we express it in terms of the other query (if necessary defining a third shared query, which is used by the other two)?

Cf. mevblocker fee calculation as an example trying to reduce code duplication to a minimum.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd prefer to keep it like this until we start seeing that more queries are starting to use the same CTE combo. Refactoring to reduce code duplication would also decrease readability + reduce the ease of forking query in UI to perform ad-hoc analysis.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

P.S. I would still do the union before aggregation to reduce that sort of duplication

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd really encourage you going forward to think if you can design similar queries (like these two) in ways that share as much as code as possible. You will see that otherwise maintaining a coherent way of doing analysis will become increasingly difficult (e.g. someone may notice a flaw in one of the queries and will want to fix it; now that person needs to somehow know that there are other queries which use the same base logic and would also need to be fixed as otherwise we are starting to see incoherent results).

Comment on lines +73 to +101
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
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

@alex-zadorozhnyi
Copy link
Contributor Author

Please review latest version @fleupold @PoloX2021

@PoloX2021
Copy link
Contributor

LGTM

@PoloX2021 PoloX2021 enabled auto-merge (squash) March 5, 2025 18:34
@PoloX2021 PoloX2021 merged commit 5141213 into main Mar 5, 2025
2 checks passed
@PoloX2021 PoloX2021 deleted the competitor-analysis branch March 5, 2025 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants