blockapi 2.11.5__tar.gz → 2.12.0__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.11.5 → blockapi-2.12.0}/PKG-INFO +1 -1
- blockapi-2.12.0/blockapi/test/v2/api/test_chainso.py +76 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_solana.py +105 -15
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/__init__.py +1 -0
- blockapi-2.12.0/blockapi/v2/api/chainso.py +69 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/solana.py +56 -14
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi.egg-info/PKG-INFO +1 -1
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi.egg-info/SOURCES.txt +2 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/setup.py +1 -1
- {blockapi-2.11.5 → blockapi-2.12.0}/LICENSE.md +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/README.md +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/services.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/test_blockapi.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/test_num.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/test_random_user_agent.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/conftest.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/covalenth/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/covalenth/test_ethereum.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/fake_sleep_provider.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/nft/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/nft/test_magic_eden.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/nft/test_opensea.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/nft/test_simple_hash.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/nft/test_unisat.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_bitaps_ltc.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_blockchain_info.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_blockchainos.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_blockchair_btc.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_blockchair_doge.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_blockchair_ltc.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_cosmos.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_ethplorer.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_haskoin.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_litecoinspace.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_multisources.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_optimistic_etherscan.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_subscan_polkadot.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_sui.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_terra.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_trezor_btc.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/api/test_trezor_zec.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/test_base.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/test_blockchain_api.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/test_blockchain_mapping.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/test_data.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/test_enumerate_classes.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/test_generic.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test/v2/test_models.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/test_data.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/utils/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/utils/address.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/utils/datetime.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/utils/num.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/utils/user_agent.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/bitaps.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/blockchain_info.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/blockchainos.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/blockchair.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/cosmos.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/arbitrum.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/astar.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/avalanche.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/axie.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/base.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/binance_smart_chain.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/ethereum.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/fantom.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/heco.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/iotex.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/klaytn.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/moonbeam.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/palm.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/polygon.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/covalenth/rsk.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/debank.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/debank_maps.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/ethplorer.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/haskoin.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/litecoinspace.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/nft/__init__.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/nft/magic_eden.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/nft/opensea.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/nft/simple_hash.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/nft/unisat.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/optimistic_etherscan.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/subscan.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/sui.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/terra.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/api/trezor.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/base.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/blockchain_mapping.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/coin_mapping.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/coins.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi/v2/models.py +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi.egg-info/dependency_links.txt +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi.egg-info/requires.txt +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/blockapi.egg-info/top_level.txt +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/pyproject.toml +0 -0
- {blockapi-2.11.5 → blockapi-2.12.0}/setup.cfg +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from decimal import Decimal
|
|
2
|
+
from unittest.mock import Mock
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
from requests.exceptions import ReadTimeout
|
|
6
|
+
|
|
7
|
+
from blockapi.test.v2.api.conftest import read_file
|
|
8
|
+
from blockapi.test.v2.test_data import ltc_test_address
|
|
9
|
+
from blockapi.v2.api import ChainSoLitecoinApi
|
|
10
|
+
from blockapi.v2.base import ApiException
|
|
11
|
+
from blockapi.v2.models import FetchResult
|
|
12
|
+
|
|
13
|
+
URL = f'https://sochain.com/api/v3/balance/LTC/{ltc_test_address}'
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_fetch_balances(requests_mock):
|
|
17
|
+
requests_mock.get(URL, text=read_file('data/chainso_balance_response.json'))
|
|
18
|
+
api = ChainSoLitecoinApi()
|
|
19
|
+
balances = api.get_balance(ltc_test_address)
|
|
20
|
+
assert len(balances) == 1
|
|
21
|
+
assert balances[0].balance == Decimal('262.99968329')
|
|
22
|
+
assert balances[0].balance_raw == Decimal('26299968329')
|
|
23
|
+
assert balances[0].coin == api.coin
|
|
24
|
+
assert requests_mock.last_request.timeout == (3.0, 10.0)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@pytest.mark.parametrize('confirmed', ['0.00000000', '0.00000001'])
|
|
28
|
+
def test_confirmed_balance_only(requests_mock, confirmed):
|
|
29
|
+
requests_mock.get(
|
|
30
|
+
URL,
|
|
31
|
+
json={
|
|
32
|
+
'status': 'success',
|
|
33
|
+
'data': {'confirmed': confirmed, 'unconfirmed': '12.00000000'},
|
|
34
|
+
},
|
|
35
|
+
)
|
|
36
|
+
balances = ChainSoLitecoinApi().get_balance(ltc_test_address)
|
|
37
|
+
if Decimal(confirmed):
|
|
38
|
+
assert balances[0].balance == Decimal(confirmed)
|
|
39
|
+
assert balances[0].balance_raw == 1
|
|
40
|
+
else:
|
|
41
|
+
assert balances == []
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@pytest.mark.parametrize(
|
|
45
|
+
'response',
|
|
46
|
+
[
|
|
47
|
+
None,
|
|
48
|
+
{},
|
|
49
|
+
{'status': 'fail', 'data': {'confirmed': '0'}},
|
|
50
|
+
{'status': 'success', 'data': {}},
|
|
51
|
+
*[
|
|
52
|
+
{'status': 'success', 'data': {'confirmed': value}}
|
|
53
|
+
for value in [None, 'bad', 'NaN', 'Infinity', '-1', '0.000000001']
|
|
54
|
+
],
|
|
55
|
+
],
|
|
56
|
+
)
|
|
57
|
+
def test_invalid_response_is_not_zero(response):
|
|
58
|
+
with pytest.raises(ApiException):
|
|
59
|
+
ChainSoLitecoinApi().parse_balances(FetchResult(data=response))
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@pytest.mark.parametrize('status', [429, 502])
|
|
63
|
+
def test_http_failure_does_not_retry_or_return_zero(requests_mock, status):
|
|
64
|
+
requests_mock.get(URL, status_code=status, json={'status': 'fail'})
|
|
65
|
+
sleep_provider = Mock()
|
|
66
|
+
api = ChainSoLitecoinApi(sleep_provider=sleep_provider)
|
|
67
|
+
with pytest.raises(ApiException):
|
|
68
|
+
api.get_balance(ltc_test_address)
|
|
69
|
+
assert requests_mock.call_count == 1
|
|
70
|
+
sleep_provider.sleep.assert_not_called()
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def test_read_timeout_is_not_zero(requests_mock):
|
|
74
|
+
requests_mock.get(URL, exc=ReadTimeout('stalled'))
|
|
75
|
+
with pytest.raises(ApiException):
|
|
76
|
+
ChainSoLitecoinApi().get_balance(ltc_test_address)
|
|
@@ -3,7 +3,7 @@ from decimal import Decimal
|
|
|
3
3
|
from unittest.mock import call, patch
|
|
4
4
|
|
|
5
5
|
import pytest
|
|
6
|
-
from requests_mock import
|
|
6
|
+
from requests_mock import Mocker
|
|
7
7
|
|
|
8
8
|
from blockapi.test.v2.api.conftest import read_file
|
|
9
9
|
from blockapi.v2.api import SolanaApi, SolscanApi
|
|
@@ -77,10 +77,41 @@ def test_use_base_url():
|
|
|
77
77
|
|
|
78
78
|
|
|
79
79
|
@pytest.mark.parametrize(
|
|
80
|
-
('rpc_url', '
|
|
80
|
+
('rpc_url', 'status', 'retry_after'),
|
|
81
81
|
[
|
|
82
|
-
('https://
|
|
83
|
-
('https://
|
|
82
|
+
('https://proxy/solana/', 429, None),
|
|
83
|
+
('https://not-helius-rpc.com/', 429, None),
|
|
84
|
+
('https://helius-rpc.com.example.org/', 429, None),
|
|
85
|
+
('https://mainnet.helius-rpc.com/', 400, None),
|
|
86
|
+
('https://mainnet.helius-rpc.com/', 500, None),
|
|
87
|
+
('https://mainnet.helius-rpc.com/', 429, '5'),
|
|
88
|
+
('https://mainnet.helius-rpc.com/', 429, 'Fri, 31 Dec 9999 23:59:59 GMT'),
|
|
89
|
+
('https://mainnet.helius-rpc.com/', 429, 'invalid'),
|
|
90
|
+
],
|
|
91
|
+
)
|
|
92
|
+
def test_fetch_does_not_retry_other_errors_or_long_cooldowns(
|
|
93
|
+
rpc_url, status, retry_after
|
|
94
|
+
):
|
|
95
|
+
headers = {'Retry-After': retry_after} if retry_after is not None else {}
|
|
96
|
+
with Mocker() as m, patch('time.sleep') as sleep:
|
|
97
|
+
m.post(rpc_url, status_code=status, headers=headers)
|
|
98
|
+
with pytest.raises(ApiException):
|
|
99
|
+
SolanaApi(base_url=rpc_url).fetch_balances('address')
|
|
100
|
+
|
|
101
|
+
assert len(m.request_history) == 1
|
|
102
|
+
sleep.assert_not_called()
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@pytest.mark.parametrize(
|
|
106
|
+
('rpc_url', 'uses_v2_staking', 'retry_index', 'retry_after'),
|
|
107
|
+
[
|
|
108
|
+
('https://mainnet.helius-rpc.com/', True, None, None),
|
|
109
|
+
('https://proxy/solana/', False, None, None),
|
|
110
|
+
('https://mainnet.helius-rpc.com/', True, 0, None),
|
|
111
|
+
('https://mainnet.helius-rpc.com/', True, 1, '0'),
|
|
112
|
+
('https://mainnet.helius-rpc.com/', True, 2, '1'),
|
|
113
|
+
('https://mainnet.helius-rpc.com/', True, 3, None),
|
|
114
|
+
('https://mainnet.helius-rpc.com/', True, 5, None),
|
|
84
115
|
],
|
|
85
116
|
)
|
|
86
117
|
def test_get_balance_supports_helius_and_legacy_staking_responses(
|
|
@@ -90,6 +121,8 @@ def test_get_balance_supports_helius_and_legacy_staking_responses(
|
|
|
90
121
|
staked_solana_response,
|
|
91
122
|
rpc_url,
|
|
92
123
|
uses_v2_staking,
|
|
124
|
+
retry_index,
|
|
125
|
+
retry_after,
|
|
93
126
|
):
|
|
94
127
|
test_addr = '5PjMxaijeVVQtuEzxK2NxyJeWwUbpTsi2uXuZ653WoHu'
|
|
95
128
|
empty_token_accounts = '{"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.17.34","slot":268207149},"value":[]},"id":1}'
|
|
@@ -97,26 +130,39 @@ def test_get_balance_supports_helius_and_legacy_staking_responses(
|
|
|
97
130
|
if not uses_v2_staking:
|
|
98
131
|
staking_response['result'] = staking_response['result']['accounts']
|
|
99
132
|
|
|
100
|
-
|
|
101
|
-
|
|
133
|
+
responses = [
|
|
134
|
+
{'text': data}
|
|
135
|
+
for data in [
|
|
102
136
|
sol_balance_response,
|
|
103
137
|
token_accounts_response,
|
|
104
138
|
empty_token_accounts,
|
|
105
139
|
das_asset_batch_response,
|
|
106
140
|
json.dumps(staking_response),
|
|
107
141
|
]
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
142
|
+
]
|
|
143
|
+
if uses_v2_staking:
|
|
144
|
+
responses.insert(
|
|
145
|
+
4, {'json': {'result': {'accounts': [], 'paginationKey': 'next-page'}}}
|
|
146
|
+
)
|
|
147
|
+
if retry_index is not None:
|
|
148
|
+
headers = {'Retry-After': retry_after} if retry_after is not None else {}
|
|
149
|
+
responses.insert(retry_index, {'status_code': 429, 'headers': headers})
|
|
150
|
+
|
|
151
|
+
with Mocker() as m, patch('time.sleep') as sleep:
|
|
152
|
+
m.post(rpc_url, responses)
|
|
117
153
|
api = SolanaApi(base_url=rpc_url)
|
|
118
154
|
balances = api.get_balance(test_addr)
|
|
119
155
|
|
|
156
|
+
if retry_index is not None:
|
|
157
|
+
sleep.assert_called_once_with(1.1)
|
|
158
|
+
assert len(m.request_history) == len(responses)
|
|
159
|
+
assert (
|
|
160
|
+
m.request_history[retry_index].body
|
|
161
|
+
== m.request_history[retry_index + 1].body
|
|
162
|
+
)
|
|
163
|
+
else:
|
|
164
|
+
sleep.assert_not_called()
|
|
165
|
+
|
|
120
166
|
staking_balances = {
|
|
121
167
|
balance.asset_type: balance.balance_raw
|
|
122
168
|
for balance in balances
|
|
@@ -128,6 +174,47 @@ def test_get_balance_supports_helius_and_legacy_staking_responses(
|
|
|
128
174
|
}
|
|
129
175
|
|
|
130
176
|
|
|
177
|
+
@pytest.mark.parametrize('fail_on_same_rpc', [True, False])
|
|
178
|
+
def test_retry_is_shared_across_fetch_and_resets_for_next_fetch(
|
|
179
|
+
token_accounts_response, fail_on_same_rpc
|
|
180
|
+
):
|
|
181
|
+
api = SolanaApi(base_url='https://mainnet.helius-rpc.com/')
|
|
182
|
+
responses = [{'status_code': 429}]
|
|
183
|
+
if not fail_on_same_rpc:
|
|
184
|
+
responses += [
|
|
185
|
+
{'json': {'result': {'value': 0}}},
|
|
186
|
+
{'text': token_accounts_response},
|
|
187
|
+
{'json': {'result': {'value': []}}},
|
|
188
|
+
{'status_code': 429}, # Optional DAS metadata may fail.
|
|
189
|
+
{'json': {'result': {'accounts': [], 'paginationKey': 'next-page'}}},
|
|
190
|
+
]
|
|
191
|
+
responses.append({'status_code': 429})
|
|
192
|
+
|
|
193
|
+
with Mocker() as m, patch('time.sleep') as sleep:
|
|
194
|
+
for attempt in range(2):
|
|
195
|
+
m.post(api.base_url, responses)
|
|
196
|
+
with pytest.raises(ApiException):
|
|
197
|
+
api.fetch_balances('address')
|
|
198
|
+
assert len(m.request_history) == (attempt + 1) * len(responses)
|
|
199
|
+
if not fail_on_same_rpc:
|
|
200
|
+
assert (
|
|
201
|
+
m.last_request.json()['params'][1]['paginationKey'] == 'next-page'
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
assert sleep.call_args_list == [call(1.1), call(1.1)]
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def test_get_coin_retries_helius_metadata_once():
|
|
208
|
+
api = SolanaApi(base_url='https://mainnet.helius-rpc.com/')
|
|
209
|
+
with Mocker() as m, patch('time.sleep') as sleep:
|
|
210
|
+
m.post(api.base_url, [{'status_code': 429}, {'json': {'result': []}}])
|
|
211
|
+
coin = api.get_coin(('mint', 6))
|
|
212
|
+
|
|
213
|
+
assert coin.address == 'mint'
|
|
214
|
+
assert len(m.request_history) == 2
|
|
215
|
+
sleep.assert_called_once_with(1.1)
|
|
216
|
+
|
|
217
|
+
|
|
131
218
|
def test_build_coin_from_das_asset():
|
|
132
219
|
api = SolanaApi()
|
|
133
220
|
asset = {
|
|
@@ -317,6 +404,7 @@ def test_fetch_staked_sol_uses_v2_pagination():
|
|
|
317
404
|
call(
|
|
318
405
|
method='getProgramAccountsV2',
|
|
319
406
|
params=[api.STAKE_PROGRAM_ID, config],
|
|
407
|
+
retry=None,
|
|
320
408
|
),
|
|
321
409
|
call(
|
|
322
410
|
method='getProgramAccountsV2',
|
|
@@ -324,6 +412,7 @@ def test_fetch_staked_sol_uses_v2_pagination():
|
|
|
324
412
|
api.STAKE_PROGRAM_ID,
|
|
325
413
|
{**config, 'paginationKey': 'next-page'},
|
|
326
414
|
],
|
|
415
|
+
retry=None,
|
|
327
416
|
),
|
|
328
417
|
call(
|
|
329
418
|
method='getProgramAccountsV2',
|
|
@@ -331,6 +420,7 @@ def test_fetch_staked_sol_uses_v2_pagination():
|
|
|
331
420
|
api.STAKE_PROGRAM_ID,
|
|
332
421
|
{**config, 'paginationKey': 'last-page'},
|
|
333
422
|
],
|
|
423
|
+
retry=None,
|
|
334
424
|
),
|
|
335
425
|
]
|
|
336
426
|
assert response == {
|
|
@@ -7,6 +7,7 @@ from blockapi.v2.api.blockchair import (
|
|
|
7
7
|
BlockchairDogecoinApi,
|
|
8
8
|
BlockchairLitecoinApi,
|
|
9
9
|
)
|
|
10
|
+
from blockapi.v2.api.chainso import ChainSoLitecoinApi
|
|
10
11
|
from blockapi.v2.api.debank import DebankApi, DebankApp, DebankPrediction
|
|
11
12
|
from blockapi.v2.api.ethplorer import EthplorerApi
|
|
12
13
|
from blockapi.v2.api.haskoin import HaskoinApi
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
from decimal import Decimal, InvalidOperation
|
|
2
|
+
|
|
3
|
+
from blockapi.v2.base import ApiException, BalanceMixin, BlockchainApi
|
|
4
|
+
from blockapi.v2.coins import COIN_LTC
|
|
5
|
+
from blockapi.v2.models import (
|
|
6
|
+
ApiOptions,
|
|
7
|
+
AssetType,
|
|
8
|
+
BalanceItem,
|
|
9
|
+
Blockchain,
|
|
10
|
+
FetchResult,
|
|
11
|
+
ParseResult,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ChainSoLitecoinApi(BlockchainApi, BalanceMixin):
|
|
16
|
+
"""Litecoin confirmed balances: https://sochain.com/docs."""
|
|
17
|
+
|
|
18
|
+
coin = COIN_LTC
|
|
19
|
+
api_options = ApiOptions(
|
|
20
|
+
blockchain=Blockchain.LITECOIN,
|
|
21
|
+
base_url='https://sochain.com',
|
|
22
|
+
rate_limit=1.0,
|
|
23
|
+
)
|
|
24
|
+
supported_requests = {
|
|
25
|
+
'get_balance': '/api/v3/balance/LTC/{address}',
|
|
26
|
+
}
|
|
27
|
+
http_timeout = (3.0, 10.0)
|
|
28
|
+
max_rate_limit_retries = 1
|
|
29
|
+
|
|
30
|
+
def fetch_balances(self, address: str) -> FetchResult:
|
|
31
|
+
return self.get_data('get_balance', address=address)
|
|
32
|
+
|
|
33
|
+
def _get_response(self, request_method, headers, params, req_args):
|
|
34
|
+
url = self._build_request_url(request_method, **req_args)
|
|
35
|
+
return self._session.get(
|
|
36
|
+
url, headers=headers, params=params, timeout=self.http_timeout
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
def parse_balances(self, fetch_result: FetchResult) -> ParseResult:
|
|
40
|
+
response = fetch_result.data
|
|
41
|
+
if not isinstance(response, dict) or response.get('status') != 'success':
|
|
42
|
+
raise ApiException('SoChain balance request was not successful')
|
|
43
|
+
|
|
44
|
+
try:
|
|
45
|
+
# SoChain returns LTC, whereas balance_raw uses litoshis.
|
|
46
|
+
balance_raw = (
|
|
47
|
+
Decimal(response['data']['confirmed']) * 10**self.coin.decimals
|
|
48
|
+
)
|
|
49
|
+
except (KeyError, TypeError, ValueError, InvalidOperation) as exc:
|
|
50
|
+
raise ApiException('Invalid SoChain confirmed balance') from exc
|
|
51
|
+
if (
|
|
52
|
+
not balance_raw.is_finite()
|
|
53
|
+
or balance_raw < 0
|
|
54
|
+
or balance_raw != balance_raw.to_integral_value()
|
|
55
|
+
):
|
|
56
|
+
raise ApiException('Invalid SoChain confirmed balance')
|
|
57
|
+
if not balance_raw:
|
|
58
|
+
return ParseResult()
|
|
59
|
+
|
|
60
|
+
return ParseResult(
|
|
61
|
+
data=[
|
|
62
|
+
BalanceItem.from_api(
|
|
63
|
+
balance_raw=balance_raw,
|
|
64
|
+
coin=self.coin,
|
|
65
|
+
asset_type=AssetType.AVAILABLE,
|
|
66
|
+
raw=response,
|
|
67
|
+
)
|
|
68
|
+
]
|
|
69
|
+
)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import logging
|
|
3
|
+
import time
|
|
4
|
+
from dataclasses import dataclass
|
|
3
5
|
from typing import Optional, Union
|
|
4
6
|
from urllib.parse import urlparse
|
|
5
7
|
|
|
@@ -32,6 +34,11 @@ from blockapi.v2.models import (
|
|
|
32
34
|
logger = logging.getLogger(__name__)
|
|
33
35
|
|
|
34
36
|
|
|
37
|
+
@dataclass
|
|
38
|
+
class _HeliusRetry:
|
|
39
|
+
used: bool = False
|
|
40
|
+
|
|
41
|
+
|
|
35
42
|
class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
36
43
|
"""Solana JSON-RPC client with DAS metadata integration.
|
|
37
44
|
|
|
@@ -67,7 +74,7 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
67
74
|
api_options = ApiOptions(
|
|
68
75
|
blockchain=Blockchain.SOLANA,
|
|
69
76
|
base_url='https://api.mainnet-beta.solana.com/',
|
|
70
|
-
rate_limit=
|
|
77
|
+
rate_limit=2,
|
|
71
78
|
start_offset=0,
|
|
72
79
|
max_items_per_page=1000,
|
|
73
80
|
page_offset_step=1,
|
|
@@ -81,6 +88,7 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
81
88
|
STAKE_PROGRAM_ID = 'Stake11111111111111111111111111111111111111'
|
|
82
89
|
STAKE_AUTHORITY_OFFSET = 44
|
|
83
90
|
HELIUS_RPC_DOMAIN = 'helius-rpc.com'
|
|
91
|
+
HELIUS_RETRY_DELAY = 1.1
|
|
84
92
|
HELIUS_PROGRAM_ACCOUNTS_PAGE_SIZE = 5000
|
|
85
93
|
DAS_BATCH_SIZE = 1000
|
|
86
94
|
_JSONRPC_INVALID_PARAMS = -32602
|
|
@@ -104,7 +112,8 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
104
112
|
|
|
105
113
|
def fetch_balances(self, address: str) -> FetchResult:
|
|
106
114
|
"""Fetch native SOL, token accounts, DAS metadata, and staking data."""
|
|
107
|
-
|
|
115
|
+
retry = _HeliusRetry()
|
|
116
|
+
sol_response = self._request('getBalance', [address], retry=retry)
|
|
108
117
|
|
|
109
118
|
raw_token_balances = self._request(
|
|
110
119
|
'getTokenAccountsByOwner',
|
|
@@ -113,6 +122,7 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
113
122
|
{'programId': self.TOKEN_PROGRAM_ID},
|
|
114
123
|
{'encoding': 'jsonParsed'},
|
|
115
124
|
],
|
|
125
|
+
retry=retry,
|
|
116
126
|
)
|
|
117
127
|
raw_token2022_balances = self._request(
|
|
118
128
|
'getTokenAccountsByOwner',
|
|
@@ -121,14 +131,15 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
121
131
|
{'programId': self.TOKEN_2022_PROGRAM_ID},
|
|
122
132
|
{'encoding': 'jsonParsed'},
|
|
123
133
|
],
|
|
134
|
+
retry=retry,
|
|
124
135
|
)
|
|
125
136
|
|
|
126
137
|
mint_addresses = self._collect_mint_addresses(
|
|
127
138
|
raw_token_balances, raw_token2022_balances
|
|
128
139
|
)
|
|
129
|
-
self._fetch_das_assets(mint_addresses)
|
|
140
|
+
self._fetch_das_assets(mint_addresses, retry=retry)
|
|
130
141
|
|
|
131
|
-
raw_staked_sol = self._fetch_staked_sol(address)
|
|
142
|
+
raw_staked_sol = self._fetch_staked_sol(address, retry=retry)
|
|
132
143
|
|
|
133
144
|
return FetchResult(
|
|
134
145
|
data=sol_response,
|
|
@@ -172,7 +183,7 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
172
183
|
def get_coin(self, fetch_params: tuple[str, int]) -> Coin:
|
|
173
184
|
"""Fetch and build a Coin for a given contract address and decimals."""
|
|
174
185
|
contract, decimals = fetch_params
|
|
175
|
-
self._fetch_das_assets([contract])
|
|
186
|
+
self._fetch_das_assets([contract], retry=_HeliusRetry())
|
|
176
187
|
return self._resolve_coin(contract, decimals)
|
|
177
188
|
|
|
178
189
|
@staticmethod
|
|
@@ -248,7 +259,9 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
248
259
|
|
|
249
260
|
# ── DAS integration ────────────────────────────────────────
|
|
250
261
|
|
|
251
|
-
def _fetch_das_assets(
|
|
262
|
+
def _fetch_das_assets(
|
|
263
|
+
self, mint_addresses: list[str], retry: Optional[_HeliusRetry] = None
|
|
264
|
+
) -> None:
|
|
252
265
|
"""Batch-fetch token metadata via DAS and populate cache."""
|
|
253
266
|
uncached = list(
|
|
254
267
|
dict.fromkeys(m for m in mint_addresses if m not in self._das_cache)
|
|
@@ -262,6 +275,7 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
262
275
|
response = self._request(
|
|
263
276
|
'getAssetBatch',
|
|
264
277
|
{'ids': chunk, 'options': {'showFungible': True}},
|
|
278
|
+
retry=retry,
|
|
265
279
|
)
|
|
266
280
|
except (ApiException, RequestException) as e:
|
|
267
281
|
logger.warning('DAS getAssetBatch failed: %s', e)
|
|
@@ -315,7 +329,9 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
315
329
|
|
|
316
330
|
# ── Staking ────────────────────────────────────────────────
|
|
317
331
|
|
|
318
|
-
def _fetch_staked_sol(
|
|
332
|
+
def _fetch_staked_sol(
|
|
333
|
+
self, address: str, retry: Optional[_HeliusRetry] = None
|
|
334
|
+
) -> dict:
|
|
319
335
|
"""Fetch staked SOL accounts for a given address."""
|
|
320
336
|
config = {
|
|
321
337
|
'filters': [
|
|
@@ -331,11 +347,7 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
331
347
|
'commitment': 'finalized',
|
|
332
348
|
}
|
|
333
349
|
|
|
334
|
-
|
|
335
|
-
is_helius_rpc = hostname == self.HELIUS_RPC_DOMAIN or hostname.endswith(
|
|
336
|
-
f'.{self.HELIUS_RPC_DOMAIN}'
|
|
337
|
-
)
|
|
338
|
-
if not is_helius_rpc:
|
|
350
|
+
if not self._is_helius_rpc():
|
|
339
351
|
return self._request(
|
|
340
352
|
method='getProgramAccounts',
|
|
341
353
|
params=[self.STAKE_PROGRAM_ID, config],
|
|
@@ -349,6 +361,7 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
349
361
|
response = self._request(
|
|
350
362
|
method='getProgramAccountsV2',
|
|
351
363
|
params=[self.STAKE_PROGRAM_ID, config],
|
|
364
|
+
retry=retry,
|
|
352
365
|
)
|
|
353
366
|
page = response['result']
|
|
354
367
|
accounts.extend(page['accounts'])
|
|
@@ -419,7 +432,12 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
419
432
|
|
|
420
433
|
# ── Infrastructure ─────────────────────────────────────────
|
|
421
434
|
|
|
422
|
-
def _request(
|
|
435
|
+
def _request(
|
|
436
|
+
self,
|
|
437
|
+
method: str,
|
|
438
|
+
params: Union[list, dict],
|
|
439
|
+
retry: Optional[_HeliusRetry] = None,
|
|
440
|
+
) -> dict:
|
|
423
441
|
"""Send a JSON-RPC request to the Solana RPC endpoint."""
|
|
424
442
|
self._request_id += 1
|
|
425
443
|
body = json.dumps(
|
|
@@ -430,7 +448,31 @@ class SolanaApi(CustomizableBlockchainApi, BalanceMixin):
|
|
|
430
448
|
'params': params,
|
|
431
449
|
}
|
|
432
450
|
)
|
|
433
|
-
|
|
451
|
+
headers = {'Content-Type': 'application/json'}
|
|
452
|
+
if not self._is_helius_rpc():
|
|
453
|
+
return self.post(body=body, headers=headers)
|
|
454
|
+
|
|
455
|
+
response = self._session.post(self.base_url, data=body, headers=headers)
|
|
456
|
+
if (
|
|
457
|
+
response.status_code == 429
|
|
458
|
+
and retry is not None
|
|
459
|
+
and not retry.used
|
|
460
|
+
and response.headers.get('Retry-After', '0').strip() in ('0', '1')
|
|
461
|
+
):
|
|
462
|
+
retry.used = True
|
|
463
|
+
response.close()
|
|
464
|
+
logger.warning(
|
|
465
|
+
f'Helius {method} rate limited; retrying in {self.HELIUS_RETRY_DELAY}s'
|
|
466
|
+
)
|
|
467
|
+
time.sleep(self.HELIUS_RETRY_DELAY)
|
|
468
|
+
response = self._session.post(self.base_url, data=body, headers=headers)
|
|
469
|
+
return self._check_and_get_from_response(response)
|
|
470
|
+
|
|
471
|
+
def _is_helius_rpc(self) -> bool:
|
|
472
|
+
hostname = urlparse(self.base_url).hostname or ''
|
|
473
|
+
return hostname == self.HELIUS_RPC_DOMAIN or hostname.endswith(
|
|
474
|
+
f'.{self.HELIUS_RPC_DOMAIN}'
|
|
475
|
+
)
|
|
434
476
|
|
|
435
477
|
def _opt_raise_on_other_error(self, response: Response) -> None:
|
|
436
478
|
"""Raise ApiException or InvalidAddressException on RPC errors."""
|
|
@@ -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_chainso.py
|
|
35
36
|
blockapi/test/v2/api/test_cosmos.py
|
|
36
37
|
blockapi/test/v2/api/test_ethplorer.py
|
|
37
38
|
blockapi/test/v2/api/test_haskoin.py
|
|
@@ -67,6 +68,7 @@ blockapi/v2/api/bitaps.py
|
|
|
67
68
|
blockapi/v2/api/blockchain_info.py
|
|
68
69
|
blockapi/v2/api/blockchainos.py
|
|
69
70
|
blockapi/v2/api/blockchair.py
|
|
71
|
+
blockapi/v2/api/chainso.py
|
|
70
72
|
blockapi/v2/api/cosmos.py
|
|
71
73
|
blockapi/v2/api/debank.py
|
|
72
74
|
blockapi/v2/api/debank_maps.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
|