Skip to content

Commit fd2de77

Browse files
committed
Feature: missing get_balances from sdk (with new field)
1 parent 3db474d commit fd2de77

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/aleph/sdk/client/http.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@
4949
RemovedMessageError,
5050
ResourceNotFoundError,
5151
)
52-
from ..query.filters import CreditsFilter, MessageFilter, PostFilter
52+
from ..query.filters import BalanceFilter, MessageFilter, PostFilter
5353
from ..query.responses import (
5454
AddressCreditResponse,
55-
CreditsResponse,
55+
BalanceResponse,
56+
CreditsHistoryResponse,
5657
MessagesResponse,
5758
Post,
5859
PostsResponse,
@@ -629,3 +630,17 @@ async def get_credit_history(
629630
resp.raise_for_status()
630631
result = await resp.json()
631632
return CreditsHistoryResponse.model_validate(result)
633+
634+
async def get_balances(
635+
self,
636+
address: str,
637+
filter: Optional[BalanceFilter] = None,
638+
) -> BalanceResponse:
639+
640+
async with self.http_session.get(
641+
f"/api/v0/addresses/{address}/balance",
642+
params=filter.as_http_params() if filter else None,
643+
) as resp:
644+
resp.raise_for_status()
645+
result = await resp.json()
646+
return BalanceResponse.model_validate(result)

src/aleph/sdk/query/responses.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import datetime as dt
4+
from decimal import Decimal
35
from typing import Any, Dict, List, Optional, Union
46

57
from aleph_message.models import (
@@ -112,4 +114,9 @@ class CreditHistoryResponseItem(BaseModel):
112114
message_timestamp: dt.datetime
113115

114116

115-
117+
class BalanceResponse(BaseModel):
118+
address: str
119+
balance: Decimal
120+
details: Optional[Dict[str, Decimal]] = None
121+
locked_amount: Decimal
122+
credit_balance: int = 0

tests/unit/test_balance.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from unittest.mock import patch
2+
3+
import pytest
4+
5+
from aleph.sdk.query.responses import BalanceResponse
6+
from tests.unit.conftest import make_mock_get_session
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_get_balances():
11+
"""
12+
Test that the get_balances method returns the correct BalanceResponse
13+
for a specific address when called on the AlephHttpClient.
14+
"""
15+
address = "0xd463495a6FEaC9921FD0C3a595B81E7B2C02B24d"
16+
17+
balance_data = {
18+
"address": address,
19+
"balance": 351.25,
20+
"details": {"ETH": 100.5, "SOL": 250.75},
21+
"locked_amount": 50.0,
22+
"credit_balance": 1000,
23+
}
24+
25+
mock_client = make_mock_get_session(balance_data)
26+
27+
expected_url = f"/api/v0/addresses/{address}/balance"
28+
# Adding type assertion to handle None case
29+
assert mock_client._http_session is not None
30+
with patch.object(
31+
mock_client._http_session, "get", wraps=mock_client._http_session.get
32+
) as spy:
33+
async with mock_client:
34+
response = await mock_client.get_balances(address)
35+
36+
# Verify the response
37+
assert isinstance(response, BalanceResponse)
38+
# Verify the balances command calls the correct URL
39+
spy.assert_called_once_with(expected_url, params=None)

0 commit comments

Comments
 (0)