From 04db2111a7254014d274352b23e90f57c48b8c7e Mon Sep 17 00:00:00 2001 From: Leonardo Cascianelli Date: Fri, 30 Jun 2023 14:04:36 +0200 Subject: [PATCH] fix: findTokenByChainIdAndAddress was broken (#176) --- src/coins.ts | 23 +++++++++++------------ test/types.unit.test.ts | 22 ++++++++++++++++++++-- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/coins.ts b/src/coins.ts index ca6f9ba5..9c9cce2a 100644 --- a/src/coins.ts +++ b/src/coins.ts @@ -1037,6 +1037,7 @@ const basicCoins: BasicCoin[] = [ chains: { [ChainId.GOR]: { address: '0x7ea6eA49B0b0Ae9c5db7907d139D9Cd3439862a1', + name: 'Goerli CXTT', decimals: 18, }, [ChainId.LNAT]: { @@ -1565,7 +1566,7 @@ export const defaultCoins: Array = basicCoins.map((coin) => { symbol: token.symbol ?? coin.key, chainId: parseInt(chainId), // Object.entries, Object.keys etc. only return keys as strings. Therefore, we have to parse them here coinKey: coin.key, - name: token.name ?? coin.key, + name: token.name ?? coin.name ?? coin.key, logoURI: coin.logoURI, } } @@ -1940,15 +1941,13 @@ export const findTokenByChainIdAndAddress = ( chainId: number, tokenAddress: string ): StaticToken | null => { - let token: StaticToken | null = null - - defaultCoins.forEach((coin) => { - Object.values(coin.chains).forEach((coinToken: StaticToken) => { - if (coinToken.chainId === chainId && coinToken.address === tokenAddress) { - token = coinToken - } - }) - }) - - return token + return ( + defaultCoins + .flatMap(({ chains }) => Object.values(chains)) + .find( + (token) => + token.chainId === chainId && + tokenAddress.toLowerCase() === token.address.toLowerCase() + ) ?? null + ) } diff --git a/test/types.unit.test.ts b/test/types.unit.test.ts index 60183dee..5009a60e 100644 --- a/test/types.unit.test.ts +++ b/test/types.unit.test.ts @@ -1,5 +1,5 @@ -import { ChainId, ChainKey, getChainByKey, getChainById, supportedEVMChains, findDefaultToken, findWrappedGasOnChain } from '../src' - +import { ChainId, ChainKey, getChainByKey, getChainById, supportedEVMChains, findDefaultToken, findWrappedGasOnChain, CoinKey } from '../src' +import { findTokenByChainIdAndAddress } from '../src/coins'; test('getChainById', () => { expect(getChainById(ChainId.ETH)).toBeDefined() }) @@ -47,3 +47,21 @@ test('native token defined for all chains', () => { } } }) + +describe('findTokenByChainIdAndAddress', () => { + describe('token has no name override', () => { + it('returns a token with the coin name', () => { + expect( + findTokenByChainIdAndAddress(ChainId.LNAT, '0xb706319d37b945727e71ae0d4353699d19112576')!.name + ).toEqual(CoinKey.CXTT) + }) + }) + + describe("token has a name override", () => { + it('returns a token with the overrode name', () => { + expect( + findTokenByChainIdAndAddress(ChainId.GOR, '0x7ea6eA49B0b0Ae9c5db7907d139D9Cd3439862a1')!.name + ).toEqual('Goerli CXTT') + }) + }) +}) \ No newline at end of file