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

Commit

Permalink
Allow for payer as User as swapoptions
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongeric committed Mar 20, 2024
1 parent 55a164a commit 9d0d485
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
33 changes: 17 additions & 16 deletions src/entities/protocols/uniswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type FlatFeeOptions = {
// so we extend swap options with the permit2 permit
// when safe mode is enabled, the SDK will add an extra ETH sweep for security
export type SwapOptions = Omit<RouterSwapOptions, 'inputTokenPermit'> & {
payerIsUser?: boolean
inputTokenPermit?: Permit2Permit
flatFee?: FlatFeeOptions
safeMode?: boolean
Expand All @@ -53,7 +54,10 @@ export class UniswapTrade implements Command {
}

encode(planner: RoutePlanner, _config: TradeConfig): void {
let payerIsUser = true
if(this.options.payerIsUser === undefined) {
// default to user as payer
Object.assign(this.options, { payerIsUser: true })
}

// If the input currency is the native currency, we need to wrap it with the router as the recipient
if (this.trade.inputAmount.currency.isNative) {
Expand All @@ -63,7 +67,7 @@ export class UniswapTrade implements Command {
this.trade.maximumAmountIn(this.options.slippageTolerance).quotient.toString(),
])
// since WETH is now owned by the router, the router pays for inputs
payerIsUser = false
Object.assign(this.options, { payerIsUser: false })
}
// The overall recipient at the end of the trade, SENDER_AS_RECIPIENT uses the msg.sender
this.options.recipient = this.options.recipient ?? SENDER_AS_RECIPIENT
Expand All @@ -81,13 +85,13 @@ export class UniswapTrade implements Command {
for (const swap of this.trade.swaps) {
switch (swap.route.protocol) {
case Protocol.V2:
addV2Swap(planner, swap, this.trade.tradeType, this.options, payerIsUser, routerMustCustody)
addV2Swap(planner, swap, this.trade.tradeType, this.options, routerMustCustody)
break
case Protocol.V3:
addV3Swap(planner, swap, this.trade.tradeType, this.options, payerIsUser, routerMustCustody)
addV3Swap(planner, swap, this.trade.tradeType, this.options, routerMustCustody)
break
case Protocol.MIXED:
addMixedSwap(planner, swap, this.trade.tradeType, this.options, payerIsUser, routerMustCustody)
addMixedSwap(planner, swap, this.trade.tradeType, this.options, routerMustCustody)
break
default:
throw new Error('UNSUPPORTED_TRADE_PROTOCOL')
Expand Down Expand Up @@ -165,7 +169,6 @@ function addV2Swap<TInput extends Currency, TOutput extends Currency>(
{ route, inputAmount, outputAmount }: Swap<TInput, TOutput>,
tradeType: TradeType,
options: SwapOptions,
payerIsUser: boolean,
routerMustCustody: boolean
): void {
const trade = new V2Trade(
Expand All @@ -181,15 +184,15 @@ function addV2Swap<TInput extends Currency, TOutput extends Currency>(
trade.maximumAmountIn(options.slippageTolerance).quotient.toString(),
trade.minimumAmountOut(options.slippageTolerance).quotient.toString(),
route.path.map((pool) => pool.address),
payerIsUser,
options.payerIsUser,
])
} else if (tradeType == TradeType.EXACT_OUTPUT) {
planner.addCommand(CommandType.V2_SWAP_EXACT_OUT, [
routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient,
trade.minimumAmountOut(options.slippageTolerance).quotient.toString(),
trade.maximumAmountIn(options.slippageTolerance).quotient.toString(),
route.path.map((pool) => pool.address),
payerIsUser,
options.payerIsUser,
])
}
}
Expand All @@ -200,7 +203,6 @@ function addV3Swap<TInput extends Currency, TOutput extends Currency>(
{ route, inputAmount, outputAmount }: Swap<TInput, TOutput>,
tradeType: TradeType,
options: SwapOptions,
payerIsUser: boolean,
routerMustCustody: boolean
): void {
const trade = V3Trade.createUncheckedTrade({
Expand All @@ -217,15 +219,15 @@ function addV3Swap<TInput extends Currency, TOutput extends Currency>(
trade.maximumAmountIn(options.slippageTolerance).quotient.toString(),
trade.minimumAmountOut(options.slippageTolerance).quotient.toString(),
path,
payerIsUser,
options.payerIsUser,
])
} else if (tradeType == TradeType.EXACT_OUTPUT) {
planner.addCommand(CommandType.V3_SWAP_EXACT_OUT, [
routerMustCustody ? ROUTER_AS_RECIPIENT : options.recipient,
trade.minimumAmountOut(options.slippageTolerance).quotient.toString(),
trade.maximumAmountIn(options.slippageTolerance).quotient.toString(),
path,
payerIsUser,
options.payerIsUser,
])
}
}
Expand All @@ -236,7 +238,6 @@ function addMixedSwap<TInput extends Currency, TOutput extends Currency>(
swap: Swap<TInput, TOutput>,
tradeType: TradeType,
options: SwapOptions,
payerIsUser: boolean,
routerMustCustody: boolean
): void {
const { route, inputAmount, outputAmount } = swap
Expand All @@ -245,9 +246,9 @@ function addMixedSwap<TInput extends Currency, TOutput extends Currency>(
// single hop, so it can be reduced to plain v2 or v3 swap logic
if (route.pools.length === 1) {
if (route.pools[0] instanceof Pool) {
return addV3Swap(planner, swap, tradeType, options, payerIsUser, routerMustCustody)
return addV3Swap(planner, swap, tradeType, options, routerMustCustody)
} else if (route.pools[0] instanceof Pair) {
return addV2Swap(planner, swap, tradeType, options, payerIsUser, routerMustCustody)
return addV2Swap(planner, swap, tradeType, options, routerMustCustody)
} else {
throw new Error('Invalid route type')
}
Expand Down Expand Up @@ -302,15 +303,15 @@ function addMixedSwap<TInput extends Currency, TOutput extends Currency>(
i == 0 ? amountIn : CONTRACT_BALANCE, // amountIn
!isLastSectionInRoute(i) ? 0 : amountOut, // amountOut
path, // path
payerIsUser && i === 0, // payerIsUser
options.payerIsUser && i === 0, // payerIsUser
])
} else {
planner.addCommand(CommandType.V2_SWAP_EXACT_IN, [
isLastSectionInRoute(i) ? tradeRecipient : ROUTER_AS_RECIPIENT, // recipient
i === 0 ? amountIn : CONTRACT_BALANCE, // amountIn
!isLastSectionInRoute(i) ? 0 : amountOut, // amountOutMin
newRoute.path.map((pool) => pool.address), // path
payerIsUser && i === 0,
options.payerIsUser && i === 0,
])
}
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { SwapRouter } from './swapRouter'
export * from './entities'
export * from './utils/routerTradeAdapter'
export { RoutePlanner, CommandType } from './utils/routerCommands'
export {
UNIVERSAL_ROUTER_ADDRESS,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/routerTradeAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ export class RouterTradeAdapter {

return {
routev3: isOnlyV3
? new V3Route(subRoute.map(RouterTradeAdapter.toPool), parsedCurrencyIn, parsedCurrencyOut)
? new V3Route((subRoute as V3PoolInRoute[]).map(RouterTradeAdapter.toPool), parsedCurrencyIn, parsedCurrencyOut)
: null,
routev2: isOnlyV2
? new V2Route(subRoute.map(RouterTradeAdapter.toPair), parsedCurrencyIn, parsedCurrencyOut)
? new V2Route((subRoute as V2PoolInRoute[]).map(RouterTradeAdapter.toPair), parsedCurrencyIn, parsedCurrencyOut)
: null,
mixedRoute:
!isOnlyV3 && !isOnlyV2
Expand Down
2 changes: 1 addition & 1 deletion test/forge/interop.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,4 @@
"calldata": "0x24856bc30000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000030b000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000000000120000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000003eb3459f0ce6ae000b00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000bb8a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480001f46b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0000000000000000000000000000000000000000000000000000000000000000",
"value": "1000000000000000000"
}
}
}

0 comments on commit 9d0d485

Please sign in to comment.