Skip to content

Commit

Permalink
chore: refactor reallocateTo action getter
Browse files Browse the repository at this point in the history
  • Loading branch information
halaprix committed Dec 2, 2024
1 parent e50fe2f commit a9bbe59
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ActionCall, IOperation, MorphoBlueMarket } from '@dma-library/types'
import BigNumber from 'bignumber.js'

import { MorphoBlueStrategyAddresses } from '../addresses'
import { getReallocateToAction } from '../helpers/reallocate'

export type MorphoBlueBorrowArgs = {
morphoBlueMarket: MorphoBlueMarket
Expand All @@ -24,11 +25,10 @@ export const borrow: MorphoBlueBorrowOperation = async (
addresses,
network,
) => {
const reallocate = getReallocateToAction(network, reallocateData)
// Import ActionCall as it assists type generation
const calls: ActionCall[] = [
actions.morphoblue.reallocate(network, {
data: reallocateData,
}),
reallocate,
actions.morphoblue.borrow(network, {
morphoBlueMarket: morphoBlueMarket,
amount: amountToBorrow,
Expand All @@ -40,7 +40,6 @@ export const borrow: MorphoBlueBorrowOperation = async (
asset: isEthToken ? addresses.tokens.ETH : morphoBlueMarket.loanToken,
}),
]
calls[0].skipped = reallocateData.length === 0
calls[2].skipped = !isEthToken

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Network } from '@deploy-configurations/types/network'
import { actions } from '@dma-library/actions'

/**
* Creates a reallocate action for the MorphoBlue protocol.
*
* @param network - The network on which the action is being executed.
* @param reallocateData - An array of strings containing data for reallocation.
*
* If the reallocateData array is empty, the action will be marked as skipped.
* This allows for conditional execution based on the presence of reallocation data.
*
* @returns The reallocate action object, which may be skipped if no data is provided.
*/
export function getReallocateToAction(network: Network, reallocateData: string[]) {
const reallocateAction = actions.morphoblue.reallocate(network, {
data: reallocateData,
})
if (reallocateData.length === 0) {
reallocateAction.skipped = true
}
return reallocateAction
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getMorphoBlueAdjustUpOperationDefinition } from '@deploy-configurations/operation-definitions'
import { ZERO } from '@dma-common/constants'
import { actions } from '@dma-library/actions'
import { getReallocateToAction } from '../helpers/reallocate'
import { ZERO } from '@dma-common/constants'
import { IOperation } from '@dma-library/types'
import {
WithCollateral,
Expand Down Expand Up @@ -76,9 +77,7 @@ export const adjustRiskUp: MorphoBlueAdjustUpOperation = async ({
})
wrapEth.skipped = !collateral.isEth

const reallocate = actions.morphoblue.reallocate(network, {
data: reallocateData,
})
const reallocate = getReallocateToAction(network, reallocateData)
// No previous actions store values with OpStorage
const swapActionStorageIndex = 1
const swapDebtTokensForCollateralTokens = actions.common.swap(network, {
Expand Down Expand Up @@ -143,7 +142,7 @@ export const adjustRiskUp: MorphoBlueAdjustUpOperation = async ({
})

return {
calls: [{ ...reallocate, skipped: reallocateData.length === 0 }, takeAFlashLoan],
calls: [reallocate, takeAFlashLoan,],
operationName: getMorphoBlueAdjustUpOperationDefinition(network).name,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '@dma-library/types/operations'
import BigNumber from 'bignumber.js'
import { ethers } from 'ethers'
import { getReallocateToAction } from '../helpers/reallocate'

export type MorphoBlueOpenOperationArgs = WithMorphoBlueMarket &
WithCollateral &
Expand Down Expand Up @@ -86,9 +87,7 @@ export const open: MorphoBlueOpenOperation = async ({
const hasAmountToDeposit = depositAmount.gt(ZERO)
pullCollateralTokensToProxy.skipped = !hasAmountToDeposit || collateral.isEth
wrapEth.skipped = !debt.isEth && !collateral.isEth
const reallocate = actions.morphoblue.reallocate(network, {
data: reallocateData,
})
const reallocate = getReallocateToAction(network, reallocateData)
// No previous actions store values with OpStorage
const swapActionStorageIndex = 1
const swapDebtTokensForCollateralTokens = actions.common.swap(network, {
Expand Down Expand Up @@ -163,7 +162,7 @@ export const open: MorphoBlueOpenOperation = async ({
})

return {
calls: [{ ...reallocate, skipped: reallocateData.length === 0 }, takeAFlashLoan],
calls: [reallocate, takeAFlashLoan],
operationName: getMorphoBlueOpenOperationDefinition(network).name,
}
}

0 comments on commit a9bbe59

Please sign in to comment.