mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-10 17:28:53 +01:00
- Currently, it was possible to generate coin images from Coingecko only in single size (24x24) in two image qualities (1x, 2x).
- Now it's possible to easily add additionally sizes to the `COIN_IMAGE_SIZES` or qualities to `COIN_IMAGE_QUALITIES`.
- This required to change the generated image name. I made sure to update all existing occurances, mainly the `getAssetLogoUrl` util to stay backwards compatible.
Coin image name:
```
type OldCoinImageName = `${string}--{'' | '2x'}.webp`
type NewCoinImageName = `${string}--${24 | 40}{'' | '2x'}.webp`
```
62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
import { sleep } from './sleep';
|
|
import { COIN_DATA_URL, COIN_LIST_URL, UPDATED_ICONS_LIST_URL } from '../constants';
|
|
import { CoinData, CoinListData, UpdatedIconsList } from '../types';
|
|
|
|
const coingeckoApiOptions = {
|
|
method: 'GET',
|
|
headers: { 'x-cg-pro-api-key': process.env.COINGECKO_API_KEY! },
|
|
};
|
|
|
|
export const getUpdatedIconsList = async (): Promise<UpdatedIconsList> => {
|
|
try {
|
|
const response = await fetch(UPDATED_ICONS_LIST_URL);
|
|
if (!response.ok) {
|
|
throw new Error(`${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
return response.json();
|
|
} catch (error) {
|
|
console.error('Failed to fetch updated icons list:', error);
|
|
|
|
return {};
|
|
}
|
|
};
|
|
|
|
export const getCoinList = async (): Promise<CoinListData[]> => {
|
|
const params = new URLSearchParams({
|
|
include_platform: true.toString(),
|
|
});
|
|
|
|
const response = await fetch(`${COIN_LIST_URL}?${params.toString()}`, coingeckoApiOptions);
|
|
if (!response.ok) {
|
|
throw new Error(`${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
return await response.json();
|
|
};
|
|
|
|
export const getCoinData = async (id: string, retry: boolean = true): Promise<CoinData> => {
|
|
const params = new URLSearchParams({
|
|
localization: false.toString(),
|
|
tickers: false.toString(),
|
|
market_data: false.toString(),
|
|
community_data: false.toString(),
|
|
developer_data: false.toString(),
|
|
sparkline: false.toString(),
|
|
});
|
|
|
|
const response = await fetch(`${COIN_DATA_URL}${id}?${params.toString()}`, coingeckoApiOptions);
|
|
if (!response.ok) {
|
|
if (retry && response.status === 429) {
|
|
console.error('Too many requests, waiting for 60 seconds...');
|
|
await sleep(60 * 1000);
|
|
|
|
return getCoinData(id, false);
|
|
}
|
|
|
|
throw new Error(`${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
return await response.json();
|
|
};
|