Skip to content

Commit

Permalink
clean up swapAmount because some swaps expected to fail after removin…
Browse files Browse the repository at this point in the history
…g liquidity
  • Loading branch information
tommyzhao451 committed Jan 14, 2025
1 parent a721e12 commit 7915243
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 43 deletions.
22 changes: 1 addition & 21 deletions src/viem/automan/automan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,6 @@ export async function simulateDecreaseLiquidity(
publicClient: PublicClient,
from: Address,
owner: Address,
position: Position,
decreaseLiquidityParams: DecreaseLiquidityParams,
blockNumber?: bigint,
): Promise<DecreaseLiquidityReturnType> {
Expand Down Expand Up @@ -642,8 +641,6 @@ export async function simulateDecreaseLiquiditySingleV3(
token0FeeAmount: bigint,
token1FeeAmount: bigint,
swapData: Hex = '0x',
swapAmountIn: bigint,
approveTarget: Address,
blockNumber?: bigint,
): Promise<DecreaseLiquiditySingleReturnType> {
const returnData = await requestDecreaseLiquiditySingleV3(
Expand All @@ -659,8 +656,6 @@ export async function simulateDecreaseLiquiditySingleV3(
token0FeeAmount,
token1FeeAmount,
swapData,
swapAmountIn,
approveTarget,
blockNumber,
);
return decodeFunctionResult({
Expand All @@ -682,8 +677,6 @@ export async function estimateDecreaseLiquiditySingleV3Gas(
token0FeeAmount: bigint,
token1FeeAmount: bigint,
swapData: Hex = '0x',
swapAmountIn: bigint,
approveTarget: Address,
blockNumber?: bigint,
): Promise<bigint> {
return hexToBigInt(
Expand All @@ -700,8 +693,6 @@ export async function estimateDecreaseLiquiditySingleV3Gas(
token0FeeAmount,
token1FeeAmount,
swapData,
swapAmountIn,
approveTarget,
blockNumber,
),
);
Expand All @@ -722,8 +713,6 @@ export async function requestDecreaseLiquiditySingleV3<
token0FeeAmount: bigint,
token1FeeAmount: bigint,
swapData: Hex = '0x',
swapAmountIn: bigint,
approveTarget: Address,
blockNumber?: bigint,
): Promise<RpcReturnType[M]> {
from = getFromAddress(from);
Expand All @@ -734,7 +723,7 @@ export async function requestDecreaseLiquiditySingleV3<
token1FeeAmount,
swapData,
);
const { apertureAutomanV3, optimalSwapRouter } = getAMMInfo(chainId, amm)!;
const { apertureAutomanV3 } = getAMMInfo(chainId, amm)!;

return tryRequestWithOverrides(
method,
Expand All @@ -747,15 +736,6 @@ export async function requestDecreaseLiquiditySingleV3<
{
...getNPMApprovalOverrides(chainId, amm, owner),
...getControllerOverrides(chainId, amm, from),
...await getERC20Overrides(
(zeroForOne
? position.pool.token1.address
: position.pool.token0.address) as Address,
optimalSwapRouter!,
approveTarget,
swapAmountIn,
publicClient,
),
},
blockNumber,
);
Expand Down
14 changes: 3 additions & 11 deletions src/viem/solver/decreaseLiquiditySingle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export async function decreaseLiquiditySingleV3(
publicClient,
from,
positionDetails.owner,
positionDetails.position,
decreaseLiquidityParams,
blockNumber,
);
Expand All @@ -83,7 +82,7 @@ export async function decreaseLiquiditySingleV3(
const token0FeeAmount = zeroForOne ? swapFeeAmount : 0n;
const token1FeeAmount = zeroForOne ? 0n : swapFeeAmount;

const estimateGas = async (swapData: Hex, approveTarget: Address) => {
const estimateGas = async (swapData: Hex) => {
try {
const [gasPrice, gasAmount] = await Promise.all([
publicClient.getGasPrice(),
Expand All @@ -99,8 +98,6 @@ export async function decreaseLiquiditySingleV3(
token0FeeAmount,
token1FeeAmount,
swapData,
swapAmountIn,
approveTarget,
blockNumber,
),
]);
Expand All @@ -118,7 +115,6 @@ export async function decreaseLiquiditySingleV3(
const solve = async (solver: E_Solver) => {
let swapData: Hex = '0x';
let swapRoute: SwapRoute | undefined = undefined;
let approveTarget: Address | undefined = undefined;
let amountOut: bigint = 0n;
let gasFeeEstimation: bigint = 0n;

Expand All @@ -128,9 +124,7 @@ export async function decreaseLiquiditySingleV3(
100;
if (swapAmountIn > 0n) {
// Although it's mintOptimal, it's the same swapData and swapRoute.
({ swapData, swapRoute, approveTarget } = await getSolver(
solver,
).mintOptimal({
({ swapData, swapRoute } = await getSolver(solver).mintOptimal({
chainId,
amm,
fromAddress: from,
Expand Down Expand Up @@ -158,11 +152,9 @@ export async function decreaseLiquiditySingleV3(
token0FeeAmount,
token1FeeAmount,
swapData,
swapAmountIn,
approveTarget!,
blockNumber,
);
gasFeeEstimation = await estimateGas(swapData, approveTarget!);
gasFeeEstimation = await estimateGas(swapData);
}

const tokenInPrice = zeroForOne ? tokenPricesUsd[0] : tokenPricesUsd[1];
Expand Down
1 change: 0 additions & 1 deletion src/viem/solver/getOkxSolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export const getOkxSolver = (): ISolver => {
tx.data,
),
swapRoute: protocols,
approveTarget,
};
},
};
Expand Down
1 change: 0 additions & 1 deletion src/viem/solver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export * from './types';

const defaultSwapInfo: SolvedSwapInfo = {
swapData: '0x',
approveTarget: '0x0000000000000000000000000000000000000000',
};

export const getSolver = (solver: E_Solver): ISolver => {
Expand Down
1 change: 0 additions & 1 deletion src/viem/solver/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export type SwapRoute = Array<Array<Array<SelectedProtocol>>>;
export interface SolvedSwapInfo {
swapData: Hex;
swapRoute?: SwapRoute;
approveTarget: Address;
}

export interface SolveMintOptimalProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ts-node test/playground/decreaseLiquiditySingle.ts
// ts-node test/playground/decreaseLiquiditySingle4063431OneForZero.ts
import { RemoveLiquidityOptions } from '@aperture_finance/uniswap-v3-sdk';
import { Percent } from '@uniswap/sdk-core';
import { AutomatedMarketMakerEnum } from 'aperture-lens/dist/src/viem';
Expand All @@ -11,9 +11,9 @@ import {
} from '../../src';
import {
DEFAULT_SOLVERS,
DecreaseLiquidityParams,
PositionDetails,
getDecreaseLiquiditySingleSwapInfoV3,
getDecreaseLiquiditySingleV3Tx,
getPublicClient,
} from '../../src/viem';

Expand All @@ -24,6 +24,7 @@ async function main() {
const client = getPublicClient(chainId);
const from = '0x1fFd5d818187917E0043522C3bE583A393c2BbF7';
const tokenId = 4063431;
const zeroForOne = false;
const positionDetails = await PositionDetails.fromPositionId(
chainId,
amm,
Expand All @@ -34,7 +35,7 @@ async function main() {
// token1 = usdc = 0xaf88d065e77c8cc2239327c5edb3a432268e5831
const decreaseLiquidityOptions: RemoveLiquidityOptions = {
tokenId,
liquidityPercentage: new Percent(10, 100), // position is $3.5 80%WETH. token0=weth, token1=usdc.. should taken out $0.35, $0.28 eth=.28/3277*1e18 token0amount = 8.5e13, $0.07 usdc. 64202, 92718273362833
liquidityPercentage: new Percent(10, 100), // position is $3.33 100%WETH. token0=weth, token1=usdc.. should take out $0.33 eth=.33/3116*1e18 token0amount = 1.06e14, $0.07 usdc
slippageTolerance: new Percent(5, 1000),
deadline: Math.floor(Date.now() / 1000 + 60 * 30),
collectOptions: {
Expand All @@ -43,8 +44,6 @@ async function main() {
recipient: from,
},
};
// fee in token0
const zeroForOne = true;
const swapInfos = await getDecreaseLiquiditySingleSwapInfoV3(
decreaseLiquidityOptions,
chainId,
Expand All @@ -56,9 +55,33 @@ async function main() {
DEFAULT_SOLVERS,
positionDetails,
);
console.log(
`swapInfos[0].liquidity: ${swapInfos[0].liquidity}, swapInfos[1].liquidity: ${swapInfos[1].liquidity}`,
);
for (const swapInfo of swapInfos) {
const {
solver,
swapData,
amount0,
amount1,
token0FeeAmount,
token1FeeAmount,
} = swapInfo;
const txRequest = await getDecreaseLiquiditySingleV3Tx(
decreaseLiquidityOptions,
zeroForOne,
from,
chainId,
amm,
client,
swapData,
positionDetails,
/* amount0Min= */ amount0,
/* amount1Min= */ amount1,
/* token0FeeAmount= */ token0FeeAmount,
/* token1FeeAmount= */ token1FeeAmount,
);
console.log(
`solver=${solver}, liquidity: ${swapInfo.liquidity}, to=${txRequest.to}, from=${txRequest.from}, data=${txRequest.data}`,
);
}
process.exit(0);
}

Expand Down
90 changes: 90 additions & 0 deletions test/playground/decreaseLiquiditySingle4063431ZeroForOne.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// ts-node test/playground/decreaseLiquiditySingle4063431ZeroForOne.ts
import { RemoveLiquidityOptions } from '@aperture_finance/uniswap-v3-sdk';
import { Percent } from '@uniswap/sdk-core';
import { AutomatedMarketMakerEnum } from 'aperture-lens/dist/src/viem';

import {
ApertureSupportedChainId,
ConsoleLogger,
IOCKEY_LOGGER,
ioc,
} from '../../src';
import {
DEFAULT_SOLVERS,
PositionDetails,
getDecreaseLiquiditySingleSwapInfoV3,
getDecreaseLiquiditySingleV3Tx,
getPublicClient,
} from '../../src/viem';

async function main() {
ioc.registerSingleton(IOCKEY_LOGGER, ConsoleLogger);
const amm = AutomatedMarketMakerEnum.enum.UNISWAP_V3;
const chainId = ApertureSupportedChainId.ARBITRUM_MAINNET_CHAIN_ID;
const client = getPublicClient(chainId);
const from = '0x1fFd5d818187917E0043522C3bE583A393c2BbF7';
const tokenId = 4063431;
const zeroForOne = true;
const positionDetails = await PositionDetails.fromPositionId(
chainId,
amm,
BigInt(tokenId),
client,
);
// put weth into pool. take usdc out.
// token0 = weth = 0x82af49447d8a07e3bd95bd0d56f35241523fbab1
// token1 = usdc = 0xaf88d065e77c8cc2239327c5edb3a432268e5831
// tenderly revert due to not enough usdc from optSwapRouter to pool
const decreaseLiquidityOptions: RemoveLiquidityOptions = {
tokenId,
liquidityPercentage: new Percent(10, 100), // position is $3.33 100%WETH. token0=weth, token1=usdc.. should take out $0.33 eth=.33/3116*1e18 token0amount = 1.06e14, $0.07 usdc
slippageTolerance: new Percent(5, 1000),
deadline: Math.floor(Date.now() / 1000 + 60 * 30),
collectOptions: {
expectedCurrencyOwed0: positionDetails.tokensOwed0,
expectedCurrencyOwed1: positionDetails.tokensOwed1,
recipient: from,
},
};
const swapInfos = await getDecreaseLiquiditySingleSwapInfoV3(
decreaseLiquidityOptions,
chainId,
amm,
zeroForOne,
from,
/* tokenPricesUsd= */ ['3225', '1'],
client,
DEFAULT_SOLVERS,
positionDetails,
);
for (const swapInfo of swapInfos) {
const {
solver,
swapData,
amount0,
amount1,
token0FeeAmount,
token1FeeAmount,
} = swapInfo;
const txRequest = await getDecreaseLiquiditySingleV3Tx(
decreaseLiquidityOptions,
zeroForOne,
from,
chainId,
amm,
client,
swapData,
positionDetails,
/* amount0Min= */ amount0,
/* amount1Min= */ amount1,
/* token0FeeAmount= */ token0FeeAmount,
/* token1FeeAmount= */ token1FeeAmount,
);
console.log(
`solver=${solver}, liquidity: ${swapInfo.liquidity}, to=${txRequest.to}, from=${txRequest.from}, data=${txRequest.data}`,
);
}
process.exit(0);
}

main();
Loading

0 comments on commit 7915243

Please sign in to comment.