def __init__(self, address, api_key=None): super().__init__(address, api_key) paging_params = self.get_tx_paging_params() if paging_params is None: raise APIError('Can\'t initialize API paging.') else: self.max_items_per_page = self.page_offset_step = paging_params[1] self.total_txs_count = paging_params[2]
def get_staking(self, offset=0, limit=20): body = json.dumps({'key': self.address, 'row': limit, 'page': offset}) response = self.request('get_staking', body=body, headers=self._headers, api_key=self.api_key) if response['code'] == 0: return safe_decimal(response['data']['account']['balance_lock']) elif response['code'] == 10004: raise AddressNotExist() else: raise APIError(response['message'])
def _request(self, method, params): body = json.dumps({ 'jsonrpc': '2.0', 'id': 1, 'method': method, 'params': params }) response = self.request( # request method is not needed, it's included in body request_method=None, body=body, headers=self._headers) if response.get('error'): raise APIError(response['error']) return response
def get_txs(self, offset=0, limit=10, unconfirmed=False): body = json.dumps({ 'address': self.address, 'page': offset, 'row': limit }) response = self.request('get_txs', body=body, headers=self._headers, api_key=self.api_key) if response['code'] == 0: transfers = (self._parse_tx(t) for t in response['data']['transfers']) return [t for t in transfers if unconfirmed or t['confirmed']] elif response['code'] == 10004: raise AddressNotExist() else: raise APIError(response['message'])
def get_rewards(self, offset=0, limit=20): body = json.dumps({ 'address': self.address, 'row': limit, 'page': offset }) response = self.request( 'get_rewards', body=body, headers=self._headers, api_key=self.api_key ) if response['code'] == 0: return [self._parse_reward(r) for r in (response['data']['list'] if response['data']['list'] is not None else [])] elif response['code'] == 10004: raise AddressNotExist() else: raise APIError(response['message'])
def get_balance(self): body = json.dumps({ 'key': self.address, 'row': 1, 'page': 0, }) response = self.request( 'get_balance', body=body, headers=self._headers, api_key=self.api_key ) if response['code'] == 0: return [{ 'symbol': self.symbol, 'amount': to_decimal(response['data']['account']['balance']) }] elif response['code'] == 10004: raise AddressNotExist() else: raise APIError(response['message'])
def _process_error(self, msg): if msg == 'Invalid Cardano address!': raise AddressNotExist() else: raise APIError(msg)