Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion api/es_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
from services.bitshares_elasticsearch_client import es
from elasticsearch.exceptions import NotFoundError
from datetime import datetime, timedelta
from . import explorer
from services.cache import cache

def get_account_history(account_id=None, operation_type=None, from_=0, size=10,

def get_account_history(account_id=None, operation_type=None, from_=0, size=10,
from_date='2015-10-10', to_date='now', sort_by='-operation_id_num',
search_after=None, type='data', agg_field='operation_type'):
s = Search(using=es, index="bitshares-*")
Expand Down
68 changes: 34 additions & 34 deletions api/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from services.bitshares_websocket_client import client as bitshares_ws_client
from services.bitshares_elasticsearch_client import client as bitshares_es_client
from services.cache import cache
import es_wrapper
from . import es_wrapper
import config

def _bad_request(detail):
Expand Down Expand Up @@ -66,14 +66,14 @@ def _enrich_operation(operation, ws_client):

def get_operation(operation_id):
res = es_wrapper.get_single_operation(operation_id)
operation = {
operation = {
"op": res["operation_history"]["op_object"],
"op_type": res["operation_type"],
"block_num": res["block_data"]["block_num"],
"block_num": res["block_data"]["block_num"],
"op_in_trx": res["operation_history"]["op_in_trx"],
"result": json.loads(res["operation_history"]["operation_result"]),
"result": json.loads(res["operation_history"]["operation_result"]),
"trx_in_block": res["operation_history"]["trx_in_block"],
"virtual_op": res["operation_history"]["virtual_op"],
"virtual_op": res["operation_history"]["virtual_op"],
"block_time": res["block_data"]["block_time"],
"trx_id": res["block_data"]["trx_id"]
}
Expand All @@ -94,7 +94,7 @@ def get_full_account(account_id):
@cache.memoize()
def get_assets():
results = []

# Get all assets active the last 24h.
markets = bitshares_es_client.get_markets('now-1d', 'now', quote=config.CORE_ASSET_ID)
bts_volume = 0.0 # BTS volume is the sum of all the others.
Expand Down Expand Up @@ -136,7 +136,7 @@ def get_asset(asset_id):
asset = bitshares_ws_client.request('database', 'lookup_asset_symbols', [[asset_id], 0])[0]
else:
asset = bitshares_ws_client.request('database', 'get_assets', [[asset_id], 0])[0]

dynamic_asset_data = bitshares_ws_client.get_object(asset["dynamic_asset_data_id"])
asset["current_supply"] = dynamic_asset_data["current_supply"]
asset["confidential_supply"] = dynamic_asset_data["confidential_supply"]
Expand All @@ -152,7 +152,7 @@ def get_asset(asset_id):
@cache.memoize()
def get_asset_and_volume(asset_id):
asset = get_asset(asset_id)

core_symbol = _get_core_asset_name()

if asset['symbol'] != core_symbol:
Expand Down Expand Up @@ -286,8 +286,8 @@ def get_most_active_markets():
'24h_volume': m['volume'] / 10**quote_asset['precision'],
'quote_id': m['quote'],
'base_id': m['base']
})
})

return results


Expand All @@ -299,7 +299,7 @@ def _ensure_safe_limit(limit):
return limit

def get_order_book(base, quote, limit=False):
limit = _ensure_safe_limit(limit)
limit = _ensure_safe_limit(limit)
order_book = bitshares_ws_client.request('database', 'get_order_book', [base, quote, limit])
return order_book

Expand Down Expand Up @@ -387,7 +387,7 @@ def get_market_chart_data(base, quote):

def get_top_proxies():
holders = _get_holders()

total_votes = reduce(lambda acc, h: acc + int(h['balance']), holders, 0)

proxies = []
Expand Down Expand Up @@ -421,7 +421,7 @@ def _get_accounts_by_chunks_via_ws(account_ids, chunk_size=1000):
all_accounts.extend(accounts)
return all_accounts

# FIXME: Should not be needed anymore when https://github.com/bitshares/bitshares-core/issues/1652 will be resolved.
# FIXME: Should not be needed anymore when https://github.com/bitshares/bitshares-core/issues/1652 will be resolved.
def _load_missing_accounts_via_ws(account_ids, accounts_already_loaded):
accounts_ids_already_loaded = [ account['id'] for account in accounts_already_loaded ]
accounts_ids_to_load = list(set(account_ids) - set(accounts_ids_already_loaded))
Expand Down Expand Up @@ -457,14 +457,14 @@ def _get_holders():
'asset_type': config.CORE_ASSET_ID
}
holders_by_account_id[proxy_id] = proxy_without_balance
proxy = holders_by_account_id[proxy_id]
proxy = holders_by_account_id[proxy_id]
if 'follower_amount' not in proxy:
proxy['follower_amount'] = 0
proxy['follower_count'] = 0
proxy['follower_amount'] += int(holder['balance'])
proxy['follower_count'] += 1
proxy['follower_amount'] += int(holder['balance'])
proxy['follower_count'] += 1

return holders_by_account_id.values()
return holders_by_account_id.values()


def get_top_holders():
Expand All @@ -479,7 +479,7 @@ def get_top_holders():
'account_name': holder['owner']['name'],
'amount': int(holder['balance']),
'voting_account': _get_voting_account(holder)
})
})
return top_holders


