def api_transaction(): tx_id = validate_id(request.form.get("id")) if tx_id: try: tx = app.web3.eth.getTransaction(tx_id) if not tx: return error( "Invalid transaction, make sure the address is correct." ) tx = dict(tx) tx_receipt = dict(app.web3.eth.getTransactionReceipt(tx_id)) if tx_receipt: tx['txReceipt'] = True tx['gasUsed'] = tx_receipt['gasUsed'] tx['cumulativeGasUsed'] = tx_receipt['cumulativeGasUsed'] return json.dumps({**tx, **tx_receipt}) except ValueError as ve: return error("Invalid transaction address.") except Exception as e: print("EXCEPTION: " + str(e)) print(e) return error("Unknown error.") else: return error("Transaction id required.")
def api_send(): tx_data = validate_id(request.form.get("tx_data")) print(request.form.get("tx_data")) if tx_data: try: tx_id = app.web3.eth.sendRawTransaction(tx_data) return json.dumps({"tx_id": tx_id}) except ValueError as e: print(e.args) return error('Error: {}'.format(e.args[0]['message'])) else: return error("Malformed transaction, check private key.")
def api_question(): question = request.form.get("question") print(request.form.get("question")) if question: return json.dumps({"answer": "Done"}) else: return error("Error - no question detected.")
def api_balance(): account_id = validate_id(request.form.get("id")) if account_id.startswith('0x'): account_id = account_id[2:] balances_index = '1' if account_id: try: balance = get_map_value(app.web3, CONTRACT_ID, balances_index, account_id) return json.dumps({"id": account_id, "balance": balance}) except ValueError as ve: print(ve) return error("Invalid account or contract address.") except Exception as e: print(e) return error("Unknown error accessing contract.") else: return error("Account id required.")
def api_account(): account_id = validate_id(request.form.get("id")) if account_id: try: balance = app.web3.eth.getBalance(account_id) tx_count = app.web3.eth.getTransactionCount(account_id) print(app.web3.txpool.status) tx_pending = int(app.web3.txpool.status.pending, 16) return json.dumps({ "id": account_id, "balance": balance, "tx_count": tx_count, "tx_pending": tx_pending }) except ValueError as ve: return error("Invalid account address.") except Exception as e: print(e) return error("Unknown error accessing account.") else: return error("Account id required.")
def api_events(): scanner_id = request.form.get("scanner_id") if scanner_id: scanner = Magic8Scanner.query.get(scanner_id) if scanner: currentBlock = safe_blocknumber(app.web3) print("Unscanned blocks: " + (currentBlock - scanner.latest_block)) if currentBlock is None: return error("Can't connect to the Ethereum node.") if scanner.latest_block == currentBlock: events = app.db.session.query(Magic8Event).filter_by( scanner_id=scanner_id).all() else: events = scan_questions(app.db, app.web3, scanner.latest_block, currentBlock) return json.dumps([e.to_web() for e in events]) else: return error("Invalid scanner id.") else: return error("Missing scanner id.")
def api_answer(): scan_result = run_question_scan(app) tx_hash = request.form.get("tx_hash") if tx_hash: entry = app.db.session.query(Magic8Event).filter_by( event_id=tx_hash).order_by(Magic8Event.id.desc()).first() if entry: events = get_events(app.db) return json.dumps({ "pending": False, "result": entry.to_web(), "events": events }) else: return json.dumps({"pending": True}) else: return error("Invalid transaction.")