Skip to content

Commit

Permalink
refactor: simplify view layer
Browse files Browse the repository at this point in the history
  • Loading branch information
zerotucks committed Aug 29, 2023
1 parent 471d07c commit e141f6c
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { resolveAaveLikeOperations } from '@dma-library/operations/aave-like'
import * as AaveCommon from '@dma-library/strategies/aave/common'
import { getAaveTokenAddress } from '@dma-library/strategies/aave/common'
import { IOperation } from '@dma-library/types'
import { AaveVersion } from '@dma-library/types/aave'
import * as SwapUtils from '@dma-library/utils/swap'
import { isAaveView, resolveAavelikeViews } from '@dma-library/views/aave-like'
import { IPosition } from '@domain'
Expand Down Expand Up @@ -127,10 +126,7 @@ async function resolveCurrentPositionForProtocol(

if (isAaveView(view)) {
if (!version) throw new Error('Version must be defined when using Aave view')
return await view.getCurrentPosition(
{ ...args, proxy: dependencies.proxy },
{ ...dependencies, protocolVersion: version as AaveVersion },
)
return await view[version]({ ...args, proxy: dependencies.proxy }, { ...dependencies })
}
return await view.getCurrentPosition({ ...args, proxy: dependencies.proxy }, { ...dependencies })
return await view({ ...args, proxy: dependencies.proxy }, { ...dependencies })
}
12 changes: 4 additions & 8 deletions packages/dma-library/src/strategies/aave/borrow/change-debt.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { ZERO } from '@dma-common/constants'
import { AaveLikeStrategyAddresses } from '@dma-library/operations/aave-like'
import { IViewPositionDependencies, IViewPositionParams, WithDebtChange } from '@dma-library/types'
import { AavePosition, AAVETokens, AaveVersion } from '@dma-library/types/aave'
import { getCurrentPosition } from '@dma-library/views/aave'
import { AavePosition, AAVETokens } from '@dma-library/types/aave'
import { views } from '@dma-library/views'

export type AaveV2ChangeDebt = (
args: IViewPositionParams<AAVETokens> & WithDebtChange<AAVETokens>,
{ addresses, provider }: IViewPositionDependencies<AaveLikeStrategyAddresses>,
) => Promise<AavePosition>

