|
| 1 | +from blockapi.v2.base import BalanceMixin, BlockchainApi |
| 2 | +from blockapi.v2.coins import COIN_LTC |
| 3 | +from blockapi.v2.models import ( |
| 4 | + ApiOptions, |
| 5 | + AssetType, |
| 6 | + BalanceItem, |
| 7 | + Blockchain, |
| 8 | + FetchResult, |
| 9 | + ParseResult, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class LitecoinSpaceApi(BlockchainApi, BalanceMixin): |
| 14 | + """ |
| 15 | + Coin: Litecoin |
| 16 | + API docs: https://litecoinspace.org/docs/api/rest |
| 17 | + Explorer: https://litecoinspace.org |
| 18 | + """ |
| 19 | + |
| 20 | + coin = COIN_LTC |
| 21 | + api_options = ApiOptions( |
| 22 | + blockchain=Blockchain.LITECOIN, |
| 23 | + base_url='https://litecoinspace.org', |
| 24 | + rate_limit=0.2, |
| 25 | + ) |
| 26 | + |
| 27 | + supported_requests = { |
| 28 | + 'get_balance': '/api/address/{address}', |
| 29 | + } |
| 30 | + |
| 31 | + def fetch_balances(self, address: str) -> FetchResult: |
| 32 | + return self.get_data('get_balance', address=address) |
| 33 | + |
| 34 | + def parse_balances(self, fetch_result: FetchResult) -> ParseResult: |
| 35 | + if not fetch_result.data: |
| 36 | + return ParseResult() |
| 37 | + |
| 38 | + chain_stats = fetch_result.data.get('chain_stats', {}) |
| 39 | + funded = chain_stats.get('funded_txo_sum', 0) |
| 40 | + spent = chain_stats.get('spent_txo_sum', 0) |
| 41 | + balance_raw = funded - spent |
| 42 | + |
| 43 | + if not balance_raw: |
| 44 | + return ParseResult() |
| 45 | + |
| 46 | + return ParseResult( |
| 47 | + data=[ |
| 48 | + BalanceItem.from_api( |
| 49 | + balance_raw=balance_raw, |
| 50 | + coin=self.coin, |
| 51 | + asset_type=AssetType.AVAILABLE, |
| 52 | + raw=fetch_result.data, |
| 53 | + ) |
| 54 | + ] |
| 55 | + ) |
0 commit comments