Commit d0faa1c1 authored by Moody Salem's avatar Moody Salem

do not throw if name/symbol/decimals differ

parent 603dee89
import { Currency } from './currency'
import invariant from 'tiny-invariant' import invariant from 'tiny-invariant'
import { ChainId } from '../constants' import { ChainId } from '../constants'
import { validateAndParseAddress } from '../utils' import { validateAndParseAddress } from '../utils'
import { Currency } from './currency'
/** /**
* Represents an ERC20 token with a unique address and some metadata. * Represents an ERC20 token with a unique address and some metadata.
...@@ -17,23 +17,15 @@ export class Token extends Currency { ...@@ -17,23 +17,15 @@ export class Token extends Currency {
} }
/** /**
* Returns true if the two tokens are equivalent. * Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
* @param other other token to compare * @param other other token to compare
* @throws if the tokens share the address and chain ID but have different metadata
*/ */
public equals(other: Token): boolean { public equals(other: Token): boolean {
// short circuit on reference equality // short circuit on reference equality
if (this === other) { if (this === other) {
return true return true
} }
const equivalent = this.chainId === other.chainId && this.address === other.address return this.chainId === other.chainId && this.address === other.address
if (equivalent) {
// reference the same token, must have the same decimals/symbol/name
invariant(this.decimals === other.decimals, 'DECIMALS')
if (this.symbol && other.symbol) invariant(this.symbol === other.symbol, 'SYMBOL')
if (this.name && other.name) invariant(this.name === other.name, 'NAME')
}
return equivalent
} }
/** /**
......
import { ChainId, Token } from '../src'
describe('Token', () => {
const ADDRESS_ONE = '0x0000000000000000000000000000000000000001'
const ADDRESS_TWO = '0x0000000000000000000000000000000000000002'
describe('#equals', () => {
it('fails if address differs', () => {
expect(new Token(ChainId.MAINNET, ADDRESS_ONE, 18).equals(new Token(ChainId.MAINNET, ADDRESS_TWO, 18))).toBe(
false
)
})
it('false if chain id differs', () => {
expect(new Token(ChainId.ROPSTEN, ADDRESS_ONE, 18).equals(new Token(ChainId.MAINNET, ADDRESS_ONE, 18))).toBe(
false
)
})
it('true if only decimals differs', () => {
expect(new Token(ChainId.MAINNET, ADDRESS_ONE, 9).equals(new Token(ChainId.MAINNET, ADDRESS_ONE, 18))).toBe(true)
})
it('true if address is the same', () => {
expect(new Token(ChainId.MAINNET, ADDRESS_ONE, 18).equals(new Token(ChainId.MAINNET, ADDRESS_ONE, 18))).toBe(true)
})
it('true on reference equality', () => {
const token = new Token(ChainId.MAINNET, ADDRESS_ONE, 18)
expect(token.equals(token)).toBe(true)
})
it('true even if name/symbol/decimals differ', () => {
const tokenA = new Token(ChainId.MAINNET, ADDRESS_ONE, 9, 'abc', 'def')
const tokenB = new Token(ChainId.MAINNET, ADDRESS_ONE, 18, 'ghi', 'jkl')
expect(tokenA.equals(tokenB)).toBe(true)
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment