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
2629aee6
Commit
2629aee6
authored
May 06, 2020
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve ranking of trades by including slippage
parent
f4bba5fc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
9 deletions
+27
-9
trade.ts
src/entities/trade.ts
+27
-9
No files found.
src/entities/trade.ts
View file @
2629aee6
...
...
@@ -25,23 +25,23 @@ interface InputOutput {
// comparator function that allows sorting trades by their output amounts, in decreasing order, and then input amounts
// in increasing order. i.e. the best trades have the most outputs for the least inputs and are sorted first
export
function
inputOutputComparator
(
tradeA
:
InputOutput
,
tradeB
:
InputOutput
):
number
{
export
function
inputOutputComparator
(
a
:
InputOutput
,
b
:
InputOutput
):
number
{
// must have same input and output token for comparison
invariant
(
tradeA
.
inputAmount
.
token
.
equals
(
tradeB
.
inputAmount
.
token
),
'
INPUT_TOKEN
'
)
invariant
(
tradeA
.
outputAmount
.
token
.
equals
(
tradeB
.
outputAmount
.
token
),
'
OUTPUT_TOKEN
'
)
if
(
tradeA
.
outputAmount
.
equalTo
(
tradeB
.
outputAmount
))
{
if
(
tradeA
.
inputAmount
.
equalTo
(
tradeB
.
inputAmount
))
{
invariant
(
a
.
inputAmount
.
token
.
equals
(
b
.
inputAmount
.
token
),
'
INPUT_TOKEN
'
)
invariant
(
a
.
outputAmount
.
token
.
equals
(
b
.
outputAmount
.
token
),
'
OUTPUT_TOKEN
'
)
if
(
a
.
outputAmount
.
equalTo
(
b
.
outputAmount
))
{
if
(
a
.
inputAmount
.
equalTo
(
b
.
inputAmount
))
{
return
0
}
// trade A requires less input than trade B, so A should come first
if
(
tradeA
.
inputAmount
.
lessThan
(
tradeB
.
inputAmount
))
{
if
(
a
.
inputAmount
.
lessThan
(
b
.
inputAmount
))
{
return
-
1
}
else
{
return
1
}
}
else
{
// tradeA has less output than trade B, so should come second
if
(
tradeA
.
outputAmount
.
lessThan
(
tradeB
.
outputAmount
))
{
if
(
a
.
outputAmount
.
lessThan
(
b
.
outputAmount
))
{
return
1
}
else
{
return
-
1
...
...
@@ -49,6 +49,24 @@ export function inputOutputComparator(tradeA: InputOutput, tradeB: InputOutput):
}
}
// extension of the input output comparator that also considers other dimensions of the trade in ranking them
export
function
tradeComparator
(
a
:
Trade
,
b
:
Trade
)
{
const
ioComp
=
inputOutputComparator
(
a
,
b
)
if
(
ioComp
!==
0
)
{
return
ioComp
}
// consider lowest slippage next, since these are less likely to fail
if
(
a
.
slippage
.
lessThan
(
b
.
slippage
))
{
return
-
1
;
}
else
if
(
a
.
slippage
.
greaterThan
(
b
.
slippage
))
{
return
1
;
}
// finally consider the number of hops since each hop costs gas
return
a
.
route
.
path
.
length
-
b
.
route
.
path
.
length
}
export
interface
BestTradeOptions
{
// how many results to return
maxNumResults
?:
number
...
...
@@ -140,7 +158,7 @@ export class Trade {
TradeType
.
EXACT_INPUT
),
maxNumResults
,
inputOutput
Comparator
trade
Comparator
)
}
else
if
(
maxHops
>
1
&&
pairs
.
length
>
1
)
{
const
pairsExcludingThisPair
=
pairs
.
slice
(
0
,
i
).
concat
(
pairs
.
slice
(
i
+
1
,
pairs
.
length
))
...
...
@@ -204,7 +222,7 @@ export class Trade {
bestTrades
,
new
Trade
(
new
Route
([
pair
,
...
currentPairs
],
tokenIn
),
originalAmountOut
,
TradeType
.
EXACT_OUTPUT
),
maxNumResults
,
inputOutput
Comparator
trade
Comparator
)
}
else
if
(
maxHops
>
1
&&
pairs
.
length
>
1
)
{
const
pairsExcludingThisPair
=
pairs
.
slice
(
0
,
i
).
concat
(
pairs
.
slice
(
i
+
1
,
pairs
.
length
))
...
...
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