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
b48a19d3
Unverified
Commit
b48a19d3
authored
Sep 14, 2020
by
Moody Salem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow specifying deadline in the router
parent
00a1eca9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
3 deletions
+35
-3
router.ts
src/router.ts
+15
-3
router.test.ts
test/router.test.ts
+20
-0
No files found.
src/router.ts
View file @
b48a19d3
...
...
@@ -28,6 +28,14 @@ export interface TradeOptions {
feeOnTransfer
?:
boolean
}
export
interface
TradeOptionsDeadline
extends
Omit
<
TradeOptions
,
'
ttl
'
>
{
/**
* When the transaction expires.
* This is an atlernate to specifying the ttl, for when you do not want to use local time.
*/
deadline
:
number
}
/**
* The parameters to use in the call to the Uniswap V2 Router to execute a trade.
*/
...
...
@@ -65,18 +73,22 @@ export abstract class Router {
* @param trade to produce call parameters for
* @param options options for the call parameters
*/
public
static
swapCallParameters
(
trade
:
Trade
,
options
:
TradeOptions
):
SwapParameters
{
public
static
swapCallParameters
(
trade
:
Trade
,
options
:
TradeOptions
|
TradeOptionsDeadline
):
SwapParameters
{
const
etherIn
=
trade
.
inputAmount
.
currency
===
ETHER
const
etherOut
=
trade
.
outputAmount
.
currency
===
ETHER
// the router does not support both ether in and out
invariant
(
!
(
etherIn
&&
etherOut
),
'
ETHER_IN_OUT
'
)
invariant
(
options
.
ttl
>
0
,
'
TTL
'
)
invariant
(
!
(
'
ttl
'
in
options
)
||
options
.
ttl
>
0
,
'
TTL
'
)
const
to
:
string
=
validateAndParseAddress
(
options
.
recipient
)
const
amountIn
:
string
=
toHex
(
trade
.
maximumAmountIn
(
options
.
allowedSlippage
))
const
amountOut
:
string
=
toHex
(
trade
.
minimumAmountOut
(
options
.
allowedSlippage
))
const
path
:
string
[]
=
trade
.
route
.
path
.
map
(
token
=>
token
.
address
)
const
deadline
=
`0x
${(
Math
.
floor
(
new
Date
().
getTime
()
/
1000
)
+
options
.
ttl
).
toString
(
16
)}
`
const
deadline
=
'
ttl
'
in
options
?
`0x
${(
Math
.
floor
(
new
Date
().
getTime
()
/
1000
)
+
options
.
ttl
).
toString
(
16
)}
`
:
`0x
${
options
.
deadline
.
toString
(
16
)}
`
const
useFeeOnTransfer
=
Boolean
(
options
.
feeOnTransfer
)
let
methodName
:
string
...
...
test/router.test.ts
View file @
b48a19d3
...
...
@@ -33,6 +33,26 @@ describe('Router', () => {
expect
(
result
.
value
).
toEqual
(
'
0x64
'
)
checkDeadline
(
result
.
args
[
result
.
args
.
length
-
1
])
})
it
(
'
deadline specified
'
,
()
=>
{
const
result
=
Router
.
swapCallParameters
(
Trade
.
exactIn
(
new
Route
([
pair_weth_0
,
pair_0_1
],
ETHER
,
token1
),
CurrencyAmount
.
ether
(
JSBI
.
BigInt
(
100
))),
{
deadline
:
50
,
recipient
:
'
0x0000000000000000000000000000000000000004
'
,
allowedSlippage
:
new
Percent
(
'
1
'
,
'
100
'
)
}
)
expect
(
result
.
methodName
).
toEqual
(
'
swapExactETHForTokens
'
)
expect
(
result
.
args
).
toEqual
([
'
0x51
'
,
[
WETH
[
ChainId
.
MAINNET
].
address
,
token0
.
address
,
token1
.
address
],
'
0x0000000000000000000000000000000000000004
'
,
'
0x32
'
])
expect
(
result
.
value
).
toEqual
(
'
0x64
'
)
})
it
(
'
token1 to ether
'
,
()
=>
{
const
result
=
Router
.
swapCallParameters
(
Trade
.
exactIn
(
new
Route
([
pair_0_1
,
pair_weth_0
],
token1
,
ETHER
),
new
TokenAmount
(
token1
,
JSBI
.
BigInt
(
100
))),
...
...
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