Last active
April 26, 2018 02:58
-
-
Save nubela/770b8e9804be9840e455804ef06b3355 to your computer and use it in GitHub Desktop.
Half-baked Python library for accessing NuMoney's API
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
import datetime | |
import json | |
import pyotp | |
import time | |
import requests | |
import action | |
from cfg import NUMONEY_OTP_SECRET, NUMONEY_API_ENDPOINT | |
from requests_http_signature import HTTPSignatureAuth | |
def _serialize_json_datetime(datetime_obj): | |
if datetime_obj is not None: | |
return time.mktime(datetime_obj.timetuple()) | |
return None | |
class NMCredential(): | |
def __init__(self, api_key, api_secret, api_url=None): | |
self.api_key = api_key | |
self.api_secret = api_secret | |
self.api_url = api_url if api_url is not None else NUMONEY_API_ENDPOINT | |
class OrderSide: | |
ASK = 'ASK' | |
SELL = 'ASK' | |
BID = 'BID' | |
BUY = 'BID' | |
class OrderType: | |
GOOD_TIL_CANCELLED = 'NORMAL' | |
IMMEDIATE_OR_CANCEL = 'IMMEDIATE_OR_CANCEL' | |
def query(query, nm_credential, op_name=None, variables=None): | |
dict = { | |
'operationName': op_name, | |
'query': query, | |
'variables': variables, | |
} | |
body = json.dumps(dict) | |
headers = {'x-timestamp': str(int(_serialize_json_datetime(datetime.datetime.now()) * 1000)), | |
'x-original-length': str(len(body.encode("utf-8"))), | |
'accept': 'application/json', | |
'content-type': 'application/json', | |
} | |
ordered_headers = ( | |
'(request-target)', | |
'host', | |
'accept', | |
'content-type', | |
'x-original-length', | |
'x-timestamp', | |
'digest' | |
) | |
req = requests.Request('POST', nm_credential.api_url, headers=headers, json=dict, auth=HTTPSignatureAuth(key=nm_credential.api_secret, | |
key_id=nm_credential.api_key, | |
headers=ordered_headers | |
)) | |
prepared = req.prepare() | |
s = requests.Session() | |
return s.send(prepared) | |
def list_markets(nm_credential): | |
query_str = """ | |
query GetMarkets { | |
markets { | |
id | |
baseCurrency { | |
...currency | |
} | |
marketCurrency { | |
...currency | |
} | |
} | |
} | |
fragment currency on Currency { | |
name | |
symbol | |
} | |
""" | |
return query(query_str, nm_credential, op_name='GetMarkets') | |
def place_order(amount, price, market_id, order_side, order_type, nm_credential, mutation_id=None): | |
if mutation_id is None: mutation_id = action.generate_uuid() | |
query_str = """ | |
mutation PlaceOrder($input: PlaceOrderInput!) { | |
placeOrder(input: $input) { | |
order { | |
id | |
} | |
} | |
} | |
""" | |
vars = { | |
'input': { | |
'amount': str(amount), | |
'market': str(market_id), | |
'price': str(price), | |
'side': order_side, | |
'type': order_type, | |
'clientMutationId': mutation_id, | |
} | |
} | |
return query(query_str, nm_credential, op_name='PlaceOrder', variables=vars) | |
def order_progress(order_id, nm_credential): | |
query_str = """ | |
query GetOrder($id: ID!) { | |
node(id: $id) { | |
... on Order { | |
baseCurrencyValue | |
marketCurrencyValue | |
} | |
} | |
} | |
""" | |
vars = { | |
'id': order_id, | |
} | |
return query(query_str, nm_credential, op_name='GetOrder', variables=vars) | |
def withdraw(amount, to_address, currency_id, nm_credential, client_mutation_id): | |
totp = pyotp.TOTP(NUMONEY_OTP_SECRET) | |
if client_mutation_id is None: client_mutation_id = action.generate_uuid() | |
query_str = """ | |
mutation Withdraw($input: WithdrawInput!) { | |
withdraw(input: $input) { | |
clientMutationId | |
transaction { | |
id | |
} | |
} | |
} | |
""" | |
vars = { | |
'input': { | |
"_otp": str(totp.now()), | |
"amount": str(amount), | |
"clientMutationId": client_mutation_id, | |
'currency': currency_id, | |
"extraData": None, | |
"toAddress": to_address, | |
} | |
} | |
return query(query_str, nm_credential, op_name='Withdraw', variables=vars) | |
def cancel_order(order_id, nm_credential): | |
query_str = """ | |
mutation CancelOrder($input: CancelOrderInput!) { | |
cancelOrder(input: $input) { | |
order { | |
amountFilled | |
} | |
} | |
}""" | |
vars = { | |
'input': { | |
"id": order_id, | |
} | |
} | |
return query(query_str, nm_credential, op_name='CancelOrder', variables=vars) | |
def wallet_balance(nm_credential): | |
query_str = ''' | |
{ | |
user: me { | |
wallets { | |
balance | |
currency { | |
symbol | |
id | |
} | |
} | |
} | |
} | |
''' | |
return query(query_str, nm_credential) | |
def open_orders(base_currency, market_currency, nm_credential): | |
query_str = """ | |
query LoadMarket($baseCurrency: String!, $marketCurrency: String!) { | |
market: marketForPair(baseCurrency: $baseCurrency, marketCurrency: $marketCurrency) { | |
orders(includeClosed: false, last: 100) { | |
edges { | |
node { | |
id | |
side | |
amount | |
price | |
state | |
baseCurrencyValue | |
marketCurrencyValue | |
} | |
} | |
} | |
} | |
} | |
""" | |
dic = { | |
"baseCurrency": base_currency, | |
"marketCurrency": market_currency, | |
} | |
return query(query_str, nm_credential, variables=dic) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment