-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from bitshares/nomics
Nomics implementation
- Loading branch information
Showing
5 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
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": "The BitShares Blockchain is an industrial-grade decentralized platform built for " | ||
"high-performance financial smart contracts mostly known for: its token factory," | ||
" a decentralized exchange as a built-in native dapp (known as the DEX) and its stablecoins" | ||
" or, as they are called in BitShares, Smartcoins. It represents the first decentralized" | ||
" autonomous community that lets its core token holders decide on its future direction and" | ||
" products by means of on-chain voting. It is also the first DPoS blockchain in existence and" | ||
" the first blockchain to implement stablecoins.", | ||
"location": "Worldwide", | ||
"logo": "https://bitshares.org/exchange-logo.png", | ||
"website": "https://bitshares.org/", | ||
"twitter": "https://twitter.com/bitshares", | ||
"capability": { | ||
"markets": True, | ||
"trades": True, | ||
"tradesSocket": False, | ||
"orders": False, | ||
"ordersSocket": False, | ||
"ordersSnapshot": True, | ||
"candles": False | ||
} | ||
} | ||
|
||
|
||
def markets(): | ||
result = [ | ||
{"id": "CNY-BTS", "base": "CNY", "quote": "BTS"}, | ||
{"id": "USD-BTS", "base": "USD", "quote": "BTS"}, | ||
{"id": "CNY-USD", "base": "CNY", "quote": "USD"}, | ||
{"id": "EUR-BTS", "base": "EUR", "quote": "BTS"}, | ||
{"id": "RUBLE-BTS", "base": "RUBLE", "quote": "BTS"}, | ||
{"id": "SILVER-BTS", "base": "SILVER", "quote": "BTS"}, | ||
{"id": "GOLD-BTS", "base": "GOLD", "quote": "BTS"} | ||
] | ||
|
||
return result | ||
|
||
|
||
@cache.memoize() | ||
def trades(market, since): | ||
|
||
market_id = market.split('-') | ||
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) | ||
|
||
trade_history = es_wrapper.get_trade_history(search_after=since, base=base_asset[0], quote=quote_asset[0], size=20, | ||
sort_by='operation_id_num') | ||
|
||
results = [] | ||
for trade in trade_history: | ||
base_amount = trade["operation_history"]["op_object"]["fill_price"]["base"]["amount"] | ||
quote_amount = trade["operation_history"]["op_object"]["fill_price"]["base"]["amount"] | ||
|
||
results.append({ | ||
"id": str(trade["operation_id_num"]), | ||
"timestamp": trade["block_data"]["block_time"], | ||
"price": str(float(float(base_amount)/int(base_asset[1]))/float(float(quote_amount)/int(quote_asset[1]))), | ||
"amount": str(trade["operation_history"]["op_object"]["receives"]["amount"]/quote_asset[1]) | ||
}) | ||
|
||
return results | ||
|
||
|
||
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([float(bid["price"]), float(bid["base"])]) | ||
|
||
result["bids"] = bids | ||
|
||
for ask in order_book["asks"]: | ||
asks.append([float(ask["price"]), float(ask["base"])]) | ||
|
||
result["asks"] = asks | ||
|
||
result["timestamp"] = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 | ||
type: string | ||
required: true | ||
description: Market | ||
responses: | ||
'200': | ||
description: History in selected period. | ||
'500': | ||
description: Error processing parameters | ||
tags: | ||
- nomics |