Get price from Uniswap V2 and rewrite to TypeScript
This commit is contained in:
parent
d0b91106fc
commit
554f049611
@ -1,2 +1,3 @@
|
|||||||
BOT_TOKEN=
|
BOT_TOKEN=
|
||||||
TELEGRAM_CHANNEL_NAME=@torn_prices
|
TELEGRAM_CHANNEL_NAME=@torn_prices
|
||||||
|
MAINNET_RPC_URL=https://ethereum.publicnode.com
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,2 @@
|
|||||||
.idea
|
node_modules
|
||||||
.env
|
.env
|
@ -5,4 +5,6 @@
|
|||||||
3. Copy `.env.example` file content to `.env`;
|
3. Copy `.env.example` file content to `.env`;
|
||||||
4. Fill in `.env` file `BOT_TOKEN` value with a string containing your telegram bot token;
|
4. Fill in `.env` file `BOT_TOKEN` value with a string containing your telegram bot token;
|
||||||
5. Replace in `.env` file `TELEGRAM_CHANNEL_NAME` value with a string containing inner telegram link to your public channel (for example, `@torn_prices`);
|
5. Replace in `.env` file `TELEGRAM_CHANNEL_NAME` value with a string containing inner telegram link to your public channel (for example, `@torn_prices`);
|
||||||
6. Start script in the background: `python3 main.py &`
|
6. Replace in `.env` file `MAINNET_RPC_URL` value, if default RPC doesn't work;
|
||||||
|
7. Install dependencies: `npm i`
|
||||||
|
8. Start script in the background: `npm run start`
|
||||||
|
41
index.ts
Normal file
41
index.ts
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
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();
|
42
main.py
42
main.py
@ -1,42 +0,0 @@
|
|||||||
from urllib.error import HTTPError
|
|
||||||
from urllib.request import urlopen as get
|
|
||||||
from threading import Timer
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
BINANCE_API_GET_ETH_PRICE_LINK = "https://api.binance.com/api/v3/ticker/price?symbol=TORNBUSD"
|
|
||||||
BOT_TOKEN = os.getenv("BOT_TOKEN")
|
|
||||||
TELEGRAM_CHANNEL_NAME = os.getenv("TELEGRAM_CHANNEL_NAME")
|
|
||||||
TELEGRAM_API_SEND_MESSAGE_LINK = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?chat_id={TELEGRAM_CHANNEL_NAME}&text="
|
|
||||||
CHECK_PRICE_INTERVAL_IN_SECONDS = 10
|
|
||||||
|
|
||||||
|
|
||||||
def send_price_to_channel(price: float):
|
|
||||||
fixed_price = "{:10.2f}".format(price).strip()
|
|
||||||
full_request_link = f'{TELEGRAM_API_SEND_MESSAGE_LINK}{fixed_price}$'
|
|
||||||
try:
|
|
||||||
get(full_request_link)
|
|
||||||
except HTTPError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def get_eth_price_from_binance() -> float:
|
|
||||||
try:
|
|
||||||
data = json.load(get(BINANCE_API_GET_ETH_PRICE_LINK))
|
|
||||||
return float(data["price"])
|
|
||||||
except HTTPError:
|
|
||||||
return -1
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
Timer(CHECK_PRICE_INTERVAL_IN_SECONDS, main).start()
|
|
||||||
current_eth_price = get_eth_price_from_binance()
|
|
||||||
if current_eth_price == -1:
|
|
||||||
return
|
|
||||||
send_price_to_channel(current_eth_price)
|
|
||||||
|
|
||||||
|
|
||||||
main()
|
|
2779
package-lock.json
generated
Normal file
2779
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
30
package.json
Normal file
30
package.json
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"name": "torn-price-bot",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Telegram bot with actual TORN price",
|
||||||
|
"main": "index.ts",
|
||||||
|
"scripts": {
|
||||||
|
"start": "ts-node index.ts &",
|
||||||
|
"dev": "nodemon index.ts"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://git.tornado.ws/Theo/torn-price-bot.git"
|
||||||
|
},
|
||||||
|
"author": "Theo",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@uniswap/sdk": "^3.0.3",
|
||||||
|
"@uniswap/sdk-core": "^4.0.9",
|
||||||
|
"@uniswap/v2-sdk": "^3.2.3",
|
||||||
|
"dotenv": "^16.3.1",
|
||||||
|
"ethers": "5.7",
|
||||||
|
"node-fetch": "^2.7.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node-fetch": "^2.6.9",
|
||||||
|
"nodemon": "^3.0.2",
|
||||||
|
"ts-node": "^10.9.2",
|
||||||
|
"typescript": "^5.3.3"
|
||||||
|
}
|
||||||
|
}
|
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
0
utils.ts
Normal file
0
utils.ts
Normal file
Loading…
Reference in New Issue
Block a user