'Making A Trading Bot' Part 5- Learning to Code

in Learn to Code6 months ago

This is what I'm calling app.py
I'll try and post the tree.

import logging
from flask import Flask, render_template, jsonify
from beem import Hive
from beem.account import Account

app = Flask(__name__)
logging.basicConfig(level=logging.DEBUG)

# Set up your Hive keys
hive_active_key = 'your_active_key'
hive_posting_key = 'your_posting_key'
hive = Hive(keys=[hive_active_key, hive_posting_key])

@app.route('/')
def index():
    try:
        app.logger.debug('Fetching account details')
        account = Account('peakecoin', blockchain_instance=hive)
        balance = account.get_token_balance('PEK')  # Adjusted to get token balance for PEK
        app.logger.debug(f'Account balance: {balance}')
        
        return render_template('index.html', balance=balance)
    except IndexError as e:
        app.logger.error(f'Index error: {str(e)}')
        return jsonify({"error": "Data not found"}), 500
    except ValueError as e:
        app.logger.error(f'Value error: {str(e)}')
        return jsonify({"error": "Invalid data"}), 500
    except Exception as e:
        app.logger.error(f'Unexpected error: {str(e)}')
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True)