Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
swap-v2-sdk
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
LuckySwap
swap-v2-sdk
Commits
d0faa1c1
Unverified
Commit
d0faa1c1
authored
Jul 21, 2020
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
do not throw if name/symbol/decimals differ
parent
603dee89
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
11 deletions
+42
-11
token.ts
src/entities/token.ts
+3
-11
token.test.ts
test/token.test.ts
+39
-0
No files found.
src/entities/token.ts
View file @
d0faa1c1
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
}
}
/**
/**
...
...
test/token.test.ts
0 → 100644
View file @
d0faa1c1
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
)
})
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment