From 192e582d8e3afabec82a482360ed80a2b1877884 Mon Sep 17 00:00:00 2001 From: James Tuckett Date: Mon, 19 Feb 2024 12:45:47 +0000 Subject: [PATCH] refactor: Update Aave V3 close functions and definitions --- .../aave/multiply/v3/close-and-remain.ts | 139 ------------------ .../v3/{close-and-exit.ts => close.ts} | 77 +++++++--- 2 files changed, 55 insertions(+), 161 deletions(-) delete mode 100644 packages/dma-library/src/operations/aave/multiply/v3/close-and-remain.ts rename packages/dma-library/src/operations/aave/multiply/v3/{close-and-exit.ts => close.ts} (66%) diff --git a/packages/dma-library/src/operations/aave/multiply/v3/close-and-remain.ts b/packages/dma-library/src/operations/aave/multiply/v3/close-and-remain.ts deleted file mode 100644 index 9c35eec1..00000000 --- a/packages/dma-library/src/operations/aave/multiply/v3/close-and-remain.ts +++ /dev/null @@ -1,139 +0,0 @@ -import { getAaveV3CloseAndRemainOperationDefinition } from '@deploy-configurations/operation-definitions' -import { ZERO } from '@dma-common/constants' -import { actions } from '@dma-library/actions' -import { - IOperation, - WithCollateral, - WithDebt, - WithFlashloan, - WithNetwork, - WithPositionAndLockedCollateral, - WithProxy, - WithSwap, -} from '@dma-library/types' -import { WithAaveLikeStrategyAddresses } from '@dma-library/types/operations' - -export type CloseArgs = WithCollateral & - WithDebt & - WithSwap & - WithFlashloan & - WithProxy & - WithPositionAndLockedCollateral & - WithAaveLikeStrategyAddresses & - WithNetwork - -export type AaveV3CloseOperation = ({ - collateral, - debt, - swap, - flashloan, - proxy, - position, - addresses, - network, -}: CloseArgs) => Promise - -export const close: AaveV3CloseOperation = async ({ - collateral, - debt, - swap, - flashloan, - proxy, - position: { - collateral: { amount: collateralAmountToBeSwapped }, - }, - addresses, - network, -}) => { - const setEModeOnCollateral = actions.aave.v3.aaveV3SetEMode(network, { - categoryId: 0, - }) - - const setFlashLoanApproval = actions.common.setApproval(network, { - amount: flashloan.token.amount, - asset: flashloan.token.address, - delegate: addresses.lendingPool, - sumAmounts: false, - }) - - const depositFlashLoan = actions.aave.v3.aaveV3Deposit(network, { - amount: flashloan.token.amount, - asset: flashloan.token.address, - sumAmounts: false, - // setAsCollateral: true - }) - - const withdrawCollateralFromAAVE = actions.aave.v3.aaveV3Withdraw(network, { - asset: collateral.address, - amount: collateralAmountToBeSwapped, - to: proxy.address, - }) - - const swapCollateralTokensForDebtTokens = actions.common.swap(network, { - fromAsset: collateral.address, - toAsset: debt.address, - amount: collateralAmountToBeSwapped || ZERO, - receiveAtLeast: swap.receiveAtLeast, - fee: swap.fee, - withData: swap.data, - collectFeeInFromToken: swap.collectFeeFrom === 'sourceToken', - }) - - const swapActionStorageIndex = 4 - const setDebtTokenApprovalOnLendingPool = actions.common.setApproval( - network, - { - asset: debt.address, - delegate: addresses.lendingPool, - amount: ZERO, - sumAmounts: true, - }, - [0, 0, swapActionStorageIndex, 0], - ) - - const paybackInAAVE = actions.aave.v3.aaveV3Payback(network, { - asset: debt.address, - amount: ZERO, - paybackAll: true, - }) - - const withdrawFlashLoan = actions.aave.v3.aaveV3WithdrawAuto( - network, - { - asset: flashloan.token.address, - amount: flashloan.token.amount, - to: addresses.operationExecutor, - }, - [1], - ) - - const returnDebtFunds = actions.common.returnFunds(network, { - asset: debt.isEth ? addresses.tokens.ETH : debt.address, - }) - - const takeAFlashLoan = actions.common.takeAFlashLoanBalancer(network, { - isDPMProxy: proxy.isDPMProxy, - asset: flashloan.token.address, - flashloanAmount: flashloan.token.amount, - isProxyFlashloan: true, - provider: flashloan.provider, - calls: [ - setFlashLoanApproval, - depositFlashLoan, - withdrawCollateralFromAAVE, - swapCollateralTokensForDebtTokens, - setDebtTokenApprovalOnLendingPool, - paybackInAAVE, - withdrawFlashLoan, - // see deploy-configurations/operation-definitions/aave/v3/close-and-remain.ts - // withdrawCollateral, - returnDebtFunds, - // returnCollateralFunds, - ], - }) - - return { - calls: [takeAFlashLoan, setEModeOnCollateral], - operationName: getAaveV3CloseAndRemainOperationDefinition(network).name, - } -} diff --git a/packages/dma-library/src/operations/aave/multiply/v3/close-and-exit.ts b/packages/dma-library/src/operations/aave/multiply/v3/close.ts similarity index 66% rename from packages/dma-library/src/operations/aave/multiply/v3/close-and-exit.ts rename to packages/dma-library/src/operations/aave/multiply/v3/close.ts index 459f68d8..f939c85e 100644 --- a/packages/dma-library/src/operations/aave/multiply/v3/close-and-exit.ts +++ b/packages/dma-library/src/operations/aave/multiply/v3/close.ts @@ -1,4 +1,8 @@ -import { getAaveV3CloseAndExitOperationDefinition } from '@deploy-configurations/operation-definitions' +import { + getAaveV3CloseAndExitOperationDefinition, + getAaveV3CloseAndRemainOperationDefinition, +} from '@deploy-configurations/operation-definitions' +import { Network } from '@deploy-configurations/types/network' import { MAX_UINT, ZERO } from '@dma-common/constants' import { actions } from '@dma-library/actions' import { @@ -21,7 +25,9 @@ export type CloseArgs = WithCollateral & WithProxy & WithPositionAndLockedCollateral & WithAaveLikeStrategyAddresses & - WithNetwork + WithNetwork & { + shouldExit: boolean + } export type AaveV3CloseOperation = ({ collateral, @@ -32,6 +38,7 @@ export type AaveV3CloseOperation = ({ position, addresses, network, + shouldExit, }: CloseArgs) => Promise export const close: AaveV3CloseOperation = async ({ @@ -45,6 +52,7 @@ export const close: AaveV3CloseOperation = async ({ }, addresses, network, + shouldExit, }) => { const setEModeOnCollateral = actions.aave.v3.aaveV3SetEMode(network, { categoryId: 0, @@ -108,18 +116,42 @@ export const close: AaveV3CloseOperation = async ({ [1], ) - const withdrawCollateral = actions.aave.v3.aaveV3Withdraw(network, { - asset: collateral.address, - amount: new BigNumber(MAX_UINT), - to: proxy.address, - }) + let withdrawCollateral + if (!shouldExit) { + withdrawCollateral = actions.aave.v3.aaveV3Withdraw(network, { + asset: collateral.address, + amount: new BigNumber(MAX_UINT), + to: proxy.address, + }) + } const returnDebtFunds = actions.common.returnFunds(network, { asset: debt.isEth ? addresses.tokens.ETH : debt.address, }) - const returnCollateralFunds = actions.common.returnFunds(network, { - asset: collateral.isEth ? addresses.tokens.ETH : collateral.address, + let returnCollateralFunds + if (!shouldExit) { + returnCollateralFunds = actions.common.returnFunds(network, { + asset: collateral.isEth ? addresses.tokens.ETH : collateral.address, + }) + } + + const calls = [ + setFlashLoanApproval, + depositFlashLoan, + withdrawCollateralFromAAVE, + swapCollateralTokensForDebtTokens, + setDebtTokenApprovalOnLendingPool, + paybackInAAVE, + withdrawFlashLoan, + withdrawCollateral, + returnDebtFunds, + returnCollateralFunds, + ] + + // Filter out undefined calls + const filteredCalls = calls.filter(call => { + return call !== undefined }) const takeAFlashLoan = actions.common.takeAFlashLoanBalancer(network, { @@ -128,22 +160,23 @@ export const close: AaveV3CloseOperation = async ({ flashloanAmount: flashloan.token.amount, isProxyFlashloan: true, provider: flashloan.provider, - calls: [ - setFlashLoanApproval, - depositFlashLoan, - withdrawCollateralFromAAVE, - swapCollateralTokensForDebtTokens, - setDebtTokenApprovalOnLendingPool, - paybackInAAVE, - withdrawFlashLoan, - withdrawCollateral, - returnDebtFunds, - returnCollateralFunds, - ], + calls: filteredCalls, }) return { calls: [takeAFlashLoan, setEModeOnCollateral], - operationName: getAaveV3CloseAndExitOperationDefinition(network).name, + operationName: getAaveV3CloseOperationDefinition(network, shouldExit).name, + } +} + +function getAaveV3CloseOperationDefinition(network: Network, shouldExit: boolean) { + if (shouldExit) { + return getAaveV3CloseAndExitOperationDefinition(network) } + + if (!shouldExit) { + return getAaveV3CloseAndRemainOperationDefinition(network) + } + + throw new Error('Invalid operation definition') }