Skip to content

Commit 7c1870b

Browse files
authored
Uniswap.trades Fees Logic Update (#8995)
* made a mistake lol * Revert "made a mistake lol" This reverts commit 0a5f0d9. * use trusted tokens prices first
1 parent a92f107 commit 7c1870b

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

dbt_subprojects/dex/macros/models/_project/uniswap_downstream_trades.sql

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ prices AS (
300300
and {{ incremental_predicate('minute') }}
301301
{% endif %}
302302
and blockchain = '{{blockchain}}'
303+
),
304+
305+
trusted_tokens AS (
306+
select
307+
contract_address
308+
, blockchain
309+
from
310+
{{ source('prices', 'trusted_tokens') }}
311+
where blockchain = '{{blockchain}}'
303312
)
304313

305314
select
@@ -342,15 +351,31 @@ prices AS (
342351
, evt_index
343352
-- uni fee columns
344353
, coalesce (
345-
token_sold_amount * uni_fee * pa.price_usd
354+
case
355+
when tt_sold.contract_address is not null
356+
then token_sold_amount * uni_fee * pa.price_usd
357+
end
358+
, case
359+
when tt_bought.contract_address is not null
360+
then ((token_bought_amount * pb.price_usd) / (1 - uni_fee)) - (token_bought_amount * pb.price_usd)
361+
end
362+
, token_sold_amount * uni_fee * pa.price_usd
346363
, ((token_bought_amount * pb.price_usd) / (1 - uni_fee)) - (token_bought_amount * pb.price_usd)
347364
) as lp_fee_amount_usd
348365
, token_sold_amount * uni_fee as lp_fee_amount
349366
, token_sold_amount_raw * uni_fee as lp_fee_amount_raw
350367
, uni_fee * 1e2 as lp_fee -- convert back to correct value
351368
-- hooks fee columns
352369
, coalesce (
353-
token_sold_amount * hooks_fee * pa.price_usd
370+
case
371+
when tt_sold.contract_address is not null
372+
then token_sold_amount * hooks_fee * pa.price_usd
373+
end
374+
, case
375+
when tt_bought.contract_address is not null
376+
then ((token_bought_amount * pb.price_usd) / (1 - hooks_fee)) - (token_bought_amount * pb.price_usd)
377+
end
378+
, token_sold_amount * hooks_fee * pa.price_usd
354379
, ((token_bought_amount * pb.price_usd) / (1 - hooks_fee)) - (token_bought_amount * pb.price_usd)
355380
) as hooks_fee_amount_usd
356381
, token_sold_amount * hooks_fee as hooks_fee_amount
@@ -372,6 +397,14 @@ prices AS (
372397
and af.blockchain = pb.price_blockchain
373398
and af.token_bought_address = pb.price_contract_address
374399
left join
400+
trusted_tokens tt_sold
401+
on af.token_sold_address = tt_sold.contract_address
402+
and af.blockchain = tt_sold.blockchain
403+
left join
404+
trusted_tokens tt_bought
405+
on af.token_bought_address = tt_bought.contract_address
406+
and af.blockchain = tt_bought.blockchain
407+
left join
375408
{{ ref('uniswap_pools') }} unp
376409
on af.blockchain = unp.blockchain
377410
and af.pool_address = unp.pool

0 commit comments

Comments
 (0)