torn-price-bot/index.ts

42 lines
1.4 KiB
TypeScript
Raw Normal View History

import { ChainId, Token, Fetcher, Route } from "@uniswap/sdk";
import { ethers } from "ethers";
import * as dotenv from "dotenv";
import fetch from "node-fetch";
dotenv.config();
const botToken = process.env.BOT_TOKEN;
const channelName = process.env.TELEGRAM_CHANNEL_NAME;
const TORN = new Token(ChainId.MAINNET, "0x77777FeDdddFfC19Ff86DB637967013e6C6A116C", 18);
const USDT = new Token(ChainId.MAINNET, "0xdAC17F958D2ee523a2206206994597C13D831ec7", 6);
const WETH = new Token(ChainId.MAINNET, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 18);
const provider = new ethers.providers.JsonRpcProvider(process.env.MAINNET_RPC_URL);
async function getPrice(): Promise<string> {
// Route via WETH, because TORN/USDT pair doesn't have enough liquidity
const pairTORNWETH = await Fetcher.fetchPairData(TORN, WETH, provider);
const pairWETHUSDT = await Fetcher.fetchPairData(WETH, USDT, provider);
const route = new Route([pairTORNWETH, pairWETHUSDT], TORN, USDT);
return route.midPrice.toSignificant(3);
}
async function sendPriceToChannel(price: string) {
try {
await fetch(`https://api.telegram.org/bot${botToken}/sendMessage?chat_id=${channelName}&text=${price}`);
} catch (error) {}
}
async function main() {
try {
const tokenPrice = await getPrice();
await sendPriceToChannel(tokenPrice);
} catch (error) {
console.error(error);
}
setTimeout(main, 1000 * 15);
}
main();