Fix LemFi EUR to NGN rate integration
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
export function parseLemfiQuote(result) {
|
||||
const quote = result?.data;
|
||||
if (quote?.exchange_from !== "EUR" || quote?.exchange_to !== "NGN") {
|
||||
throw new Error("LemFi did not return a EUR to NGN quote");
|
||||
}
|
||||
|
||||
// Match LemFi's calculator: divide the encoded rate by the digits in its ID.
|
||||
// Use the base rate so referral rewards and LemFi Cash do not inflate comparisons.
|
||||
const divisor = typeof quote.ID === "string" ? Number(quote.ID.replace(/\D/g, "")) : 0;
|
||||
const encodedRate = typeof quote.rate === "string" || typeof quote.rate === "number"
|
||||
? Number(quote.rate)
|
||||
: NaN;
|
||||
const rate = encodedRate / divisor;
|
||||
if (!Number.isFinite(divisor) || divisor <= 0 || !Number.isFinite(rate) || rate <= 0) {
|
||||
throw new Error("LemFi returned an invalid exchange rate");
|
||||
}
|
||||
|
||||
return rate;
|
||||
}
|
||||
|
||||
export async function fetchLemfiRate() {
|
||||
const url = new URL("https://lemfi.com/api/exchange-quote");
|
||||
url.search = new URLSearchParams({ from: "EUR", to: "NGN", country: "Netherlands", lang: "en" });
|
||||
const response = await fetch(url, {
|
||||
headers: { accept: "application/json" },
|
||||
signal: AbortSignal.timeout(8000),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`LemFi quote request failed (${response.status})`);
|
||||
}
|
||||
|
||||
return parseLemfiQuote(await response.json());
|
||||
}
|
||||
Reference in New Issue
Block a user