|
from flask import Flask, Response, request |
|
import requests |
|
|
|
app = Flask(__name__) |
|
|
|
@app.route('/') |
|
def index(): |
|
html_content = ''' |
|
<!DOCTYPE html> |
|
<html lang="ru"> |
|
<head> |
|
<meta charset="UTF-8"> |
|
<title>TON Connect</title> |
|
<script src="https://unpkg.com/@tonconnect/ui@latest/dist/tonconnect-ui.min.js"></script> |
|
<style> |
|
body { |
|
font-family: Arial, sans-serif; |
|
background-color: #111; |
|
color: white; |
|
display: flex; |
|
flex-direction: column; |
|
align-items: center; |
|
justify-content: center; |
|
height: 100vh; |
|
margin: 0; |
|
} |
|
h1 { |
|
margin-bottom: 20px; |
|
} |
|
#ton-connect-button { |
|
margin-top: 10px; |
|
} |
|
#status, #balance { |
|
margin-top: 15px; |
|
font-size: 16px; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<h1>Подключение к TON</h1> |
|
<div id="ton-connect-button"></div> |
|
<div id="status">Статус: не подключено</div> |
|
<div id="balance">Баланс: —</div> |
|
|
|
<script> |
|
const tonConnectUI = new TON_CONNECT_UI.TonConnectUI({ |
|
manifestUrl: 'https://huggingface.co/spaces/Aleksmorshen/MorshenGroup/resolve/main/tonconnect-manifest.json', |
|
buttonRootId: 'ton-connect-button' |
|
}); |
|
|
|
async function fetchBalance(address) { |
|
try { |
|
const res = await fetch(`/get_balance?address=${address}`); |
|
const data = await res.json(); |
|
if (data.balance !== undefined) { |
|
document.getElementById('balance').innerText = `Баланс: ${data.balance} TON`; |
|
} else { |
|
document.getElementById('balance').innerText = `Баланс: ошибка`; |
|
} |
|
} catch (e) { |
|
document.getElementById('balance').innerText = `Баланс: ошибка`; |
|
} |
|
} |
|
|
|
tonConnectUI.onStatusChange(wallet => { |
|
const statusDiv = document.getElementById('status'); |
|
if (wallet) { |
|
const address = wallet.account.address; |
|
statusDiv.innerText = `✅ Подключено: ${address}`; |
|
fetchBalance(address); |
|
} else { |
|
statusDiv.innerText = '❌ Не подключено'; |
|
document.getElementById('balance').innerText = `Баланс: —`; |
|
} |
|
}); |
|
</script> |
|
</body> |
|
</html> |
|
''' |
|
return Response(html_content, mimetype='text/html') |
|
|
|
|
|
@app.route('/get_balance') |
|
def get_balance(): |
|
address = request.args.get('address') |
|
if not address: |
|
return {'error': 'No address provided'}, 400 |
|
try: |
|
res = requests.get(f'https://tonapi.io/v2/accounts/{address}') |
|
data = res.json() |
|
raw_balance = int(data.get('balance', 0)) |
|
ton_balance = raw_balance / 1e9 |
|
return {'balance': round(ton_balance, 4)} |
|
except: |
|
return {'error': 'failed to fetch'}, 500 |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=7860) |