async def getAccountBlocks(viteAddress: str, hash: str, tokenTypeID: str, blocks: int): node_ip = get_node_ip() if node_ip == False: print('Connection ERROR...') return {'errorMsg': 'Connection error...'} url = get_url(node_ip) header = get_header() body = { "jsonrpc": "2.0", "id": 1, "method": "ledger_getAccountBlocks", "params": [viteAddress, hash, tokenTypeID, blocks] } try: response = requests.post(url=url, json=body, headers=header) if response.status_code == 200: resp = response.json() if not 'result' in resp: return {'errorMsg': 'No data received'} if len(resp['result']) == 0: return {'errorMsg': 'No token details received'} return resp['result'] else: return {'errorMsg': response.status_code} except Exception as e: print(e) return {'errorMsg': 'Exception during request.'}
async def get_token_info_by_id(tokenID: str): node_ip = get_node_ip() if node_ip == False: print('Connection ERROR...') return {'errorMsg': 'Connection error...'} url = get_url(node_ip) header = get_header() body = { "jsonrpc": "2.0", "id": 1, "method": "contract_getTokenInfoById", "params": tokenID } try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() if not 'result' in resp: return {'errorMsg': 'No data received'} if len(resp['result']) == 0: return {'errorMsg': 'No token details received'} return {'data': resp} else: return {'errorMsg': response.status_code} except Exception as e: print(e) return {'errorMsg': 'Exception during request.'}
async def get_quota(viteAddress: str): nodeIP = get_node_ip() if nodeIP == False: print('Connection ERROR...') raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail='Could not find a node') url = get_url(nodeIP) header = get_header() body = { "jsonrpc": "2.0", "id": 1, "method": "contract_getStakeList", "params": [viteAddress, 0, 100] } try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() if not 'result' in resp: raise Exception('No data received') return resp['result'] else: raise HTTPException(status_code=response.status_code) except Exception as e: print(e) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=e)
async def get_sbp_reward_by_timestamp(sbpName: str): node_ip = get_node_ip() if node_ip == False: print('Connection ERROR...') raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail='No nodes available.') url = get_url(node_ip) header = get_header() body = { "jsonrpc": "2.0", "id": 2, "method": "contract_getSBP", "params": [sbpName] } try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() if not 'result' in resp or len(resp['result']) == 0: raise HTTPException(status_code=status.HTTP_204_NO_CONTENT, detail='No data received') return resp else: raise HTTPException(status_code=response.status_code) except Exception as e: print(e) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
async def get_quota(viteAddress: str): nodeIP = get_node_ip() if nodeIP == False: print('Connection ERROR...') raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail='Could not find a node') url = get_url(nodeIP) header = get_header() body = { "jsonrpc": "2.0", "id": 1, "method": "contract_getQuotaByAccount", "params": [viteAddress] } try: response = requests.post(url=url, json=body, headers=header) response2 = requests.get( url= f'https://vitex.vite.net/reward/pledge/full/list?address={viteAddress}' ) account_info = await get_account_info(viteAddress) if response.status_code == status.HTTP_200_OK: resp = response.json() if not 'result' in resp: raise Exception('No data received') quota = resp['result']['maxQuota'] currentQuota = resp['result']['currentQuota'] stakeAmountQuota = resp['result']['stakeAmount'] vite_amount = account_info['balanceInfoMap'][ 'tti_5649544520544f4b454e6e40']['balance'] or 0 pledgedata = response2.json()['data'] pledge_infos = pledgedata[ 'fullNodePledgeInfos'] if pledgedata is not None else None stakedVite = sum( float(x['amount']) / 10**18 for x in pledge_infos) if pledge_infos is not None else 0 basic = { 'viteBalance': float(vite_amount) / 10**18, 'quota': int(quota) / 21000, 'maxQuota': quota, 'currentQuota': currentQuota, 'stakeAmountQuota': float(stakeAmountQuota) / 10**18, 'stakedVite': stakedVite } return basic else: raise HTTPException(status_code=response.status_code) except Exception as e: print(e) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=e)
async def get_vote_details_by_cycle( cycle: int, blockProducerNames: Optional[List[str]] = Query(None)): node_ip = get_node_ip() if node_ip == False: raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail='No nodes available.') url = get_url(node_ip) header = get_header() body = get_body(cycle) results = [] try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() for result in resp['result']: print(result) block_producer_name = result['blockProducerName'] if (blockProducerNames == None or block_producer_name in blockProducerNames): for key, value in result['addressVoteMap'].items(): data = { 'cycle': cycle, 'blockProducerName': block_producer_name, 'voteAddress': key, 'votes': value, 'votesDecimals': float(value) / 10**18 } results.append(data) except Exception as e: print(e) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail='Exception during request.') if len(results) > 0: generated_message = generate_return_file_msg(results) response = StreamingResponse(content=generated_message['file'], media_type='text/csv', headers={ 'Content-Disposition': 'filename=sbpVoteDetails_{cycle}.csv' }) return response else: raise HTTPException(status_code=status.HTTP_204_NO_CONTENT, detail='No transactions available')
async def get_token_info_list(node_ip: Optional[str] = None): if node_ip == None: node_ip = get_node_ip() if node_ip == False: print('Connection ERROR...') return {'errorMsg': 'Connection error...'} url = get_url(node_ip) header = get_header() body = { "jsonrpc": "2.0", "id": 1, "method": "contract_getTokenInfoList", "params": [0, 500] } try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() #print(resp) if not 'result' in resp: return {'errorMsg': 'No data received'} if len(resp['result']) == 0: return {'errorMsg': 'No token details received'} token_info_list = resp['result']['tokenInfoList'] requested_list = [] for element in token_info_list: requested_list.append({ "tokenName": element['tokenName'], "tokenSymbol": element['tokenSymbol'], "totalSupply": float(element['totalSupply']) / 10**element['decimals'], "decimals": element['decimals'], "owner": element['owner'], "tokenId": element['tokenId'], "maxSupply": element['maxSupply'], "ownerBurnOnly": element['ownerBurnOnly'], "isReIssuable": element['isReIssuable'], "index": element['index'], "isOwnerBurnOnly": element['isOwnerBurnOnly'] }) return requested_list else: return {'errorMsg': response.status_code} except Exception as e: print(e) return {'errorMsg': 'Exception during request.'}
async def load_net(ip: str, method: str): node_ip = ip url = get_url(node_ip) header = get_header() body = {"jsonrpc": "2.0", "id": 2, "method": method, "params": None} try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() if not 'result' in resp or len(resp['result']) == 0: raise HTTPException(status_code=status.HTTP_204_NO_CONTENT, detail='No data received') return resp else: raise HTTPException(status_code=response.status_code) except Exception as e: print(e) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
async def load_transaction(hash: str): node_ip = get_node_ip() if node_ip == False: raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail='No nodes available.') url = get_url(node_ip) header = get_header() body = {"jsonrpc": "2.0", "id": 2, "method": "ledger_getAccountBlockByHash", "params": [hash]} try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() if not 'result' in resp or len(resp['result']) == 0: raise HTTPException(status_code=status.HTTP_204_NO_CONTENT, detail='No data received') return resp else: raise HTTPException(status_code=response.status_code) except Exception as e: print(e) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
async def load_transactions(viteAddress: str, fromDate: Optional[date] = None, toDate: Optional[date] = None, viteAddressSender: Optional[List[str]] = Query(None)): node_ip = get_node_ip() if node_ip == False: raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail='No nodes available.') filter_token = [] # ['VITE', 'BAN'] url = get_url(node_ip) header = get_header() initial_page = 0 end_page = True transactions = [] transactions_per_request = 1000 page_max = 2 if (fromDate != None and toDate != None): toDate_plus_1 = toDate + timedelta(1) else: toDate_plus_1 = None print(viteAddressSender) while initial_page < page_max and end_page: body = get_body(viteAddress, initial_page, transactions_per_request) try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() #print(resp) if resp['result'] == None: print('Last requested page has no transaction results.') end_page = False break for result in resp['result']: if result['tokenInfo'] is not None and result['tokenInfo']['tokenSymbol'] not in filter_token: if (fromDate == None and toDate_plus_1 == None) or (fromDate <= date.fromtimestamp(result['timestamp']) and date.fromtimestamp(result['timestamp']) < toDate_plus_1): if (viteAddressSender is None) or (viteAddressSender is not None and result['fromAddress'] in viteAddressSender): toAddress = result['toAddress'] if toAddress == viteAddress: transaction_type = 'Recieved' transaction_multiplier = 1 else: transaction_type = 'Sent' transaction_multiplier = -1 amount = int(result['amount']) decimals = int(result['tokenInfo']['decimals']) decimal_amount = ( amount / 10**decimals) * transaction_multiplier memo = None if result['data'] is not None: memo_base64 = str(result['data']) memo = base64.b64decode(memo_base64).decode('utf-8',errors='ignore') dtobj = datetime.fromtimestamp( result['timestamp'], timezone.utc) transaction = { 'fromAddress': result['fromAddress'], 'toAddress': toAddress, 'transactionType': transaction_type, 'decimalAmount': decimal_amount, 'amount': str(amount), 'decimals': decimals, 'fee': result['fee'], 'tokenName': result['tokenInfo']['tokenName'], 'tokenSymbol': result['tokenInfo']['tokenSymbol'], 'datetime': result['timestamp'], 'dt': dtobj.strftime('%d/%m/%Y %H:%M:%S.%f')[:-3], 'txHash': result['hash'], 'memo': memo } transactions.append(transaction) elif (fromDate > date.fromtimestamp(result['timestamp'])): end_page = False break else: print(response.status_code) end_page = False break except Exception as e: print(e) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail='Exception during request.') initial_page = initial_page + 1 if len(transactions) > 0: generated_message = generate_return_file_msg(transactions) response = StreamingResponse(content=generated_message['file'], media_type='text/csv', headers={'Content-Disposition': 'filename=transactions_{viteAddress}.csv'}) return response else: raise HTTPException(status_code=status.HTTP_204_NO_CONTENT, detail='No transactions available')
async def get_account_info(viteAddress: str): nodeIP = get_node_ip() if nodeIP == False: print('Connection ERROR...') raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail='Could not find a node') url = get_url(nodeIP) header = get_header() body = { "jsonrpc": "2.0", "id": 2, "method": "ledger_getAccountInfoByAddress", "params": [viteAddress] } try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() if not 'result' in resp: raise Exception('No data received') balance_info_map = resp['result']['balanceInfoMap'].values() token_list = [] for each in balance_info_map: symbol = each['tokenInfo']['tokenSymbol'] decimals = each['tokenInfo']['decimals'] token_index = each['tokenInfo']['index'] amount = float(each['balance']) / 10**decimals if (symbol not in ['VITE', 'VX', 'VCP']): symbol = symbol + '-' + \ '{:03}'.format(token_index) if amount > 0: usd_value = 0 if (symbol != 'USDT'): try: value_request_json = requests.get( f'https://api.vitex.net/api/v2/exchange-rate?tokenSymbols={symbol}' ).json()['data'][0] usd_rate = value_request_json['usdRate'] usd_value = float(amount) * float(usd_rate) except: print('maybe illegal symbol') else: usd_value = float(amount) * 1 usd_value = round(usd_value, 2) available_token = { "symbol": symbol, "amount": amount, "usdt_value": usd_value } token_list.append(available_token) token_list.sort(key=lambda json: json['usdt_value'], reverse=True) return token_list else: raise HTTPException(status_code=response.status_code) except Exception as e: print(e) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=e)
async def get_sbp(node_ip: Optional[str] = None, sbp_details: Optional[bool] = True): if node_ip == None: node_ip = get_node_ip() dt = datetime.now(timezone.utc) utc_time = dt.replace(tzinfo=timezone.utc) utc_timestamp = utc_time.timestamp() last_cycle = int((utc_timestamp - 1558411200) / 24 / 3600) - 1 if node_ip == False: print('Connection ERROR...') raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail='No nodes available.') url = get_url(node_ip) header = get_header() body = { "jsonrpc": "2.0", "id": 2, "method": "contract_getSBPVoteList", "params": None } try: response = requests.post(url=url, json=body, headers=header) if response.status_code == status.HTTP_200_OK: resp = response.json() if not 'result' in resp or len(resp['result']) == 0: raise HTTPException(status_code=status.HTTP_204_NO_CONTENT, detail='No data received') if sbp_details: sbp_cycle = (await get_sbp_reward_by_cycle( last_cycle, node_ip))['result']['rewardMap'] modified_response = [] for e in resp['result']: name = e['sbpName'] node_in_cycle = sbp_cycle[name] sbp = SBPVotes.parse_obj({ 'sbp_name': name, 'block_producing_address': e['blockProducingAddress'], 'votes': e['votes'][:len(e['votes']) - 18], 'count': node_in_cycle['producedBlocks'], 'ratio': (float(node_in_cycle['producedBlocks']) / float(node_in_cycle['targetBlocks'])) * 100, 'block_creation_rewards': float(node_in_cycle['blockProducingReward']) / 10**18, 'rewards_of_votes': float(node_in_cycle['votingReward']) / 10**18, 'rewards_in_total': float(node_in_cycle['totalReward']) / 10**18, }) modified_response.append(sbp) return modified_response else: return resp['result'] else: raise HTTPException(status_code=response.status_code) except Exception as e: print(e) raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)