Expand All @@ -499,10 +499,10 @@ def get_witnesses_votes():
vote_id = witness["vote_id"]
id_witness = witness["id"]
witness_account_name = witness["witness_account_name"]
proxy_votes = _get_formatted_proxy_votes(proxies, vote_id)
proxy_votes = _get_formatted_proxy_votes(proxies, vote_id)

witnesses_votes.append({
'witness_account_name': witness_account_name,
'witness_account_name': witness_account_name,
'witness_id': id_witness,
'top_proxy_votes': proxy_votes
})
Expand All @@ -524,11 +524,11 @@ def get_workers_votes():
id_worker = worker[0]["id"]
worker_account_name = worker[0]["worker_account_name"]
worker_name = worker[0]["name"]
proxy_votes = _get_formatted_proxy_votes(proxies, vote_id)
proxy_votes = _get_formatted_proxy_votes(proxies, vote_id)

workers_votes.append({
'worker_account_name': worker_account_name,
'worker_id': id_worker,
'worker_account_name': worker_account_name,
'worker_id': id_worker,
'worker_name': worker_name,
'top_proxy_votes': proxy_votes
})
Expand All @@ -549,10 +549,10 @@ def get_committee_votes():
vote_id = committee_member[0]["vote_id"]
id_committee = committee_member[0]["id"]
committee_account_name = committee_member[0]["committee_member_account_name"]
proxy_votes = _get_formatted_proxy_votes(proxies, vote_id)
proxy_votes = _get_formatted_proxy_votes(proxies, vote_id)

committee_votes.append({
'committee_account_name': committee_account_name,
committee_votes.append({
'committee_account_name': committee_account_name,
'committee_id': id_committee,
'top_proxy_votes': proxy_votes
})
Expand Down Expand Up @@ -641,11 +641,11 @@ def get_dex_total_volume():
market_cap += a['market_cap']

res = {
"volume_bts": round(volume),
"volume_usd": round(volume/usd_price) if usd_price != 0 else 'nan',
"volume_bts": round(volume),
"volume_usd": round(volume/usd_price) if usd_price != 0 else 'nan',
"volume_cny": round(volume/cny_price) if cny_price != 0 else 'nan',
"market_cap_bts": round(market_cap),
"market_cap_usd": round(market_cap/usd_price) if usd_price != 0 else 'nan',
"market_cap_bts": round(market_cap),
"market_cap_usd": round(market_cap/usd_price) if usd_price != 0 else 'nan',
"market_cap_cny": round(market_cap/cny_price) if cny_price != 0 else 'nan'
}

Expand All @@ -658,7 +658,7 @@ def get_daily_volume_dex_dates():
date_list = [d.strftime("%Y-%m-%d") for d in date_list]
return list(reversed(date_list))


@cache.memoize(86400) # 1d TTL
def get_daily_volume_dex_data():
daily_volumes = bitshares_es_client.get_daily_volume('now-60d', 'now')
Expand Down Expand Up @@ -696,11 +696,11 @@ def get_referrer_count(account_id):

def get_all_referrers(account_id, page=0):
account_id = _get_account_id(account_id)

page_size = 20
offset = int(page) * page_size
_, accounts = bitshares_es_client.get_accounts_with_referrer(account_id, size=page_size, from_=offset)

results = []
for account in accounts:
results.append({
Expand All @@ -716,7 +716,7 @@ def get_all_referrers(account_id, page=0):


def get_grouped_limit_orders(quote, base, group=10, limit=False):
limit = _ensure_safe_limit(limit)
limit = _ensure_safe_limit(limit)

base = _ensure_asset_id(base)
quote = _ensure_asset_id(quote)
Expand Down
10 changes: 5 additions & 5 deletions api/nomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from services.bitshares_websocket_client import client as bitshares_ws_client
from services.bitshares_elasticsearch_client import client as bitshares_es_client
from services.cache import cache
import api.explorer
import es_wrapper
from . import explorer
from . import es_wrapper
import config


Expand Down Expand Up @@ -54,8 +54,8 @@ def trades(market, since):
base = market_id[0]
quote = market_id[1]

base_asset = api.explorer._get_asset_id_and_precision(base)
quote_asset = api.explorer._get_asset_id_and_precision(quote)
base_asset = explorer._get_asset_id_and_precision(base)
quote_asset = explorer._get_asset_id_and_precision(quote)

trade_history = es_wrapper.get_trade_history(search_after=since, base=base_asset[0], quote=quote_asset[0], size=1000,
sort_by='operation_id_num')
Expand All @@ -82,7 +82,7 @@ def snapshot(market):
base = market_id[0]
quote = market_id[1]

order_book = api.explorer.get_order_book(base, quote, 100)
order_book = explorer.get_order_book(base, quote, 100)

bids = []
asks = []
Expand Down
30 changes: 30 additions & 0 deletions api/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from functools import wraps
import elasticsearch
from flask import abort


def index_exists(es, index_name):
if not es.indices.exists(index=index_name):
raise Exception("Index does not exist")


def needs_es(index_name=None):

def inner_function(function):
@wraps(function)
def wrapper(*args, **kwargs):
if index_name is not None:
# TODO pre-check, might not be necessary
pass
try:
return function(*args, **kwargs)
except elasticsearch.exceptions.NotFoundError as e:
not_found = getattr(e, "info", {}).get("error", {}).get("root_cause", [{}])[0].get("resource.id", None)
message = "The required index does not exist in this ElasticSearch database"
if not_found is not None:
message = message + " (" + str(not_found) + ")"
print(message)
abort(404, message)
return wrapper

return inner_function
Loading