Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
from flask import Flask, Response
|
|
|
|
| 2 |
|
| 3 |
app = Flask(__name__)
|
| 4 |
|
|
@@ -29,12 +30,17 @@ def index():
|
|
| 29 |
#ton-connect-button {
|
| 30 |
margin-top: 10px;
|
| 31 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
</style>
|
| 33 |
</head>
|
| 34 |
<body>
|
| 35 |
<h1>Подключение к TON</h1>
|
| 36 |
<div id="ton-connect-button"></div>
|
| 37 |
<div id="status">Статус: не подключено</div>
|
|
|
|
| 38 |
|
| 39 |
<script>
|
| 40 |
const tonConnectUI = new TON_CONNECT_UI.TonConnectUI({
|
|
@@ -42,12 +48,29 @@ def index():
|
|
| 42 |
buttonRootId: 'ton-connect-button'
|
| 43 |
});
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
tonConnectUI.onStatusChange(wallet => {
|
| 46 |
const statusDiv = document.getElementById('status');
|
| 47 |
if (wallet) {
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
} else {
|
| 50 |
statusDiv.innerText = '❌ Не подключено';
|
|
|
|
| 51 |
}
|
| 52 |
});
|
| 53 |
</script>
|
|
@@ -56,5 +79,20 @@ def index():
|
|
| 56 |
'''
|
| 57 |
return Response(html_content, mimetype='text/html')
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if __name__ == '__main__':
|
| 60 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, Response, request
|
| 2 |
+
import requests
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
|
|
|
| 30 |
#ton-connect-button {
|
| 31 |
margin-top: 10px;
|
| 32 |
}
|
| 33 |
+
#status, #balance {
|
| 34 |
+
margin-top: 15px;
|
| 35 |
+
font-size: 16px;
|
| 36 |
+
}
|
| 37 |
</style>
|
| 38 |
</head>
|
| 39 |
<body>
|
| 40 |
<h1>Подключение к TON</h1>
|
| 41 |
<div id="ton-connect-button"></div>
|
| 42 |
<div id="status">Статус: не подключено</div>
|
| 43 |
+
<div id="balance">Баланс: —</div>
|
| 44 |
|
| 45 |
<script>
|
| 46 |
const tonConnectUI = new TON_CONNECT_UI.TonConnectUI({
|
|
|
|
| 48 |
buttonRootId: 'ton-connect-button'
|
| 49 |
});
|
| 50 |
|
| 51 |
+
async function fetchBalance(address) {
|
| 52 |
+
try {
|
| 53 |
+
const res = await fetch(`/get_balance?address=${address}`);
|
| 54 |
+
const data = await res.json();
|
| 55 |
+
if (data.balance !== undefined) {
|
| 56 |
+
document.getElementById('balance').innerText = `Баланс: ${data.balance} TON`;
|
| 57 |
+
} else {
|
| 58 |
+
document.getElementById('balance').innerText = `Баланс: ошибка`;
|
| 59 |
+
}
|
| 60 |
+
} catch (e) {
|
| 61 |
+
document.getElementById('balance').innerText = `Баланс: ошибка`;
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
tonConnectUI.onStatusChange(wallet => {
|
| 66 |
const statusDiv = document.getElementById('status');
|
| 67 |
if (wallet) {
|
| 68 |
+
const address = wallet.account.address;
|
| 69 |
+
statusDiv.innerText = `✅ Подключено: ${address}`;
|
| 70 |
+
fetchBalance(address);
|
| 71 |
} else {
|
| 72 |
statusDiv.innerText = '❌ Не подключено';
|
| 73 |
+
document.getElementById('balance').innerText = `Баланс: —`;
|
| 74 |
}
|
| 75 |
});
|
| 76 |
</script>
|
|
|
|
| 79 |
'''
|
| 80 |
return Response(html_content, mimetype='text/html')
|
| 81 |
|
| 82 |
+
|
| 83 |
+
@app.route('/get_balance')
|
| 84 |
+
def get_balance():
|
| 85 |
+
address = request.args.get('address')
|
| 86 |
+
if not address:
|
| 87 |
+
return {'error': 'No address provided'}, 400
|
| 88 |
+
try:
|
| 89 |
+
res = requests.get(f'https://tonapi.io/v2/accounts/{address}')
|
| 90 |
+
data = res.json()
|
| 91 |
+
raw_balance = int(data.get('balance', 0))
|
| 92 |
+
ton_balance = raw_balance / 1e9
|
| 93 |
+
return {'balance': round(ton_balance, 4)}
|
| 94 |
+
except:
|
| 95 |
+
return {'error': 'failed to fetch'}, 500
|
| 96 |
+
|
| 97 |
if __name__ == '__main__':
|
| 98 |
app.run(host='0.0.0.0', port=7860)
|