Skip to content

Commit

Permalink
storage: sort tokens by total value
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Oct 1, 2024
1 parent 4e36cc1 commit 96f7091
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions .changelog/755.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
storage: sort tokens by total value
31 changes: 30 additions & 1 deletion storage/client/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,36 @@ const (
($3::text IS NULL OR tokens.token_name ILIKE '%' || $3 || '%' OR tokens.symbol ILIKE '%' || $3 || '%') AND
tokens.token_type IS NOT NULL AND -- exclude token _candidates_ that we haven't inspected yet
tokens.token_type != 0 -- exclude unknown-type tokens; they're often just contracts that emitted Transfer events but don't expose the token ticker, name, balance etc.
ORDER BY num_holders DESC, contract_addr
ORDER BY
(
CASE
-- For the reference token itself, it is 1:1 in value with, you know, itself.
WHEN
tokens.token_address = $5
THEN 1.0
-- The pool keeps a proportion of reserves so that reserve0 of token0 is worth about as much as reserve1 of token1.
-- When token0 is the reference token, more reserve0 means token1 is worth more than the reference token.
WHEN
ref_swap_pair_creations.token0_address = $5 AND
ref_swap_pairs.reserve0 IS NOT NULL AND
ref_swap_pairs.reserve0 > 0 AND
ref_swap_pairs.reserve1 IS NOT NULL AND
ref_swap_pairs.reserve1 > 0
THEN ref_swap_pairs.reserve0::REAL / ref_swap_pairs.reserve1::REAL
-- When token1 is the reference token, more reserve1 means token0 is worth more than the reference token.
WHEN
ref_swap_pair_creations.token1_address = $5 AND
ref_swap_pairs.reserve0 IS NOT NULL AND
ref_swap_pairs.reserve0 > 0 AND
ref_swap_pairs.reserve1 IS NOT NULL AND
ref_swap_pairs.reserve1 > 0
THEN ref_swap_pairs.reserve1::REAL / ref_swap_pairs.reserve0::REAL
ELSE 0.0
END *
COALESCE(tokens.total_supply, 0)
) DESC,
num_holders DESC,
contract_addr
LIMIT $6::bigint
OFFSET $7::bigint`

Expand Down

0 comments on commit 96f7091

Please sign in to comment.