Skip to content

Commit

Permalink
Pw/refinance-fix-bugs (#3948)
Browse files Browse the repository at this point in the history
* Fixed BUG Any protocol and pool to Aave V3 Asset [sc-15714]

* Added refinance supported networks check

* Added dpm and owner addresses to raw positon info

* Type Fix

* Added comment

* Disabled emode for stablecoins disabled on aave

* Updated emode logic

* Added missing params
  • Loading branch information
piotrwitek committed Jun 17, 2024
1 parent 0a79993 commit d535728
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 38 deletions.
2 changes: 2 additions & 0 deletions components/portfolio/positions/PortfolioPositionBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PortfolioPositionAutomationIcons } from 'components/portfolio/positions
import { PortfolioPositionBlockDetail } from 'components/portfolio/positions/PortfolioPositionBlockDetail'
import { ProtocolLabel } from 'components/ProtocolLabel'
import dayjs from 'dayjs'
import { isRefinanceSupportedNetwork } from 'features/aave/helpers/isRefinanceSupportedNetwork'
import { shouldShowPairId } from 'features/omni-kit/helpers'
import { OmniProductType } from 'features/omni-kit/types'
import { RefinancePortfolioBanner } from 'features/refinance/components'
Expand Down Expand Up @@ -100,6 +101,7 @@ export const PortfolioPositionBlock = ({ position }: { position: PortfolioPositi
}}
>
{isRefinanceEnabled &&
isRefinanceSupportedNetwork(position.network) &&
position.availableToRefinance &&
position.netValue >= emptyPortfolioPositionNetValueThreshold && (
<RefinancePortfolioBanner position={position} />
Expand Down
5 changes: 5 additions & 0 deletions features/aave/helpers/isRefinanceSupportedNetwork.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NetworkNames } from 'blockchain/networks'

export const isRefinanceSupportedNetwork = (networkName: NetworkNames) => {
return networkName === NetworkNames.ethereumMainnet
}
53 changes: 49 additions & 4 deletions features/refinance/components/RefinancePortfolioBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { ProtocolName } from '@summer_fi/summerfi-sdk-common'
import { useAccountContext } from 'components/context/AccountContextProvider'
import { useRefinanceGeneralContext } from 'features/refinance/contexts/RefinanceGeneralContext'
import { RefinanceModalController } from 'features/refinance/controllers'
import { getRefinanceContextInput } from 'features/refinance/helpers'
import {
getAaveLikePoolId,
getAaveLikePositionId,
getMorphoPositionId,
getRefinanceContextInput,
} from 'features/refinance/helpers'
import { getMorphoPoolId } from 'features/refinance/helpers/getMorphoPoolId'
import { omniProductTypeToSDKType } from 'features/refinance/helpers/omniProductTypeToSDKType'
import { useWalletManagement } from 'features/web3OnBoard/useConnection'
import type { PortfolioPosition } from 'handlers/portfolio/types'
Expand Down Expand Up @@ -106,17 +112,50 @@ export const RefinancePortfolioBanner: FC<RefinancePortfolioBannerProps> = ({ po
borrowRate,
maxLtv,
ltv,
poolId,
positionId,
poolId: poolIdRaw,
positionId: positionIdRaw,
pairId,
collateralAmount,
collateralPrice,
debtAmount,
debtPrice,
liquidationPrice,
ethPrice,
dpmAddress,
// ownerAddress,
} = position.rawPositionDetails

let positionId: any = positionIdRaw
let poolId: any = poolIdRaw

// we need to override raw positionId and poolId for Aavelike and Morpho as they require class instance
switch (protocol) {
case LendingProtocol.AaveV3:
positionId = getAaveLikePositionId(LendingProtocol.AaveV3, positionId.id)
poolId = getAaveLikePoolId(
LendingProtocol.SparkV3,
poolId.protocol.chainInfo,
poolId.collateralToken,
poolId.debtToken,
poolId.emodeType,
)
break
case LendingProtocol.SparkV3:
positionId = getAaveLikePositionId(LendingProtocol.SparkV3, positionId.id)
poolId = getAaveLikePoolId(
LendingProtocol.SparkV3,
poolId.protocol.chainInfo,
poolId.collateralToken,
poolId.debtToken,
poolId.emodeType,
)
break
case LendingProtocol.MorphoBlue:
positionId = getMorphoPositionId(positionId.id)
poolId = getMorphoPoolId(poolId.protocol.chainInfo, poolId.marketId)
break
}

// To be useful once we will have clear definition on all copy variants within banner
// const refinanceToProtocols = {
// [LendingProtocol.Maker]: [LendingProtocol.SparkV3],
Expand Down Expand Up @@ -216,7 +255,13 @@ export const RefinancePortfolioBanner: FC<RefinancePortfolioBannerProps> = ({ po
positionType: omniProductTypeToSDKType(productType),
pairId,
isOwner: wallet?.address.toLowerCase() === portfolioAddress?.toLowerCase(),
owner: undefined,
owner: dpmAddress,
dpm: dpmAddress
? {
id: positionId.id,
address: dpmAddress,
}
: undefined,
})

handleSetContext(contextInput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const RefinanceFlowSidebarController = () => {
token: 'ETH',
// Take only proxies without CreatePosition events
filterConsumedProxy: async (events) => events.length === 0,
// will trigger on DPM step
onEverythingReady: ({ availableProxies }) => {
// Check if owner is already dpm and use it as dpm for refinance, if not fallback to first available dpm
const dpm =
Expand Down
28 changes: 1 addition & 27 deletions features/refinance/emodeStablecoinTokens.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1 @@
export const emodeStablecoinTokensUpperCase = [
'ADAI',
'AETHDAI',
'AETHLUSD',
'AETHPYUSD',
'AETHSDAI',
'AETHUSDC',
'AETHUSDT',
'AUSDC',
'AUSDT',
'CDAI',
'CRVUSD',
'CUSDC',
'CUSDCV3',
'DAI',
'FRAX',
'GHO',
'GUSD',
'PYUSD',
'SDAI',
'SUSD',
'SUSDE',
'USDC.E',
'USDC',
'USDE',
'USDT',
]
export const emodeStablecoinTokensSparkUpperCase = ['SDAI', 'USDC', 'USDT']
9 changes: 7 additions & 2 deletions features/refinance/helpers/getEmode.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { EmodeType } from '@summer_fi/summerfi-sdk-client'
import type { IToken } from '@summer_fi/summerfi-sdk-common'
import { emodeEthCorrelatedTokensUpperCase } from 'features/refinance/emodeEthCorrelatedTokens'
import { emodeStablecoinTokensUpperCase } from 'features/refinance/emodeStablecoinTokens'
import { emodeStablecoinTokensSparkUpperCase } from 'features/refinance/emodeStablecoinTokens'
import { LendingProtocol } from 'lendingProtocols'

export function getEmode(collateralToken: IToken, debtToken: IToken) {
export function getEmode(collateralToken: IToken, debtToken: IToken, protocol: LendingProtocol) {
const collateralTokenSymbolUpperCase = collateralToken.symbol.toUpperCase()
const debtTokenSymbolUpperCase = debtToken.symbol.toUpperCase()

// Stablecoins are disabled for aave v3
const emodeStablecoinTokensUpperCase =
protocol === LendingProtocol.SparkV3 ? emodeStablecoinTokensSparkUpperCase : []

if (
emodeStablecoinTokensUpperCase.includes(collateralTokenSymbolUpperCase) &&
emodeStablecoinTokensUpperCase.includes(debtTokenSymbolUpperCase)
Expand Down
5 changes: 3 additions & 2 deletions features/refinance/helpers/getTargetPoolId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getEmode } from 'features/refinance/helpers/getEmode'
import { mapTokenToSdkToken } from 'features/refinance/helpers/mapTokenToSdkToken'
import { replaceTokenSymbolETHWithWETH } from 'features/refinance/helpers/replaceETHwithWETH'
import { getTokenDisplayName } from 'helpers/getTokenDisplayName'
import { LendingProtocol } from 'lendingProtocols'

export const getTargetPoolId = (protocol: IProtocol, ctx: RefinanceGeneralContextBase): IPoolId => {
const {
Expand Down Expand Up @@ -42,7 +43,7 @@ export const getTargetPoolId = (protocol: IProtocol, ctx: RefinanceGeneralContex
name: 'Spark',
chainInfo,
},
emodeType: getEmode(collateralToken, debtToken),
emodeType: getEmode(collateralToken, debtToken, LendingProtocol.SparkV3),
collateralToken,
debtToken,
})
Expand All @@ -52,7 +53,7 @@ export const getTargetPoolId = (protocol: IProtocol, ctx: RefinanceGeneralContex
name: 'AaveV3',
chainInfo,
},
emodeType: getEmode(collateralToken, debtToken),
emodeType: getEmode(collateralToken, debtToken, LendingProtocol.AaveV3),
collateralToken,
debtToken,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const useAaveLikeRefinanceContextInputs = ({

const collateralToken = mapTokenToSdkToken(chainFamily.chainInfo, collateralTokenSymbol)
const debtToken = mapTokenToSdkToken(chainFamily.chainInfo, debtTokenSymbol)
const emodeType = getEmode(collateralToken, debtToken)
const emodeType = getEmode(collateralToken, debtToken, lendingProtocol)

const poolId = getAaveLikePoolId(
lendingProtocol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function getRawPositionDetails(

const collateralToken = mapTokenToSdkToken(chainFamily.chainInfo, commonData.primaryToken)
const debtToken = mapTokenToSdkToken(chainFamily.chainInfo, commonData.secondaryToken)
const emodeType = getEmode(collateralToken, debtToken)
const emodeType = getEmode(collateralToken, debtToken, lendingProtocol)

const poolId = getAaveLikePoolId(
lendingProtocol,
Expand All @@ -97,7 +97,9 @@ export function getRawPositionDetails(
borrowRate: borrowRate.toString(),
positionId,
poolId,
pairId: 1, // TODO: investigate what used for
pairId: 1, // TODO: investigate what used for,
dpmAddress: dpm.id,
ownerAddress: dpm.user,
}
return rawPositionDetails
}
2 changes: 2 additions & 0 deletions handlers/portfolio/positions/handlers/maker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export const makerPositionsHandler: PortfolioPositionsHandler = async ({
poolId,
positionId,
pairId: 1,
dpmAddress: undefined,
ownerAddress: undefined,
},
...(type === OmniProductType.Earn && { lendingType: 'passive' }),
network: NetworkNames.ethereumMainnet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export function getRawPositionDetails(
maxRiskRatio: RiskRatio,
rate: string,
prices: TokensPricesList,
proxyAddress: string,
ownerAddress: string,
) {
const chainFamily = getChainInfoByChainId(networkId)
if (!chainFamily) {
Expand Down Expand Up @@ -57,6 +59,8 @@ export function getRawPositionDetails(
positionId,
poolId,
pairId,
dpmAddress: proxyAddress,
ownerAddress,
}
return rawPositionDetails
}
2 changes: 2 additions & 0 deletions handlers/portfolio/positions/handlers/morpho-blue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ async function getMorphoPositions({
position.maxRiskRatio,
rate,
prices,
proxyAddress,
position.owner,
)

return {
Expand Down
2 changes: 2 additions & 0 deletions handlers/portfolio/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export type PortfolioPosition = {
poolId: IPoolId
positionId: IPositionId
pairId: number
dpmAddress: string | undefined
ownerAddress: string | undefined
}
/*
lendingType:
Expand Down

0 comments on commit d535728

Please sign in to comment.