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
d8cc5865
Unverified
Commit
d8cc5865
authored
Jul 02, 2020
by
Moody Salem
Committed by
GitHub
Jul 02, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some small edits to wording (#24)
parent
35d6c34f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
8 deletions
+18
-8
trade.ts
src/entities/trade.ts
+15
-5
entities.test.ts
test/entities.test.ts
+3
-3
No files found.
src/entities/trade.ts
View file @
d8cc5865
...
...
@@ -9,7 +9,9 @@ import { Price } from './fractions/price'
import
{
Pair
}
from
'
./pair
'
import
{
Route
}
from
'
./route
'
function
getSlippage
(
midPrice
:
Price
,
inputAmount
:
TokenAmount
,
outputAmount
:
TokenAmount
):
Percent
{
// returns the percent difference between the mid price and the execution price
// we call this price impact in the UI
function
computePriceImpact
(
midPrice
:
Price
,
inputAmount
:
TokenAmount
,
outputAmount
:
TokenAmount
):
Percent
{
const
exactQuote
=
midPrice
.
raw
.
multiply
(
inputAmount
.
raw
)
// calculate slippage := (exactQuote - outputAmount) / exactQuote
const
slippage
=
exactQuote
.
subtract
(
outputAmount
.
raw
).
divide
(
exactQuote
)
...
...
@@ -56,9 +58,9 @@ export function tradeComparator(a: Trade, b: Trade) {
}
// consider lowest slippage next, since these are less likely to fail
if
(
a
.
slippage
.
lessThan
(
b
.
slippage
))
{
if
(
a
.
priceImpact
.
lessThan
(
b
.
priceImpact
))
{
return
-
1
}
else
if
(
a
.
slippage
.
greaterThan
(
b
.
slippage
))
{
}
else
if
(
a
.
priceImpact
.
greaterThan
(
b
.
priceImpact
))
{
return
1
}
...
...
@@ -78,9 +80,17 @@ export class Trade {
public
readonly
tradeType
:
TradeType
public
readonly
inputAmount
:
TokenAmount
public
readonly
outputAmount
:
TokenAmount
// the price expressed in terms of output/input
public
readonly
executionPrice
:
Price
// the mid price after the trade executes assuming zero slippage
public
readonly
nextMidPrice
:
Price
public
readonly
slippage
:
Percent
// the percent difference between the mid price before the trade and the price after the trade
public
readonly
priceImpact
:
Percent
// this is a misnomer for price impact, but kept for compatibility
public
get
slippage
():
Percent
{
return
this
.
priceImpact
}
public
constructor
(
route
:
Route
,
amount
:
TokenAmount
,
tradeType
:
TradeType
)
{
invariant
(
amount
.
token
.
equals
(
tradeType
===
TradeType
.
EXACT_INPUT
?
route
.
input
:
route
.
output
),
'
TOKEN
'
)
...
...
@@ -112,7 +122,7 @@ export class Trade {
this
.
outputAmount
=
outputAmount
this
.
executionPrice
=
new
Price
(
route
.
input
,
route
.
output
,
inputAmount
.
raw
,
outputAmount
.
raw
)
this
.
nextMidPrice
=
Price
.
fromRoute
(
new
Route
(
nextPairs
,
route
.
input
))
this
.
slippage
=
getSlippage
(
route
.
midPrice
,
inputAmount
,
outputAmount
)
this
.
priceImpact
=
computePriceImpact
(
route
.
midPrice
,
inputAmount
,
outputAmount
)
}
// get the minimum amount that must be received from this trade for the given slippage tolerance
...
...
test/entities.test.ts
View file @
d8cc5865
...
...
@@ -122,7 +122,7 @@ describe('entities', () => {
expect
(
trade
.
nextMidPrice
.
toSignificant
(
18
)).
toEqual
(
'
1.38958368072925352
'
)
expect
(
trade
.
nextMidPrice
.
invert
().
toSignificant
(
18
)).
toEqual
(
'
0.71964
'
)
expect
(
trade
.
slippage
.
toSignificant
(
18
)).
toEqual
(
'
16.8751042187760547
'
)
expect
(
trade
.
priceImpact
.
toSignificant
(
18
)).
toEqual
(
'
16.8751042187760547
'
)
})
it
(
'
TradeType.EXACT_OUTPUT
'
,
()
=>
{
...
...
@@ -142,7 +142,7 @@ describe('entities', () => {
expect
(
trade
.
nextMidPrice
.
toSignificant
(
18
)).
toEqual
(
'
1.38958368072925352
'
)
expect
(
trade
.
nextMidPrice
.
invert
().
toSignificant
(
18
)).
toEqual
(
'
0.71964
'
)
expect
(
trade
.
slippage
.
toSignificant
(
18
)).
toEqual
(
'
16.8751042187760547
'
)
expect
(
trade
.
priceImpact
.
toSignificant
(
18
)).
toEqual
(
'
16.8751042187760547
'
)
})
it
(
'
minimum TradeType.EXACT_INPUT
'
,
()
=>
{
...
...
@@ -163,7 +163,7 @@ describe('entities', () => {
const
outputAmount
=
new
TokenAmount
(
tokens
[
1
],
'
1
'
)
const
trade
=
new
Trade
(
route
,
outputAmount
,
TradeType
.
EXACT_INPUT
)
expect
(
trade
.
slippage
.
toSignificant
(
18
)).
toEqual
(
expect
(
trade
.
priceImpact
.
toSignificant
(
18
)).
toEqual
(
tokens
[
1
].
decimals
===
9
?
'
0.300000099400899902
'
:
'
0.3000000000000001
'
)
}
...
...
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