-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(lib): @ledgerhq/crypto-icons updates (#4)
- Loading branch information
1 parent
9ff334f
commit e8fa289
Showing
14 changed files
with
210 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,96 @@ | ||
TODO | ||
# @ledgerhq/crypto-icons | ||
|
||
A package which provides a `<CryptoIcon />` component that can be consumed by Ledger applications in a React environment and used with a `ledgerId` to render icons. | ||
|
||
<!-- TODO: add Storybook link --> | ||
|
||
## Installation | ||
|
||
```bash | ||
pnpm i @ledgerhq/crypto-icons | ||
# or | ||
yarn add @ledgerhq/crypto-icons | ||
``` | ||
|
||
### Usage example | ||
|
||
```JSX | ||
import { CryptoIcon } from '@ledgerhq/crypto-icons'; | ||
|
||
const Page = () => { | ||
return ( | ||
<> | ||
<CryptoIcon ledgerId="bitcoin" ticker="BTC" /> | ||
<CryptoIcon ledgerId="ethereum" ticker="ETH" size="32px" /> | ||
<CryptoIcon ledgerId="solana" ticker="SOL" size="48px" theme="light" /> | ||
</> | ||
) | ||
} | ||
``` | ||
|
||
## Icon sources | ||
|
||
The component's primary source of icons is Ledger's CDN which contains the [assets](../assets/index.json) from this repository. It attempts to fetch a [mapping from Ledger's CDN](https://crypto-icons.ledger.com/index.json) and if the ledgerId that is passed in as a prop to the component is found, the URL for that key is used as the image source. Otherwise, a request to the [Ledger mapping service](https://ledgerhq.atlassian.net/wiki/spaces/BE/pages/3973022073/Mapping+Service) is made to retrieve a [CoinGecko mapping](https://mapping-service.api.ledger.com/v1/coingecko/mapped-assets) as a fallback. If a match for an icon is found using the ledgerId then it is used as the image source. If neither mapping has a match, a `<FallbackIcon />` component is returned with the first letter of the currency ticker as its content e.g. B for BTC. | ||
|
||
```mermaid | ||
flowchart TD | ||
A[getIconUrl with ledgerId] --> B{ledgerMapping is null?} | ||
B --> |Yes| C[setLedgerIconMapping] --> D[fetchIconMapping from Ledger CDN] --> E{success?} | ||
E --> |Yes| F[cache data in ledgerMapping] --> G | ||
E --> |No| I{coinGeckoMapping is null?} | ||
B --> |No| G{ledgerId in ledgerMapping?} | ||
G --> |Yes| H[return URL] | ||
G --> |No| I{coinGeckoMapping is null?} | ||
I --> |Yes| J[setCoinGeckoIconMapping] --> K[fetchIconMapping from CoinGecko] --> L{success?} | ||
I --> |No| O | ||
L --> |Yes| M[cache data in coinGeckoMapping] --> O{ledgerId in coinGeckoMapping?} | ||
L --> |No| N[return null] | ||
O --> |Yes| P[return URL] | ||
O --> |No| R[return null] | ||
``` | ||
|
||
## Contributing | ||
|
||
Make sure you're in the correct directory: | ||
|
||
```bash | ||
cd lib | ||
``` | ||
|
||
### Install dependencies | ||
|
||
```bash | ||
pnpm i | ||
``` | ||
|
||
### Run tests | ||
|
||
```bash | ||
pnpm test | ||
# or | ||
pnpm test:watch # to run in watch mode | ||
``` | ||
|
||
### Run storybook | ||
|
||
```bash | ||
pnpm storybook | ||
``` | ||
|
||
### Lint | ||
|
||
```bash | ||
pnpm lint # to find issues | ||
# or | ||
pnpm lint:fix # to find and fix issues | ||
``` | ||
|
||
### Build package with Rollup | ||
|
||
```bash | ||
pnpm build | ||
``` | ||
|
||
### Test locally | ||
|
||
Package can be tested locally with `pnpm-link` or `file:` protocol. Details can be found here: [https://pnpm.io/cli/link#whats-the-difference-between-pnpm-link-and-using-the-file-protocol](https://pnpm.io/cli/link#whats-the-difference-between-pnpm-link-and-using-the-file-protocol). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'test-file-stub'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,45 @@ | ||
import axios from 'axios'; | ||
import { coinGeckoMock, ledgerCDNMock } from '../__mocks__/handlers'; | ||
import { CRYPTO_ICONS_CDN_BASE } from '../src/constants'; | ||
import { getIconUrl, resetIconCacheForTesting } from '../src/iconMapping'; | ||
|
||
const axiosSpy = jest.spyOn(axios, 'get'); | ||
|
||
describe('iconMapping', () => { | ||
it.todo('test caching logic'); | ||
beforeEach(() => { | ||
resetIconCacheForTesting(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should request icon data from Ledger CDN mapping, store the response and not re-fetch', async () => { | ||
const ledgerIconUrl = await getIconUrl('bitcoin'); | ||
expect(ledgerIconUrl).toBe(`${CRYPTO_ICONS_CDN_BASE}/${ledgerCDNMock?.['bitcoin'].icon}`); | ||
|
||
const ledgerIconUrl2 = await getIconUrl('arbitrum'); | ||
expect(ledgerIconUrl2).toBe(`${CRYPTO_ICONS_CDN_BASE}/${ledgerCDNMock?.['arbitrum'].icon}`); | ||
|
||
expect(axiosSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should request fallback icon data from CoinGecko mapping if icon is not available in existing data, store the response and not re-fetch', async () => { | ||
const ledgerIconUrl = await getIconUrl('bitcoin'); | ||
expect(ledgerIconUrl).toBe(`${CRYPTO_ICONS_CDN_BASE}/${ledgerCDNMock?.['bitcoin'].icon}`); | ||
|
||
const coinGeckoIconUrl = await getIconUrl('decred'); | ||
expect(coinGeckoIconUrl).toBe(coinGeckoMock?.find((i) => i.ledgerId === 'decred')?.data.img); | ||
|
||
const coinGeckoIconUrl2 = await getIconUrl('particl'); | ||
expect(coinGeckoIconUrl2).toBe(coinGeckoMock?.find((i) => i.ledgerId === 'particl')?.data.img); | ||
|
||
expect(axiosSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should return null if icon is not found in either data', async () => { | ||
const iconUrl = await getIconUrl('nonexisting'); | ||
expect(axiosSpy).toHaveBeenCalledTimes(2); | ||
expect(iconUrl).toBe(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export const CRYPTO_ICONS_CDN_BASE = 'https://crypto-icons.ledger.com'; | ||
export const COINGECKO_MAPPED_ASSETS_URL = | ||
'https://mapping-service.api.aws.stg.ldg-tech.com/v1/coingecko/mapped-assets'; | ||
'https://mapping-service.api.ledger.com/v1/coingecko/mapped-assets'; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module '*.woff2' { | ||
const src: string; | ||
export default src; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["__tests__"] | ||
"include": ["__tests__", "jest.setup.ts", "src"] | ||
} |