-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataSync.ts
106 lines (92 loc) · 3.56 KB
/
dataSync.ts
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { db, bot } from '../../tools/index'
import { User } from '../../classes'
import { okxAxios } from "../API/index"
import { Markup } from 'telegraf'
import packageJson from '../../../web/package.json'
const updateCurrency = async () => {
const { data } = (await okxAxios.get('/api/v5/asset/currencies')).data
for (const cur of data) {
await db.Coin.findOneAndUpdate(
{ ccy: cur.ccy },
{ ...cur },
{ upsert: true, new: true }
).lean()
}
}
const searchPair = async () => {
const { data } = (await okxAxios.get('/api/v5/public/instruments', { params: { instType: 'SPOT' } })).data
for (const item of data) {
if (item.quoteCcy !== 'USDT')
continue
try {
const { _id: baseCcy } = await db.Coin.findOne({ ccy: item.baseCcy }, { _id: 1 })
const { _id: quoteCcy } = await db.Coin.findOne({ ccy: item.quoteCcy }, { _id: 1 })
await db.Tickers.findOneAndUpdate(
{ instId: item.instId },
{
...item,
baseCcy,
quoteCcy
},
{ upsert: true, new: true }
).lean()
}
catch {
continue
}
}
}
const updateData = async () => {
const { data } = (await okxAxios.get('/api/v5/market/tickers', { params: { instType: 'SPOT' } })).data
for (const ticker of data)
try {
const { last } = await db.Tickers.findOneAndUpdate(
{ instId: ticker.instId },
{ ...ticker },
{ projection: { last: 1 }, returnDocument: 'before' }
).lean()
for await (const user of db.Users.find(
{ 'subscriptions.instId': ticker.instId },
{
subscriptions: 1,
id: 1,
language_code: 1
}
).cursor().addCursorFlag('noCursorTimeout', true)) {
const u = new User(user.id, user.language_code)
for (const sub of user?.subscriptions?.filter(e => e.instId === ticker.instId)) {
const priceCrossed = (last <= sub.price && sub.price <= ticker.last) || (last >= sub.price && sub.price >= ticker.last)
if (priceCrossed && (sub.trend === 'any' || (sub.trend === 'up' && last <= ticker.last) || (sub.trend === 'down' && last >= ticker.last))) {
console.log('Notification triggered', sub, last, ticker)
bot.telegram.sendMessage(
user.id,
`<b>${sub.instId}</b> ${last < ticker.last ? '📈' : '📉'} <b>${u.t('current')}:</b> ${ticker.last}`,
{
parse_mode: 'HTML',
...Markup.inlineKeyboard([
[Markup.button.webApp(u.t('openMiniApp'), `${packageJson.homepage}/#/ccy/${sub.instId}`)],
])
}
).catch(console.error)
}
}
}
}
catch {
continue
}
}
const updater = async (period) => {
console.log('Updating the database...')
try {
await updateCurrency()
await searchPair()
await updateData()
}
catch (e) {
console.error(e.message)
}
console.log(`Completed, sleeping ${period / 1000}s...`)
setTimeout(updater, period, period)
}
export default updater