Skip to content

Commit

Permalink
feat: create close-and-remain ops in library using new definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
zerotucks committed Feb 19, 2024
1 parent 4b057dd commit a2d8b02
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions packages/deploy-configurations/operation-definitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -144,6 +144,6 @@ export const close: AaveV3CloseOperation = async ({

return {
calls: [takeAFlashLoan, setEModeOnCollateral],
operationName: getAaveCloseV3OperationDefinition(network).name,
operationName: getAaveV3CloseAndExitOperationDefinition(network).name,
}
}
Original file line number Diff line number Diff line change
@@ -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<IOperation>

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,
}
}
4 changes: 2 additions & 2 deletions packages/dma-library/src/operations/spark/multiply/close.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -115,6 +115,6 @@ export const close: SparkCloseOperation = async ({

return {
calls: [takeAFlashLoan, returnDebtFunds, returnCollateralFunds],
operationName: getSparkCloseOperationDefinition(network).name,
operationName: getSparkCloseAndExitOperationDefinition(network).name,
}
}

0 comments on commit a2d8b02

Please sign in to comment.