Skip to content

Commit 522bf0c

Browse files
committed
Optimize rates engine
- Round robin query all rates servers - Increase batch size and query frequency - Do not write unchanged docs
1 parent f6b8146 commit 522bf0c

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

src/ratesEngine.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ import { datelog, safeParseFloat, standardizeNames } from './util'
1515
import { isFiatCurrency } from './util/fiatCurrency'
1616

1717
const nanoDb = nano(config.couchDbFullpath)
18-
const QUERY_FREQ_MS = 3000
19-
const QUERY_LIMIT = 10
18+
const QUERY_FREQ_MS = 2000
19+
const QUERY_LIMIT = 20
20+
const RATES_SERVERS = [
21+
'https://rates1.edge.app',
22+
'https://rates2.edge.app',
23+
'https://rates3.edge.app',
24+
'https://rates4.edge.app'
25+
]
26+
2027
const snooze: Function = async (ms: number) =>
2128
await new Promise((resolve: Function) => setTimeout(resolve, ms))
2229

@@ -178,7 +185,9 @@ async function updateTxValuesV3(transaction: DbTx): Promise<void> {
178185
return
179186
}
180187

181-
const ratesResponse = await fetch('https://rates3.edge.app/v3/rates', {
188+
const server = RATES_SERVERS[Math.floor(Math.random() * RATES_SERVERS.length)]
189+
datelog(`Getting v3 rates from ${server}`)
190+
const ratesResponse = await fetch(`${server}/v3/rates`, {
182191
method: 'POST',
183192
headers: {
184193
'Content-Type': 'application/json'
@@ -205,6 +214,7 @@ async function updateTxValuesV3(transaction: DbTx): Promise<void> {
205214
const depositRate = depositRateObf?.rate
206215
const payoutRate = payoutRateObf?.rate
207216

217+
let changed = false
208218
// Calculate and fill out payoutAmount if it is zero
209219
if (payoutAmount === 0) {
210220
if (depositRate == null) {
@@ -220,6 +230,7 @@ async function updateTxValuesV3(transaction: DbTx): Promise<void> {
220230
}
221231
if (depositRate != null && payoutRate != null) {
222232
transaction.payoutAmount = (depositAmount * depositRate) / payoutRate
233+
changed = true
223234
}
224235
}
225236

@@ -229,6 +240,7 @@ async function updateTxValuesV3(transaction: DbTx): Promise<void> {
229240
if (transaction.usdValue == null || transaction.usdValue <= 0) {
230241
if (depositRate != null) {
231242
transaction.usdValue = depositAmount * depositRate
243+
changed = true
232244
datelog(
233245
`V3 SUCCESS id:${t._id} ${t.isoDate.slice(0, 10)} deposit:${
234246
t.depositCurrency
@@ -238,6 +250,7 @@ async function updateTxValuesV3(transaction: DbTx): Promise<void> {
238250
)
239251
} else if (payoutRate != null) {
240252
transaction.usdValue = transaction.payoutAmount * payoutRate
253+
changed = true
241254
datelog(
242255
`V3 SUCCESS id:${t._id} ${t.isoDate.slice(0, 10)} payout:${
243256
t.payoutCurrency
@@ -247,6 +260,14 @@ async function updateTxValuesV3(transaction: DbTx): Promise<void> {
247260
)
248261
}
249262
}
263+
if (!changed) {
264+
datelog(
265+
`V3 NO CHANGE id:${t._id} ${t.isoDate.slice(0, 10)} ${
266+
t.depositCurrency
267+
} ${t.payoutCurrency}`
268+
)
269+
transaction._id = undefined
270+
}
250271
}
251272

252273
async function updateTxValues(
@@ -394,7 +415,9 @@ async function getExchangeRate(
394415
currencyA = isFiatCurrency(currencyA) ? `iso:${currencyA}` : currencyA
395416
currencyB = isFiatCurrency(currencyB) ? `iso:${currencyB}` : currencyB
396417

397-
const url = `https://rates2.edge.app/v2/exchangeRate?currency_pair=${currencyA}_${currencyB}&date=${hourDate}`
418+
const server = RATES_SERVERS[Math.floor(Math.random() * RATES_SERVERS.length)]
419+
const url = `${server}/v2/exchangeRate?currency_pair=${currencyA}_${currencyB}&date=${hourDate}`
420+
datelog(`Getting v2 exchange rate from ${server}`)
398421
try {
399422
const result = await fetch(url, { method: 'GET' })
400423
if (!result.ok) {

0 commit comments

Comments
 (0)