webhook in webui

This commit is contained in:
Dobromir Popov
2024-10-27 22:59:38 +02:00
parent e56f007082
commit 800cbede4d
5 changed files with 55 additions and 101 deletions

View File

@ -4,7 +4,7 @@ from flask_login import LoginManager, UserMixin, login_user, login_required, log
import secrets
from modules import storage, utils, SolanaAPI
import os
import logging
def init_app():
app = Flask(__name__, template_folder='../templates', static_folder='../static')
@ -31,6 +31,54 @@ def init_app():
login_manager = LoginManager()
login_manager.init_app(app)
logger = logging.getLogger(__name__)
# API
@app.route('/tr/<wallet>/<tx_signature>', methods=['GET', 'POST'])
async def transaction_notified(wallet, tx_signature):
try:
logger.info(f"Processing transaction notification for wallet: {wallet}, tx: {tx_signature}")
request_data = request.get_json()
if not request_data:
# Process the transaction
# tr = await get_swap_transaction_details(tx_signature)
tr = await SolanaAPI.SAPI.get_transaction_details_info(tx_signature, [])
else:
tr = request_data
# ToDo - probably optimize
tr['symbol_in'] = await SolanaAPI.SAPI.get_token_metadata_symbol(tr['token_in'])
tr['symbol_out'] = await SolanaAPI.SAPI.get_token_metadata_symbol(tr['token_out'])
notification = (
f"<b>Got TXN notification:</b>: {tr['amount_in']} {tr['symbol_in']} swapped for {tr['symbol_out']} \n"
)
logging.info(notification)
await utils.telegram_utils.send_telegram_message(notification)
# Store the notified transaction in the database
storage.store_transaction(tr)
# Attempt to execute the copytrade transaction
try:
await SolanaAPI.SAPI.follow_move(tr)
# Store the successful copytrade transaction
storage.store_copytrade_transaction(tr, success=True)
except Exception as e:
# Store the failed copytrade transaction
storage.store_copytrade_transaction(tr, success=False, error=str(e))
logging.error(f"Copytrade transaction failed: {e}")
# ToDo - probably optimize
await SolanaAPI.SAPI.save_token_info()
return jsonify(tr), 200
except Exception as e:
logging.error(f"Error processing transaction: {e}")
return jsonify({"error": "Failed to process transaction"}), 500
@app.route('/login/google/authorized')
def authorized():
# resp = google.authorized_response()
@ -135,33 +183,6 @@ def init_app():
return jsonify({"error": "Failed to process log"}), 500
#const webhookPath = `/tr/${followedWallet.toBase58()}/${logs.signature}`;
@app.route('/tr/<wallet>/<tx_signature>', methods=['GET', 'POST'])
async def transaction_notified(wallet, tx_signature):
try:
utils.log.info(f"Processing transaction notification for wallet: {wallet}, tx: {tx_signature}")
# Process the transaction
# tr = await get_swap_transaction_details(tx_signature)
tr = await get_transaction_details_info(tx_signature, [])
# ToDo - probably optimize
await get_token_metadata_symbol(tr['token_in'])
await get_token_metadata_symbol(tr['token_out'])
await follow_move(tr)
await save_token_info()
return jsonify(tr), 200
except Exception as e:
utils.log.error(f"Error processing transaction: {e}")
return jsonify({"error": "Failed to process transaction"}), 500
return app
# Function to find the latest log file