Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/blockapi-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
with:
fetch-depth: 0

- name: Set up Python 3.12.11
- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: '3.12.11'
python-version: '3.12'

- name: Verify Python
run: python --version
Expand Down Expand Up @@ -72,10 +72,10 @@ jobs:
with:
fetch-depth: 0

- name: Set up Python 3.12.11
- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: '3.12.11'
python-version: '3.12'

- name: Install dependencies
run: |
Expand Down Expand Up @@ -123,10 +123,10 @@ jobs:
VER=$(git tag --points-at HEAD)
[ -z "$VER" ] && grep -q "__version__ = \"${VER/v}\"" setup.py || true

- name: Set up Python 3.12.11
- name: Set up Python 3.12
uses: actions/setup-python@v4
with:
python-version: '3.12.11'
python-version: '3.12'

- name: Restore cache
uses: actions/cache@v3
Expand Down
57 changes: 54 additions & 3 deletions blockapi/test/v2/api/test_subscan_polkadot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from blockapi.test.v2.api.conftest import read_file
from blockapi.v2.api import PolkadotSubscanApi
from blockapi.v2.base import InvalidAddressException
from blockapi.v2.models import AssetType


Expand All @@ -30,7 +31,7 @@ def api():
)
def test_fetch_balances(requests_mock, address, response_path, expected_balance, api):
requests_mock.post(
'https://polkadot.api.subscan.io/api/v2/scan/search',
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
text=read_file(response_path),
)

Expand All @@ -44,7 +45,7 @@ def test_real_problematic_address_overlap(requests_mock, api):
Test the real problematic address that exhibits balance overlap.
"""
requests_mock.post(
'https://polkadot.api.subscan.io/api/v2/scan/search',
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
text=read_file('data/subscan_polkadot_balance_overlap_1567z.json'),
)

Expand All @@ -69,7 +70,7 @@ def test_second_problematic_address_overlap(requests_mock, api):
amount (154.31 DOT) but no staking locks, demonstrating reserved > locked case.
"""
requests_mock.post(
'https://polkadot.api.subscan.io/api/v2/scan/search',
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
text=read_file('data/subscan_polkadot_balance_overlap_15gT3.json'),
)

Expand All @@ -91,3 +92,53 @@ def test_second_problematic_address_overlap(requests_mock, api):
other_balance = other_balances[0]
assert other_balance.balance == Decimal('154.3078601187')
assert other_balance.asset_type == AssetType.LOCKED


def test_bonded_above_reserved_does_not_overcount(requests_mock, api):
# 15j4dg5G... on Asset Hub: staking ledger 7920963.40 > held 7920960.99
account = {
'balance': '7921211.1136402109',
'reserved': '79209609960724902',
'bonded': '79209634033624902',
}
requests_mock.post(
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
json={'code': 0, 'data': {'account': account}},
)

balances = api.get_balance('15j4dg5GzsL1bw2U2AWgeyAk6QTxq43V7ZPbXdAmbVLjvDCK')

assert {b.asset_type: b.balance for b in balances} == {
AssetType.AVAILABLE: Decimal('250.1175677207'),
AssetType.STAKED: Decimal('7920960.9960724902'),
}


NOT_FOUND = {'code': 10004, 'message': 'Record Not Found', 'data': None}


def test_account_unknown_to_asset_hub_has_no_balances(requests_mock, api):
requests_mock.post(
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
json=NOT_FOUND,
)

assert api.get_balance('148x3evJb8aLBrpvPU3AN8cRAphjhNLiT3o12BvFyDdhL289') == []


@pytest.mark.parametrize(
'address',
[
'not-an-address',
# valid address with its last character changed: bad checksum
'148x3evJb8aLBrpvPU3AN8cRAphjhNLiT3o12BvFyDdhL288',
],
)
def test_invalid_address_raises(requests_mock, api, address):
requests_mock.post(
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
json=NOT_FOUND,
)

with pytest.raises(InvalidAddressException):
api.get_balance(address)
37 changes: 33 additions & 4 deletions blockapi/v2/api/subscan.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import hashlib
import json
from abc import ABC
from decimal import Decimal
from typing import Iterable, List, Optional

import base58
from requests import Response

from blockapi.utils.num import decimals_to_raw, safe_opt_decimal, to_decimal
Expand All @@ -23,6 +25,18 @@
)


def _is_ss58_address(address: str) -> bool:
"""Checksum check of a 1-byte-prefix SS58 address (Polkadot, Kusama)."""
try:
raw = base58.b58decode(address)
except ValueError:
return False

payload, checksum = raw[:-2], raw[-2:]
expected = hashlib.blake2b(b'SS58PRE' + payload, digest_size=64).digest()[:2]
return len(raw) == 35 and checksum == expected


class SubscanApi(BlockchainApi, BalanceMixin, ABC):
"""
API docs: https://docs.api.subscan.io/
Expand All @@ -39,10 +53,21 @@ class SubscanApi(BlockchainApi, BalanceMixin, ABC):

def fetch_balances(self, address: str) -> FetchResult:
body = json.dumps({'key': address})
response = self._post('get_balance', body=body)
try:
response = self._post('get_balance', body=body)
except InvalidAddressException:
# Subscan answers "Record Not Found" both for malformed addresses
# and for accounts that never held anything on Asset Hub.
if not _is_ss58_address(address):
raise
return FetchResult()

return FetchResult(data=response)

def parse_balances(self, fetch_result: FetchResult) -> ParseResult:
if not fetch_result.data:
return ParseResult(data=[])

balances = list(self._yield_native_balances_zero_zum(fetch_result.data))

# add staking rewards (and slashes) too? it's a lot of requests
Expand All @@ -69,7 +94,8 @@ def _yield_native_balances_zero_zum(self, response: dict) -> Iterable[BalanceIte
raw=data,
)

b_bonded = safe_opt_decimal(data.get('bonded'))
# The staking ledger can exceed the held amount (seen on Asset Hub)
b_bonded = min(safe_opt_decimal(data.get('bonded')), b_reserved)
if b_bonded:
yield BalanceItem.from_api(
balance_raw=int(b_bonded),
Expand Down Expand Up @@ -153,15 +179,18 @@ def _get_api_options(blockchain: Blockchain, base_url: str) -> ApiOptions:
)


# Balances and staking moved from the relay chains to Asset Hub (Kusama Oct
# 2025, Polkadot Nov 2025); relay chain accounts now hold nothing.
class PolkadotSubscanApi(SubscanApi):
coin = COIN_DOT
api_options = SubscanApi._get_api_options(
blockchain=Blockchain.POLKADOT, base_url='https://polkadot.api.subscan.io'
blockchain=Blockchain.POLKADOT,
base_url='https://assethub-polkadot.api.subscan.io',
)


class KusamaSubscanApi(SubscanApi):
coin = COIN_KSM
api_options = SubscanApi._get_api_options(
blockchain=Blockchain.KUSAMA, base_url='https://kusama.api.subscan.io'
blockchain=Blockchain.KUSAMA, base_url='https://assethub-kusama.api.subscan.io'
)
Loading