From c8704165bea8f69d62bbb2498d92c73f7cfa04be Mon Sep 17 00:00:00 2001 From: James Tuckett Date: Thu, 10 Aug 2023 14:15:21 +0100 Subject: [PATCH] chore: remove remaining isV2 / isV3 helpers --- .../dma-library/src/protocols/aave/config.ts | 34 ------------------- .../dma-library/src/protocols/aave/index.ts | 6 ---- packages/dma-library/src/protocols/index.ts | 10 +----- .../src/strategies/aave/common/is-v2.ts | 5 ++- .../src/strategies/aave/common/is-v3.ts | 5 ++- .../strategies/aave/get-current-position.ts | 29 ++++++---------- .../src/strategies/aave/open/open.ts | 13 +++---- 7 files changed, 24 insertions(+), 78 deletions(-) delete mode 100644 packages/dma-library/src/protocols/aave/config.ts diff --git a/packages/dma-library/src/protocols/aave/config.ts b/packages/dma-library/src/protocols/aave/config.ts deleted file mode 100644 index 56bac52a9..000000000 --- a/packages/dma-library/src/protocols/aave/config.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { AAVEStrategyAddresses } from '@dma-library/operations/aave/v2/addresses' -import { AAVEV3StrategyAddresses } from '@dma-library/operations/aave/v3/addresses' - -type IsAaveV3Key = keyof AAVEV3StrategyAddresses -type IsAaveV2Key = keyof AAVEStrategyAddresses -type IsUniqueKeyToAaveV2 = Exclude -type IsUniqueKeyToAaveV3 = Exclude - -const uniqueKeysToAaveV2: IsUniqueKeyToAaveV2[] = [ - 'lendingPool', - 'priceOracle', - 'protocolDataProvider', -] -const uniqueKeysToAaveV3: IsUniqueKeyToAaveV3[] = ['pool', 'aaveOracle', 'poolDataProvider'] - -// Type guard function -export function isAaveV3Addresses( - addresses: AAVEStrategyAddresses | AAVEV3StrategyAddresses, -): addresses is AAVEV3StrategyAddresses { - const addressKeys = Object.keys(addresses) - return uniqueKeysToAaveV3.every(key => addressKeys.includes(key)) -} - -export function isAaveV2Addresses( - addresses: AAVEStrategyAddresses | AAVEV3StrategyAddresses, -): addresses is AAVEStrategyAddresses { - const addressKeys = Object.keys(addresses) - return uniqueKeysToAaveV2.every(key => addressKeys.includes(key)) -} - -/** @deprecated use isAaveV2Addresses */ -export const aaveV2UniqueContractName: 'lendingPool' & IsUniqueKeyToAaveV2 = 'lendingPool' -/** @deprecated use isAaveV3Addresses */ -export const aaveV3UniqueContractName: 'pool' & IsUniqueKeyToAaveV3 = 'pool' diff --git a/packages/dma-library/src/protocols/aave/index.ts b/packages/dma-library/src/protocols/aave/index.ts index 3bf964be7..c9ccc123b 100644 --- a/packages/dma-library/src/protocols/aave/index.ts +++ b/packages/dma-library/src/protocols/aave/index.ts @@ -1,9 +1,3 @@ -export { - aaveV2UniqueContractName, - aaveV3UniqueContractName, - isAaveV2Addresses, - isAaveV3Addresses, -} from './config' export { AaveProtocolData, AaveProtocolDataArgs, diff --git a/packages/dma-library/src/protocols/index.ts b/packages/dma-library/src/protocols/index.ts index 48a3559b5..a4776e182 100644 --- a/packages/dma-library/src/protocols/index.ts +++ b/packages/dma-library/src/protocols/index.ts @@ -1,10 +1,4 @@ -import { - AaveProtocolData, - AaveProtocolDataArgs, - getAaveProtocolData, - isAaveV2Addresses, - isAaveV3Addresses, -} from './aave' +import { AaveProtocolData, AaveProtocolDataArgs, getAaveProtocolData } from './aave' import { calculateAjnaApyPerDays } from './ajna' const aave: { @@ -18,7 +12,5 @@ export const protocols = { aave, } -export { isAaveV2Addresses, isAaveV3Addresses } export { calculateAjnaApyPerDays } - export { calculateAjnaMaxLiquidityWithdraw, getAjnaLiquidationPrice } from './ajna/index' diff --git a/packages/dma-library/src/strategies/aave/common/is-v2.ts b/packages/dma-library/src/strategies/aave/common/is-v2.ts index 54ce2dbf4..dcb0186d0 100644 --- a/packages/dma-library/src/strategies/aave/common/is-v2.ts +++ b/packages/dma-library/src/strategies/aave/common/is-v2.ts @@ -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 } diff --git a/packages/dma-library/src/strategies/aave/common/is-v3.ts b/packages/dma-library/src/strategies/aave/common/is-v3.ts index 453c82d04..17cb88745 100644 --- a/packages/dma-library/src/strategies/aave/common/is-v3.ts +++ b/packages/dma-library/src/strategies/aave/common/is-v3.ts @@ -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 } diff --git a/packages/dma-library/src/strategies/aave/get-current-position.ts b/packages/dma-library/src/strategies/aave/get-current-position.ts index 9d430c123..8de567231 100644 --- a/packages/dma-library/src/strategies/aave/get-current-position.ts +++ b/packages/dma-library/src/strategies/aave/get-current-position.ts @@ -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 { @@ -28,9 +29,17 @@ export async function getCurrentPosition( args: AaveGetCurrentPositionArgs, dependencies: AaveGetCurrentPositionDependencies, ): Promise { - if (isV2(dependencies)) { + if ( + AaveCommon.isV2( + dependencies, + ) + ) { return getCurrentPositionAaveV2(args, dependencies) - } else if (isV3(dependencies)) { + } else if ( + AaveCommon.isV3( + dependencies, + ) + ) { return getCurrentPositionAaveV3(args, dependencies) } else { throw new Error('Invalid Aave version') @@ -168,19 +177,3 @@ async function getCurrentPositionAaveV3( }, ) } - -function isV2( - dependencies: AaveGetCurrentPositionDependencies, -): dependencies is IViewPositionDependencies & { - protocolVersion: AaveVersion.v2 -} { - return dependencies.protocolVersion === AaveVersion.v2 -} - -function isV3( - dependencies: AaveGetCurrentPositionDependencies, -): dependencies is IViewPositionDependencies & { - protocolVersion: AaveVersion.v3 -} { - return dependencies.protocolVersion === AaveVersion.v3 -} diff --git a/packages/dma-library/src/strategies/aave/open/open.ts b/packages/dma-library/src/strategies/aave/open/open.ts index 1eaaaa638..638abb270 100644 --- a/packages/dma-library/src/strategies/aave/open/open.ts +++ b/packages/dma-library/src/strategies/aave/open/open.ts @@ -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, @@ -61,6 +61,7 @@ export interface AaveOpenSharedDependencies { slippage: BigNumber, ) => Promise } + export type AaveV2OpenDependencies = AaveOpenSharedDependencies & WithV2Addresses & WithV2Protocol export type AaveV3OpenDependencies = AaveOpenSharedDependencies & WithV3Addresses & WithV3Protocol export type AaveOpenDependencies = AaveV2OpenDependencies | AaveV3OpenDependencies @@ -167,10 +168,7 @@ async function simulatePositionTransition( */ let currentPosition: Position | undefined let protocolData: Unbox | undefined - if ( - dependencies.protocol.version === AaveVersion.v2 && - isAaveV2Addresses(dependencies.addresses) - ) { + if (AaveCommon.isV2(dependencies)) { currentPosition = await dependencies.protocol.getCurrentPosition( { collateralToken: args.collateralToken, @@ -192,10 +190,7 @@ async function simulatePositionTransition( protocolVersion: dependencies.protocol.version, }) } - if ( - dependencies.protocol.version === AaveVersion.v3 && - isAaveV3Addresses(dependencies.addresses) - ) { + if (AaveCommon.isV3(dependencies)) { currentPosition = await dependencies.protocol.getCurrentPosition( { collateralToken: args.collateralToken,