-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathqueryEngine.ts
More file actions
404 lines (382 loc) · 12.1 KB
/
Copy pathqueryEngine.ts
File metadata and controls
404 lines (382 loc) · 12.1 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import { Semaphore } from 'async-mutex'
import nano from 'nano'
import { config } from './config'
import { pagination } from './dbutils'
import { banxa } from './partners/banxa'
import { bitaccess } from './partners/bitaccess'
import { bitrefill } from './partners/bitrefill'
import { bitsofgold } from './partners/bitsofgold'
import { bity } from './partners/bity'
import { changehero } from './partners/changehero'
import { changelly } from './partners/changelly'
import { changenow } from './partners/changenow'
import { exolix } from './partners/exolix'
import { foxExchange } from './partners/foxExchange'
import { gebo } from './partners/gebo'
import { godex } from './partners/godex'
import { ioniaGiftCards } from './partners/ioniagiftcard'
import { ioniaVisaRewards } from './partners/ioniavisarewards'
import { kado } from './partners/kado'
import { letsexchange } from './partners/letsexchange'
import { libertyx } from './partners/libertyx'
import { lifi } from './partners/lifi'
import { moonpay } from './partners/moonpay'
import { paybis } from './partners/paybis'
import { paytrie } from './partners/paytrie'
import { rango } from './partners/rango'
import { safello } from './partners/safello'
import { sideshift } from './partners/sideshift'
import { simplex } from './partners/simplex'
import { swapuz } from './partners/swapuz'
import { switchain } from './partners/switchain'
import { maya, thorchain } from './partners/thorchain'
import { transak } from './partners/transak'
import { wyre } from './partners/wyre'
import { xanpool } from './partners/xanpool'
import {
asApp,
asApps,
asDisablePartnerQuery,
asProgressSettings,
DbTx,
DisablePartnerQuery,
ScopedLog,
StandardTx
} from './types'
import {
createScopedLog,
datelog,
promiseTimeout,
standardizeNames
} from './util'
const nanoDb = nano(config.couchDbFullpath)
const plugins = [
banxa,
bitaccess,
bitsofgold,
bity,
bitrefill,
changelly,
changenow,
changehero,
exolix,
foxExchange,
gebo,
godex,
ioniaVisaRewards,
ioniaGiftCards,
kado,
letsexchange,
libertyx,
lifi,
maya,
moonpay,
paybis,
paytrie,
rango,
safello,
sideshift,
simplex,
swapuz,
switchain,
thorchain,
transak,
wyre,
xanpool
]
const QUERY_FREQ_MS = 60 * 1000
const MAX_CONCURRENT_QUERIES = 3
const BULK_FETCH_SIZE = 500
const snooze: Function = async (ms: number) =>
await new Promise((resolve: Function) => setTimeout(resolve, ms))
const dbProgress = nanoDb.db.use('reports_progresscache')
const dbApps = nanoDb.db.use('reports_apps')
const dbSettings: nano.DocumentScope<unknown> = nanoDb.db.use(
'reports_settings'
)
export async function queryEngine(): Promise<void> {
while (true) {
datelog('Starting query loop...')
let disablePartnerQuery: DisablePartnerQuery = {
plugins: {},
appPartners: {}
}
try {
const disablePartnerQueryDoc = await dbSettings.get('disablePartnerQuery')
if (disablePartnerQueryDoc != null) {
disablePartnerQuery = asDisablePartnerQuery(disablePartnerQueryDoc)
}
} catch (e) {
datelog('Error getting disablePartnerQuery', e)
}
// get the contents of all reports_apps docs
const query = {
selector: {
appId: { $exists: true }
},
limit: 1000000
}
const rawApps = await dbApps.find(query)
const apps = asApps(rawApps.docs)
// loop over every app
for (const app of apps) {
const semaphore = new Semaphore(MAX_CONCURRENT_QUERIES)
if (config.soloAppIds != null && !config.soloAppIds.includes(app.appId)) {
continue
}
let partnerStatus: string[] = []
const runPlugins: RunPluginParams[] = []
let remainingPlugins: RunPluginParams[] = []
// loop over every pluginId that app uses
for (const partnerId in app.partnerIds) {
const pluginId = app.partnerIds[partnerId].pluginId ?? partnerId
if (config.soloPartnerIds?.includes(partnerId) !== true) {
if (disablePartnerQuery.plugins[pluginId]) {
continue
}
const appPartnerId = `${app.appId}_${partnerId}`
if (disablePartnerQuery.appPartners[appPartnerId]) {
continue
}
if (
config.soloPartnerIds != null &&
!config.soloPartnerIds.includes(partnerId)
) {
continue
}
}
const runPluginParams: RunPluginParams = { app, partnerId, pluginId }
runPlugins.push(runPluginParams)
remainingPlugins.push(runPluginParams)
}
const promises: Array<Promise<void>> = []
for (const runPluginParams of runPlugins) {
await semaphore.acquire()
const promise = runPlugin(runPluginParams)
.then(status => {
partnerStatus = [...partnerStatus, status]
})
.finally(() => {
semaphore.release()
// remove the plugin from the remaining plugins
remainingPlugins = remainingPlugins.filter(
plugin => plugin !== runPluginParams
)
if (remainingPlugins.length > 0) {
datelog(
`REMAINING PLUGINS for ${app.appId}:`,
remainingPlugins.map(plugin => plugin.partnerId).join(', ')
)
}
})
promises.push(promise)
}
await Promise.all(promises)
datelog(partnerStatus.join('\n'))
}
datelog(`Snoozing for ${QUERY_FREQ_MS} milliseconds`)
await snooze(QUERY_FREQ_MS)
}
}
const checkUpdateTx = (oldTx: StandardTx, newTx: StandardTx): string[] => {
const fields = [
'status',
'depositTxid',
'depositChainPluginId',
'depositEvmChainId',
'depositTokenId',
'payoutTxid',
'payoutChainPluginId',
'payoutEvmChainId',
'payoutTokenId'
] as const
const changedFields: string[] = []
for (const field of fields) {
if (oldTx[field] !== newTx[field]) changedFields.push(field)
}
return changedFields
}
const filterAddNewTxs = async (
pluginId: string,
dbTransactions: nano.DocumentScope<StandardTx>,
docIds: string[],
transactions: StandardTx[],
log: ScopedLog
): Promise<void> => {
if (docIds.length < 1 || transactions.length < 1) return
const queryResults = await dbTransactions.fetch(
{ keys: docIds },
{ include_docs: true }
)
const newDocs: DbTx[] = []
for (const docId of docIds) {
const queryResult = queryResults.rows.find(
doc => 'id' in doc && doc.id === docId && doc.doc != null
)
const orderId = docId.split(':')[1] ?? ''
const tx = transactions.find(tx => tx.orderId === orderId)
if (tx == null) {
throw new Error(`Cant find tx from docId ${docId}`)
}
if (
queryResult == null ||
!('doc' in queryResult) ||
queryResult.doc == null
) {
// Get the full transaction
const newObj = { _id: docId, _rev: undefined, ...tx }
// replace all fields with non-standard names
newObj.depositCurrency = standardizeNames(newObj.depositCurrency)
newObj.payoutCurrency = standardizeNames(newObj.payoutCurrency)
log(`[filterAddNewTxs] new doc id: ${newObj._id}`)
newDocs.push(newObj)
} else {
const changedFields = checkUpdateTx(queryResult.doc, tx)
if (changedFields.length > 0) {
const oldStatus = queryResult.doc?.status
const newStatus = tx.status
const newObj = { _id: docId, _rev: queryResult.doc?._rev, ...tx }
newDocs.push(newObj)
log(
`[filterAddNewTxs] updated doc id: ${
newObj._id
} ${oldStatus} -> ${newStatus} [${changedFields.join(', ')}]`
)
}
}
}
try {
await promiseTimeout(
'pagination',
pagination(newDocs, dbTransactions, log),
log
)
} catch (e) {
log.error('[filterAddNewTxs] Error doing bulk transaction insert', e)
throw e
}
}
async function insertTransactions(
transactions: StandardTx[],
pluginId: string,
log: ScopedLog
): Promise<any> {
const dbTransactions: nano.DocumentScope<StandardTx> = nanoDb.db.use(
'reports_transactions'
)
let docIds: string[] = []
let startIndex = 0
for (let i = 0; i < transactions.length; i++) {
const transaction = transactions[i]
transaction.orderId = transaction.orderId.toLowerCase()
const key = `${pluginId}:${transaction.orderId}`
docIds.push(key)
// Collect a batch of docIds
if (docIds.length < BULK_FETCH_SIZE) continue
log(`[insertTransactions] ${startIndex} to ${i} of ${transactions.length}`)
await filterAddNewTxs(pluginId, dbTransactions, docIds, transactions, log)
docIds = []
startIndex = i + 1
}
await filterAddNewTxs(pluginId, dbTransactions, docIds, transactions, log)
}
interface RunPluginParams {
app: ReturnType<typeof asApp>
partnerId: string
pluginId: string
}
async function runPlugin(params: RunPluginParams): Promise<string> {
const { app, partnerId, pluginId } = params
const start = Date.now()
const log = createScopedLog(app.appId, partnerId)
let errorText = ''
try {
// obtains function that corresponds to current pluginId
const plugin = plugins.find(plugin => plugin.pluginId === pluginId)
// if current plugin is not within the list of partners skip to next
if (plugin === undefined) {
errorText = `[runPlugin] ${partnerId} Missing or disabled plugin`
log(errorText)
return errorText
}
// get progress cache to see where previous query ended
log(`[runPlugin] Starting with plugin:${pluginId}`)
const progressCacheFileName = `${app.appId.toLowerCase()}:${partnerId}`
const out = await dbProgress.get(progressCacheFileName).catch(e => {
if (e.error != null && e.error === 'not_found') {
log(`[runPlugin] Previous Progress Record Not Found`)
return {}
} else {
log.error('[runPlugin] Error fetching progress', e)
}
})
// initialize progress settings if unrecognized format
let progressSettings: ReturnType<typeof asProgressSettings>
try {
progressSettings = asProgressSettings(out)
} catch (e) {
progressSettings = {
progressCache: {},
_id: undefined,
_rev: undefined
}
}
// set apiKeys and settings for use in partner's function
const { apiKeys } = app.partnerIds[partnerId]
const settings = progressSettings.progressCache
log(`[runPlugin] Querying`)
// run the plugin function
const result = await promiseTimeout(
'queryFunc',
plugin.queryFunc({
apiKeys,
settings,
log
}),
log
)
log(`[runPlugin] Successful query`)
await promiseTimeout(
'insertTransactions',
insertTransactions(result.transactions, `${app.appId}_${partnerId}`, log),
log
).catch(e => {
throw new Error(`Error inserting transactions: ${String(e)}`)
})
progressSettings.progressCache = result.settings
progressSettings._id = progressCacheFileName
// Attempt to insert progress with retry on conflict
const maxAttempts = 2
let attempt = 0
while (attempt < maxAttempts) {
attempt++
try {
await promiseTimeout(
`dbProgress.insert (attempt ${attempt})`,
dbProgress.insert(progressSettings),
log
)
break
} catch (e) {
const err: any = e
const isConflict = err.statusCode === 409 || err.error === 'conflict'
if (isConflict && attempt < maxAttempts) {
log(`[runPlugin] Document conflict detected, re-reading and retrying`)
const updatedDoc = await dbProgress.get(progressCacheFileName)
progressSettings._rev = updatedDoc._rev
continue
}
throw new Error(`Error inserting progress: ${String(e)}`)
}
}
// Returning a successful completion message
const completionTime = (Date.now() - start) / 1000
const successfulCompletionMessage = `[runPlugin] ${partnerId} Successful update in ${completionTime} seconds.`
log(successfulCompletionMessage)
return successfulCompletionMessage
} catch (e) {
errorText = `[runPlugin] ${partnerId} Error: ${String(e)}`
log.error(errorText)
return errorText
}
}