Skip to content

Commit

Permalink
fix: Using old prod fallback and using /currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
CremaFR committed Sep 6, 2024
1 parent 3abf295 commit 4cb7078
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
21 changes: 15 additions & 6 deletions libs/ledger-live-common/src/exchange/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { CryptoCurrency, TokenCurrency } from "@ledgerhq/types-cryptoassets
import { getEnv } from "@ledgerhq/live-env";
import { findTestExchangeCurrencyConfig } from "./testCurrencyConfig";
import { findExchangeCurrencyData } from "./providers/swap";
import { findExchangeCurrencyConfig as findProdExchangeCurrencyConfig } from "@ledgerhq/cryptoassets";
// Minimum version of a currency app which has exchange capabilities, meaning it can be used
// for sell/swap, and do silent signing.
const exchangeSupportAppVersions = {
Expand Down Expand Up @@ -39,14 +40,22 @@ export const isExchangeSupportedByApp = (appName: string, appVersion: string): b
export const getCurrencyExchangeConfig = async (
currency: CryptoCurrency | TokenCurrency,
): Promise<ExchangeCurrencyNameAndSignature> => {
const type = currency.type === "TokenCurrency" ? "tokens" : "coins";
let res;
try {
res = getEnv("MOCK_EXCHANGE_TEST_CONFIG")
? await findTestExchangeCurrencyConfig(currency.id)
: await findExchangeCurrencyData(currency.id);

const res = getEnv("MOCK_EXCHANGE_TEST_CONFIG")
? await findTestExchangeCurrencyConfig(currency.id)
: await findExchangeCurrencyData(currency.id, type);
if (!res) {
throw new Error("Missing primary config");
}
} catch (error) {
// Fallback to old production config if the primary fetch fails, should be removed when we have a HA CAL
res = await findProdExchangeCurrencyConfig(currency.id);

if (!res) {
throw new Error(`Exchange, missing configuration for ${currency.id}`);
if (!res) {
throw new Error(`Exchange, missing configuration for ${currency.id}`);
}
}

return {
Expand Down
10 changes: 2 additions & 8 deletions libs/ledger-live-common/src/exchange/providers/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,18 @@ export const getProvidersData = async (): Promise<Record<string, ProviderData>>

/**
* Retrieves the currency data for a given ID
* coins and tokens are litteraly the same on our case but we need to call the correct endpoint.
* @param currencyId The unique identifier for the currency.
* @param type Specifies the type of currency, either "tokens" or "coins".
* @returns A promise that resolves to the currency data including ID, serialized config, and signature.
*/
export const findExchangeCurrencyData = async (
currencyId: string,
type: "tokens" | "coins",
): Promise<CurrencyData> => {
export const findExchangeCurrencyData = async (currencyId: string): Promise<CurrencyData> => {
const { data: currencyData } = await network<CurrencyDataResponse>({
method: "GET",
url: `https://crypto-assets-service.api.ledger.com/v1/${type}`,
url: "https://crypto-assets-service.api.ledger.com/v1/currencies",
params: {
output: "id,exchange_app_config_serialized,exchange_app_signature",
id: currencyId,
},
});
// TODO: maybe throw errors/warning if many currencies fetched with the same ID.
if (!currencyData.length) {
throw new Error(`Exchange, missing configuration for ${currencyId}`);
}
Expand Down

0 comments on commit 4cb7078

Please sign in to comment.