Skip to content

Commit

Permalink
Rays with multipliers in portfolio (#3963)
Browse files Browse the repository at this point in the history
  • Loading branch information
piekczyk authored Jun 24, 2024
1 parent ec0b8c1 commit 199c59e
Show file tree
Hide file tree
Showing 30 changed files with 360 additions and 80 deletions.
3 changes: 1 addition & 2 deletions components/portfolio/positions/PortfolioPositionBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ export const PortfolioPositionBlock = ({ position }: { position: PortfolioPositi
),
}}
>
+ {formatCryptoBalance(new BigNumber(position.raysPerYear)).split('.')[0]} Rays /
year
+ {formatCryptoBalance(new BigNumber(position.raysPerYear))} Rays / year
</Text>
)}
<ProtocolLabel network={position.network} protocol={position.protocol} />
Expand Down
30 changes: 16 additions & 14 deletions features/omni-kit/helpers/getOmniSidebarRaysBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getProtocolBoost } from 'features/rays/getProtocolBoost'
import { getRaysMultiplyMultipliers } from 'features/rays/getRaysMultiplyMultipliers'
import { getRaysNextProtocolBonus } from 'features/rays/getRaysNextProtocolBonus'
import { getSwapBoost } from 'features/rays/getSwapBoost'
import { getTimeOpenBoost } from 'features/rays/getTimeOpenBoost'
import type { PositionRaysMultipliersData } from 'features/rays/types'
import { formatCryptoBalance } from 'helpers/formatters/format'
import { getPointsPerYear } from 'helpers/rays'
Expand Down Expand Up @@ -65,6 +66,7 @@ export const getOmniSidebarRaysBanner = ({
const protocolBoost = getProtocolBoost(positionRaysMultipliersData)
const swapBoost = getSwapBoost(positionRaysMultipliersData)
const automationBoost = getAutomationBoost(positionRaysMultipliersData)
const timeOpenBoost = getTimeOpenBoost(positionRaysMultipliersData)

// In general all positions should have `netValue` field since all positions extend either Lending or Supply interface
const positionNetValue = position && 'netValue' in position ? position.netValue.toNumber() : 0
Expand All @@ -74,8 +76,8 @@ export const getOmniSidebarRaysBanner = ({
const raysPerYear = getPointsPerYear(positionNetValue)
const newRaysPerYear = getPointsPerYear(simulationNetValue)

const raysPerYearWithProtocolBoost = raysPerYear * protocolBoost
const newRaysPerYearWithProtocolBoost = newRaysPerYear * protocolBoost
const raysPerYearWithBasicBoosts = raysPerYear * protocolBoost * timeOpenBoost
const newRaysPerYearWithBasicBoosts = newRaysPerYear * protocolBoost * timeOpenBoost

if (isOpening) {
const simulatedBaseRaysPerYear = getPointsPerYear(simulationNetValue)
Expand Down Expand Up @@ -121,10 +123,10 @@ export const getOmniSidebarRaysBanner = ({
// return (
// <RaysSidebarBanner
// title={t('rays.sidebar.banner.closing.title', {
// rays: formatCryptoBalance(new BigNumber(raysPerYearWithProtocolBoost)),
// rays: formatCryptoBalance(new BigNumber(raysPerYearWithBasicBoosts)),
// })}
// description={t('rays.sidebar.banner.closing.description', {
// raysPerYear: formatCryptoBalance(new BigNumber(raysPerYearWithProtocolBoost)),
// raysPerYear: formatCryptoBalance(new BigNumber(raysPerYearWithBasicBoosts)),
// })}
// // daysLeft="2"
// />
Expand Down Expand Up @@ -157,9 +159,9 @@ export const getOmniSidebarRaysBanner = ({
}

if (simulation && positionNetValue < simulationNetValue) {
const rays = new BigNumber(
newRaysPerYearWithProtocolBoost - raysPerYearWithProtocolBoost,
).times(automationBoost)
const rays = new BigNumber(newRaysPerYearWithBasicBoosts - raysPerYearWithBasicBoosts).times(
automationBoost,
)

return (
<RaysSidebarBanner
Expand All @@ -169,9 +171,9 @@ export const getOmniSidebarRaysBanner = ({
}

if (simulation && positionNetValue > simulationNetValue) {
const rays = new BigNumber(
raysPerYearWithProtocolBoost - newRaysPerYearWithProtocolBoost,
).times(automationBoost)
const rays = new BigNumber(raysPerYearWithBasicBoosts - newRaysPerYearWithBasicBoosts).times(
automationBoost,
)

return (
<RaysSidebarBanner
Expand All @@ -196,17 +198,17 @@ export const getOmniSidebarRaysBanner = ({

if (!isOptimizationActive && !isProtectionActive) {
extraRays =
raysPerYearWithProtocolBoost * RAYS_OPTIMIZATION_BOOST * RAYS_PROTECTION_BOOST -
raysPerYearWithProtocolBoost
raysPerYearWithBasicBoosts * RAYS_OPTIMIZATION_BOOST * RAYS_PROTECTION_BOOST -
raysPerYearWithBasicBoosts
}

if (isOptimizationActive && !isProtectionActive) {
const baseRays = raysPerYearWithProtocolBoost * RAYS_OPTIMIZATION_BOOST
const baseRays = raysPerYearWithBasicBoosts * RAYS_OPTIMIZATION_BOOST
extraRays = baseRays * RAYS_PROTECTION_BOOST - baseRays
}

if (!isOptimizationActive && isProtectionActive) {
const baseRays = raysPerYearWithProtocolBoost * RAYS_PROTECTION_BOOST
const baseRays = raysPerYearWithBasicBoosts * RAYS_PROTECTION_BOOST
extraRays = baseRays * RAYS_OPTIMIZATION_BOOST - baseRays
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { NetworkNames } from 'blockchain/networks'
import type { DpmPositionData } from 'features/omni-kit/observables'
import type { RaysUserMultipliersResponse } from 'features/rays/getRaysUserMultipliers'
import { mapUserAndPositionRays } from 'features/rays/mapUserAndPositionRays'
import { getRaysMappedNetwork } from 'handlers/rays/getRaysMappedNetwork'
Expand All @@ -10,14 +9,18 @@ export const mapAaveLikeRaysMultipliers = ({
multipliers,
protocol,
networkName,
dpmPositionData,
collateralTokenAddress,
quoteTokenAddress,
dpmProxy,
}: {
protocol: LendingProtocol
networkName: NetworkNames
dpmPositionData?: DpmPositionData
collateralTokenAddress?: string
quoteTokenAddress?: string
dpmProxy?: string
multipliers?: RaysUserMultipliersResponse
}) => {
if (!multipliers || !dpmPositionData) {
if (!multipliers || !collateralTokenAddress || !quoteTokenAddress || !dpmProxy) {
return {
user: [],
position: [],
Expand All @@ -37,10 +40,10 @@ export const mapAaveLikeRaysMultipliers = ({

return (
_network === resolvedNetwork &&
_proxy.toLowerCase() === dpmPositionData.proxy.toLowerCase() &&
_proxy.toLowerCase() === dpmProxy.toLowerCase() &&
resolvedProtocol.includes(_protocol) &&
_collateralTokenAddress === dpmPositionData.collateralTokenAddress &&
_debtTokenAddress === dpmPositionData.quoteTokenAddress
_collateralTokenAddress === collateralTokenAddress &&
_debtTokenAddress === quoteTokenAddress
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ export function useAaveLikeData({

const positionRaysMultipliersData = mapAaveLikeRaysMultipliers({
multipliers,
dpmPositionData,
collateralTokenAddress: dpmPositionData?.collateralTokenAddress,
quoteTokenAddress: dpmPositionData?.collateralTokenAddress,
dpmProxy: dpmPositionData?.proxy,
protocol,
networkName,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import type { NetworkNames } from 'blockchain/networks'
import type { DpmPositionData } from 'features/omni-kit/observables'
import type { RaysUserMultipliersResponse } from 'features/rays/getRaysUserMultipliers'
import { mapUserAndPositionRays } from 'features/rays/mapUserAndPositionRays'
import { getRaysMappedNetwork } from 'handlers/rays/getRaysMappedNetwork'
import { getRaysMappedProtocol } from 'handlers/rays/getRaysMappedProtocol'
import type { LendingProtocol } from 'lendingProtocols'
import { LendingProtocol } from 'lendingProtocols'

export const mapAjnaRaysMultipliers = ({
multipliers,
protocol,
dpmProxy,
networkName,
dpmPositionData,
poolId,
}: {
protocol: LendingProtocol
networkName: NetworkNames
dpmPositionData?: DpmPositionData
dpmProxy?: string
multipliers?: RaysUserMultipliersResponse
poolId?: string
}) => {
if (!multipliers || !dpmPositionData || !poolId) {
if (!multipliers || !dpmProxy || !poolId) {
return {
user: [],
position: [],
Expand All @@ -31,13 +30,13 @@ export const mapAjnaRaysMultipliers = ({
const resolvedNetwork = getRaysMappedNetwork(networkName)

const positionMultipliersKey = Object.keys(multipliers.positionMultipliers)
.filter((item) => item.includes('ajna'))
.filter((item) => item.includes(LendingProtocol.Ajna))
.find((item) => {
const [_network, , _proxy, _protocol, _poolId] = item.split('-')

return (
_network === resolvedNetwork &&
_proxy.toLowerCase() === dpmPositionData.proxy.toLowerCase() &&
_proxy.toLowerCase() === dpmProxy.toLowerCase() &&
resolvedProtocol.includes(_protocol) &&
_poolId.toLowerCase() === poolId.toLowerCase()
)
Expand Down
2 changes: 1 addition & 1 deletion features/omni-kit/protocols/ajna/hooks/useAjnaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function useAjnaData({

const positionRaysMultipliersData = mapAjnaRaysMultipliers({
multipliers,
dpmPositionData,
dpmProxy: dpmPositionData?.proxy,
protocol,
networkName,
poolId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { NetworkNames } from 'blockchain/networks'
import type { DpmPositionData } from 'features/omni-kit/observables'
import { Erc4626PseudoProtocol } from 'features/omni-kit/protocols/morpho-blue/constants'
import type { RaysUserMultipliersResponse } from 'features/rays/getRaysUserMultipliers'
import { mapUserAndPositionRays } from 'features/rays/mapUserAndPositionRays'
Expand All @@ -8,15 +7,15 @@ import { getRaysMappedNetwork } from 'handlers/rays/getRaysMappedNetwork'
export const mapErc4626RaysMultipliers = ({
multipliers,
networkName,
dpmPositionData,
dpmProxy,
poolId,
}: {
networkName: NetworkNames
dpmPositionData?: DpmPositionData
dpmProxy?: string
multipliers?: RaysUserMultipliersResponse
poolId: string
}) => {
if (!multipliers || !dpmPositionData || !poolId) {
if (!multipliers || !dpmProxy || !poolId) {
return {
user: [],
position: [],
Expand All @@ -34,7 +33,7 @@ export const mapErc4626RaysMultipliers = ({

return (
_network === resolvedNetwork &&
_proxy.toLowerCase() === dpmPositionData.proxy.toLowerCase() &&
_proxy.toLowerCase() === dpmProxy.toLowerCase() &&
resolvedProtocol.includes(_protocol) &&
_poolId.toLowerCase().includes(poolId.toLowerCase())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function useErc4626Data({

const positionRaysMultipliersData = mapErc4626RaysMultipliers({
multipliers,
dpmPositionData,
dpmProxy: dpmPositionData?.proxy,
networkName,
poolId: address,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { NetworkNames } from 'blockchain/networks'
import type { DpmPositionData } from 'features/omni-kit/observables'
import { morphoMarkets } from 'features/omni-kit/protocols/morpho-blue/settings'
import type { OmniSupportedNetworkIds } from 'features/omni-kit/types'
import type { RaysUserMultipliersResponse } from 'features/rays/getRaysUserMultipliers'
import { mapUserAndPositionRays } from 'features/rays/mapUserAndPositionRays'
import { replaceTokenSymbolWETHWithETH } from 'features/refinance/helpers/replaceWETHWithETH'
import { getRaysMappedNetwork } from 'handlers/rays/getRaysMappedNetwork'
import { getRaysMappedProtocol } from 'handlers/rays/getRaysMappedProtocol'
import type { LendingProtocol } from 'lendingProtocols'
Expand All @@ -13,28 +13,35 @@ export const mapMorphoBlueRaysMultipliers = ({
protocol,
networkName,
networkId,
dpmPositionData,
collateralToken,
quoteToken,
dpmProxy,
pairId,
}: {
protocol: LendingProtocol
networkName: NetworkNames
networkId: OmniSupportedNetworkIds
dpmPositionData?: DpmPositionData
collateralToken?: string
quoteToken?: string
dpmProxy?: string
multipliers?: RaysUserMultipliersResponse
pairId: number
}) => {
if (!multipliers || !dpmPositionData) {
if (!multipliers || !collateralToken || !quoteToken || !dpmProxy) {
return {
user: [],
position: [],
allUserProtocols: [],
}
}

const resolvedCollateralToken = replaceTokenSymbolWETHWithETH(collateralToken.toUpperCase())
const resolvedQuoteToken = replaceTokenSymbolWETHWithETH(quoteToken.toUpperCase())

const poolId =
morphoMarkets[networkId]?.[`${dpmPositionData.collateralToken}-${dpmPositionData.quoteToken}`][
pairId - 1
]
morphoMarkets[networkId]?.[
`${resolvedCollateralToken.toUpperCase()}-${resolvedQuoteToken.toUpperCase()}`
][pairId - 1]

if (!poolId) {
return {
Expand All @@ -48,13 +55,13 @@ export const mapMorphoBlueRaysMultipliers = ({
const resolvedNetwork = getRaysMappedNetwork(networkName)

const positionMultipliersKey = Object.keys(multipliers.positionMultipliers)
.filter((item) => item.includes('morphoblue'))
.filter((item) => item.includes('morphoblue') || item.includes('erc4626'))
.find((item) => {
const [_network, , _proxy, _protocol, _poolId] = item.split('-')

return (
_network === resolvedNetwork &&
_proxy.toLowerCase() === dpmPositionData.proxy.toLowerCase() &&
_proxy.toLowerCase() === dpmProxy.toLowerCase() &&
resolvedProtocol.includes(_protocol) &&
_poolId.toLowerCase() === poolId.toLowerCase()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export function useMorphoData({

const positionRaysMultipliersData = mapMorphoBlueRaysMultipliers({
multipliers,
dpmPositionData,
collateralToken: dpmPositionData?.collateralToken,
quoteToken: dpmPositionData?.quoteToken,
dpmProxy: dpmPositionData?.proxy,
protocol,
networkName,
networkId,
Expand Down
7 changes: 4 additions & 3 deletions features/rays/getAutomationBoost.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PositionRaysMultipliersData } from 'features/rays/types'
import { RaysMultipliersType } from 'features/rays/types'
import { PositionRaysMultipliersType } from 'features/rays/types'

export const getAutomationBoost = (positionRaysMultipliersData: PositionRaysMultipliersData) =>
positionRaysMultipliersData.position.find((item) => item.type === RaysMultipliersType.AUTOMATION)
?.value || 1
positionRaysMultipliersData.position.find(
(item) => item.type === PositionRaysMultipliersType.AUTOMATION,
)?.value || 1
7 changes: 4 additions & 3 deletions features/rays/getProtocolBoost.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { PositionRaysMultipliersData } from 'features/rays/types'
import { RaysMultipliersType } from 'features/rays/types'
import { UserRaysMultipliersType } from 'features/rays/types'

export const getProtocolBoost = (positionRaysMultipliersData: PositionRaysMultipliersData) =>
positionRaysMultipliersData.user.find((item) => item.type === RaysMultipliersType.PROTOCOL_BOOST)
?.value || 1
positionRaysMultipliersData.user.find(
(item) => item.type === UserRaysMultipliersType.PROTOCOL_BOOST,
)?.value || 1
11 changes: 8 additions & 3 deletions features/rays/getRaysUserMultipliers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import type { RaysMultipliersType } from 'features/rays/types'
import type { PositionRaysMultipliersType, UserRaysMultipliersType } from 'features/rays/types'

export interface RaysUserMultipliersResponse {
address: string
positionMultipliers: Record<
string,
{ address: null; externalId: string; type: RaysMultipliersType; value: string }[]
{ address: null; externalId: string; type: PositionRaysMultipliersType; value: string }[]
>
userMultipliers: { address: string; externalId: null; type: RaysMultipliersType; value: string }[]
userMultipliers: {
address: string
externalId: null
type: UserRaysMultipliersType
value: string
}[]
}

export const getRaysUserMultipliers = async ({
Expand Down
6 changes: 3 additions & 3 deletions features/rays/getSwapBoost.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PositionRaysMultipliersData } from 'features/rays/types'
import { RaysMultipliersType } from 'features/rays/types'
import { UserRaysMultipliersType } from 'features/rays/types'

export const getSwapBoost = (positionRaysMultipliersData: PositionRaysMultipliersData) =>
positionRaysMultipliersData.user.find((item) => item.type === RaysMultipliersType.SWAP)?.value ||
1
positionRaysMultipliersData.user.find((item) => item.type === UserRaysMultipliersType.SWAP)
?.value || 1
7 changes: 7 additions & 0 deletions features/rays/getTimeOpenBoost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { PositionRaysMultipliersData } from 'features/rays/types'
import { PositionRaysMultipliersType } from 'features/rays/types'

export const getTimeOpenBoost = (positionRaysMultipliersData: PositionRaysMultipliersData) =>
positionRaysMultipliersData.position.find(
(item) => item.type === PositionRaysMultipliersType.TIME_OPEN,
)?.value || 1
Loading

0 comments on commit 199c59e

Please sign in to comment.