Skip to content

Commit

Permalink
fix: token holders balance scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
v1nvn committed Aug 28, 2024
1 parent 56ae35b commit 8b1fb62
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
25 changes: 15 additions & 10 deletions models/credmark/tokens/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,17 +718,19 @@ def run(self, input: TokenHolderInput) -> TokenHoldersOutput:
order_by = q.field("balance").dquote().desc()

having = (
q.AMOUNT.as_numeric().sum_().gt(0)
q.RAW_AMOUNT.as_numeric().sum_().gt(0)
if input.min_amount == -1
else q.AMOUNT.as_numeric().sum_().ge(input.min_amount)
else q.RAW_AMOUNT.as_numeric().sum_().ge(input.unscaled(input.min_amount))
)

if input.max_amount != -1:
having = having.and_(q.AMOUNT.as_numeric().sum_().le(input.max_amount))
having = having.and_(
q.RAW_AMOUNT.as_numeric().sum_().le(input.unscaled(input.max_amount))
)

df = q.select(
aggregates=[
(q.AMOUNT.as_numeric().sum_(), "balance"),
(q.RAW_AMOUNT.as_numeric().sum_(), "balance"),
(q.BLOCK_NUMBER.min_(), "first_block_number"),
(q.BLOCK_TIMESTAMP.min_(), "first_block_timestamp"),
(q.BLOCK_NUMBER.max_(), "last_block_number"),
Expand All @@ -752,9 +754,12 @@ def run(self, input: TokenHolderInput) -> TokenHoldersOutput:
PriceWithQuote(price=0, src="", quoteAddress=input.quote.address)
)

total_holders = df["total_holders"].values[0]
if total_holders is None:
if df.empty:
total_holders = 0
else:
total_holders = df["total_holders"].values[0]
if total_holders is None:
total_holders = 0

return TokenHoldersOutput(
price=token_price,
Expand Down Expand Up @@ -798,13 +803,13 @@ def run(self, input: Token) -> TokenHoldersCountOutput:
with self.context.ledger.TokenBalance as q:
df = q.select(
aggregates=[
(q.AMOUNT.as_numeric().sum_(), "balance"),
(q.RAW_AMOUNT.as_numeric().sum_(), "balance"),
("COUNT(*) OVER()", "total_holders"),
],
where=q.TOKEN_ADDRESS.eq(input.address),
group_by=[q.ADDRESS],
order_by=q.AMOUNT.as_numeric().sum_().desc(),
having=q.AMOUNT.as_numeric().sum_().gt(0),
order_by=q.RAW_AMOUNT.as_numeric().sum_().desc(),
having=q.RAW_AMOUNT.as_numeric().sum_().gt(0),
).to_dataframe()

if df.empty:
Expand Down Expand Up @@ -834,7 +839,7 @@ def run(self, input: Token) -> dict:
aggregates=[],
group_by=[q.ADDRESS],
where=q.TOKEN_ADDRESS.eq(input.address),
having=q.AMOUNT.as_numeric().sum_().gt(0),
having=q.RAW_AMOUNT.as_numeric().sum_().gt(0),
).to_dataframe()
return df.to_dict()

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ packages = [{ include = "models" }]

[tool.poetry.dependencies]
python = ">=3.11"
credmark-model-framework = { git = "https://github.com/credmark/credmark-model-framework-py.git", tag = "0.8.146" }
credmark-model-framework = { git = "https://github.com/credmark/credmark-model-framework-py.git", tag = "0.8.148" }
ipfshttpclient = ">=0.8.0a2"
matplotlib = ">=3.7.1"
networkx = ">=3.1"
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
git+https://github.com/credmark/credmark-model-framework-py.git@0.8.146
git+https://github.com/credmark/credmark-model-framework-py.git@0.8.148
ipfshttpclient>=0.8.0a2
matplotlib>=3.7.1
networkx>=3.1
Expand Down

0 comments on commit 8b1fb62

Please sign in to comment.