From e97ffba2673cfdf39e4f5852c4e2ad47941ea8d6 Mon Sep 17 00:00:00 2001 From: Keith Broughton Date: Wed, 2 Aug 2023 09:49:58 +1000 Subject: [PATCH] Use decode function data in tests --- .../dex/sdk/src/lib/transactionUtils/swap.ts | 5 ++ packages/internal/dex/sdk/src/test/utils.ts | 89 ++++++++++--------- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts b/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts index 16945c5e85..730a163114 100644 --- a/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts +++ b/packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts @@ -44,6 +44,7 @@ function buildSwapParametersForSinglePoolSwap( })); if (trade.tradeType === TradeType.EXACT_INPUT) { + console.log('exactInputSingleWithServiceFee'); return secondaryFeeContract.encodeFunctionData('exactInputSingleWithServiceFee', [secondaryFeeValues, { tokenIn: route.tokenPath[0].address, tokenOut: route.tokenPath[1].address, @@ -55,6 +56,7 @@ function buildSwapParametersForSinglePoolSwap( }]); } + console.log('exactOutputSingleWithServiceFee'); return secondaryFeeContract.encodeFunctionData('exactOutputSingleWithServiceFee', [secondaryFeeValues, { tokenIn: route.tokenPath[0].address, tokenOut: route.tokenPath[1].address, @@ -83,6 +85,7 @@ function buildSwapParametersForMultiPoolSwap( })); if (trade.tradeType === TradeType.EXACT_INPUT) { + console.log('exactInputWithServiceFee'); return secondaryFeeContract.encodeFunctionData('exactInputWithServiceFee', [secondaryFeeValues, { path, recipient: fromAddress, @@ -91,6 +94,7 @@ function buildSwapParametersForMultiPoolSwap( }]); } + console.log('exactOutputWithServiceFee'); return secondaryFeeContract.encodeFunctionData('exactOutputWithServiceFee', [secondaryFeeValues, { path, recipient: fromAddress, @@ -214,6 +218,7 @@ export function getSwap( gasPrice: ethers.BigNumber | null, secondaryFees: SecondaryFee[], ): TransactionDetails { + console.log('getSwap'); const calldata = createSwapParameters( routeAndQuote.trade, fromAddress, diff --git a/packages/internal/dex/sdk/src/test/utils.ts b/packages/internal/dex/sdk/src/test/utils.ts index 6148ff7649..5bad6ec56c 100644 --- a/packages/internal/dex/sdk/src/test/utils.ts +++ b/packages/internal/dex/sdk/src/test/utils.ts @@ -8,8 +8,10 @@ import { import { ethers } from 'ethers'; import JSBI from 'jsbi'; import { Pool, Route, TickMath } from '@uniswap/v3-sdk'; +import { SwapRouter } from '@uniswap/router-sdk'; import { Environment, ImmutableConfiguration } from '@imtbl/config'; import { slippageToFraction } from 'lib/transactionUtils/slippage'; +import { SecondaryFee__factory } from 'contracts/types'; import { QuoteTradeInfo, Router, @@ -68,28 +70,6 @@ export const FUN_TEST_TOKEN = new Token( 'The Fungibles Token', ); -const exactInputOutputSingleParamTypes = [ - 'address', - 'address', - 'uint24', - 'address', - 'uint256', - 'uint256', - 'uint160', -]; - -const exactInputOutputSingleWithFeesParamTypes = [ - '(address,uint16)[]', - '(address,address,uint24,address,uint256,uint256,uint160)', -]; - -const exactInputOutWithFeesParamTypes = [ - '(address,uint16)[]', - '(bytes,address,uint256,uint256)', -]; - -const multicallParamTypes = ['uint256', 'bytes[]']; - export const TEST_IMMUTABLE_CONFIGURATION: ImmutableConfiguration = new ImmutableConfiguration({ environment: Environment.SANDBOX, }); @@ -180,23 +160,39 @@ export function decodePath(path: string) { }; } -function decodeParams(calldata: ethers.utils.BytesLike, paramTypes: string[]) { - // eslint-disable-next-line no-param-reassign - const data = ethers.utils.hexDataSlice(calldata, 4); +type SecondaryFeeFunctionName = 'exactInputSingleWithServiceFee' | +'exactOutputSingleWithServiceFee' | +'exactInputWithServiceFee' | +'exactOutputWithServiceFee'; + +type SwapRouterFunctionName = 'exactInputSingle'; - const topLevelParams = ethers.utils.defaultAbiCoder.decode( - multicallParamTypes, - data, +function decodeSecondaryFeeCall(calldata: ethers.utils.BytesLike, functionName: SecondaryFeeFunctionName) { + const iface = SecondaryFee__factory.createInterface(); + const topLevelParams = iface.decodeFunctionData('multicall(uint256,bytes[])', calldata); + + const decodedParams = iface.decodeFunctionData( + functionName, + topLevelParams.data[0], ); - const decodedParams = ethers.utils.defaultAbiCoder - .decode(paramTypes, ethers.utils.hexDataSlice(topLevelParams[1][0], 4)); + return { topLevelParams, decodedParams }; +} + +function decodeSwapRouterCall(calldata: ethers.utils.BytesLike, functionName: SwapRouterFunctionName) { + const iface = SwapRouter.INTERFACE; + const topLevelParams = iface.decodeFunctionData('multicall(uint256,bytes[])', calldata); + + const decodedParams = iface.decodeFunctionData( + functionName, + topLevelParams.data[0], + ); return { topLevelParams, decodedParams }; } export function decodeMulticallExactInputOutputWithFees(data: ethers.utils.BytesLike) { - const { topLevelParams, decodedParams } = decodeParams(data, exactInputOutWithFeesParamTypes); + const { topLevelParams, decodedParams } = decodeSecondaryFeeCall(data, 'exactInputWithServiceFee'); const secondaryFeeParams: SecondaryFee[] = []; @@ -218,7 +214,7 @@ export function decodeMulticallExactInputOutputWithFees(data: ethers.utils.Bytes } export function decodeMulticallExactInputOutputSingleWithFees(data: ethers.utils.BytesLike) { - const { topLevelParams, decodedParams } = decodeParams(data, exactInputOutputSingleWithFeesParamTypes); + const { topLevelParams, decodedParams } = decodeSecondaryFeeCall(data, 'exactInputSingleWithServiceFee'); const secondaryFeeParams: SecondaryFee[] = []; @@ -243,16 +239,29 @@ export function decodeMulticallExactInputOutputSingleWithFees(data: ethers.utils } export function decodeMulticallExactInputOutputSingleWithoutFees(data: ethers.utils.BytesLike) { - const { topLevelParams, decodedParams } = decodeParams(data, exactInputOutputSingleParamTypes); + const { topLevelParams, decodedParams } = decodeSwapRouterCall(data, 'exactInputSingle'); const swapParams: ExactInputOutputSingleParams = { - tokenIn: decodedParams[0], - tokenOut: decodedParams[1], - fee: decodedParams[2], - recipient: decodedParams[3], - firstAmount: decodedParams[4], - secondAmount: decodedParams[5], - sqrtPriceLimitX96: decodedParams[6], + tokenIn: decodedParams[0][0], + tokenOut: decodedParams[0][1], + fee: decodedParams[0][2], + recipient: decodedParams[0][3], + firstAmount: decodedParams[0][4], + secondAmount: decodedParams[0][5], + sqrtPriceLimitX96: decodedParams[0][6], + }; + + return { topLevelParams, swapParams }; +} + +export function decodeMulticallExactInputOutputWithoutFees(data: ethers.utils.BytesLike) { + const { topLevelParams, decodedParams } = decodeSecondaryFeeCall(data, 'exactInputWithServiceFee'); + + const swapParams: ExactInputOutputParams = { + path: decodedParams[0], + recipient: decodedParams[1], + amountIn: decodedParams[5], + amountOut: decodedParams[6], }; return { topLevelParams, swapParams };