diff --git a/packages/deploy-configurations/operation-definitions/aave/v3/close-and-remain.ts b/packages/deploy-configurations/operation-definitions/aave/v3/close-and-remain.ts index 488da111..59c1ade1 100644 --- a/packages/deploy-configurations/operation-definitions/aave/v3/close-and-remain.ts +++ b/packages/deploy-configurations/operation-definitions/aave/v3/close-and-remain.ts @@ -49,7 +49,7 @@ export function getAaveV3CloseAndRemainOperationDefinition(network: Network) { hash: getActionHash(SERVICE_REGISTRY_NAMES.aave.v3.WITHDRAW_AUTO), optional: false, }, - // @dev see comment above + // @remarks see comment above // { // hash: getActionHash(SERVICE_REGISTRY_NAMES.aave.v3.WITHDRAW), // optional: false, @@ -58,7 +58,7 @@ export function getAaveV3CloseAndRemainOperationDefinition(network: Network) { hash: getActionHash(SERVICE_REGISTRY_NAMES.common.RETURN_FUNDS), optional: false, }, - // @dev see comment above + // @remarks see comment above // { // hash: getActionHash(SERVICE_REGISTRY_NAMES.common.RETURN_FUNDS), // optional: false, diff --git a/packages/deploy-configurations/operation-definitions/index.ts b/packages/deploy-configurations/operation-definitions/index.ts index 45faee7e..194479b3 100644 --- a/packages/deploy-configurations/operation-definitions/index.ts +++ b/packages/deploy-configurations/operation-definitions/index.ts @@ -12,7 +12,8 @@ export { getAavePaybackWithdrawV2OperationDefinition } from './aave/v2/payback-w export { getAaveAdjustDownV3OperationDefinition } from './aave/v3/adjust-down' export { getAaveAdjustUpV3OperationDefinition } from './aave/v3/adjust-up' export { getAaveBorrowV3OperationDefinition } from './aave/v3/borrow' -export { getAaveCloseV3OperationDefinition } from './aave/v3/close' +export { getAaveV3CloseAndExitOperationDefinition } from './aave/v3/close-and-exit' +export { getAaveV3CloseAndRemainOperationDefinition } from './aave/v3/close-and-remain' export { getAaveDepositV3OperationDefinition } from './aave/v3/deposit' export { getAaveDepositBorrowV3OperationDefinition } from './aave/v3/deposit-borrow' export { getAaveOpenV3OperationDefinition } from './aave/v3/open' @@ -33,7 +34,7 @@ export { getSparkDepositBorrowOperationDefinition } from './spark/borrow' export { getSparkOpenDepositBorrowOperationDefinition } from './spark/borrow' export { getSparkPaybackWithdrawOperationDefinition } from './spark/borrow' export { getSparkOpenOperationDefinition } from './spark/multiply' -export { getSparkCloseOperationDefinition } from './spark/multiply' +export { getSparkCloseAndExitOperationDefinition } from './spark/multiply' export { getSparkAdjustDownOperationDefinition } from './spark/multiply' export { getSparkAdjustUpOperationDefinition } from './spark/multiply' diff --git a/packages/deploy-configurations/operation-definitions/spark/multiply/close-and-remain.ts b/packages/deploy-configurations/operation-definitions/spark/multiply/close-and-remain.ts deleted file mode 100644 index 61933a2c..00000000 --- a/packages/deploy-configurations/operation-definitions/spark/multiply/close-and-remain.ts +++ /dev/null @@ -1,64 +0,0 @@ -import { loadContractNames, OPERATION_NAMES } from '@deploy-configurations/constants' -import { Network } from '@deploy-configurations/types/network' -import { getActionHash } from '@deploy-configurations/utils/action-hash' - -/** - * Operation Definition for Spark Close and Remain - * - * @remarks - * Actions not required relative to Close and Exit left for context - * This operation is used to close a position and remain with the collateral in protocol - * - * @param network - */ -export function getSparkCloseAndRemainOperationDefinition(network: Network) { - const SERVICE_REGISTRY_NAMES = loadContractNames(network) - - return { - name: OPERATION_NAMES.spark.CLOSE_AND_REMAIN, - actions: [ - { - hash: getActionHash(SERVICE_REGISTRY_NAMES.common.TAKE_A_FLASHLOAN), - optional: false, - }, - { - hash: getActionHash(SERVICE_REGISTRY_NAMES.common.SET_APPROVAL), - optional: false, - }, - { - hash: getActionHash(SERVICE_REGISTRY_NAMES.spark.PAYBACK), - optional: false, - }, - { - hash: getActionHash(SERVICE_REGISTRY_NAMES.spark.SET_EMODE), - optional: false, - }, - { - hash: getActionHash(SERVICE_REGISTRY_NAMES.spark.WITHDRAW), - optional: false, - }, - { - hash: getActionHash(SERVICE_REGISTRY_NAMES.common.SWAP_ACTION), - optional: false, - }, - { - hash: getActionHash(SERVICE_REGISTRY_NAMES.common.SEND_TOKEN), - optional: false, - }, - { - hash: getActionHash(SERVICE_REGISTRY_NAMES.common.UNWRAP_ETH), - optional: true, - }, - // @dev see comment above - // { - // hash: getActionHash(SERVICE_REGISTRY_NAMES.common.RETURN_FUNDS), - // optional: false, - // }, - { - hash: getActionHash(SERVICE_REGISTRY_NAMES.common.RETURN_FUNDS), - optional: false, - }, - ], - log: false, - } -} diff --git a/packages/deploy-configurations/operation-definitions/spark/multiply/index.ts b/packages/deploy-configurations/operation-definitions/spark/multiply/index.ts index 65f67fa5..6dca5a27 100644 --- a/packages/deploy-configurations/operation-definitions/spark/multiply/index.ts +++ b/packages/deploy-configurations/operation-definitions/spark/multiply/index.ts @@ -1,4 +1,4 @@ export { getSparkAdjustDownOperationDefinition } from './adjust-down' export { getSparkAdjustUpOperationDefinition } from './adjust-up' -export { getSparkCloseOperationDefinition } from './close' +export { getSparkCloseAndExitOperationDefinition } from './close-and-exit' export { getSparkOpenOperationDefinition } from './open' diff --git a/packages/dma-library/src/operations/aave/multiply/v3/close.ts b/packages/dma-library/src/operations/aave/multiply/v3/close-and-exit.ts similarity index 95% rename from packages/dma-library/src/operations/aave/multiply/v3/close.ts rename to packages/dma-library/src/operations/aave/multiply/v3/close-and-exit.ts index 7a1836f7..459f68d8 100644 --- a/packages/dma-library/src/operations/aave/multiply/v3/close.ts +++ b/packages/dma-library/src/operations/aave/multiply/v3/close-and-exit.ts @@ -1,4 +1,4 @@ -import { getAaveCloseV3OperationDefinition } from '@deploy-configurations/operation-definitions' +import { getAaveV3CloseAndExitOperationDefinition } from '@deploy-configurations/operation-definitions' import { MAX_UINT, ZERO } from '@dma-common/constants' import { actions } from '@dma-library/actions' import { @@ -144,6 +144,6 @@ export const close: AaveV3CloseOperation = async ({ return { calls: [takeAFlashLoan, setEModeOnCollateral], - operationName: getAaveCloseV3OperationDefinition(network).name, + operationName: getAaveV3CloseAndExitOperationDefinition(network).name, } } 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 new file mode 100644 index 00000000..9c35eec1 --- /dev/null +++ b/packages/dma-library/src/operations/aave/multiply/v3/close-and-remain.ts @@ -0,0 +1,139 @@ +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/spark/multiply/close.ts b/packages/dma-library/src/operations/spark/multiply/close.ts index 999734c2..9673452d 100644 --- a/packages/dma-library/src/operations/spark/multiply/close.ts +++ b/packages/dma-library/src/operations/spark/multiply/close.ts @@ -1,4 +1,4 @@ -import { getSparkCloseOperationDefinition } from '@deploy-configurations/operation-definitions' +import { getSparkCloseAndExitOperationDefinition } from '@deploy-configurations/operation-definitions' import { FEE_BASE, MAX_UINT, ZERO } from '@dma-common/constants' import { actions } from '@dma-library/actions' import { BALANCER_FEE } from '@dma-library/config/flashloan-fees' @@ -115,6 +115,6 @@ export const close: SparkCloseOperation = async ({ return { calls: [takeAFlashLoan, returnDebtFunds, returnCollateralFunds], - operationName: getSparkCloseOperationDefinition(network).name, + operationName: getSparkCloseAndExitOperationDefinition(network).name, } }