Skip to content

Commit 75220b7

Browse files
committed
Add a check-due function to bitProvider for caching support queries
1 parent 5546ece commit 75220b7

File tree

2 files changed

+81
-46
lines changed

2 files changed

+81
-46
lines changed

src/plugins/gui/providers/bityProvider.ts

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
FiatProviderGetQuoteParams,
2121
FiatProviderQuote
2222
} from '../fiatProviderTypes'
23+
import { makeCheckDue } from './common'
2324
import { ProviderSupportStore } from './ProviderSupportStore'
2425

2526
const providerId = 'bity'
@@ -364,6 +365,7 @@ export const bityProvider: FiatProviderFactory = {
364365
const { apiKeys, getTokenId } = params
365366
const clientId = asBityApiKeys(apiKeys).clientId
366367

368+
const isCheckDue = makeCheckDue(1000 * 60 * 60) // 1 hour
367369
const supportedAssets = new ProviderSupportStore(providerId)
368370

369371
// Bit supports buy and sell directions
@@ -397,55 +399,58 @@ export const bityProvider: FiatProviderFactory = {
397399
throw new FiatProviderError({ providerId, errorType: 'paymentUnsupported' })
398400
}
399401

400-
const response = await fetch(`https://exchange.api.bity.com/v2/currencies`).catch(e => undefined)
401-
if (response == null || !response.ok) {
402-
console.error(`Bity getSupportedAssets response error: ${await response?.text()}`)
403-
return supportedAssets.getFiatProviderAssetMap({
404-
direction,
405-
region,
406-
payment
407-
})
408-
}
402+
if (isCheckDue()) {
403+
const response = await fetch(`https://exchange.api.bity.com/v2/currencies`).catch(e => undefined)
404+
if (response == null || !response.ok) {
405+
console.error(`Bity getSupportedAssets response error: ${await response?.text()}`)
406+
isCheckDue(true)
407+
return supportedAssets.getFiatProviderAssetMap({
408+
direction,
409+
region,
410+
payment
411+
})
412+
}
409413

410-
const result = await response.json()
411-
let bityCurrencies: BityCurrency[] = []
412-
try {
413-
bityCurrencies = asBityCurrencyResponse(result).currencies
414-
} catch (error: any) {
415-
console.error(error)
416-
return supportedAssets.getFiatProviderAssetMap({
417-
direction,
418-
region,
419-
payment
420-
})
421-
}
414+
const result = await response.json()
415+
let bityCurrencies: BityCurrency[] = []
416+
try {
417+
bityCurrencies = asBityCurrencyResponse(result).currencies
418+
} catch (error: any) {
419+
console.error(error)
420+
return supportedAssets.getFiatProviderAssetMap({
421+
direction,
422+
region,
423+
payment
424+
})
425+
}
422426

423-
for (const currency of bityCurrencies) {
424-
if (currency.tags.length === 1 && currency.tags[0] === 'fiat') {
425-
const fiatCurrencyCode = 'iso:' + currency.code.toUpperCase()
426-
supportedAssets.add.direction('*').region('*').fiat(fiatCurrencyCode).payment('*')
427-
supportedAssets.addFiatInfo(fiatCurrencyCode, currency)
428-
} else if (currency.tags.includes('crypto')) {
429-
// Bity reports cryptos with a set of multiple tags such that there is
430-
// overlap, such as USDC being 'crypto', 'ethereum', 'erc20'.
431-
const pluginId = currency.tags.includes('erc20') && currency.tags.includes('ethereum') ? 'ethereum' : CURRENCY_PLUGINID_MAP[currency.code]
432-
if (pluginId == null) continue
433-
434-
const tokenId = getTokenId(pluginId, currency.code)
435-
if (tokenId === undefined) continue
436-
437-
// If token is not in the no-KYC list do not add it
438-
const list = noKycCurrencyCodes[direction].crypto[pluginId]
439-
if (list == null || !list.some(t => t.tokenId === tokenId)) {
440-
continue
427+
for (const currency of bityCurrencies) {
428+
if (currency.tags.length === 1 && currency.tags[0] === 'fiat') {
429+
const fiatCurrencyCode = 'iso:' + currency.code.toUpperCase()
430+
supportedAssets.add.direction('*').region('*').fiat(fiatCurrencyCode).payment('*')
431+
supportedAssets.addFiatInfo(fiatCurrencyCode, currency)
432+
} else if (currency.tags.includes('crypto')) {
433+
// Bity reports cryptos with a set of multiple tags such that there is
434+
// overlap, such as USDC being 'crypto', 'ethereum', 'erc20'.
435+
const pluginId = currency.tags.includes('erc20') && currency.tags.includes('ethereum') ? 'ethereum' : CURRENCY_PLUGINID_MAP[currency.code]
436+
if (pluginId == null) continue
437+
438+
const tokenId = getTokenId(pluginId, currency.code)
439+
if (tokenId === undefined) continue
440+
441+
// If token is not in the no-KYC list do not add it
442+
const list = noKycCurrencyCodes[direction].crypto[pluginId]
443+
if (list == null || !list.some(t => t.tokenId === tokenId)) {
444+
continue
445+
}
446+
447+
const crypto = `${pluginId}:${tokenId}`
448+
supportedAssets.add.direction('*').region('*').fiat('*').payment('*').crypto(crypto)
449+
supportedAssets.addCryptoInfo(crypto, currency)
450+
} else {
451+
// Unhandled combination not caught by cleaner. Skip to be safe.
452+
console.log('Unhandled Bity supported currency: ', currency)
441453
}
442-
443-
const crypto = `${pluginId}:${tokenId}`
444-
supportedAssets.add.direction('*').region('*').fiat('*').payment('*').crypto(crypto)
445-
supportedAssets.addCryptoInfo(crypto, currency)
446-
} else {
447-
// Unhandled combination not caught by cleaner. Skip to be safe.
448-
console.log('Unhandled Bity supported currency: ', currency)
449454
}
450455
}
451456

src/plugins/gui/providers/common.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,33 @@ export const isDailyCheckDue = (lastCheck: number): boolean => {
106106
const last = new Date(lastCheck).getTime()
107107
return now - last > DAILY_INTERVAL_MS
108108
}
109+
110+
/**
111+
* Checks if a interval has passed based on the last check time. If the check
112+
* is due, the last check time is updated to the current time.
113+
*
114+
* @param interval the interval in milliseconds
115+
* @returns a "check-due" function which will return `true` if the check interval
116+
* has elapsed since the last check, `false` otherwise. Also allows for an override
117+
* to reset the last check time.
118+
*/
119+
export const makeCheckDue = (interval: number) => {
120+
let last: number = 0
121+
/**
122+
* Checks if a interval has passed based on the last check time. If the check
123+
* is due, the last check time is updated to the current time.
124+
* Also allows for an override to reset the last check time.
125+
*/
126+
return function checkDue(override?: boolean): boolean {
127+
if (override != null) {
128+
last = override ? last : 0
129+
return override
130+
}
131+
const now = Date.now()
132+
if (now - last > interval) {
133+
last = now
134+
return true
135+
}
136+
return false
137+
}
138+
}

0 commit comments

Comments
 (0)