Skip to content

Commit

Permalink
Add a check-due function to bitProvider for caching support queries
Browse files Browse the repository at this point in the history
  • Loading branch information
samholmes authored and paullinator committed Nov 12, 2024
1 parent cdb0d8c commit ffb97f9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 46 deletions.
97 changes: 51 additions & 46 deletions src/plugins/gui/providers/bityProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
FiatProviderGetQuoteParams,
FiatProviderQuote
} from '../fiatProviderTypes'
import { makeCheckDue } from './common'
import { ProviderSupportStore } from './ProviderSupportStore'

const providerId = 'bity'
Expand Down Expand Up @@ -364,6 +365,7 @@ export const bityProvider: FiatProviderFactory = {
const { apiKeys, getTokenId } = params
const clientId = asBityApiKeys(apiKeys).clientId

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

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

const response = await fetch(`https://exchange.api.bity.com/v2/currencies`).catch(e => undefined)
if (response == null || !response.ok) {
console.error(`Bity getSupportedAssets response error: ${await response?.text()}`)
return supportedAssets.getFiatProviderAssetMap({
direction,
region,
payment
})
}
if (isCheckDue()) {
const response = await fetch(`https://exchange.api.bity.com/v2/currencies`).catch(e => undefined)
if (response == null || !response.ok) {
console.error(`Bity getSupportedAssets response error: ${await response?.text()}`)
isCheckDue(true)
return supportedAssets.getFiatProviderAssetMap({
direction,
region,
payment
})
}

const result = await response.json()
let bityCurrencies: BityCurrency[] = []
try {
bityCurrencies = asBityCurrencyResponse(result).currencies
} catch (error: any) {
console.error(error)
return supportedAssets.getFiatProviderAssetMap({
direction,
region,
payment
})
}
const result = await response.json()
let bityCurrencies: BityCurrency[] = []
try {
bityCurrencies = asBityCurrencyResponse(result).currencies
} catch (error: any) {
console.error(error)
return supportedAssets.getFiatProviderAssetMap({
direction,
region,
payment
})
}

for (const currency of bityCurrencies) {
if (currency.tags.length === 1 && currency.tags[0] === 'fiat') {
const fiatCurrencyCode = 'iso:' + currency.code.toUpperCase()
supportedAssets.add.direction('*').region('*').fiat(fiatCurrencyCode).payment('*')
supportedAssets.addFiatInfo(fiatCurrencyCode, currency)
} else if (currency.tags.includes('crypto')) {
// Bity reports cryptos with a set of multiple tags such that there is
// overlap, such as USDC being 'crypto', 'ethereum', 'erc20'.
const pluginId = currency.tags.includes('erc20') && currency.tags.includes('ethereum') ? 'ethereum' : CURRENCY_PLUGINID_MAP[currency.code]
if (pluginId == null) continue

const tokenId = getTokenId(pluginId, currency.code)
if (tokenId === undefined) continue

// If token is not in the no-KYC list do not add it
const list = noKycCurrencyCodes[direction].crypto[pluginId]
if (list == null || !list.some(t => t.tokenId === tokenId)) {
continue
for (const currency of bityCurrencies) {
if (currency.tags.length === 1 && currency.tags[0] === 'fiat') {
const fiatCurrencyCode = 'iso:' + currency.code.toUpperCase()
supportedAssets.add.direction('*').region('*').fiat(fiatCurrencyCode).payment('*')
supportedAssets.addFiatInfo(fiatCurrencyCode, currency)
} else if (currency.tags.includes('crypto')) {
// Bity reports cryptos with a set of multiple tags such that there is
// overlap, such as USDC being 'crypto', 'ethereum', 'erc20'.
const pluginId = currency.tags.includes('erc20') && currency.tags.includes('ethereum') ? 'ethereum' : CURRENCY_PLUGINID_MAP[currency.code]
if (pluginId == null) continue

const tokenId = getTokenId(pluginId, currency.code)
if (tokenId === undefined) continue

// If token is not in the no-KYC list do not add it
const list = noKycCurrencyCodes[direction].crypto[pluginId]
if (list == null || !list.some(t => t.tokenId === tokenId)) {
continue
}

const crypto = `${pluginId}:${tokenId}`
supportedAssets.add.direction('*').region('*').fiat('*').payment('*').crypto(crypto)
supportedAssets.addCryptoInfo(crypto, currency)
} else {
// Unhandled combination not caught by cleaner. Skip to be safe.
console.log('Unhandled Bity supported currency: ', currency)
}

const crypto = `${pluginId}:${tokenId}`
supportedAssets.add.direction('*').region('*').fiat('*').payment('*').crypto(crypto)
supportedAssets.addCryptoInfo(crypto, currency)
} else {
// Unhandled combination not caught by cleaner. Skip to be safe.
console.log('Unhandled Bity supported currency: ', currency)
}
}

Expand Down
30 changes: 30 additions & 0 deletions src/plugins/gui/providers/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,33 @@ export const isDailyCheckDue = (lastCheck: number): boolean => {
const last = new Date(lastCheck).getTime()
return now - last > DAILY_INTERVAL_MS
}

/**
* Checks if a interval has passed based on the last check time. If the check
* is due, the last check time is updated to the current time.
*
* @param interval the interval in milliseconds
* @returns a "check-due" function which will return `true` if the check interval
* has elapsed since the last check, `false` otherwise. Also allows for an override
* to reset the last check time.
*/
export const makeCheckDue = (interval: number) => {
let last: number = 0
/**
* Checks if a interval has passed based on the last check time. If the check
* is due, the last check time is updated to the current time.
* Also allows for an override to reset the last check time.
*/
return function checkDue(override?: boolean): boolean {
if (override != null) {
last = override ? last : 0
return override
}
const now = Date.now()
if (now - last > interval) {
last = now
return true
}
return false
}
}

0 comments on commit ffb97f9

Please sign in to comment.