Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nomics implementation #64

Merged
merged 7 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions api/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,17 @@ def lookup_assets(start):
asset_names = bitshares_es_client.get_asset_names(start)
return [ [ asset_name ] for asset_name in asset_names ]


def get_last_block_number():
dynamic_global_properties = bitshares_ws_client.request('database', 'get_dynamic_global_properties', [])
return dynamic_global_properties["head_block_number"]


def get_last_block_time():
dynamic_global_properties = bitshares_ws_client.request('database', 'get_dynamic_global_properties', [])
return dynamic_global_properties["time"]


def get_account_history(account_id, page, search_after):
account_id = _get_account_id(account_id)

Expand Down
125 changes: 125 additions & 0 deletions api/nomics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import datetime
import calendar
import connexion

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
import config


def info():
return {
"name": "Bitshares",
"description": "An exchange description of at least 1000 characters in plain text (no html)",
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
"location": "Anywhere",
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
"logo": "https://example.com/exchange-logo.png",
"website": "https://bitshares.org/",
"twitter": "example",
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
"capability": {
"markets": True,
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
"trades": True,
"tradesSocket": False,
"orders": False,
"ordersSocket": False,
"ordersSnapshot": True,
"candles": False
}
}


def markets():
result = []
top_markets = bitshares_ws_client.request('database', 'get_top_markets', [100])
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved

for market in top_markets:
result.append({
'id': market["base"] + "_" + market["quote"],
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
'base': market["base"],
'quote': market["quote"]
})
return result


@cache.memoize()
def trades(market, since):
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved

market_id = market.split('_')
base = market_id[0]
quote = market_id[1]

now = datetime.datetime.now()
start = now
stop = now - datetime.timedelta(days=3)

results = []

_trades = bitshares_ws_client.request('database', 'get_trade_history',
[base, quote, start.strftime("%Y-%m-%dT%H:%M:%S"),
stop.strftime("%Y-%m-%dT%H:%M:%S"), 100])

for trade in _trades:
results.append({
"id": trade["sequence"],
"timestamp": trade["date"],
"price": trade["price"],
"amount": trade["amount"],
"order": "",
"type": "",
"side": "",
"raw": ""
})

while len(_trades) == 100:
start_seq = _trades[99]["sequence"]
_trades = bitshares_ws_client.request('database', 'get_trade_history_by_sequence',
[base, quote, start_seq, stop.strftime("%Y-%m-%dT%H:%M:%S"), 100])
for trade in _trades:
results.append({
"id": trade["sequence"],
"timestamp": trade["date"],
"price": trade["price"],
"amount": trade["amount"],
"order": "",
"type": "",
"side": "",
"raw": ""
})

if not since:
return list(reversed(results))[0:100]
else:
new_results = []
for r in results:
if int(r["id"]) > int(since):
new_results.append(r)
return list(reversed(new_results))[0:100]


def snapshot(market):
result = {}

market_id = market.split('_')
base = market_id[0]
quote = market_id[1]

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

bids = []
asks = []

for bid in order_book["bids"]:
bids.append([bid["price"], bid["base"]])

result["bids"] = bids

for ask in order_book["asks"]:
asks.append([ask["price"], ask["base"]])

result["asks"] = asks

result["timestamp"] = api.explorer.get_last_block_time()
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved

return result
12 changes: 12 additions & 0 deletions swagger/paths_explorer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,18 @@ paths:
tags:
- api
- operation
"/last_block_time":
get:
description: Get current block time
operationId: api.explorer.get_last_block_time
responses:
'200':
description: last known block time
'500':
description: Error processing parameters
tags:
- api
- operation
"/dex_total_volume":
get:
description: Get some total dex volume figures
Expand Down
65 changes: 65 additions & 0 deletions swagger/paths_nomics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
paths:
"/nomics/info":
get:
description: Returns information about the exchange as a whole, and is used by Nomics to display information about Bitshares to users.
operationId: api.nomics.info
responses:
'200':
description: JSON object containing excchange info
'500':
description: Error processing parameters
tags:
- nomics
"/nomics/markets":
get:
description: Returns a list of the top markets on the Bitshares exchange.
operationId: api.nomics.markets
responses:
'200':
description: Top markets
'500':
description: Error processing parameters
tags:
- nomics
"/nomics/trades":
get:
description: Search for an asset(symbol)
operationId: api.nomics.trades
parameters:
- in: query
name: market
default: USD_BTS
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
type: string
required: true
description: Market
- in: query
name: since
default: ''
type: string
required: false
description: A trade ID from a previous /trades response. If none is provided, the oldest trades should be returned
responses:
'200':
description: Found assets
'500':
description: Error processing parameters
tags:
- nomics
"/nomics/orders/snapshot":
get:
description: The /orders/snapshot endpoint returns the current order book for a given market. It allows Nomics to get a simple snapshot of open orders.
operationId: api.nomics.snapshot
parameters:
- in: query
name: market
default: USD_CNY
oxarbitrage marked this conversation as resolved.
Show resolved Hide resolved
type: string
required: true
description: Market
responses:
'200':
description: History in selected period.
'500':
description: Error processing parameters
tags:
- nomics