-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert "Refinance integration with the new SDK version v0.2.1 (…
- Loading branch information
Showing
44 changed files
with
706 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { useOmniGeneralContext } from 'features/omni-kit/contexts/OmniGeneralContext' | ||
import { useOmniProductContext } from 'features/omni-kit/contexts/OmniProductContext' | ||
import type { RefinanceContextInput } from 'features/refinance/contexts' | ||
import { omniProductTypeToSDKType } from 'features/refinance/helpers/omniProductTypeToSDKType' | ||
import { useMorphoRefinanceContextInputs } from 'features/refinance/hooks/useMorphoRefinanceContextInputs' | ||
import { useAccount } from 'helpers/useAccount' | ||
import { LendingProtocol } from 'lendingProtocols' | ||
import React, { type PropsWithChildren, useContext, useMemo } from 'react' | ||
|
||
interface OmniRefinanceContext { | ||
refinanceContextInput?: RefinanceContextInput | ||
} | ||
|
||
const omniRefinanceContext = React.createContext<OmniRefinanceContext | undefined>(undefined) | ||
|
||
export function useOmniRefinanceContext(): OmniRefinanceContext { | ||
const context = useContext(omniRefinanceContext) | ||
|
||
if (!context) throw new Error('OmniRefinanceContext not available!') | ||
return context | ||
} | ||
|
||
export function OmniRefinanceContextProvider({ | ||
children, | ||
}: PropsWithChildren<OmniRefinanceContext>) { | ||
const { walletAddress } = useAccount() | ||
|
||
const { | ||
environment: { | ||
collateralToken, | ||
quoteToken, | ||
collateralPrice, | ||
quotePrice, | ||
ethPrice, | ||
isOwner, | ||
networkId, | ||
poolId, | ||
pairId, | ||
positionId, | ||
productType, | ||
protocol, | ||
slippage, | ||
}, | ||
} = useOmniGeneralContext() | ||
const { | ||
position: { | ||
currentPosition: { position }, | ||
}, | ||
automation: { positionTriggers }, | ||
} = useOmniProductContext(productType) | ||
|
||
const borrowRate = 'borrowRate' in position ? position.borrowRate : undefined | ||
const ltv = 'riskRatio' in position ? position.riskRatio.loanToValue : undefined | ||
const maxLtv = 'maxRiskRatio' in position ? position.maxRiskRatio.loanToValue : undefined | ||
const liquidationPrice = 'liquidationPrice' in position ? position.liquidationPrice : undefined | ||
const collateralAmount = 'collateralAmount' in position ? position.collateralAmount : undefined | ||
const debtAmount = 'debtAmount' in position ? position.debtAmount : undefined | ||
|
||
const refinanceHasAllData = | ||
positionId && | ||
borrowRate && | ||
poolId && | ||
ltv && | ||
maxLtv && | ||
liquidationPrice && | ||
collateralAmount && | ||
debtAmount | ||
|
||
let refinanceInput: RefinanceContextInput | undefined | ||
// TODO: Add support for other protocols | ||
switch (protocol) { | ||
case LendingProtocol.MorphoBlue: | ||
const morphoRefinanceInput = !refinanceHasAllData | ||
? undefined | ||
: useMorphoRefinanceContextInputs({ | ||
address: walletAddress, | ||
networkId, | ||
collateralTokenSymbol: collateralToken, | ||
debtTokenSymbol: quoteToken, | ||
collateralAmount: collateralAmount.toString(), | ||
debtAmount: debtAmount.toString(), | ||
vaultId: positionId, | ||
slippage: slippage.toNumber(), | ||
collateralPrice: collateralPrice.toString(), | ||
debtPrice: quotePrice.toString(), | ||
ethPrice: ethPrice.toString(), | ||
borrowRate: borrowRate.toString(), | ||
liquidationPrice: liquidationPrice.toString(), | ||
ltv: ltv.toString(), | ||
maxLtv: maxLtv.toString(), | ||
marketId: poolId, | ||
positionType: omniProductTypeToSDKType(productType), | ||
isOwner, | ||
pairId, | ||
triggerData: positionTriggers, | ||
owner: position.owner, | ||
}) | ||
refinanceInput = morphoRefinanceInput | ||
break | ||
} | ||
|
||
const refinanceContextInput = !refinanceHasAllData ? undefined : refinanceInput | ||
|
||
const context: OmniRefinanceContext = useMemo(() => { | ||
return { | ||
refinanceContextInput, | ||
} | ||
}, [refinanceContextInput]) | ||
|
||
return <omniRefinanceContext.Provider value={context}>{children}</omniRefinanceContext.Provider> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
features/omni-kit/protocols/morpho-blue/components/banners/MorphoOverviewBanner.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useOmniRefinanceContext } from 'features/omni-kit/contexts/OmniRefinanceContext' | ||
import { RefinanceBanner } from 'features/refinance/components' | ||
import { useAppConfig } from 'helpers/config' | ||
import React from 'react' | ||
|
||
export const OverviewBanner = () => { | ||
const omniRefinance = useOmniRefinanceContext() | ||
const { EnableRefinance: refinanceEnabled } = useAppConfig('features') | ||
|
||
if (!refinanceEnabled || omniRefinance.refinanceContextInput == null) { | ||
return null | ||
} | ||
|
||
return <RefinanceBanner contextInput={omniRefinance.refinanceContextInput} /> | ||
} |
6 changes: 6 additions & 0 deletions
6
features/omni-kit/protocols/morpho-blue/hooks/useMorphoBanner.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { OverviewBanner } from 'features/omni-kit/protocols/morpho-blue/components/banners/MorphoOverviewBanner' | ||
import React from 'react' | ||
|
||
export const useMorphoBanner = () => { | ||
return { renderOverviewBanner: () => <OverviewBanner /> } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.