From 8909586e2fb561c725ab0f4f2c9bdf44a3c2912b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Galv=C3=A1nek?= Date: Mon, 16 Nov 2026 16:29:57 +0100 Subject: [PATCH 1/2] fix: fetch Polkadot and Kusama balances from Asset Hub Balances and staking moved from the relay chains to Asset Hub (Kusama Oct 2025, Polkadot Nov 2025), so the relay chain Subscan returned zero for every account. Query the Asset Hub Subscan instead. - Asset Hub Subscan answers "Record Not Found" for valid accounts that never held anything there; return no balances for a valid SS58 address and keep raising InvalidAddressException otherwise. - Cap staked at reserved: the staking ledger can exceed the held amount, which double counted the difference. Co-Authored-By: Claude Opus 5.5 --- blockapi/test/v2/api/test_subscan_polkadot.py | 57 ++++++++++++++++++- blockapi/v2/api/subscan.py | 37 ++++++++++-- 2 files changed, 87 insertions(+), 7 deletions(-) diff --git a/blockapi/test/v2/api/test_subscan_polkadot.py b/blockapi/test/v2/api/test_subscan_polkadot.py index 8528ee23..0b33d1e8 100644 --- a/blockapi/test/v2/api/test_subscan_polkadot.py +++ b/blockapi/test/v2/api/test_subscan_polkadot.py @@ -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 @@ -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), ) @@ -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'), ) @@ -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'), ) @@ -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) diff --git a/blockapi/v2/api/subscan.py b/blockapi/v2/api/subscan.py index d2fa8f85..b0255ff4 100644 --- a/blockapi/v2/api/subscan.py +++ b/blockapi/v2/api/subscan.py @@ -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 @@ -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/ @@ -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 @@ -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), @@ -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' ) From 8022912ae0675406de26084642cde1172be40d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Galv=C3=A1nek?= Date: Mon, 16 Nov 2026 17:40:04 +0100 Subject: [PATCH 2/2] ci: use latest Python 3.12 patch on runners ubuntu-latest is moving to Ubuntu 26.04, which has no prebuilt Python 3.12.11, so setup-python fails before any test runs. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/blockapi-ci.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/blockapi-ci.yml b/.github/workflows/blockapi-ci.yml index c4dbcea5..7662ab68 100644 --- a/.github/workflows/blockapi-ci.yml +++ b/.github/workflows/blockapi-ci.yml @@ -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 @@ -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: | @@ -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