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

in Learn to Code8 months ago

peakebot/
├── app.py
├── templates/
│ └── index.html
├── venv/
│ ├── bin/
│ ├── include/
│ ├── lib/
│ └── ... (other virtual environment files and directories)
└── requirements.txt (optional, if you have one for dependencies)

app.py

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

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])

def get_peakecoin_balance(account_name):
    url = "https://api.hive-engine.com/rpc/contracts"
    headers = {"Content-type": "application/json"}
    data = {
        "jsonrpc": "2.0",
        "method": "findOne",
        "params": {
            "contract": "tokens",
            "table": "balances",
            "query": {
                "symbol": "PEK",
                "account": account_name
            }
        },
        "id": 1
    }

    response = requests.post(url, headers=headers, json=data)
    result = response.json()
    
    if "result" in result and result["result"]:
        return result["result"]["balance"]
    else:
        return None

@app.route('/')
def index():
    try:
        app.logger.debug('Fetching account details')
        balance = get_peakecoin_balance('peakecoin')
        
        if balance is None:
            raise ValueError("Token balance not found")
        
        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)

templates/index.html

Its basic currently once I actually get this code to work I'll update the front-end.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PeakeCoin Bot</title>
</head>
<body>
    <h1>PeakeCoin Balance</h1>
    <p>Balance: {{ balance }}</p>
</body>
</html>

requirements.txt


Flask
beem
python-dotenv
gunicorn

Sort:  

Curated by @arc7icwolf.byte for the #LearnToCode community.

I'm trying. It's a lot!!!