Skip to content

Commit

Permalink
Add contracts supply endpoint - #21
Browse files Browse the repository at this point in the history
  • Loading branch information
abchrisxyz committed Apr 2, 2022
1 parent 3ef0fd4 commit 738c133
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
### New features
[Issue 13](https://github.com/abchrisxyz/ergowatch/issues/13) - Made node poll interval configurable.
[Issue 14](https://github.com/abchrisxyz/ergowatch/issues/14) - Added utxo count API.
[Issue 16](https://github.com/abchrisxyz/ergowatch/issues/17) - Added token details endpoint
[Issue 16](https://github.com/abchrisxyz/ergowatch/issues/17) - Added token details endpoint.
[Issue 17](https://github.com/abchrisxyz/ergowatch/issues/17) - Added sync status API.
[Issue 21](https://github.com/abchrisxyz/ergowatch/issues/21) - Added contracts supply endpoint.

### Changed
[Issue 19](https://github.com/abchrisxyz/ergowatch/issues/19) - Token supply endpoint breaks down circulating supply between P2PK and contract addresses.
Expand Down
27 changes: 27 additions & 0 deletions api/src/api/routes/contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,30 @@ async def get_contract_address_count(
if row["cnt"] is None:
raise HTTPException(status_code=404)
return row["cnt"]


@r.get("/supply", description="Supply in contracts")
async def supply_in_contracts(
request: Request,
token_id: TokenID = Query(None, description="Optional token id"),
):
"""
Current supply in contract addresses. Excludes coinbase address.
"""
query = """
select sum(value) as value
from bal.erg
where address <> '2Z4YBkDsDvQj8BX7xiySFewjitqp2ge9c99jfes2whbtKitZTxdBYqbrVZUvZvKv6aqn9by4kp3LE1c26LCyosFnVnm6b6U1JYvWpYmL2ZnixJbXLjWAWuBThV1D6dLpqZJYQHYDznJCk49g5TUiS4q8khpag2aNmHwREV7JSsypHdHLgJT7MGaw51aJfNubyzSKxZ4AJXFS27EfXwyCLzW1K6GVqwkJtCoPvrcLqmqwacAWJPkmh78nke9H4oT88XmSbRt2n9aWZjosiZCafZ4osUDxmZcc5QVEeTWn8drSraY3eFKe8Mu9MSCcVU'
and (address not like '9%' or length(address) <> 51)
"""
args = []
if token_id is not None:
args.append(token_id)
query = query.replace("bal.erg", "bal.tokens")
query += f" and token_id = $1"

async with request.app.state.db.acquire() as conn:
row = await conn.fetchrow(query, *args)
if row["value"] is None:
raise HTTPException(status_code=404)
return row["value"]
16 changes: 16 additions & 0 deletions api/src/tests/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,19 @@ def test_unknown_token_id(self, client):
assert response.status_code == 200
assert response.json() == 0


class TestSupply:
def test_default(self, client):
url = "/contracts/supply"
response = client.get(url)
assert response.status_code == 200
assert response.json() == 15_000_000

response = client.get(url + f"?token_id={TOKEN_A}")
assert response.status_code == 200
assert response.json() == 19_000_000

def test_unknown_token_id(self, client):
url = "/contracts/supply"
response = client.get(url + f"?token_id={TOKEN_X}")
assert response.status_code == 404

0 comments on commit 738c133

Please sign in to comment.