diff --git a/src/router.ts b/src/router.ts index b90029b00..058befca3 100644 --- a/src/router.ts +++ b/src/router.ts @@ -28,6 +28,14 @@ export interface TradeOptions { feeOnTransfer?: boolean } +export interface TradeOptionsDeadline extends Omit { + /** + * 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 diff --git a/test/router.test.ts b/test/router.test.ts index dd9dc11fa..0b4caa512 100644 --- a/test/router.test.ts +++ b/test/router.test.ts @@ -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))),