Skip to content

Commit

Permalink
feat: added from_address param to token.transfers model
Browse files Browse the repository at this point in the history
fix: check for missing usd_amount in token.transfers model
  • Loading branch information
iwyrkore authored and v1nvn committed Aug 26, 2024
1 parent f9aa590 commit 9f1364e
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions models/credmark/tokens/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ class TokenTransferInput(Token):
offset: int = DTOField(0,
ge=0,
description="Omit a specified number of transfers from beginning of result set")
from_address: str | None = DTOField(
None, description='Optionally filter transactions to only those from a specific address')


class TokenTransfer(DTO):
Expand Down Expand Up @@ -558,6 +560,10 @@ class TokenTransferOutput(IterableListGenericDTO[TokenTransfer]):
class TokenTransfers(Model):
def run(self, input: TokenTransferInput) -> TokenTransferOutput:
with self.context.ledger.TokenTransfer as q:
where = q.TOKEN_ADDRESS.eq(input.address)
if input.from_address:
where = where.and_(q.FROM_ADDRESS.eq(input.from_address))

rows = q.select(
aggregates=[('COUNT(*) OVER()', 'total_transfers')],
columns=[q.BLOCK_NUMBER,
Expand All @@ -568,7 +574,7 @@ def run(self, input: TokenTransferInput) -> TokenTransferOutput:
q.USD_AMOUNT,
q.TRANSACTION_HASH,
q.LOG_INDEX],
where=q.TOKEN_ADDRESS.eq(input.address),
where=where,
order_by=q.BLOCK_NUMBER.desc(),
limit=input.limit,
offset=input.offset,
Expand All @@ -590,7 +596,7 @@ def run(self, input: TokenTransferInput) -> TokenTransferOutput:
amount=math.floor(Decimal(row['raw_amount'])),
amount_str=str(math.floor(Decimal(row['raw_amount']))),
amount_scaled=input.scaled(math.floor(Decimal(row['raw_amount']))),
usd_amount=float(row['usd_amount']),
usd_amount=float(row['usd_amount']) if row['usd_amount'] is not None else -1,
) for row in rows],
total_transfers=total_transfers)

Expand Down

0 comments on commit 9f1364e

Please sign in to comment.