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
f4bba5fc
Commit
f4bba5fc
authored
May 06, 2020
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make the #bestTradeExactIn and #bestTradeExactOut more strict about input pairs
parent
2d948bba
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
10 deletions
+22
-10
trade.ts
src/entities/trade.ts
+4
-10
trade.test.ts
test/trade.test.ts
+18
-0
No files found.
src/entities/trade.ts
View file @
f4bba5fc
...
@@ -112,10 +112,7 @@ export class Trade {
...
@@ -112,10 +112,7 @@ export class Trade {
originalAmountIn
:
TokenAmount
=
amountIn
,
originalAmountIn
:
TokenAmount
=
amountIn
,
bestTrades
:
Trade
[]
=
[]
bestTrades
:
Trade
[]
=
[]
):
Trade
[]
{
):
Trade
[]
{
if
(
pairs
.
length
===
0
)
{
invariant
(
pairs
.
length
>
0
,
'
PAIRS
'
)
return
bestTrades
}
invariant
(
maxHops
>
0
,
'
MAX_HOPS
'
)
invariant
(
maxHops
>
0
,
'
MAX_HOPS
'
)
invariant
(
originalAmountIn
===
amountIn
||
currentPairs
.
length
>
0
,
'
INVALID_RECURSION
'
)
invariant
(
originalAmountIn
===
amountIn
||
currentPairs
.
length
>
0
,
'
INVALID_RECURSION
'
)
...
@@ -145,7 +142,7 @@ export class Trade {
...
@@ -145,7 +142,7 @@ export class Trade {
maxNumResults
,
maxNumResults
,
inputOutputComparator
inputOutputComparator
)
)
}
else
if
(
maxHops
>
1
)
{
}
else
if
(
maxHops
>
1
&&
pairs
.
length
>
1
)
{
const
pairsExcludingThisPair
=
pairs
.
slice
(
0
,
i
).
concat
(
pairs
.
slice
(
i
+
1
,
pairs
.
length
))
const
pairsExcludingThisPair
=
pairs
.
slice
(
0
,
i
).
concat
(
pairs
.
slice
(
i
+
1
,
pairs
.
length
))
// otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
// otherwise, consider all the other paths that lead from this token as long as we have not exceeded maxHops
...
@@ -182,10 +179,7 @@ export class Trade {
...
@@ -182,10 +179,7 @@ export class Trade {
originalAmountOut
:
TokenAmount
=
amountOut
,
originalAmountOut
:
TokenAmount
=
amountOut
,
bestTrades
:
Trade
[]
=
[]
bestTrades
:
Trade
[]
=
[]
):
Trade
[]
{
):
Trade
[]
{
if
(
pairs
.
length
===
0
)
{
invariant
(
pairs
.
length
>
0
,
'
PAIRS
'
)
return
bestTrades
}
invariant
(
maxHops
>
0
,
'
MAX_HOPS
'
)
invariant
(
maxHops
>
0
,
'
MAX_HOPS
'
)
invariant
(
originalAmountOut
===
amountOut
||
currentPairs
.
length
>
0
,
'
INVALID_RECURSION
'
)
invariant
(
originalAmountOut
===
amountOut
||
currentPairs
.
length
>
0
,
'
INVALID_RECURSION
'
)
...
@@ -212,7 +206,7 @@ export class Trade {
...
@@ -212,7 +206,7 @@ export class Trade {
maxNumResults
,
maxNumResults
,
inputOutputComparator
inputOutputComparator
)
)
}
else
if
(
maxHops
>
1
)
{
}
else
if
(
maxHops
>
1
&&
pairs
.
length
>
1
)
{
const
pairsExcludingThisPair
=
pairs
.
slice
(
0
,
i
).
concat
(
pairs
.
slice
(
i
+
1
,
pairs
.
length
))
const
pairsExcludingThisPair
=
pairs
.
slice
(
0
,
i
).
concat
(
pairs
.
slice
(
i
+
1
,
pairs
.
length
))
// otherwise, consider all the other paths that arrive at this token as long as we have not exceeded maxHops
// otherwise, consider all the other paths that arrive at this token as long as we have not exceeded maxHops
...
...
test/trade.test.ts
View file @
f4bba5fc
...
@@ -14,6 +14,15 @@ describe('Trade', () => {
...
@@ -14,6 +14,15 @@ describe('Trade', () => {
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
)))
describe
(
'
#bestTradeExactIn
'
,
()
=>
{
describe
(
'
#bestTradeExactIn
'
,
()
=>
{
it
(
'
throws with empty pairs
'
,
()
=>
{
expect
(()
=>
Trade
.
bestTradeExactIn
([],
new
TokenAmount
(
token0
,
JSBI
.
BigInt
(
100
)),
token2
)).
toThrow
(
'
PAIRS
'
)
})
it
(
'
throws with max hops of 0
'
,
()
=>
{
expect
(()
=>
Trade
.
bestTradeExactIn
([
pair_0_2
],
new
TokenAmount
(
token0
,
JSBI
.
BigInt
(
100
)),
token2
,
{
maxHops
:
0
})
).
toThrow
(
'
MAX_HOPS
'
)
})
it
(
'
provides best route
'
,
()
=>
{
it
(
'
provides best route
'
,
()
=>
{
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
],
...
@@ -77,6 +86,15 @@ describe('Trade', () => {
...
@@ -77,6 +86,15 @@ describe('Trade', () => {
})
})
describe
(
'
#bestTradeExactOut
'
,
()
=>
{
describe
(
'
#bestTradeExactOut
'
,
()
=>
{
it
(
'
throws with empty pairs
'
,
()
=>
{
expect
(()
=>
Trade
.
bestTradeExactOut
([],
token0
,
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
100
)))).
toThrow
(
'
PAIRS
'
)
})
it
(
'
throws with max hops of 0
'
,
()
=>
{
expect
(()
=>
Trade
.
bestTradeExactOut
([
pair_0_2
],
token0
,
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
100
)),
{
maxHops
:
0
})
).
toThrow
(
'
MAX_HOPS
'
)
})
it
(
'
provides best route
'
,
()
=>
{
it
(
'
provides best route
'
,
()
=>
{
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