blockapi 2.12.1__tar.gz → 2.13.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {blockapi-2.12.1 → blockapi-2.13.1}/PKG-INFO +1 -1
- blockapi-2.13.1/blockapi/test/v2/api/test_blockonomics.py +106 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_subscan_polkadot.py +54 -3
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/__init__.py +1 -0
- blockapi-2.13.1/blockapi/v2/api/blockonomics.py +89 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/subscan.py +33 -4
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi.egg-info/PKG-INFO +1 -1
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi.egg-info/SOURCES.txt +2 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/setup.py +1 -1
- {blockapi-2.12.1 → blockapi-2.13.1}/LICENSE.md +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/README.md +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/services.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/test_blockapi.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/test_num.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/test_random_user_agent.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/conftest.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/covalenth/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/covalenth/test_ethereum.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/fake_sleep_provider.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/nft/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/nft/test_magic_eden.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/nft/test_opensea.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/nft/test_simple_hash.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/nft/test_unisat.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_bitaps_ltc.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_blockchain_info.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_blockchainos.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_blockchair_btc.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_blockchair_doge.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_blockchair_ltc.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_chainso.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_cosmos.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_ethplorer.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_haskoin.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_litecoinspace.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_multisources.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_optimistic_etherscan.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_solana.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_sui.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_terra.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_trezor_btc.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/api/test_trezor_zec.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/test_base.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/test_blockchain_api.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/test_blockchain_mapping.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/test_data.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/test_enumerate_classes.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/test_generic.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test/v2/test_models.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/test_data.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/utils/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/utils/address.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/utils/datetime.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/utils/num.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/utils/user_agent.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/bitaps.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/blockchain_info.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/blockchainos.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/blockchair.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/chainso.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/cosmos.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/arbitrum.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/astar.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/avalanche.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/axie.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/base.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/binance_smart_chain.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/ethereum.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/fantom.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/heco.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/iotex.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/klaytn.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/moonbeam.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/palm.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/polygon.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/covalenth/rsk.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/debank.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/debank_maps.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/ethplorer.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/haskoin.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/litecoinspace.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/nft/__init__.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/nft/magic_eden.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/nft/opensea.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/nft/simple_hash.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/nft/unisat.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/optimistic_etherscan.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/solana.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/sui.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/terra.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/api/trezor.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/base.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/blockchain_mapping.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/coin_mapping.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/coins.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi/v2/models.py +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi.egg-info/dependency_links.txt +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi.egg-info/requires.txt +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/blockapi.egg-info/top_level.txt +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/pyproject.toml +0 -0
- {blockapi-2.12.1 → blockapi-2.13.1}/setup.cfg +0 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
from decimal import Decimal
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
from requests.exceptions import ConnectionError, ReadTimeout
|
|
5
|
+
|
|
6
|
+
from blockapi.v2.api import BlockonomicsApi
|
|
7
|
+
from blockapi.v2.base import ApiException
|
|
8
|
+
from blockapi.v2.models import FetchResult
|
|
9
|
+
|
|
10
|
+
# Deliberately synthetic identifiers: these tests never access a live wallet.
|
|
11
|
+
KEY = 'synthetic-api-key'
|
|
12
|
+
URL = 'https://www.blockonomics.co/api/balance'
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def row(address='synthetic-derived-address', confirmed=123456789, unconfirmed=0):
|
|
16
|
+
return dict(addr=address, confirmed=confirmed, unconfirmed=unconfirmed)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@pytest.mark.parametrize(
|
|
20
|
+
'address', ['synthetic-address', 'xpubSynthetic', 'ypubSynthetic', 'zpubSynthetic']
|
|
21
|
+
)
|
|
22
|
+
def test_fetch_and_aggregate(requests_mock, address):
|
|
23
|
+
payload = {
|
|
24
|
+
'response': [
|
|
25
|
+
row(confirmed=123456789, unconfirmed=999),
|
|
26
|
+
row('synthetic-change-address', 1, -10),
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
requests_mock.get(URL, json=payload)
|
|
30
|
+
balances = BlockonomicsApi(api_key=KEY).get_balance(address)
|
|
31
|
+
assert len(balances) == 1
|
|
32
|
+
assert balances[0].balance == Decimal('1.23456790')
|
|
33
|
+
assert balances[0].raw == payload
|
|
34
|
+
request = requests_mock.last_request
|
|
35
|
+
assert request.headers['Authorization'] == 'Bearer ' + KEY
|
|
36
|
+
assert request.qs == {'addr': [address.lower()]}
|
|
37
|
+
assert KEY not in request.url
|
|
38
|
+
assert request.timeout == (5, 30)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def test_zero_excludes_unconfirmed(requests_mock):
|
|
42
|
+
requests_mock.get(URL, json={'response': [row(confirmed=0, unconfirmed=100)]})
|
|
43
|
+
assert BlockonomicsApi(api_key=KEY).get_balance('synthetic-address') == []
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_missing_key_does_not_request(requests_mock):
|
|
47
|
+
with pytest.raises(ApiException, match='API key'):
|
|
48
|
+
BlockonomicsApi().get_balance('synthetic-address')
|
|
49
|
+
assert requests_mock.call_count == 0
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
@pytest.mark.parametrize('address', ['', 'first second', 'first\nsecond'])
|
|
53
|
+
def test_reject_batch_input(requests_mock, address):
|
|
54
|
+
with pytest.raises(ApiException, match='Expected one'):
|
|
55
|
+
BlockonomicsApi(api_key=KEY).get_balance(address)
|
|
56
|
+
assert requests_mock.call_count == 0
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@pytest.mark.parametrize('status', [401, 403, 429, 500, 502])
|
|
60
|
+
def test_http_error_not_zero(requests_mock, status):
|
|
61
|
+
requests_mock.get(URL, status_code=status, json={'message': 'unavailable'})
|
|
62
|
+
with pytest.raises(ApiException):
|
|
63
|
+
BlockonomicsApi(api_key=KEY).get_balance('synthetic-address')
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@pytest.mark.parametrize(
|
|
67
|
+
'error', [ConnectionError('disconnected'), ReadTimeout('timeout')]
|
|
68
|
+
)
|
|
69
|
+
def test_transport_error_not_zero(requests_mock, error):
|
|
70
|
+
requests_mock.get(URL, exc=error)
|
|
71
|
+
with pytest.raises(ApiException):
|
|
72
|
+
BlockonomicsApi(api_key=KEY).get_balance('synthetic-address')
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_invalid_json(requests_mock):
|
|
76
|
+
requests_mock.get(URL, text='<html>error</html>')
|
|
77
|
+
with pytest.raises(ApiException, match='Invalid JSON'):
|
|
78
|
+
BlockonomicsApi(api_key=KEY).get_balance('synthetic-address')
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@pytest.mark.parametrize(
|
|
82
|
+
'data',
|
|
83
|
+
[
|
|
84
|
+
None,
|
|
85
|
+
{},
|
|
86
|
+
[],
|
|
87
|
+
{'response': []},
|
|
88
|
+
{'response': {}},
|
|
89
|
+
{'response': [None]},
|
|
90
|
+
{'response': [{}]},
|
|
91
|
+
{'response': [row(confirmed=True)]},
|
|
92
|
+
{'response': [row(confirmed=-1)]},
|
|
93
|
+
{'response': [row(confirmed='1')]},
|
|
94
|
+
{'response': [row(confirmed=1.5)]},
|
|
95
|
+
{'response': [row(address=None)]},
|
|
96
|
+
{'response': [row(), row()]},
|
|
97
|
+
],
|
|
98
|
+
)
|
|
99
|
+
def test_invalid_response_not_zero(data):
|
|
100
|
+
with pytest.raises(ApiException):
|
|
101
|
+
BlockonomicsApi().parse_balances(FetchResult(data=data))
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def test_direct_parse_preserves_error():
|
|
105
|
+
with pytest.raises(ApiException, match='unavailable'):
|
|
106
|
+
BlockonomicsApi().parse_balances(FetchResult(errors=['unavailable']))
|
|
@@ -4,6 +4,7 @@ import pytest
|
|
|
4
4
|
|
|
5
5
|
from blockapi.test.v2.api.conftest import read_file
|
|
6
6
|
from blockapi.v2.api import PolkadotSubscanApi
|
|
7
|
+
from blockapi.v2.base import InvalidAddressException
|
|
7
8
|
from blockapi.v2.models import AssetType
|
|
8
9
|
|
|
9
10
|
|
|
@@ -30,7 +31,7 @@ def api():
|
|
|
30
31
|
)
|
|
31
32
|
def test_fetch_balances(requests_mock, address, response_path, expected_balance, api):
|
|
32
33
|
requests_mock.post(
|
|
33
|
-
'https://polkadot.api.subscan.io/api/v2/scan/search',
|
|
34
|
+
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
|
|
34
35
|
text=read_file(response_path),
|
|
35
36
|
)
|
|
36
37
|
|
|
@@ -44,7 +45,7 @@ def test_real_problematic_address_overlap(requests_mock, api):
|
|
|
44
45
|
Test the real problematic address that exhibits balance overlap.
|
|
45
46
|
"""
|
|
46
47
|
requests_mock.post(
|
|
47
|
-
'https://polkadot.api.subscan.io/api/v2/scan/search',
|
|
48
|
+
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
|
|
48
49
|
text=read_file('data/subscan_polkadot_balance_overlap_1567z.json'),
|
|
49
50
|
)
|
|
50
51
|
|
|
@@ -69,7 +70,7 @@ def test_second_problematic_address_overlap(requests_mock, api):
|
|
|
69
70
|
amount (154.31 DOT) but no staking locks, demonstrating reserved > locked case.
|
|
70
71
|
"""
|
|
71
72
|
requests_mock.post(
|
|
72
|
-
'https://polkadot.api.subscan.io/api/v2/scan/search',
|
|
73
|
+
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
|
|
73
74
|
text=read_file('data/subscan_polkadot_balance_overlap_15gT3.json'),
|
|
74
75
|
)
|
|
75
76
|
|
|
@@ -91,3 +92,53 @@ def test_second_problematic_address_overlap(requests_mock, api):
|
|
|
91
92
|
other_balance = other_balances[0]
|
|
92
93
|
assert other_balance.balance == Decimal('154.3078601187')
|
|
93
94
|
assert other_balance.asset_type == AssetType.LOCKED
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_bonded_above_reserved_does_not_overcount(requests_mock, api):
|
|
98
|
+
# 15j4dg5G... on Asset Hub: staking ledger 7920963.40 > held 7920960.99
|
|
99
|
+
account = {
|
|
100
|
+
'balance': '7921211.1136402109',
|
|
101
|
+
'reserved': '79209609960724902',
|
|
102
|
+
'bonded': '79209634033624902',
|
|
103
|
+
}
|
|
104
|
+
requests_mock.post(
|
|
105
|
+
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
|
|
106
|
+
json={'code': 0, 'data': {'account': account}},
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
balances = api.get_balance('15j4dg5GzsL1bw2U2AWgeyAk6QTxq43V7ZPbXdAmbVLjvDCK')
|
|
110
|
+
|
|
111
|
+
assert {b.asset_type: b.balance for b in balances} == {
|
|
112
|
+
AssetType.AVAILABLE: Decimal('250.1175677207'),
|
|
113
|
+
AssetType.STAKED: Decimal('7920960.9960724902'),
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
NOT_FOUND = {'code': 10004, 'message': 'Record Not Found', 'data': None}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_account_unknown_to_asset_hub_has_no_balances(requests_mock, api):
|
|
121
|
+
requests_mock.post(
|
|
122
|
+
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
|
|
123
|
+
json=NOT_FOUND,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
assert api.get_balance('148x3evJb8aLBrpvPU3AN8cRAphjhNLiT3o12BvFyDdhL289') == []
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@pytest.mark.parametrize(
|
|
130
|
+
'address',
|
|
131
|
+
[
|
|
132
|
+
'not-an-address',
|
|
133
|
+
# valid address with its last character changed: bad checksum
|
|
134
|
+
'148x3evJb8aLBrpvPU3AN8cRAphjhNLiT3o12BvFyDdhL288',
|
|
135
|
+
],
|
|
136
|
+
)
|
|
137
|
+
def test_invalid_address_raises(requests_mock, api, address):
|
|
138
|
+
requests_mock.post(
|
|
139
|
+
'https://assethub-polkadot.api.subscan.io/api/v2/scan/search',
|
|
140
|
+
json=NOT_FOUND,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
with pytest.raises(InvalidAddressException):
|
|
144
|
+
api.get_balance(address)
|
|
@@ -7,6 +7,7 @@ from blockapi.v2.api.blockchair import (
|
|
|
7
7
|
BlockchairDogecoinApi,
|
|
8
8
|
BlockchairLitecoinApi,
|
|
9
9
|
)
|
|
10
|
+
from blockapi.v2.api.blockonomics import BlockonomicsApi
|
|
10
11
|
from blockapi.v2.api.chainso import ChainSoLitecoinApi
|
|
11
12
|
from blockapi.v2.api.debank import DebankApi, DebankApp, DebankPrediction
|
|
12
13
|
from blockapi.v2.api.ethplorer import EthplorerApi
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from blockapi.v2.base import ApiException, BalanceMixin, BlockchainApi
|
|
2
|
+
from blockapi.v2.coins import COIN_BTC
|
|
3
|
+
from blockapi.v2.models import (
|
|
4
|
+
ApiOptions,
|
|
5
|
+
AssetType,
|
|
6
|
+
BalanceItem,
|
|
7
|
+
Blockchain,
|
|
8
|
+
FetchResult,
|
|
9
|
+
ParseResult,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BlockonomicsApi(BlockchainApi, BalanceMixin):
|
|
14
|
+
"""BTC balances for addresses and xpub/ypub/zpub extended public keys.
|
|
15
|
+
|
|
16
|
+
Requires a Blockonomics API key. Only provider-confirmed balances are
|
|
17
|
+
included: Blockonomics classifies fewer than two confirmations as
|
|
18
|
+
unconfirmed. Extended-key discovery follows the provider's gap policy.
|
|
19
|
+
Docs: https://developers.blockonomics.co/docs/search/getbalance
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
coin = COIN_BTC
|
|
23
|
+
api_options = ApiOptions(
|
|
24
|
+
blockchain=Blockchain.BITCOIN,
|
|
25
|
+
base_url='https://www.blockonomics.co/api/',
|
|
26
|
+
rate_limit=1,
|
|
27
|
+
)
|
|
28
|
+
supported_requests = {'get_balance': 'balance'}
|
|
29
|
+
request_timeout = (5, 30)
|
|
30
|
+
|
|
31
|
+
def _get_response(self, request_method, headers, params, req_args):
|
|
32
|
+
return self._session.get(
|
|
33
|
+
self._build_request_url(request_method, **req_args),
|
|
34
|
+
headers=headers,
|
|
35
|
+
params=params,
|
|
36
|
+
timeout=self.request_timeout,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
def fetch_balances(self, address: str) -> FetchResult:
|
|
40
|
+
if not self.api_key:
|
|
41
|
+
return FetchResult(errors=['Blockonomics API key is required'])
|
|
42
|
+
# One wallet per request; whitespace is the provider's batch separator.
|
|
43
|
+
if not address or any(char.isspace() for char in address):
|
|
44
|
+
return FetchResult(errors=['Expected one Bitcoin address or extended key'])
|
|
45
|
+
try:
|
|
46
|
+
return self.get_data(
|
|
47
|
+
'get_balance',
|
|
48
|
+
headers={'Authorization': f'Bearer {self.api_key}'},
|
|
49
|
+
params={'addr': address},
|
|
50
|
+
)
|
|
51
|
+
except ValueError:
|
|
52
|
+
return FetchResult(errors=['Invalid JSON in Blockonomics response'])
|
|
53
|
+
|
|
54
|
+
def parse_balances(self, fetch_result: FetchResult) -> ParseResult:
|
|
55
|
+
if fetch_result.errors:
|
|
56
|
+
raise ApiException(fetch_result.errors[0])
|
|
57
|
+
data = fetch_result.data
|
|
58
|
+
rows = data.get('response') if isinstance(data, dict) else None
|
|
59
|
+
if not isinstance(rows, list) or not rows:
|
|
60
|
+
raise ApiException('Invalid Blockonomics response: missing balance rows')
|
|
61
|
+
addresses = set()
|
|
62
|
+
balance_raw = 0
|
|
63
|
+
for row in rows:
|
|
64
|
+
if not isinstance(row, dict):
|
|
65
|
+
raise ApiException('Invalid Blockonomics balance row')
|
|
66
|
+
address = row.get('addr')
|
|
67
|
+
confirmed = row.get('confirmed')
|
|
68
|
+
if (
|
|
69
|
+
not isinstance(address, str)
|
|
70
|
+
or not address
|
|
71
|
+
or address in addresses
|
|
72
|
+
or type(confirmed) is not int
|
|
73
|
+
or confirmed < 0
|
|
74
|
+
):
|
|
75
|
+
raise ApiException('Invalid Blockonomics balance row')
|
|
76
|
+
addresses.add(address)
|
|
77
|
+
balance_raw += confirmed
|
|
78
|
+
if not balance_raw:
|
|
79
|
+
return ParseResult()
|
|
80
|
+
return ParseResult(
|
|
81
|
+
data=[
|
|
82
|
+
BalanceItem.from_api(
|
|
83
|
+
balance_raw=balance_raw,
|
|
84
|
+
coin=self.coin,
|
|
85
|
+
asset_type=AssetType.AVAILABLE,
|
|
86
|
+
raw=data,
|
|
87
|
+
)
|
|
88
|
+
]
|
|
89
|
+
)
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import hashlib
|
|
1
2
|
import json
|
|
2
3
|
from abc import ABC
|
|
3
4
|
from decimal import Decimal
|
|
4
5
|
from typing import Iterable, List, Optional
|
|
5
6
|
|
|
7
|
+
import base58
|
|
6
8
|
from requests import Response
|
|
7
9
|
|
|
8
10
|
from blockapi.utils.num import decimals_to_raw, safe_opt_decimal, to_decimal
|
|
@@ -23,6 +25,18 @@ from blockapi.v2.models import (
|
|
|
23
25
|
)
|
|
24
26
|
|
|
25
27
|
|
|
28
|
+
def _is_ss58_address(address: str) -> bool:
|
|
29
|
+
"""Checksum check of a 1-byte-prefix SS58 address (Polkadot, Kusama)."""
|
|
30
|
+
try:
|
|
31
|
+
raw = base58.b58decode(address)
|
|
32
|
+
except ValueError:
|
|
33
|
+
return False
|
|
34
|
+
|
|
35
|
+
payload, checksum = raw[:-2], raw[-2:]
|
|
36
|
+
expected = hashlib.blake2b(b'SS58PRE' + payload, digest_size=64).digest()[:2]
|
|
37
|
+
return len(raw) == 35 and checksum == expected
|
|
38
|
+
|
|
39
|
+
|
|
26
40
|
class SubscanApi(BlockchainApi, BalanceMixin, ABC):
|
|
27
41
|
"""
|
|
28
42
|
API docs: https://docs.api.subscan.io/
|
|
@@ -39,10 +53,21 @@ class SubscanApi(BlockchainApi, BalanceMixin, ABC):
|
|
|
39
53
|
|
|
40
54
|
def fetch_balances(self, address: str) -> FetchResult:
|
|
41
55
|
body = json.dumps({'key': address})
|
|
42
|
-
|
|
56
|
+
try:
|
|
57
|
+
response = self._post('get_balance', body=body)
|
|
58
|
+
except InvalidAddressException:
|
|
59
|
+
# Subscan answers "Record Not Found" both for malformed addresses
|
|
60
|
+
# and for accounts that never held anything on Asset Hub.
|
|
61
|
+
if not _is_ss58_address(address):
|
|
62
|
+
raise
|
|
63
|
+
return FetchResult()
|
|
64
|
+
|
|
43
65
|
return FetchResult(data=response)
|
|
44
66
|
|
|
45
67
|
def parse_balances(self, fetch_result: FetchResult) -> ParseResult:
|
|
68
|
+
if not fetch_result.data:
|
|
69
|
+
return ParseResult(data=[])
|
|
70
|
+
|
|
46
71
|
balances = list(self._yield_native_balances_zero_zum(fetch_result.data))
|
|
47
72
|
|
|
48
73
|
# add staking rewards (and slashes) too? it's a lot of requests
|
|
@@ -69,7 +94,8 @@ class SubscanApi(BlockchainApi, BalanceMixin, ABC):
|
|
|
69
94
|
raw=data,
|
|
70
95
|
)
|
|
71
96
|
|
|
72
|
-
|
|
97
|
+
# The staking ledger can exceed the held amount (seen on Asset Hub)
|
|
98
|
+
b_bonded = min(safe_opt_decimal(data.get('bonded')), b_reserved)
|
|
73
99
|
if b_bonded:
|
|
74
100
|
yield BalanceItem.from_api(
|
|
75
101
|
balance_raw=int(b_bonded),
|
|
@@ -153,15 +179,18 @@ class SubscanApi(BlockchainApi, BalanceMixin, ABC):
|
|
|
153
179
|
)
|
|
154
180
|
|
|
155
181
|
|
|
182
|
+
# Balances and staking moved from the relay chains to Asset Hub (Kusama Oct
|
|
183
|
+
# 2025, Polkadot Nov 2025); relay chain accounts now hold nothing.
|
|
156
184
|
class PolkadotSubscanApi(SubscanApi):
|
|
157
185
|
coin = COIN_DOT
|
|
158
186
|
api_options = SubscanApi._get_api_options(
|
|
159
|
-
blockchain=Blockchain.POLKADOT,
|
|
187
|
+
blockchain=Blockchain.POLKADOT,
|
|
188
|
+
base_url='https://assethub-polkadot.api.subscan.io',
|
|
160
189
|
)
|
|
161
190
|
|
|
162
191
|
|
|
163
192
|
class KusamaSubscanApi(SubscanApi):
|
|
164
193
|
coin = COIN_KSM
|
|
165
194
|
api_options = SubscanApi._get_api_options(
|
|
166
|
-
blockchain=Blockchain.KUSAMA, base_url='https://kusama.api.subscan.io'
|
|
195
|
+
blockchain=Blockchain.KUSAMA, base_url='https://assethub-kusama.api.subscan.io'
|
|
167
196
|
)
|
|
@@ -32,6 +32,7 @@ blockapi/test/v2/api/test_blockchainos.py
|
|
|
32
32
|
blockapi/test/v2/api/test_blockchair_btc.py
|
|
33
33
|
blockapi/test/v2/api/test_blockchair_doge.py
|
|
34
34
|
blockapi/test/v2/api/test_blockchair_ltc.py
|
|
35
|
+
blockapi/test/v2/api/test_blockonomics.py
|
|
35
36
|
blockapi/test/v2/api/test_chainso.py
|
|
36
37
|
blockapi/test/v2/api/test_cosmos.py
|
|
37
38
|
blockapi/test/v2/api/test_ethplorer.py
|
|
@@ -68,6 +69,7 @@ blockapi/v2/api/bitaps.py
|
|
|
68
69
|
blockapi/v2/api/blockchain_info.py
|
|
69
70
|
blockapi/v2/api/blockchainos.py
|
|
70
71
|
blockapi/v2/api/blockchair.py
|
|
72
|
+
blockapi/v2/api/blockonomics.py
|
|
71
73
|
blockapi/v2/api/chainso.py
|
|
72
74
|
blockapi/v2/api/cosmos.py
|
|
73
75
|
blockapi/v2/api/debank.py
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|