-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (64 loc) · 1.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const dotenv = require("dotenv");
const { Telegraf } = require("telegraf");
const axios = require("axios");
dotenv.config();
const defaultDescription = process.env.DESCRIPTION.replace(/\\n/g, "\n");
const channelId = process.env.CHANNEL_ID;
const USDTAddress = "EQBynBO23ywHy_CgarY9NK9FTz0yDsG82PtcbSTQgGoXwiuA";
const PTONAddress = "EQCM3B12QK1e4yZSf8GtBRT0aLMNyEsBc_DhVfRRtOEffLez";
const AEAddress = "EQCHwHSBnJYmyefutCuvKJMjFh9dCoCF4SDHVr2wEDmOwJzp";
const bot = new Telegraf(process.env.BOT_TOKEN);
async function main() {
setInterval(async () => {
updateDescription();
}, 120000);
updateDescription();
}
main();
async function updateDescription() {
try {
const { data: tonAe } = await axios.post(
`https://api.ston.fi/v1/swap/simulate`,
{},
{
params: {
offer_address: AEAddress,
ask_address: PTONAddress,
units: 1000000000,
slippage_tolerance: 0.1,
},
}
);
const { data: tonUsdt } = await axios.post(
`https://api.ston.fi/v1/swap/simulate`,
{},
{
params: {
offer_address: USDTAddress,
ask_address: PTONAddress,
units: 1000000000,
slippage_tolerance: 0.1,
},
}
);
const aeTonPrice = tonAe.swap_rate;
const usdTonPrice = tonUsdt.swap_rate;
const aeUsdPrice = (1 / parseFloat(usdTonPrice)) * parseFloat(aeTonPrice);
const newDescription = `AE Jetton ≈ ${formatNumber(
aeTonPrice
)}💎 (${formatNumber(aeUsdPrice, 2)}$)
\n${defaultDescription}`;
await bot.telegram.setChatDescription(channelId, newDescription);
console.log("updated description", new Date());
} catch (e) {
if (e?.message?.includes("Bad Request: chat description is not modified")) {
return;
}
console.error(e);
}
}
function formatNumber(n, digits = 2) {
const num = parseFloat(n);
const digitsCount = Math.abs(Math.floor(Math.log10(n))); // 0.02 -> -2
return num.toFixed(digitsCount + digits);
}