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
f0a6fdb9
Unverified
Commit
f0a6fdb9
authored
May 13, 2020
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Don't crash for zero liquidity pairs
parent
9da0e6a6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
0 deletions
+16
-0
trade.ts
src/entities/trade.ts
+2
-0
trade.test.ts
test/trade.test.ts
+14
-0
No files found.
src/entities/trade.ts
View file @
f0a6fdb9
...
@@ -166,6 +166,7 @@ export class Trade {
...
@@ -166,6 +166,7 @@ export class Trade {
const
pair
=
pairs
[
i
]
const
pair
=
pairs
[
i
]
// pair irrelevant
// pair irrelevant
if
(
!
pair
.
token0
.
equals
(
amountIn
.
token
)
&&
!
pair
.
token1
.
equals
(
amountIn
.
token
))
continue
if
(
!
pair
.
token0
.
equals
(
amountIn
.
token
)
&&
!
pair
.
token1
.
equals
(
amountIn
.
token
))
continue
if
(
pair
.
reserve0
.
equalTo
(
ZERO
)
||
pair
.
reserve1
.
equalTo
(
ZERO
))
continue
let
amountOut
:
TokenAmount
let
amountOut
:
TokenAmount
try
{
try
{
...
@@ -234,6 +235,7 @@ export class Trade {
...
@@ -234,6 +235,7 @@ export class Trade {
const
pair
=
pairs
[
i
]
const
pair
=
pairs
[
i
]
// pair irrelevant
// pair irrelevant
if
(
!
pair
.
token0
.
equals
(
amountOut
.
token
)
&&
!
pair
.
token1
.
equals
(
amountOut
.
token
))
continue
if
(
!
pair
.
token0
.
equals
(
amountOut
.
token
)
&&
!
pair
.
token1
.
equals
(
amountOut
.
token
))
continue
if
(
pair
.
reserve0
.
equalTo
(
ZERO
)
||
pair
.
reserve1
.
equalTo
(
ZERO
))
continue
let
amountIn
:
TokenAmount
let
amountIn
:
TokenAmount
try
{
try
{
...
...
test/trade.test.ts
View file @
f0a6fdb9
...
@@ -13,6 +13,8 @@ describe('Trade', () => {
...
@@ -13,6 +13,8 @@ describe('Trade', () => {
const
pair_1_2
=
new
Pair
(
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
1200
)),
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
1000
)))
const
pair_1_2
=
new
Pair
(
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
1200
)),
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
1000
)))
const
pair_1_3
=
new
Pair
(
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
1200
)),
new
TokenAmount
(
token3
,
JSBI
.
BigInt
(
1300
)))
const
pair_1_3
=
new
Pair
(
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
1200
)),
new
TokenAmount
(
token3
,
JSBI
.
BigInt
(
1300
)))
const
empty_pair_0_1
=
new
Pair
(
new
TokenAmount
(
token0
,
JSBI
.
BigInt
(
0
)),
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
0
)))
describe
(
'
#bestTradeExactIn
'
,
()
=>
{
describe
(
'
#bestTradeExactIn
'
,
()
=>
{
it
(
'
throws with empty pairs
'
,
()
=>
{
it
(
'
throws with empty pairs
'
,
()
=>
{
expect
(()
=>
Trade
.
bestTradeExactIn
([],
new
TokenAmount
(
token0
,
JSBI
.
BigInt
(
100
)),
token2
)).
toThrow
(
'
PAIRS
'
)
expect
(()
=>
Trade
.
bestTradeExactIn
([],
new
TokenAmount
(
token0
,
JSBI
.
BigInt
(
100
)),
token2
)).
toThrow
(
'
PAIRS
'
)
...
@@ -40,6 +42,12 @@ describe('Trade', () => {
...
@@ -40,6 +42,12 @@ describe('Trade', () => {
expect
(
result
[
1
].
outputAmount
).
toEqual
(
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
69
)))
expect
(
result
[
1
].
outputAmount
).
toEqual
(
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
69
)))
})
})
it
(
'
doesnt throw for zero liquidity pairs
'
,
()
=>
{
expect
(
Trade
.
bestTradeExactIn
([
empty_pair_0_1
],
new
TokenAmount
(
token0
,
JSBI
.
BigInt
(
100
)),
token1
)).
toHaveLength
(
0
)
})
it
(
'
respects maxHops
'
,
()
=>
{
it
(
'
respects maxHops
'
,
()
=>
{
const
result
=
Trade
.
bestTradeExactIn
(
const
result
=
Trade
.
bestTradeExactIn
(
[
pair_0_1
,
pair_0_2
,
pair_1_2
],
[
pair_0_1
,
pair_0_2
,
pair_1_2
],
...
@@ -224,6 +232,12 @@ describe('Trade', () => {
...
@@ -224,6 +232,12 @@ describe('Trade', () => {
expect
(
result
[
1
].
outputAmount
).
toEqual
(
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
100
)))
expect
(
result
[
1
].
outputAmount
).
toEqual
(
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
100
)))
})
})
it
(
'
doesnt throw for zero liquidity pairs
'
,
()
=>
{
expect
(
Trade
.
bestTradeExactOut
([
empty_pair_0_1
],
token1
,
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
100
)))).
toHaveLength
(
0
)
})
it
(
'
respects maxHops
'
,
()
=>
{
it
(
'
respects maxHops
'
,
()
=>
{
const
result
=
Trade
.
bestTradeExactOut
(
const
result
=
Trade
.
bestTradeExactOut
(
[
pair_0_1
,
pair_0_2
,
pair_1_2
],
[
pair_0_1
,
pair_0_2
,
pair_1_2
],
...
...
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