def main(): #Define our connection string conn_string = "host='localhost' dbname='{0}' user='******' password='******'".format(Config.SQLALCHEMY_DATABASE_URI.split("///")[1], Config.APIWALL_DB_USER, Config.APIWALL_DB_USER_PASSWORD) # get a connection, if a connect cannot be made an exception will be raised here conn = psycopg2.connect(conn_string) # conn.cursor will return a cursor object, you can use this cursor to perform queries cursor = conn.cursor() rpc = RPC(url="http://{0}:{1}@localhost:{2}".format(Config.RPC_USER, Config.RPC_PASSWORD, Config.RPC_PORT)) tx_json = rpc.call("gettransaction", txid) #We want to check and see if the transaction already exists in the DB before inserting cursor.execute("""SELECT * from BLOCKCHAIN_TRANSACTIONS WHERE blkchtx_id = '{0}'""".format(txid)) rows = cursor.fetchall() if len(rows) > 0: return address = tx_json.get("details")[0].get("address") cursor.execute("""SELECT * from invoices WHERE payment_address = '{0}'""".format(address)) rows = cursor.fetchall() #Insert new transaction cursor.execute("INSERT INTO BLOCKCHAIN_TRANSACTIONS (BLKCHTX_JSON, INVOICE_ID, BLKCHTX_ID) VALUES ('{0}', '{1}', '{2}')".format(json.dumps(tx_json), rows[0][0], txid)); conn.commit()
def account_new_transaction(accountid): account = Accounts.query.filter_by(account_id = accountid).first() if account: if request.method == 'POST': #check to see if its valid json first try: data = json.loads(request.data) except ValueError: return json.dumps({"error":"bad json"}), 400 if 'password' in data: if 'invoice_value' in data: if 'currency_code' in data: if account.check_password(data.get("password")): rpc = RPC(url="http://{0}:{1}@localhost:{2}".format(Config.RPC_USER, Config.RPC_PASSWORD, Config.RPC_PORT)) address = rpc.call("getnewaddress") invoice = Invoices() invoice.payment_address = address invoice.account = account invoice.currency_code = data.get('currency_code') invoice.ip_address = request.remote_addr invoice.invoice_value = data.get("invoice_value") db.session.add(invoice) db.session.commit() return newaddress_schema.jsonify(invoice) else: return json.dumps({"error":"bad password"}), 400 else: return json.dumps({"error":"missing currency_code"}), 400 return json.dumps({"error":"missing invoice_value"}), 400 else: return json.dumps({"error":"missing password"}), 400 return json.dumps({"error":"you need to post here"}), 400 else: return json.dumps({"error":"invalid account"}), 400