Compare commits
No commits in common. "682e1964bc1caef7d4d5dd6acbbe1556915461e8" and "586698259af32ba08831450c691e609fbeb2d9c3" have entirely different histories.
682e1964bc
...
586698259a
1 changed files with 6 additions and 32 deletions
|
|
@ -21,37 +21,6 @@ function cacheKey(amount: number, from: string, to: string): string {
|
||||||
* conversion preview as best-effort UX, never as a blocker. 60s in-memory
|
* conversion preview as best-effort UX, never as a blocker. 60s in-memory
|
||||||
* cache de-duplicates dialog re-renders.
|
* cache de-duplicates dialog re-renders.
|
||||||
*/
|
*/
|
||||||
/**
|
|
||||||
* Pull the converted value out of an LNbits `/conversion` response.
|
|
||||||
*
|
|
||||||
* The endpoint does not echo the `to` key it was given. It always names the
|
|
||||||
* satoshi amount `"sats"` (plural) regardless of whether the caller asked for
|
|
||||||
* "sat" or "sats", and it returns fiat under the upper-case ISO code:
|
|
||||||
*
|
|
||||||
* POST {from_: "EUR", to: "sat", amount: 5.50} -> {EUR: 5.5, sats: 7298, BTC: 7.298e-05}
|
|
||||||
* POST {from_: "sat", to: "EUR", amount: 7295} -> {BTC: ..., sats: 7295, EUR: 5.4972}
|
|
||||||
*
|
|
||||||
* So a lookup of `data["sat"]` misses, which meant every sat-denominated
|
|
||||||
* conversion silently returned null.
|
|
||||||
*/
|
|
||||||
function pickConverted(data: Record<string, number>, to: string): number | undefined {
|
|
||||||
const lower = to.toLowerCase()
|
|
||||||
const candidates =
|
|
||||||
lower === 'sat' || lower === 'sats'
|
|
||||||
? ['sats', 'sat']
|
|
||||||
: [to, to.toUpperCase(), lower]
|
|
||||||
|
|
||||||
for (const key of candidates) {
|
|
||||||
if (typeof data[key] === 'number') return data[key]
|
|
||||||
}
|
|
||||||
// Older/other shapes kept as a last resort.
|
|
||||||
return typeof data.amount === 'number'
|
|
||||||
? data.amount
|
|
||||||
: typeof data.result === 'number'
|
|
||||||
? data.result
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
export function usePriceConversion() {
|
export function usePriceConversion() {
|
||||||
const lnbitsAPI = injectService<LnbitsAPI>(SERVICE_TOKENS.LNBITS_API)
|
const lnbitsAPI = injectService<LnbitsAPI>(SERVICE_TOKENS.LNBITS_API)
|
||||||
|
|
||||||
|
|
@ -69,7 +38,12 @@ export function usePriceConversion() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const data = await lnbitsAPI.getConversion({ from, to, amount })
|
const data = await lnbitsAPI.getConversion({ from, to, amount })
|
||||||
const result = pickConverted(data, to)
|
const result =
|
||||||
|
data[to] ??
|
||||||
|
data[to.toUpperCase()] ??
|
||||||
|
data[to.toLowerCase()] ??
|
||||||
|
(data as Record<string, number>).amount ??
|
||||||
|
(data as Record<string, number>).result
|
||||||
if (typeof result !== 'number') return null
|
if (typeof result !== 'number') return null
|
||||||
cache.set(key, { value: result, expiresAt: Date.now() + TTL_MS })
|
cache.set(key, { value: result, expiresAt: Date.now() + TTL_MS })
|
||||||
return result
|
return result
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue