Skip to content

Commit

Permalink
fix: findTokenByChainIdAndAddress was broken
Browse files Browse the repository at this point in the history
  • Loading branch information
H3xept committed Jun 29, 2023
1 parent f7e665b commit c75b34f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
18 changes: 16 additions & 2 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 @@ -1944,8 +1945,21 @@ export const findTokenByChainIdAndAddress = (

defaultCoins.forEach((coin) => {
Object.values(coin.chains).forEach((coinToken: StaticToken) => {
if (coinToken.chainId === chainId && coinToken.address === tokenAddress) {
token = coinToken
if (
coinToken.chainId === chainId &&
coinToken.address.toLowerCase() === tokenAddress.toLowerCase()
) {
// First read attributes from coin
const coinAttributes = {
...(coin.key ? { key: coin.key } : {}),
...(coin.name ? { name: coin.name } : {}),
...(coin.logoURI ? { logoURI: coin.logoURI } : {}),
}
token = {
...coinAttributes,
// Apply token-specific overrides
...coinToken,
}
}
})
})
Expand Down
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')
})
})
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"declaration": true /* Generates corresponding '.d.ts' file. */,
"outDir": "./dist" /* Redirect output structure to the directory. */
},
"include": ["src/**/*"]
"include": ["src/**/*", "test/**/*"]
}

0 comments on commit c75b34f

Please sign in to comment.