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
edac4138
Unverified
Commit
edac4138
authored
Apr 27, 2021
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add worst execution price to the sdk
parent
875a1b89
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
1 deletion
+54
-1
trade.test.ts
src/entities/trade.test.ts
+41
-1
trade.ts
src/entities/trade.ts
+13
-0
No files found.
src/entities/trade.test.ts
View file @
edac4138
...
@@ -2,7 +2,7 @@ import { Pair } from './pair'
...
@@ -2,7 +2,7 @@ import { Pair } from './pair'
import
{
Route
}
from
'
./route
'
import
{
Route
}
from
'
./route
'
import
{
Trade
}
from
'
./trade
'
import
{
Trade
}
from
'
./trade
'
import
JSBI
from
'
jsbi
'
import
JSBI
from
'
jsbi
'
import
{
ChainId
,
ETHER
,
CurrencyAmount
,
Percent
,
Token
,
TokenAmount
,
TradeType
,
WETH9
}
from
'
@uniswap/sdk-core
'
import
{
ChainId
,
ETHER
,
CurrencyAmount
,
Percent
,
Token
,
TokenAmount
,
TradeType
,
WETH9
,
Price
}
from
'
@uniswap/sdk-core
'
describe
(
'
Trade
'
,
()
=>
{
describe
(
'
Trade
'
,
()
=>
{
const
token0
=
new
Token
(
ChainId
.
MAINNET
,
'
0x0000000000000000000000000000000000000001
'
,
18
,
'
t0
'
)
const
token0
=
new
Token
(
ChainId
.
MAINNET
,
'
0x0000000000000000000000000000000000000001
'
,
18
,
'
t0
'
)
...
@@ -280,6 +280,46 @@ describe('Trade', () => {
...
@@ -280,6 +280,46 @@ describe('Trade', () => {
})
})
})
})
describe
(
'
#worstExecutionPrice
'
,
()
=>
{
describe
(
'
tradeType = EXACT_INPUT
'
,
()
=>
{
const
exactIn
=
new
Trade
(
new
Route
([
pair_0_1
,
pair_1_2
],
token0
),
new
TokenAmount
(
token0
,
100
),
TradeType
.
EXACT_INPUT
)
it
(
'
throws if less than 0
'
,
()
=>
{
expect
(()
=>
exactIn
.
minimumAmountOut
(
new
Percent
(
-
1
,
100
))).
toThrow
(
'
SLIPPAGE_TOLERANCE
'
)
})
it
(
'
returns exact if 0
'
,
()
=>
{
expect
(
exactIn
.
worstExecutionPrice
(
new
Percent
(
0
,
100
))).
toEqual
(
exactIn
.
executionPrice
)
})
it
(
'
returns exact if nonzero
'
,
()
=>
{
expect
(
exactIn
.
worstExecutionPrice
(
new
Percent
(
0
,
100
))).
toEqual
(
new
Price
(
token0
,
token2
,
100
,
69
))
expect
(
exactIn
.
worstExecutionPrice
(
new
Percent
(
5
,
100
))).
toEqual
(
new
Price
(
token0
,
token2
,
100
,
65
))
expect
(
exactIn
.
worstExecutionPrice
(
new
Percent
(
200
,
100
))).
toEqual
(
new
Price
(
token0
,
token2
,
100
,
23
))
})
})
describe
(
'
tradeType = EXACT_OUTPUT
'
,
()
=>
{
const
exactOut
=
new
Trade
(
new
Route
([
pair_0_1
,
pair_1_2
],
token0
),
new
TokenAmount
(
token2
,
100
),
TradeType
.
EXACT_OUTPUT
)
it
(
'
throws if less than 0
'
,
()
=>
{
expect
(()
=>
exactOut
.
worstExecutionPrice
(
new
Percent
(
-
1
,
100
))).
toThrow
(
'
SLIPPAGE_TOLERANCE
'
)
})
it
(
'
returns exact if 0
'
,
()
=>
{
expect
(
exactOut
.
worstExecutionPrice
(
new
Percent
(
0
,
100
))).
toEqual
(
exactOut
.
executionPrice
)
})
it
(
'
returns slippage amount if nonzero
'
,
()
=>
{
expect
(
exactOut
.
worstExecutionPrice
(
new
Percent
(
0
,
100
))).
toEqual
(
new
Price
(
token0
,
token2
,
156
,
100
))
expect
(
exactOut
.
worstExecutionPrice
(
new
Percent
(
5
,
100
))).
toEqual
(
new
Price
(
token0
,
token2
,
163
,
100
))
expect
(
exactOut
.
worstExecutionPrice
(
new
Percent
(
200
,
100
))).
toEqual
(
new
Price
(
token0
,
token2
,
468
,
100
))
})
})
})
describe
(
'
#bestTradeExactOut
'
,
()
=>
{
describe
(
'
#bestTradeExactOut
'
,
()
=>
{
it
(
'
throws with empty pairs
'
,
()
=>
{
it
(
'
throws with empty pairs
'
,
()
=>
{
expect
(()
=>
Trade
.
bestTradeExactOut
([],
token0
,
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
100
)))).
toThrow
(
'
PAIRS
'
)
expect
(()
=>
Trade
.
bestTradeExactOut
([],
token0
,
new
TokenAmount
(
token2
,
JSBI
.
BigInt
(
100
)))).
toThrow
(
'
PAIRS
'
)
...
...
src/entities/trade.ts
View file @
edac4138
...
@@ -327,6 +327,19 @@ export class Trade {
...
@@ -327,6 +327,19 @@ export class Trade {
return
bestTrades
return
bestTrades
}
}
/**
* Return the execution price after accounting for slippage tolerance
* @param slippageTolerance the allowed tolerated slippage
*/
public
worstExecutionPrice
(
slippageTolerance
:
Percent
):
Price
{
return
new
Price
(
this
.
inputAmount
.
currency
,
this
.
outputAmount
.
currency
,
this
.
maximumAmountIn
(
slippageTolerance
).
raw
,
this
.
minimumAmountOut
(
slippageTolerance
).
raw
)
}
/**
/**
* similar to the above method but instead targets a fixed output amount
* similar to the above method but instead targets a fixed output amount
* given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
* given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
...
...
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