Skip to content

Commit

Permalink
Add utils to format token list tokens for aggregators and icon url. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Cal-L authored May 5, 2022
1 parent 638856e commit f685920
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/assets/assetsUtil.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NetworksChainId } from '../network/NetworkController';
import * as assetsUtil from './assetsUtil';
import { Collectible, CollectibleMetadata } from './CollectiblesController';

Expand Down Expand Up @@ -99,5 +100,25 @@ describe('assetsUtil', () => {
);
expect(different).toStrictEqual(false);
});

it('should format aggregator names', () => {
const formattedAggregatorNames = assetsUtil.formatAggregatorNames([
'bancor',
'aave',
'coinGecko',
]);
const expectedValue = ['Bancor', 'Aave', 'CoinGecko'];
expect(formattedAggregatorNames).toStrictEqual(expectedValue);
});

it('should format icon url with Codefi proxy', () => {
const linkTokenAddress = '0x514910771af9ca656af840dff83e8264ecf986ca';
const formattedIconUrl = assetsUtil.formatIconUrlWithProxy({
chainId: NetworksChainId.mainnet,
tokenAddress: linkTokenAddress,
});
const expectedValue = `https://static.metaswap.codefi.network/api/v1/tokenIcons/${NetworksChainId.mainnet}/${linkTokenAddress}.png`;
expect(formattedIconUrl).toStrictEqual(expectedValue);
});
});
});
93 changes: 93 additions & 0 deletions src/assets/assetsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,96 @@ export function compareCollectiblesMetadata(
}, 0);
return differentValues > 0;
}

export type AggregatorKey =
| 'aave'
| 'bancor'
| 'cmc'
| 'cryptocom'
| 'coinGecko'
| 'oneInch'
| 'paraswap'
| 'pmm'
| 'zapper'
| 'zerion'
| 'zeroEx'
| 'synthetix'
| 'yearn'
| 'apeswap'
| 'binanceDex'
| 'pancakeTop100'
| 'pancakeExtended'
| 'balancer'
| 'quickswap'
| 'matcha'
| 'pangolinDex'
| 'pangolinDexStableCoin'
| 'pangolinDexAvaxBridge'
| 'traderJoe'
| 'airswapLight'
| 'kleros';

type AggregatorNameByKey = {
[key in AggregatorKey]: string;
};

const aggregatorNameByKey: AggregatorNameByKey = {
aave: 'Aave',
bancor: 'Bancor',
cmc: 'CMC',
cryptocom: 'Crypto.com',
coinGecko: 'CoinGecko',
oneInch: '1inch',
paraswap: 'Paraswap',
pmm: 'PMM',
zapper: 'Zapper',
zerion: 'Zerion',
zeroEx: '0x',
synthetix: 'Synthetix',
yearn: 'Yearn',
apeswap: 'ApeSwap',
binanceDex: 'BinanceDex',
pancakeTop100: 'PancakeTop100',
pancakeExtended: 'PancakeExtended',
balancer: 'Balancer',
quickswap: 'QuickSwap',
matcha: 'Matcha',
pangolinDex: 'PangolinDex',
pangolinDexStableCoin: 'PangolinDexStableCoin',
pangolinDexAvaxBridge: 'PangolinDexAvaxBridge',
traderJoe: 'TraderJoe',
airswapLight: 'AirswapLight',
kleros: 'Kleros',
};

/**
* Formats aggregator names to presentable format.
*
* @param aggregators - List of token list names in camelcase.
* @returns Formatted aggregator names.
*/
export const formatAggregatorNames = (aggregators: AggregatorKey[]) => {
return aggregators.map(
(key) =>
aggregatorNameByKey[key] ||
`${key[0].toUpperCase()}${key.substring(1, key.length)}`,
);
};

/**
* Format token list assets to use image proxy from Codefi.
*
* @param params - Object that contains chainID and tokenAddress.
* @param params.chainId - ChainID of network.
* @param params.tokenAddress - Address of token in lowercase.
* @returns Formatted image url
*/
export const formatIconUrlWithProxy = ({
chainId,
tokenAddress,
}: {
chainId: string;
tokenAddress: string;
}) => {
return `https://static.metaswap.codefi.network/api/v1/tokenIcons/${chainId}/${tokenAddress}.png`;
};

0 comments on commit f685920

Please sign in to comment.