Commit b48a19d3 authored by Moody Salem's avatar Moody Salem

allow specifying deadline in the router

parent 00a1eca9
...@@ -28,6 +28,14 @@ export interface TradeOptions { ...@@ -28,6 +28,14 @@ export interface TradeOptions {
feeOnTransfer?: boolean 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. * The parameters to use in the call to the Uniswap V2 Router to execute a trade.
*/ */
...@@ -65,18 +73,22 @@ export abstract class Router { ...@@ -65,18 +73,22 @@ export abstract class Router {
* @param trade to produce call parameters for * @param trade to produce call parameters for
* @param options options for the call parameters * @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 etherIn = trade.inputAmount.currency === ETHER
const etherOut = trade.outputAmount.currency === ETHER const etherOut = trade.outputAmount.currency === ETHER
// the router does not support both ether in and out // the router does not support both ether in and out
invariant(!(etherIn && etherOut), 'ETHER_IN_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 to: string = validateAndParseAddress(options.recipient)
const amountIn: string = toHex(trade.maximumAmountIn(options.allowedSlippage)) const amountIn: string = toHex(trade.maximumAmountIn(options.allowedSlippage))
const amountOut: string = toHex(trade.minimumAmountOut(options.allowedSlippage)) const amountOut: string = toHex(trade.minimumAmountOut(options.allowedSlippage))
const path: string[] = trade.route.path.map(token => token.address) 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) const useFeeOnTransfer = Boolean(options.feeOnTransfer)
let methodName: string let methodName: string
......
...@@ -33,6 +33,26 @@ describe('Router', () => { ...@@ -33,6 +33,26 @@ describe('Router', () => {
expect(result.value).toEqual('0x64') expect(result.value).toEqual('0x64')
checkDeadline(result.args[result.args.length - 1]) 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', () => { it('token1 to ether', () => {
const result = Router.swapCallParameters( const result = Router.swapCallParameters(
Trade.exactIn(new Route([pair_0_1, pair_weth_0], token1, ETHER), new TokenAmount(token1, JSBI.BigInt(100))), Trade.exactIn(new Route([pair_0_1, pair_weth_0], token1, ETHER), new TokenAmount(token1, JSBI.BigInt(100))),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment