flask and other bits and bobs
This commit is contained in:
parent
5e7ab5a09d
commit
fc16d4340d
|
|
@ -1,6 +1,7 @@
|
||||||
__pycache__/
|
__pycache__/
|
||||||
libraries/__pycache__/*
|
libraries/__pycache__/*
|
||||||
libraries/wrappers/__pycache__/*
|
libraries/wrappers/__pycache__/*
|
||||||
|
keys/api_credentials.db
|
||||||
*.pyc
|
*.pyc
|
||||||
*.pyo
|
*.pyo
|
||||||
credentials.py
|
credentials.py
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
import uuid
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
|
||||||
|
users = ["user1", "user2", "user3", "user4"]
|
||||||
|
keys = []
|
||||||
|
|
||||||
|
for user in users:
|
||||||
|
keys.append([user,str(uuid.uuid4())])
|
||||||
|
|
||||||
|
|
||||||
|
database_connection = sqlite3.connect("api_credentials.db")
|
||||||
|
database_cursor = database_connection.cursor()
|
||||||
|
database_cursor.execute("CREATE TABLE IF NOT EXISTS credentials_table (user TEXT, key TEXT)")
|
||||||
|
|
||||||
|
for pair in keys:
|
||||||
|
database_cursor.execute('INSERT INTO credentials_table VALUES(?, ?)', pair)
|
||||||
|
database_connection.commit()
|
||||||
|
database_connection.close()
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
valid_keys = []
|
||||||
|
|
||||||
|
database_connection = sqlite3.connect("api_credentials.db")
|
||||||
|
database_cursor = database_connection.cursor()
|
||||||
|
|
||||||
|
database_cursor.execute("SELECT * FROM credentials_table")
|
||||||
|
data = database_cursor.fetchall()
|
||||||
|
|
||||||
|
|
||||||
|
for line in data:
|
||||||
|
valid_keys.append(line[1])
|
||||||
|
|
||||||
|
print(valid_keys)
|
||||||
|
|
@ -19,6 +19,8 @@ class earner:
|
||||||
self.trading_balance = 0
|
self.trading_balance = 0
|
||||||
self.earning_balance = 0
|
self.earning_balance = 0
|
||||||
|
|
||||||
|
self.is_paused = True
|
||||||
|
|
||||||
self.status_string = ""
|
self.status_string = ""
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -26,6 +28,11 @@ class earner:
|
||||||
return str(self.connector)
|
return str(self.connector)
|
||||||
|
|
||||||
|
|
||||||
|
def toggle_pause(self):
|
||||||
|
self.is_paused = not self.is_paused
|
||||||
|
return self.is_paused
|
||||||
|
|
||||||
|
|
||||||
def get_last_subscription(self):
|
def get_last_subscription(self):
|
||||||
return {str(self.connector): self.last_subscription_time}
|
return {str(self.connector): self.last_subscription_time}
|
||||||
|
|
||||||
|
|
@ -88,14 +95,13 @@ class earner:
|
||||||
else:
|
else:
|
||||||
self.earning_balance = None
|
self.earning_balance = None
|
||||||
|
|
||||||
#Call balance_accounts
|
|
||||||
|
if not self.is_paused:
|
||||||
target_trading_amount, target_earning_amount = balance_accounts(float(self.trading_balance),
|
target_trading_amount, target_earning_amount = balance_accounts(float(self.trading_balance),
|
||||||
float(self.earning_balance),
|
float(self.earning_balance),
|
||||||
self.minimum_amount_in_trading_account,
|
self.minimum_amount_in_trading_account,
|
||||||
self.step_size,
|
self.step_size,
|
||||||
self.percentage)
|
self.percentage)
|
||||||
|
|
||||||
#Check difference
|
|
||||||
earning_delta = 0
|
earning_delta = 0
|
||||||
if self.earning_balance is None:
|
if self.earning_balance is None:
|
||||||
print(f"{str(self.connector)} - There was an error fetching earning balance")
|
print(f"{str(self.connector)} - There was an error fetching earning balance")
|
||||||
|
|
@ -106,12 +112,13 @@ class earner:
|
||||||
self.subscribe(earning_delta)
|
self.subscribe(earning_delta)
|
||||||
if earning_delta<-self.step_size and time.time()-self.last_redemption_time>self.time_between_redemptions:
|
if earning_delta<-self.step_size and time.time()-self.last_redemption_time>self.time_between_redemptions:
|
||||||
self.redeem(-earning_delta)
|
self.redeem(-earning_delta)
|
||||||
print(f"Difference: {earning_delta}")
|
print(f"{str(self.connector)} - Difference: {earning_delta}")
|
||||||
|
else:
|
||||||
|
paused_string = "| PAUSED - NOT SUBSCRIBING NOR REDEEMING"
|
||||||
|
|
||||||
#Output status to status_string
|
#Output status to status_string
|
||||||
balances_string = f"Trading balance: {float(self.trading_balance):.2f} {self.currency} | Earning balance: {float(self.earning_balance):.2f} {self.currency}"
|
balances_string = f"Trading balance: {float(self.trading_balance):.2f} {self.currency} | Earning balance: {float(self.earning_balance):.2f} {self.currency}"
|
||||||
percentages_string = f"On earn: {float(self.earning_balance)/float(self.trading_balance):.2%}"
|
percentages_string = f"On earn: {float(self.earning_balance)/float(self.trading_balance):.2%} {paused_string}"
|
||||||
|
|
||||||
self.status_string = f"{self} | {balances_string} | {percentages_string}"
|
self.status_string = f"{self} | {balances_string} | {percentages_string}"
|
||||||
|
|
||||||
96
main.py
96
main.py
|
|
@ -5,9 +5,36 @@ from libraries.wrappers import earn_okx
|
||||||
from libraries.wrappers import earn_gateio
|
from libraries.wrappers import earn_gateio
|
||||||
from libraries.earner import earner
|
from libraries.earner import earner
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
from flask import Flask, jsonify, request
|
||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
|
import sqlite3
|
||||||
|
|
||||||
|
def load_keys_from_db(file_name: str) -> list:
|
||||||
|
'''
|
||||||
|
Load valid API keys
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
file_name : str
|
||||||
|
Name of the database file
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
valid_keys : list
|
||||||
|
List of valid API keys
|
||||||
|
'''
|
||||||
|
|
||||||
|
database_connection = sqlite3.connect(file_name)
|
||||||
|
database_cursor = database_connection.cursor()
|
||||||
|
database_cursor.execute("SELECT * FROM credentials_table")
|
||||||
|
data = database_cursor.fetchall()
|
||||||
|
database_connection.close()
|
||||||
|
|
||||||
|
valid_keys = [line[1] for line in data]
|
||||||
|
|
||||||
|
return valid_keys
|
||||||
|
|
||||||
|
|
||||||
def seconds_to_time(total_seconds: float) -> str:
|
def seconds_to_time(total_seconds: float) -> str:
|
||||||
|
|
@ -31,23 +58,7 @@ def seconds_to_time(total_seconds: float) -> str:
|
||||||
return f"{time_delta.days}:{hours:02d}:{minutes:02d}:{seconds:02d}"
|
return f"{time_delta.days}:{hours:02d}:{minutes:02d}:{seconds:02d}"
|
||||||
|
|
||||||
|
|
||||||
if __name__=="__main__":
|
def main():
|
||||||
|
|
||||||
version = "2025.01.06"
|
|
||||||
start_time = time.time()
|
|
||||||
|
|
||||||
with open("config.json") as f:
|
|
||||||
config = json.load(f)
|
|
||||||
|
|
||||||
connectors = {"binance": earn_binance.binance_earn(),
|
|
||||||
"gateio": earn_gateio.gateio_earn(),
|
|
||||||
"kucoin": earn_kucoin.kucoin_earn(),
|
|
||||||
"okx": earn_okx.okx_earn()}
|
|
||||||
earners = []
|
|
||||||
|
|
||||||
for item in config["exchanges"]:
|
|
||||||
earners.append(earner(connectors[item], config["exchanges"][item]))
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
threads = []
|
threads = []
|
||||||
|
|
||||||
|
|
@ -96,5 +107,56 @@ if __name__=="__main__":
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#########################
|
||||||
|
######### API ###########
|
||||||
|
#########################
|
||||||
|
|
||||||
|
earn_api = Flask(__name__)
|
||||||
|
|
||||||
|
@earn_api.route("/toggle_pause", methods=['POST'])
|
||||||
|
def return_global_status():
|
||||||
|
'''
|
||||||
|
GET request
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
broker: str
|
||||||
|
'''
|
||||||
|
|
||||||
|
if "X-API-KEY" in request.headers and request.headers.get("X-API-KEY") in valid_keys:
|
||||||
|
if request.json is None:
|
||||||
|
return jsonify({'Error': 'request.json is None'})
|
||||||
|
broker = request.json["broker"]
|
||||||
|
for item in earners:
|
||||||
|
if str(item.connector)==broker:
|
||||||
|
item.toggle_pause()
|
||||||
|
return jsonify({'Status': item.is_paused})
|
||||||
|
return jsonify({'Error': 'broker not found'})
|
||||||
|
return jsonify({'Error': 'API key invalid'}), 401
|
||||||
|
|
||||||
|
|
||||||
|
if __name__=="__main__":
|
||||||
|
|
||||||
|
version = "2025.01.07"
|
||||||
|
start_time = time.time()
|
||||||
|
|
||||||
|
with open("config.json") as f:
|
||||||
|
config = json.load(f)
|
||||||
|
|
||||||
|
connectors = {"binance": earn_binance.binance_earn(),
|
||||||
|
"gateio": earn_gateio.gateio_earn(),
|
||||||
|
"kucoin": earn_kucoin.kucoin_earn(),
|
||||||
|
"okx": earn_okx.okx_earn()}
|
||||||
|
earners = []
|
||||||
|
|
||||||
|
for item in config["exchanges"]:
|
||||||
|
earners.append(earner(connectors[item], config["exchanges"][item]))
|
||||||
|
|
||||||
|
#Load valid API keys
|
||||||
|
valid_keys = load_keys_from_db("keys/api_credentials.db")
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue