Skip to content

Commit

Permalink
chore: remove remaining isV2 / isV3 helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
zerotucks committed Aug 10, 2023
1 parent e1f128d commit c870416
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 78 deletions.
34 changes: 0 additions & 34 deletions packages/dma-library/src/protocols/aave/config.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/dma-library/src/protocols/aave/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export {
aaveV2UniqueContractName,
aaveV3UniqueContractName,
isAaveV2Addresses,
isAaveV3Addresses,
} from './config'
export {
AaveProtocolData,
AaveProtocolDataArgs,
Expand Down
10 changes: 1 addition & 9 deletions packages/dma-library/src/protocols/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
AaveProtocolData,
AaveProtocolDataArgs,
getAaveProtocolData,
isAaveV2Addresses,
isAaveV3Addresses,
} from './aave'
import { AaveProtocolData, AaveProtocolDataArgs, getAaveProtocolData } from './aave'
import { calculateAjnaApyPerDays } from './ajna'

const aave: {
Expand All @@ -18,7 +12,5 @@ export const protocols = {
aave,
}

export { isAaveV2Addresses, isAaveV3Addresses }
export { calculateAjnaApyPerDays }

export { calculateAjnaMaxLiquidityWithdraw, getAjnaLiquidationPrice } from './ajna/index'
5 changes: 4 additions & 1 deletion packages/dma-library/src/strategies/aave/common/is-v2.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { AaveVersion } from '@dma-library/strategies'

export function isV2<
GeneralDeps extends { protocol: { version: AaveVersion } },
GeneralDeps extends { protocol: { version: AaveVersion } } | { protocolVersion: AaveVersion },
SpecificDeps extends GeneralDeps,
>(dependencies: GeneralDeps): dependencies is SpecificDeps {
if ('protocolVersion' in dependencies) {
return dependencies.protocolVersion === AaveVersion.v2
}
return dependencies.protocol.version === AaveVersion.v2
}
5 changes: 4 additions & 1 deletion packages/dma-library/src/strategies/aave/common/is-v3.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { AaveVersion } from '@dma-library/strategies'

export function isV3<
GeneralDeps extends { protocol: { version: AaveVersion } },
GeneralDeps extends { protocol: { version: AaveVersion } } | { protocolVersion: AaveVersion },
SpecificDeps extends GeneralDeps,
>(dependencies: GeneralDeps): dependencies is SpecificDeps {
if ('protocolVersion' in dependencies) {
return dependencies.protocolVersion === AaveVersion.v3
}
return dependencies.protocol.version === AaveVersion.v3
}
29 changes: 11 additions & 18 deletions packages/dma-library/src/strategies/aave/get-current-position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AAVEStrategyAddresses } from '../../operations/aave/v2'
import { AAVEV3StrategyAddresses } from '../../operations/aave/v3'
import { IViewPositionDependencies, IViewPositionParams } from '../../types'
import { AavePosition, AAVETokens } from '../../types/aave'
import * as AaveCommon from './common'
import { getAaveTokenAddresses } from './get-aave-token-addresses'

export enum AaveVersion {
Expand All @@ -28,9 +29,17 @@ export async function getCurrentPosition(
args: AaveGetCurrentPositionArgs,
dependencies: AaveGetCurrentPositionDependencies,
): Promise<AavePosition> {
if (isV2(dependencies)) {
if (
AaveCommon.isV2<AaveGetCurrentPositionDependencies, AaveV2GetCurrentPositionDependencies>(
dependencies,
)
) {
return getCurrentPositionAaveV2(args, dependencies)
} else if (isV3(dependencies)) {
} else if (
AaveCommon.isV3<AaveGetCurrentPositionDependencies, AaveV3GetCurrentPositionDependencies>(
dependencies,
)
) {
return getCurrentPositionAaveV3(args, dependencies)
} else {
throw new Error('Invalid Aave version')
Expand Down Expand Up @@ -168,19 +177,3 @@ async function getCurrentPositionAaveV3(
},
)
}

function isV2(
dependencies: AaveGetCurrentPositionDependencies,
): dependencies is IViewPositionDependencies<AAVEStrategyAddresses> & {
protocolVersion: AaveVersion.v2
} {
return dependencies.protocolVersion === AaveVersion.v2
}

function isV3(
dependencies: AaveGetCurrentPositionDependencies,
): dependencies is IViewPositionDependencies<AAVEV3StrategyAddresses> & {
protocolVersion: AaveVersion.v3
} {
return dependencies.protocolVersion === AaveVersion.v3
}
13 changes: 4 additions & 9 deletions packages/dma-library/src/strategies/aave/open/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { calculateFee } from '@dma-common/utils/swap'
import { AAVEStrategyAddresses, AAVEV3StrategyAddresses } from '@dma-library/index'
import { operations } from '@dma-library/operations'
import { OpenOperationArgs } from '@dma-library/operations/aave/v3/open'
import { isAaveV2Addresses, isAaveV3Addresses } from '@dma-library/protocols/aave/config'
import { AaveProtocolData } from '@dma-library/protocols/aave/get-aave-protocol-data'
import * as AaveCommon from '@dma-library/strategies/aave/common'
import {
getAaveTokenAddress,
getAaveTokenAddresses,
Expand Down Expand Up @@ -61,6 +61,7 @@ export interface AaveOpenSharedDependencies {
slippage: BigNumber,
) => Promise<SwapData>
}

export type AaveV2OpenDependencies = AaveOpenSharedDependencies & WithV2Addresses & WithV2Protocol
export type AaveV3OpenDependencies = AaveOpenSharedDependencies & WithV3Addresses & WithV3Protocol
export type AaveOpenDependencies = AaveV2OpenDependencies | AaveV3OpenDependencies
Expand Down Expand Up @@ -167,10 +168,7 @@ async function simulatePositionTransition(
*/
let currentPosition: Position | undefined
let protocolData: Unbox<AaveProtocolData> | undefined
if (
dependencies.protocol.version === AaveVersion.v2 &&
isAaveV2Addresses(dependencies.addresses)
) {
if (AaveCommon.isV2<AaveOpenDependencies, AaveV2OpenDependencies>(dependencies)) {
currentPosition = await dependencies.protocol.getCurrentPosition(
{
collateralToken: args.collateralToken,
Expand All @@ -192,10 +190,7 @@ async function simulatePositionTransition(
protocolVersion: dependencies.protocol.version,
})
}
if (
dependencies.protocol.version === AaveVersion.v3 &&
isAaveV3Addresses(dependencies.addresses)
) {
if (AaveCommon.isV3<AaveOpenDependencies, AaveV3OpenDependencies>(dependencies)) {
currentPosition = await dependencies.protocol.getCurrentPosition(
{
collateralToken: args.collateralToken,
Expand Down

0 comments on commit c870416

Please sign in to comment.