Skip to content

Commit ee5f4f0

Browse files
committed
Fix volume attribution for CTA and fee payers
1 parent c67f665 commit ee5f4f0

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

api/v1_coins_volume_leaders.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,39 +73,54 @@ func (app *ApiServer) v1CoinsVolumeLeaders(c *fiber.Ctx) error {
7373
sql := `
7474
-- Get pool vaults for both DBC and DAMM V2 to match against
7575
WITH pool_vaults AS (
76-
SELECT quote_vault AS vault_account, 'dbc' AS src FROM sol_meteora_dbc_pools
77-
UNION
78-
SELECT token_b_vault AS vault_account, 'damm_v2' AS src FROM sol_meteora_damm_v2_pools
76+
SELECT quote_vault AS vault_account, 'dbc' AS src FROM sol_meteora_dbc_pools
77+
UNION
78+
SELECT token_b_vault AS vault_account, 'damm_v2' AS src FROM sol_meteora_damm_v2_pools
7979
),
8080
leaders as (SELECT
81-
user_change.owner,
82-
user_change.account,
83-
SUM(ABS(user_change.change)) / 100000000 AS volume -- dividing by AUDIO decimals
84-
FROM sol_token_account_balance_changes user_change
85-
JOIN sol_token_account_balance_changes vault_change
81+
COALESCE(scat_match.account, vault_change.fee_payer) as address,
82+
-- vault_change.change
83+
SUM(ABS(vault_change.change)) / 100000000 AS volume -- dividing by AUDIO decimals
84+
FROM sol_token_account_balance_changes vault_change
85+
LEFT JOIN sol_token_account_balance_changes user_change
8686
ON vault_change.signature = user_change.signature
8787
AND vault_change.change = 0 - user_change.change
8888
AND vault_change.mint = user_change.mint
8989
JOIN pool_vaults pv
9090
ON pv.vault_account = vault_change.account
91+
-- If the tx involved a claimable tokens transfer, it will get credited to
92+
-- the AUDIO user bank address for the user that sent the tx.
93+
LEFT JOIN LATERAL (
94+
SELECT DISTINCT ON (sca.account)
95+
sca.account
96+
FROM sol_claimable_account_transfers scat
97+
JOIN sol_claimable_accounts sca
98+
ON sca.ethereum_address = scat.sender_eth_address
99+
-- Return AUDIO claimable token account for sender with each matching tx.
100+
-- TODO: Maybe this can just be vault_change.mint
101+
AND sca.mint = '9LzCMqDgTKYz9Drzqnpgee3SGa89up3a247ypMj2xrqM'
102+
WHERE scat.signature = vault_change.signature
103+
ORDER BY sca.account, scat.instruction_index
104+
) scat_match ON TRUE
91105
WHERE
106+
-- Exclude volume from pool migrations
92107
user_change.owner != '` + meteora_dbc.POOL_AUTHORITY_ADDRESS + `'
93108
AND user_change.owner != '` + meteora_damm_v2.POOL_AUTHORITY_ADDRESS + `'
109+
AND vault_change.change > 0
94110
AND vault_change.created_at >= @fromDate
95111
AND vault_change.created_at < @toDate
96112
AND user_change.created_at >= @fromDate
97113
AND user_change.created_at < @toDate
98-
GROUP BY user_change.owner, user_change.account
114+
GROUP BY address
99115
ORDER BY volume DESC
100116
)
101117
select
102118
COALESCE(u.user_id, aw.user_id) as user_id,
103-
-- Use account address if it's a claimable tokens ATA
104-
COALESCE(sca.account, l.owner) as address,
119+
l.address,
105120
l.volume
106121
FROM leaders l
107-
LEFT JOIN associated_wallets aw ON aw.wallet = l.owner
108-
LEFT JOIN sol_claimable_accounts sca ON sca.account = l.account
122+
LEFT JOIN associated_wallets aw ON aw.wallet = l.address
123+
LEFT JOIN sol_claimable_accounts sca ON sca.account = l.address
109124
LEFT JOIN users u ON u.wallet = sca.ethereum_address
110125
WHERE l.volume > 0
111126
LIMIT @limit

0 commit comments

Comments
 (0)