Skip to content

Commit

Permalink
Add useOmniKitLinks parameter to portfolio positions handler
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinciarka committed Mar 26, 2024
1 parent ce37132 commit d70e01f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
42 changes: 27 additions & 15 deletions handlers/portfolio/positions/handlers/aave-like/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ interface CommonDataMapperParams {
apiVaults?: Vault[]
allOraclePrices?: AaveLikeOraclePriceData
debug?: boolean
useOmniKitLinks?: boolean
}

export const commonDataMapper = ({
Expand All @@ -87,6 +88,7 @@ export const commonDataMapper = ({
apiVaults,
allOraclePrices,
debug,
useOmniKitLinks,
}: CommonDataMapperParams) => {
const primaryToken = getTokenName(dpm.networkId, dpm.collateralToken)
const secondaryToken = getTokenName(dpm.networkId, dpm.debtToken)
Expand Down Expand Up @@ -119,27 +121,37 @@ export const commonDataMapper = ({
Spark: emptyAutomations,
AAVE_V3: emptyAutomations,
}[dpm.protocol]
const primaryTokenSymbol = getTokenDisplayName(primaryToken)
const secondaryTokenSymbol = getTokenDisplayName(secondaryToken)
const omniKitUrl = `/${networksById[dpm.networkId].name.toLowerCase()}/omni/${
{
AAVE_V3: 'aave/v3',
Spark: 'spark',
}[dpm.protocol]
}/${positionType}/${primaryTokenSymbol.toLocaleLowerCase()}-${secondaryTokenSymbol.toLocaleLowerCase()}/${dpm.vaultId}`
const regularUrl = `/${networksById[dpm.networkId].name.toLowerCase()}/${
{
AAVE_V3: 'aave',
Spark: 'spark',
AAVE: 'aave',
}[dpm.protocol]
}/${
{
AAVE_V3: 'v3',
Spark: 'v3',
AAVE: 'v2',
}[dpm.protocol]
}/${dpm.vaultId}`
const url = useOmniKitLinks && dpm.protocol !== 'AAVE' ? omniKitUrl : regularUrl
return {
commonData: {
positionId: positionIdAsString ? dpm.vaultId : Number(dpm.vaultId),
type: positionType,
network: networksById[dpm.networkId].name,
protocol,
primaryToken: getTokenDisplayName(primaryToken),
secondaryToken: getTokenDisplayName(secondaryToken),
url: `/${networksById[dpm.networkId].name.toLowerCase()}/${
{
AAVE_V3: 'aave',
Spark: 'spark',
AAVE: 'aave',
}[dpm.protocol]
}/${
{
AAVE_V3: 'v3',
Spark: 'v3',
AAVE: 'v2',
}[dpm.protocol]
}/${dpm.vaultId}`,
primaryToken: primaryTokenSymbol,
secondaryToken: secondaryTokenSymbol,
url,
automations: {
...(dpm.positionType !== OmniProductType.Earn
? {
Expand Down
8 changes: 8 additions & 0 deletions handlers/portfolio/positions/handlers/aave-like/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const getAaveLikeBorrowPosition: GetAaveLikePositionHandlerType = async ({
allOraclePrices,
apiVaults,
debug,
useOmniKitLinks,
}) => {
const positionAutomations = allPositionsAutomations
.filter(filterAutomation(dpm))
Expand All @@ -50,6 +51,7 @@ const getAaveLikeBorrowPosition: GetAaveLikePositionHandlerType = async ({
apiVaults,
allOraclePrices,
debug,
useOmniKitLinks,
})
const [primaryTokenReserveData, secondaryTokenReserveData, onChainPositionData] =
await Promise.all([
Expand Down Expand Up @@ -124,6 +126,7 @@ const getAaveLikeMultiplyPosition: GetAaveLikePositionHandlerType = async ({
allOraclePrices,
apiVaults,
debug,
useOmniKitLinks,
}) => {
const positionAutomations = allPositionsAutomations
.filter(filterAutomation(dpm))
Expand All @@ -135,6 +138,7 @@ const getAaveLikeMultiplyPosition: GetAaveLikePositionHandlerType = async ({
apiVaults,
allOraclePrices,
debug,
useOmniKitLinks,
})
const [
primaryTokenReserveConfiguration,
Expand Down Expand Up @@ -247,12 +251,14 @@ const getAaveLikeEarnPosition: GetAaveLikePositionHandlerType = async ({
allPositionsHistory,
allOraclePrices,
debug,
useOmniKitLinks,
}) => {
const { commonData, primaryTokenPrice, secondaryTokenPrice, ...commonRest } = commonDataMapper({
dpm,
prices,
allOraclePrices,
debug,
useOmniKitLinks,
})
const [onChainPositionData, primaryTokenReserveData, secondaryTokenReserveData] =
await Promise.all([
Expand Down Expand Up @@ -351,6 +357,7 @@ export const aaveLikePositionsHandler: PortfolioPositionsHandler = async ({
apiVaults,
positionsCount,
debug,
useOmniKitLinks,
}) => {
const aaveLikeDpmList = dpmList.filter(({ protocol }) =>
[aaveLikeProtocolNames.aavev3, aaveLikeProtocolNames.sparkv3].includes(protocol),
Expand Down Expand Up @@ -420,6 +427,7 @@ export const aaveLikePositionsHandler: PortfolioPositionsHandler = async ({
allOraclePrices,
apiVaults,
debug,
useOmniKitLinks,
}
switch (dpm.positionType.toLowerCase()) {
case OmniProductType.Multiply:
Expand Down
2 changes: 2 additions & 0 deletions handlers/portfolio/positions/handlers/aave-like/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type GetAaveLikePositionHandlerType = ({
allOraclePrices,
apiVaults,
debug,
useOmniKitLinks,
}: {
dpm: DpmSubgraphData
prices: TokensPricesList
Expand All @@ -33,4 +34,5 @@ export type GetAaveLikePositionHandlerType = ({
allOraclePrices: AaveLikeOraclePriceData
apiVaults?: Vault[]
debug?: boolean
useOmniKitLinks?: boolean
}) => Promise<PortfolioPosition>
2 changes: 2 additions & 0 deletions handlers/portfolio/positions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const portfolioPositionsHandler = async ({
}: NextApiRequest): Promise<PortfolioPositionsReply | PortfolioPositionsCountReply> => {
const address = (query.address as string).toLowerCase()
const debug = 'debug' in query
const useOmniKitLinks = 'useOmniKitLinks' in query
const positionsCount = 'positionsCount' in query
if (portfolioCache.has(address)) {
if (positionsCount) {
Expand Down Expand Up @@ -55,6 +56,7 @@ export const portfolioPositionsHandler = async ({
prices: prices.data.tokens,
positionsCount,
debug,
useOmniKitLinks,
}

const positionsReply = await Promise.all([
Expand Down
2 changes: 2 additions & 0 deletions handlers/portfolio/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type PortfolioPositionsHandler = ({
prices,
positionsCount,
debug,
useOmniKitLinks,
}: {
address: string
apiVaults?: Vault[]
Expand All @@ -74,6 +75,7 @@ export type PortfolioPositionsHandler = ({
positionsCount?: boolean
allPositionsHistory?: HistoryResponse
debug?: boolean
useOmniKitLinks?: boolean
}) => Promise<PortfolioPositionsReply | PortfolioPositionsCountReply>

export type DetailsTypeCommon =
Expand Down
8 changes: 6 additions & 2 deletions helpers/clients/portfolio-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
PortfolioOverviewResponse,
} from 'components/portfolio/types/domain-types'
import type { PortfolioPositionsReply } from 'handlers/portfolio/types'
import { getLocalAppConfig } from 'helpers/config'
import { useCallback, useMemo } from 'react'

/**
Expand All @@ -12,6 +13,7 @@ import { useCallback, useMemo } from 'react'
* @returns getters for portfolio data
*/
export const usePortfolioClient = (baseUrl?: string, headers?: HeadersInit) => {
const { UseOmniKitLinks } = getLocalAppConfig('features')
const fetchPortfolioGeneric = useCallback(
async <ResponseType>(
section: 'overview' | 'assets' | 'positions' | 'positionsCount',
Expand All @@ -20,7 +22,9 @@ export const usePortfolioClient = (baseUrl?: string, headers?: HeadersInit) => {
let callUrl
switch (section) {
case 'positions':
callUrl = `/api/positions/${address}`
callUrl = UseOmniKitLinks
? `/api/positions/${address}?useOmniKitLinks`
: `/api/positions/${address}`
break
case 'overview':
case 'assets':
Expand All @@ -36,7 +40,7 @@ export const usePortfolioClient = (baseUrl?: string, headers?: HeadersInit) => {
.catch((err) => console.error(err))
return response
},
[baseUrl, headers],
[baseUrl, headers, UseOmniKitLinks],
)

return useMemo(
Expand Down

0 comments on commit d70e01f

Please sign in to comment.