export const changeDebt: AaveV2ChangeDebt = async (args, { addresses, provider }) => {
const currentPosition = await getCurrentPosition(args, {
const currentPosition = await views.aave.v2(args, {
addresses,
provider,
protocolVersion: AaveVersion.v2,
})

if (currentPosition.debt.amount.gt(ZERO)) {
throw new Error('Debt must be zero to change debt')
}

return await getCurrentPosition(
{ ...args, debtToken: args.newDebtToken },
{ addresses, provider, protocolVersion: AaveVersion.v2 },
)
return await views.aave.v2({ ...args, debtToken: args.newDebtToken }, { addresses, provider })
}
26 changes: 13 additions & 13 deletions packages/dma-library/src/strategies/aave/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getAaveProtocolData } from '@dma-library/protocols/aave/get-aave-protocol-data'
import { PositionTransition } from '@dma-library/types'
import { AaveVersion } from '@dma-library/types/aave'
import { getCurrentPosition } from '@dma-library/views/aave'
import { views } from '@dma-library/views'

import { AaveV2ChangeDebt, changeDebt } from './borrow/change-debt'
import { AaveV2DepositBorrow, AaveV3DepositBorrow, depositBorrow } from './borrow/deposit-borrow'
Expand Down Expand Up @@ -34,7 +34,7 @@ export const aave: {
v2: {
changeDebt: AaveV2ChangeDebt
depositBorrow: AaveV2DepositBorrow
openDepositAndBorrowDebt: AaveV2OpenDepositBorrow
openDepositBorrow: AaveV2OpenDepositBorrow
paybackWithdraw: AaveV2PaybackWithdraw
}
v3: {
Expand Down Expand Up @@ -82,16 +82,16 @@ export const aave: {
...dependencies,
protocol: {
version: AaveVersion.v2,
getCurrentPosition,
getCurrentPosition: views.aave.v2,
getProtocolData: getAaveProtocolData,
},
}),
openDepositAndBorrowDebt: (args, dependencies) =>
openDepositBorrow: (args, dependencies) =>
openDepositBorrow(args, {
...dependencies,
protocol: {
version: AaveVersion.v2,
getCurrentPosition,
getCurrentPosition: views.aave.v2,
getProtocolData: getAaveProtocolData,
},
}),
Expand All @@ -100,7 +100,7 @@ export const aave: {
...dependencies,
protocol: {
version: AaveVersion.v2,
getCurrentPosition,
getCurrentPosition: views.aave.v2,
getProtocolData: getAaveProtocolData,
},
}),
Expand All @@ -111,7 +111,7 @@ export const aave: {
...dependencies,
protocol: {
version: AaveVersion.v3,
getCurrentPosition,
getCurrentPosition: views.aave.v3,
getProtocolData: getAaveProtocolData,
},
}),
Expand All @@ -120,7 +120,7 @@ export const aave: {
...dependencies,
protocol: {
version: AaveVersion.v3,
getCurrentPosition,
getCurrentPosition: views.aave.v3,
getProtocolData: getAaveProtocolData,
},
}),
Expand All @@ -129,7 +129,7 @@ export const aave: {
...dependencies,
protocol: {
version: AaveVersion.v3,
getCurrentPosition,
getCurrentPosition: views.aave.v3,
getProtocolData: getAaveProtocolData,
},
}),
Expand All @@ -142,7 +142,7 @@ export const aave: {
...dependencies,
protocol: {
version: AaveVersion.v2,
getCurrentPosition,
getCurrentPosition: views.aave.v2,
getProtocolData: getAaveProtocolData,
},
}),
Expand All @@ -153,7 +153,7 @@ export const aave: {
...dependencies,
protocol: {
version: AaveVersion.v2,
getCurrentPosition,
getCurrentPosition: views.aave.v2,
getProtocolData: getAaveProtocolData,
},
}),
Expand All @@ -164,7 +164,7 @@ export const aave: {
...dependencies,
protocol: {
version: AaveVersion.v3,
getCurrentPosition,
getCurrentPosition: views.aave.v3,
getProtocolData: getAaveProtocolData,
},
}),
Expand All @@ -175,7 +175,7 @@ export const aave: {
...dependencies,
protocol: {
version: AaveVersion.v3,
getCurrentPosition,
getCurrentPosition: views.aave.v3,
getProtocolData: getAaveProtocolData,
},
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export type SparkDepositBorrowArgs = StrategyParams.WithAaveLikeBorrowStrategyAr
StrategyParams.WithDepositCollateral &
StrategyParams.WithBorrowDebt

export type SparkDepositBorrowDependencies = StrategyParams.WithAaveLikeStrategyDependencies &
export type SparkDepositBorrowDependencies = Omit<
StrategyParams.WithAaveLikeStrategyDependencies,
'protocolType'
> &
StrategyParams.WithOptionalSwap

export type IDepositBorrowStrategy = Strategies.IStrategy & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export type SparkOpenDepositBorrowArgs = SparkDepositBorrowArgs

type IOpenDepositBorrowStrategy = IDepositBorrowStrategy

export type SparkOpenDepositBorrowDependencies = SparkDepositBorrowDependencies &
export type SparkOpenDepositBorrowDependencies = Omit<
SparkDepositBorrowDependencies,
'currentPosition'
> &
StrategyParams.WithPositionType
export type SparkOpenDepositBorrow = (
args: SparkOpenDepositBorrowArgs,
Expand Down
25 changes: 0 additions & 25 deletions packages/dma-library/src/views/aave/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as AaveCommon from '@dma-library/strategies/aave/common'
import { AavePosition } from '@dma-library/types/aave'
import {
AaveGetCurrentPositionArgs,
AaveGetCurrentPositionDependencies,
AaveV2GetCurrentPositionDependencies,
AaveV3GetCurrentPositionDependencies,
} from '@dma-library/views/aave/types'
Expand All @@ -16,7 +15,6 @@ export {
AaveV3GetCurrentPositionDependencies,
}
export type AaveView = {
getCurrentPosition: AaveGetCurrentPosition
v2: (
args: AaveGetCurrentPositionArgs,
dependencies: Omit<AaveV2GetCurrentPositionDependencies, 'protocolVersion'>,
Expand All @@ -27,29 +25,6 @@ export type AaveView = {
) => Promise<AavePosition>
}

export type AaveGetCurrentPosition = (
args: AaveGetCurrentPositionArgs,
addresses: AaveGetCurrentPositionDependencies,
) => Promise<AavePosition>

export const getCurrentPosition: AaveGetCurrentPosition = async (args, dependencies) => {
if (
AaveCommon.isV2<AaveGetCurrentPositionDependencies, AaveV2GetCurrentPositionDependencies>(
dependencies,
)
) {
return getCurrentPositionAaveV2(args, dependencies)
} else if (
AaveCommon.isV3<AaveGetCurrentPositionDependencies, AaveV3GetCurrentPositionDependencies>(
dependencies,
)
) {
return getCurrentPositionAaveV3(args, dependencies)
} else {
throw new Error('Invalid Aave version')
}
}

export type AaveV2GetCurrentPosition = (
args: AaveGetCurrentPositionArgs,
dependencies: AaveV2GetCurrentPositionDependencies,
Expand Down
7 changes: 2 additions & 5 deletions packages/dma-library/src/views/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { AaveVersion } from '@dma-library/types/aave'
import { AaveView, getCurrentPositionAaveV2, getCurrentPositionAaveV3 } from './aave'
import type { GetEarnData } from './ajna'
import { getEarnPosition, getPosition } from './ajna'
import { getCurrentPosition, SparkView } from './spark'
import { getCurrentSparkPosition, SparkView } from './spark'

const aave: AaveView = {
getCurrentPosition,
v2: (args, dependencies) =>
getCurrentPositionAaveV2(args, {
...dependencies,
Expand All @@ -18,9 +17,7 @@ const aave: AaveView = {
protocolVersion: AaveVersion.v3,
}),
}
const spark: SparkView = {
getCurrentPosition: getCurrentPosition,
}
const spark: SparkView = getCurrentSparkPosition
const ajna = {
getPosition,
getEarnPosition,
Expand Down
6 changes: 2 additions & 4 deletions packages/dma-library/src/views/spark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import {
} from '@dma-library/views/spark/types'
import BigNumber from 'bignumber.js'

export type SparkView = {
getCurrentPosition: SparkGetCurrentPosition
}
export type SparkView = SparkGetCurrentPosition

export type SparkGetCurrentPosition = (
args: SparkGetCurrentPositionArgs,
addresses: SparkGetCurrentPositionDependencies,
) => Promise<AavePosition>

export const getCurrentPosition: SparkGetCurrentPosition = async (args, dependencies) => {
export const getCurrentSparkPosition: SparkGetCurrentPosition = async (args, dependencies) => {
const debtToken = args.debtToken
const collateralToken = args.collateralToken
const { collateralTokenAddress, debtTokenAddress } = AaveCommon.getAaveTokenAddresses(
Expand Down

0 comments on commit e141f6c

Please sign in to comment.