Skip to content

Commit

Permalink
fix: findTokenByChainIdAndAddress was broken (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
H3xept authored Jun 30, 2023
1 parent f7e665b commit 04db211
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
23 changes: 11 additions & 12 deletions src/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,7 @@ const basicCoins: BasicCoin[] = [
chains: {
[ChainId.GOR]: {
address: '0x7ea6eA49B0b0Ae9c5db7907d139D9Cd3439862a1',
name: 'Goerli CXTT',
decimals: 18,
},
[ChainId.LNAT]: {
Expand Down Expand Up @@ -1565,7 +1566,7 @@ export const defaultCoins: Array<Coin> = 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,
}
}
Expand Down Expand Up @@ -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
)
}
22 changes: 20 additions & 2 deletions test/types.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
Expand Down Expand Up @@ -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')
})
})
})

0 comments on commit 04db211

Please sign in to comment.