def log(): buy_transactions = c.search('txns', {'buy_user_email': g.user_email}) sell_transactions = c.search('txns', {'sell_user_email': g.user_email}) buy_entries = [dict(row) for row in buy_transactions] sell_entries = [dict(row) for row in sell_transactions] return render_template('log.html', buy_entries=buy_entries, sell_entries=sell_entries)
def process_orders(): users = c.search('users', {}) user_emails = [] for user in users: user_emails.append(user['email']) for user_email in user_emails: x = c.begin_transaction() pending_orders = c.search('orders', {'user_email' : user_email, 'is_complete' : 0}) for order in pending_orders: order_book.process_order(order['order_id'], x) x.commit() flash(u'Finished reprocessing orders') return redirect(url_for('admin_home'))
def admin_user_manager(): users = c.search('users', {}) user_emails = [] i = 0 for user in users: user_emails.append((str(i), user['email'])) i = i + 1 if 'email' in request.form: email_id = request.form['email'] if email_id.isdigit(): email_id = user_emails[int(request.form['email'])][1] form = AdminUserManageForm() if request.method == 'POST': if not form.validate_on_submit(): user_details = c.get('users', str(email_id)) form = AdminUserManageForm(email=str(email_id), name=str(user_details['name']), Bitcoin=user_details['Bitcoin'], Dogecoin=user_details['Dogecoin'], funds=user_details['funds']) form.email.data = str(email_id) form.name.data = str(user_details['name']) form.bitcoin_address.data = str(user_details['bitcoin_address']) form.dogecoin_address.data = str(user_details['dogecoin_address']) return render_template('admin_user_manage.html', form=form) added_funds = float(request.form['funds']) bitcoins = float(request.form['Bitcoin']) dogecoins = float(request.form['Dogecoin']) name = request.form['name'] name = unicodedata.normalize('NFKD', name).encode('ascii','ignore') c.atomic_add('users', str(email_id), {'Bitcoin' : bitcoins, 'Dogecoin' : dogecoins, 'funds' : added_funds}) c.put('users', str(email_id), {'name' : str(name)}) flash(u'Edited details successfully') return render_template('admin_user_manage.html', form=form)
def funds(): form = AddFundsForm() if request.method == 'POST': # 1. if not form.validate_on_submit(): flash(u'Invalid input') return render_template('add_funds.html', form=form) # form is validated now process add funds added_funds = float(request.form['funds']) bitcoins = float(request.form['Bitcoin']) dogecoins = float(request.form['Dogecoin']) c.atomic_add('users', g.user_email, {'Bitcoin' : bitcoins, 'Dogecoin' : dogecoins, 'funds' : added_funds}) # 3. # When a user adds funds we need to reprocess his orders as there may # be some orders he has placed that were not executed due to # insufficient resources. # No transaction support for the search() function yet :( pending_orders = c.search('orders', {'user_email' : g.user_email, 'is_complete' : 0}) x = c.begin_transaction() for order in pending_orders: order_book.process_order(order['order_id'], x) x.commit() # 4. flash(u'Added funds successfully') return redirect(url_for('funds')) # 5. return render_template('add_funds.html', form=form)
def manage_dogecoin(): x = c.begin_transaction() user_data = x.get('users', g.user_email) pub_key = user_data['dogecoin_address'] form = SendDogecoinForm() if request.method == 'POST': if not form.validate_on_submit(): flash(u'Invalid Input') else: input_address = str(request.form['address']).strip() try: addr = CDogecoinAddress(input_address) amount = float(request.form['amount']) if amount == 0.0: raise user_balance = x.get('users', g.user_email)['Dogecoin'] if amount > user_balance: flash('Insufficient Funds') else: x.put('dogecoin_txns', datetime.now(), {'pub_key': input_address, 'amount': amount, 'txid': '', 'email': g.user_email}) #x.put('users', g.user_email, {'Dogecoin': (user_balance - amount)}) x.atomic_sub('users', g.user_email, {'Dogecoin': amount}) flash(u'Your transaction for %s to %s is pending' % (amount, input_address)) except Exception as inst: print inst error_string = "Couldn't process send. " if type(inst) == dogecoin.base58.Base58ChecksumError: error_string += "Invalid Address!" elif type(inst) == dogecoin.rpc.JSONRPCException: error_string += "Insufficient Funds!" flash(u'%s' % (error_string)) if user_data['bitcoin_address'] == '': return "<div class='code'>We are still assigning you an address.<br/> Contact [email protected] if you continue to see this.</div>" # Fetch UTXO for the user's address confirmed = c.get('users', g.user_email)['Dogecoin'] pending = 0.0 addrs=pub_key response = requests.get('https://chain.so/api/v2/get_tx_unspent/DOGETEST/' + addrs) if response.status_code != 200: return "We're facing issues with our Dogecoin API, please try again in a bit. :(" content = response.json() txns = content['data']['txs'] for txn in txns: pending += float(str(txn['value'])) # Fetch transactions all_transactions = c.search('dogecoin_txns', {'email': g.user_email}) txns = [dict(row) for row in all_transactions] x.commit() return render_template('dogecoin.html', pub_key=pub_key, confirmed="%0.8f" % (confirmed), pending="%0.8f" % (pending), txns=txns, form=form)
def process_orders(): users = c.search('users', {}) user_emails = [] for user in users: user_emails.append(user['email']) for user_email in user_emails: x = c.begin_transaction() pending_orders = c.search('orders', { 'user_email': user_email, 'is_complete': 0 }) for order in pending_orders: order_book.process_order(order['order_id'], x) x.commit() flash(u'Finished reprocessing orders') return redirect(url_for('admin_home'))
def admin_user(): users = c.search('users', {}) user_emails = [] i = 0 for user in users: user_emails.append((str(i), user['email'])) i = i + 1 form = AdminUserForm() form.email.choices = user_emails return render_template('admin_user.html', form=form)
def home(): user_funds = c.get('users', g.user_email) funds_details = {} for currency in currency_dict: funds_details[currency[1]] = str(user_funds[currency[1]]) welcome_message = '' if g.username in ['None', None] else g.username welcome_message = 'Welcome ' + welcome_message if welcome_message is not '' else '' pending_orders_search = c.search('orders', {'user_email' : g.user_email, 'is_complete' : 0}) pending_orders = [dict(row) for row in pending_orders_search] return render_template('home.html', funds_details=funds_details, \ funds=str(user_funds['funds']), welcome_message=welcome_message, pending_orders=pending_orders)
def manage_bitcoin(): x = c.begin_transaction() user_data = x.get('users', g.user_email) pub_key = user_data['bitcoin_address'] form = SendBitcoinForm() if request.method == 'POST': if not form.validate_on_submit(): flash(u'Invalid Input') else: input_address = str(request.form['address']).strip() try: addr = CBitcoinAddress(input_address) amount = float(request.form['amount']) if amount == 0.0: raise user_balance = x.get('users', g.user_email)['Bitcoin'] if amount > user_balance: flash('Insufficient Funds') else: x.put('bitcoin_txns', datetime.now(), {'pub_key': input_address, 'amount': amount, 'txid': '', 'email': g.user_email}) #x.put('users', g.user_email, {'Bitcoin': (user_balance - amount)}) x.atomic_sub('users', g.user_email, {'Bitcoin': amount}) flash(u'Your transaction for %s to %s is pending' % (amount, input_address)) except Exception as inst: print inst error_string = "Couldn't process send. " if type(inst) == bitcoin.base58.Base58ChecksumError: error_string += "Invalid Address!" elif type(inst) == bitcoin.rpc.JSONRPCException: error_string += "Insufficient Funds!" flash(u'%s' % (error_string)) if user_data['bitcoin_address'] == '': return "<div class='code'>We are still assigning you an address.<br/> Contact [email protected] if you continue to see this.</div>" # Fetch UTXO for the user's address confirmed = c.get('users', g.user_email)['Bitcoin'] pending = 0.0 addr = [] addr.append(pub_key) txns = rpc.listunspent(addrs=addr) for txn in txns: pending += float(txn['amount'])/COIN # Fetch transactions all_transactions = c.search('bitcoin_txns', {'email': g.user_email}) txns = [dict(row) for row in all_transactions] x.commit() return render_template('bitcoin.html', pub_key=pub_key, confirmed="%0.8f" % (confirmed), pending="%0.8f" % (pending), txns=txns, form=form)
def admin_delete_user(): users = c.search('users', {}) user_emails = [] i = 0 for user in users: user_emails.append((str(i), user['email'])) i = i + 1 form = AdminUserForm() if request.method == 'POST': email_id = request.form['email'] email_id = user_emails[int(email_id)][1] c.delete('users', email_id) flash(u'User ' + email_id + ' deleted') return redirect(url_for('admin_user'))
def funds(): form = AddFundsForm() if request.method == 'POST': # 1. if not form.validate_on_submit(): flash(u'Invalid input') return render_template('add_funds.html', form=form) # form is validated now process add funds added_funds = float(request.form['funds']) bitcoins = float(request.form['Bitcoin']) dogecoins = float(request.form['Dogecoin']) c.atomic_add('users', g.user_email, { 'Bitcoin': bitcoins, 'Dogecoin': dogecoins, 'funds': added_funds }) # 3. # When a user adds funds we need to reprocess his orders as there may # be some orders he has placed that were not executed due to # insufficient resources. # No transaction support for the search() function yet :( pending_orders = c.search('orders', { 'user_email': g.user_email, 'is_complete': 0 }) x = c.begin_transaction() for order in pending_orders: order_book.process_order(order['order_id'], x) x.commit() # 4. flash(u'Added funds successfully') return redirect(url_for('funds')) # 5. return render_template('add_funds.html', form=form)
def book(): orders = c.search('orders', {'user_email' : g.user_email}) entries = [dict(row) for row in orders] for entry in entries: entry['order_id'] = url_safe_serializer.dumps(entry['order_id']) return render_template('book.html', entries=entries)
def log(): buy_transactions = c.search('txns', {'buy_user_email' : g.user_email}) sell_transactions = c.search('txns', {'sell_user_email' : g.user_email}) buy_entries = [dict(row) for row in buy_transactions] sell_entries = [dict(row) for row in sell_transactions] return render_template('log.html', buy_entries=buy_entries, sell_entries=sell_entries)
def manage_dogecoin(): x = c.begin_transaction() user_data = x.get('users', g.user_email) pub_key = user_data['dogecoin_address'] form = SendDogecoinForm() if request.method == 'POST': if not form.validate_on_submit(): flash(u'Invalid Input') else: input_address = str(request.form['address']).strip() try: addr = CDogecoinAddress(input_address) amount = float(request.form['amount']) if amount == 0.0: raise user_balance = x.get('users', g.user_email)['Dogecoin'] if amount > user_balance: flash('Insufficient Funds') else: x.put( 'dogecoin_txns', datetime.now(), { 'pub_key': input_address, 'amount': amount, 'txid': '', 'email': g.user_email }) #x.put('users', g.user_email, {'Dogecoin': (user_balance - amount)}) x.atomic_sub('users', g.user_email, {'Dogecoin': amount}) flash(u'Your transaction for %s to %s is pending' % (amount, input_address)) except Exception as inst: print inst error_string = "Couldn't process send. " if type(inst) == dogecoin.base58.Base58ChecksumError: error_string += "Invalid Address!" elif type(inst) == dogecoin.rpc.JSONRPCException: error_string += "Insufficient Funds!" flash(u'%s' % (error_string)) if user_data['bitcoin_address'] == '': return "<div class='code'>We are still assigning you an address.<br/> Contact [email protected] if you continue to see this.</div>" # Fetch UTXO for the user's address confirmed = c.get('users', g.user_email)['Dogecoin'] pending = 0.0 addrs = pub_key response = requests.get( 'https://chain.so/api/v2/get_tx_unspent/DOGETEST/' + addrs) if response.status_code != 200: return "We're facing issues with our Dogecoin API, please try again in a bit. :(" content = response.json() txns = content['data']['txs'] for txn in txns: pending += float(str(txn['value'])) # Fetch transactions all_transactions = c.search('dogecoin_txns', {'email': g.user_email}) txns = [dict(row) for row in all_transactions] x.commit() return render_template('dogecoin.html', pub_key=pub_key, confirmed="%0.8f" % (confirmed), pending="%0.8f" % (pending), txns=txns, form=form)
def manage_bitcoin(): x = c.begin_transaction() user_data = x.get('users', g.user_email) pub_key = user_data['bitcoin_address'] form = SendBitcoinForm() if request.method == 'POST': if not form.validate_on_submit(): flash(u'Invalid Input') else: input_address = str(request.form['address']).strip() try: addr = CBitcoinAddress(input_address) amount = float(request.form['amount']) if amount == 0.0: raise user_balance = x.get('users', g.user_email)['Bitcoin'] if amount > user_balance: flash('Insufficient Funds') else: x.put( 'bitcoin_txns', datetime.now(), { 'pub_key': input_address, 'amount': amount, 'txid': '', 'email': g.user_email }) #x.put('users', g.user_email, {'Bitcoin': (user_balance - amount)}) x.atomic_sub('users', g.user_email, {'Bitcoin': amount}) flash(u'Your transaction for %s to %s is pending' % (amount, input_address)) except Exception as inst: print inst error_string = "Couldn't process send. " if type(inst) == bitcoin.base58.Base58ChecksumError: error_string += "Invalid Address!" elif type(inst) == bitcoin.rpc.JSONRPCException: error_string += "Insufficient Funds!" flash(u'%s' % (error_string)) if user_data['bitcoin_address'] == '': return "<div class='code'>We are still assigning you an address.<br/> Contact [email protected] if you continue to see this.</div>" # Fetch UTXO for the user's address confirmed = c.get('users', g.user_email)['Bitcoin'] pending = 0.0 addr = [] addr.append(pub_key) txns = rpc.listunspent(addrs=addr) for txn in txns: pending += float(txn['amount']) / COIN # Fetch transactions all_transactions = c.search('bitcoin_txns', {'email': g.user_email}) txns = [dict(row) for row in all_transactions] x.commit() return render_template('bitcoin.html', pub_key=pub_key, confirmed="%0.8f" % (confirmed), pending="%0.8f" % (pending), txns=txns, form=form)
def book(): orders = c.search('orders', {'user_email': g.user_email}) entries = [dict(row) for row in orders] for entry in entries: entry['order_id'] = url_safe_serializer.dumps(entry['order_id']) return render_template('book.html', entries=entries)