Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
allow specifying deadline in the router
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed Sep 14, 2020
1 parent 00a1eca commit b48a19d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))),
Expand Down

0 comments on commit b48a19d

Please sign in to comment.