diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2fb95e3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,60 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ + +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +#misc bitcoin stuff +bitcoinrpc/* +jsonrpc/* +*.db +*.txt \ No newline at end of file diff --git a/app.py b/app.py index 81c2240..9ecb247 100644 --- a/app.py +++ b/app.py @@ -15,6 +15,7 @@ import json import time from config import config +from decimal import ExtendedContext, Decimal, getcontext """ ================= @@ -77,13 +78,13 @@ def withdraw(currency): if 'amount' not in request.form or 'address' not in request.form: return account_page(danger="Please enter an address and an amount!") #TODO: change this error try: - total = float(request.form['amount']) + total = string_to_currency_unit(request.form['amount'], config.get_multiplier(currency)) except: return account_page(danger="Invalid amount!") - if check_balance(currency,session['userid']) < int(total * config.get_multiplier(currency)) or total < 0: + if check_balance(currency,session['userid']) < total or total < 0: return account_page(danger="Balance too low to execute withdrawal!") #TODO: add valid address checking - adjustbalance(currency,session['userid'],-1 * int(total * config.get_multiplier(currency))) + adjustbalance(currency,session['userid'],-1 * total) co = CompletedOrder(currency + "_" + currency, "WITHDRAWAL", total, 0, session['userid'], is_withdrawal=True, withdrawal_address=request.form['address']) db_session.add(co) db_session.commit() @@ -180,6 +181,7 @@ def logout(): @app.route('/addorder',methods=['POST']) def addorder(): + """ Checks balance and essential stuff, generates an order ID then adds order to a redis queue. """ instrument = request.form['currency_pair'] if not is_logged_in(session): @@ -192,16 +194,20 @@ def addorder(): base_currency = request.form['currency_pair'].split("_")[0] quote_currency = request.form['currency_pair'].split("_")[1] try: - rprice = float(request.form['price']) - ramount = int(float(request.form['amount'])* config.get_multiplier(base_currency)) - except: + rprice = Decimal(request.form['price']) + ramount = string_to_currency_unit(request.form['amount'], config.get_multiplier(base_currency)) + print(ramount) + except Exception as e: + print(e) return home_page(instrument, danger="Please enter numerical values for price and amount!") if ramount < 1: #TODO: find a good amount for this return home_page(instrument, danger="Transaction amount too low!") if rprice <= 0: return home_page(instrument, danger="Price must be greater than 0!") - total = int(rprice * ramount) + getcontext().prec = 6 + whole, dec = ExtendedContext.divmod(Decimal(rprice), Decimal(1)) + total = whole * config.get_multiplier(base_currency) + dec * config.get_multiplier(base_currency) uid = session['userid'] orderid = generate_password_hash(str(random.random())) @@ -256,7 +262,7 @@ def balance_processor(): For example, in the template one can do {{ getbalance("btc", 48549) }}. The division is done because this is what the front-end user sees, and they do not want prices in satoshis or cents""" def getbalance(c, uid): - return check_balance(c, uid)/config.get_multiplier(c) + return "{:.4f}".format(check_balance(c, uid)/float(config.get_multiplier(c))) return dict(getbalance=getbalance) @app.route('/volume/') @@ -352,7 +358,7 @@ def openorders(uid): base_currency = c['instrument'][0:c['instrument'].find("_")] quote_currency = c['instrument'][c['instrument'].find("_")+1:] instrument = (base_currency+"/"+quote_currency).upper() - r.append([instrument, c['ordertype'], c['price'] + " " + instrument, str(int(c['amount'])/config.get_multiplier(base_currency)) + " " + base_currency.upper(), o]) + r.append([instrument, c['ordertype'], c['price'] + " " + instrument, str(float(c['amount'])/config.get_multiplier(base_currency)) + " " + base_currency.upper(), o]) return r def getvolume(instrument): @@ -390,6 +396,20 @@ def home_page(p, **kwargs): def account_page(**kwargs): return render_template('account.html', currencies=config.get_currencies(),openorders=openorders(session['userid']),**kwargs) +def string_to_currency_unit(s, prec): + print(s, prec) + if s.count('.') > 1: + return + if s.count('.') == 0: + return int(s) * prec + base, dec = s.split(".") + total = prec * int(base) + while prec > 1 and dec: + prec /= 10 + total += int(dec[0]) * prec + dec = dec[1:] + return total + if __name__ == '__main__': app.run(host='0.0.0.0') diff --git a/jsonrpc/__init__.pyc b/jsonrpc/__init__.pyc index 199da3e..36ec9fa 100644 Binary files a/jsonrpc/__init__.pyc and b/jsonrpc/__init__.pyc differ diff --git a/jsonrpc/json.pyc b/jsonrpc/json.pyc index d1c80ae..51d6804 100644 Binary files a/jsonrpc/json.pyc and b/jsonrpc/json.pyc differ diff --git a/jsonrpc/proxy.pyc b/jsonrpc/proxy.pyc index 48404b6..ddd6078 100644 Binary files a/jsonrpc/proxy.pyc and b/jsonrpc/proxy.pyc differ diff --git a/models.py b/models.py index 61c9409..aecc71a 100644 --- a/models.py +++ b/models.py @@ -1,4 +1,4 @@ -from sqlalchemy import Column, Integer, String, Float, ForeignKey, DateTime, Boolean +from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean from sqlalchemy.orm import relationship, backref from database import Base @@ -8,8 +8,8 @@ class User(Base): name = Column(String(50), unique=True) email = Column(String(120), unique=True) password = Column(String(120)) - btc_balance = Column(Float) - ltc_balance = Column(Float) + btc_balance = Column(Integer) + ltc_balance = Column(Integer) #orders = relationship("Order",backref="users") def __init__(self, name, email, password): self.name = name diff --git a/worker.py b/worker.py index 3db18c5..9a9f60e 100644 --- a/worker.py +++ b/worker.py @@ -162,6 +162,10 @@ def fill_order(): db_session.commit() -with daemon.DaemonContext(): - while True: - fill_order() +#with daemon.DaemonContext(): +# while True: +# fill_order() + +#For testing +while True: + fill_order() \ No newline at end of file