Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rays real data #3959

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion components/portfolio/positions/PortfolioPositionBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BigNumber from 'bignumber.js'
import { AssetsTableDataCellAsset } from 'components/assetsTable/cellComponents/AssetsTableDataCellAsset'
import { AppLink } from 'components/Links'
import { Pill } from 'components/Pill'
Expand All @@ -12,6 +13,7 @@ import { OmniProductType } from 'features/omni-kit/types'
import { RefinancePortfolioBanner } from 'features/refinance/components'
import type { PortfolioPosition } from 'handlers/portfolio/types'
import { getLocalAppConfig, useAppConfig } from 'helpers/config'
import { formatCryptoBalance } from 'helpers/formatters/format'
import { getGradientColor } from 'helpers/getGradientColor'
import { LendingProtocol, LendingProtocolLabel } from 'lendingProtocols'
import { upperFirst } from 'lodash'
Expand Down Expand Up @@ -140,7 +142,8 @@ export const PortfolioPositionBlock = ({ position }: { position: PortfolioPositi
),
}}
>
+ {position.raysPerYear.toFixed(0)} Rays / year
+ {formatCryptoBalance(new BigNumber(position.raysPerYear)).split('.')[0]} Rays /
year
</Text>
)}
<ProtocolLabel network={position.network} protocol={position.protocol} />
Expand Down
53 changes: 53 additions & 0 deletions features/omni-kit/automation/helpers/getIsLastAutomationOfKind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { AutomationFeatures } from 'features/automation/common/types'
import type { AutomationMetadataFlags } from 'features/omni-kit/types'
import { TriggerAction } from 'helpers/lambda/triggers'

interface LastTriggerStatus {
isLastProtection: boolean
isLastOptimization: boolean
}

export function getIsLastAutomationOfKind({
automationFlags,
automationFeature,
action,
}: {
automationFlags: AutomationMetadataFlags
automationFeature: AutomationFeatures
action: TriggerAction
}): LastTriggerStatus {
// Determine which features are considered protection and optimization
const protectionFeatures = [
AutomationFeatures.STOP_LOSS,
AutomationFeatures.AUTO_SELL,
AutomationFeatures.TRAILING_STOP_LOSS,
]

const optimizationFeatures = [
AutomationFeatures.AUTO_BUY,
AutomationFeatures.CONSTANT_MULTIPLE,
AutomationFeatures.AUTO_TAKE_PROFIT,
AutomationFeatures.PARTIAL_TAKE_PROFIT,
]

// Map features to their respective flags in the automationFlags
const featureFlags: { [key in AutomationFeatures]: boolean } = {
[AutomationFeatures.STOP_LOSS]: automationFlags.isStopLossEnabled,
[AutomationFeatures.AUTO_SELL]: automationFlags.isAutoSellEnabled,
[AutomationFeatures.TRAILING_STOP_LOSS]: automationFlags.isTrailingStopLossEnabled,
[AutomationFeatures.AUTO_BUY]: automationFlags.isAutoBuyEnabled,
[AutomationFeatures.CONSTANT_MULTIPLE]: false, // Assuming this feature is always disabled as no flag is provided
[AutomationFeatures.AUTO_TAKE_PROFIT]: false, // Assuming this feature is always disabled as no flag is provided
[AutomationFeatures.PARTIAL_TAKE_PROFIT]: automationFlags.isPartialTakeProfitEnabled,
}

if (action === TriggerAction.Remove || action === TriggerAction.Update) {
featureFlags[automationFeature] = false
}

// Check if the feature being removed is the last protection or optimization
const isLastProtection = protectionFeatures.every((f) => !featureFlags[f])
const isLastOptimization = optimizationFeatures.every((f) => !featureFlags[f])

return { isLastProtection, isLastOptimization }
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,71 @@
import type { LendingPosition, SupplyPosition } from '@oasisdex/dma-library'
import BigNumber from 'bignumber.js'
import { RaysSidebarBanner } from 'components/RaysSidebarBanner'
import type { AutomationFeatures } from 'features/automation/common/types'
import { getIsLastAutomationOfKind } from 'features/omni-kit/automation/helpers/getIsLastAutomationOfKind'
import type { AutomationMetadataFlags } from 'features/omni-kit/types'
import { RAYS_OPTIMIZATION_BOOST, RAYS_PROTECTION_BOOST } from 'features/rays/consts'
import { getProtocolBoost } from 'features/rays/getProtocolBoost'
import { type PositionRaysMultipliersData } from 'features/rays/types'
import { formatCryptoBalance } from 'helpers/formatters/format'
import { isAnyValueDefined } from 'helpers/isAnyValueDefined'
import { TriggerAction } from 'helpers/lambda/triggers'
import { getPointsPerYear } from 'helpers/rays'
import { useTranslation } from 'next-i18next'
import React from 'react'

export const getOmniAutomationSidebarRaysBanner = ({
activeTriggersNumber,
action,
positionRaysMultipliersData,
position,
automationFlags,
automationFeature,
hidden,
}: {
activeTriggersNumber: number
action: TriggerAction
positionRaysMultipliersData: PositionRaysMultipliersData
position: LendingPosition | SupplyPosition
automationFlags?: AutomationMetadataFlags
automationFeature: AutomationFeatures
hidden: boolean
}) => {
const { t } = useTranslation()

if (!automationFlags || hidden) {
return null
}
const {
isAutoBuyEnabled,
isPartialTakeProfitEnabled,
isAutoSellEnabled,
isTrailingStopLossEnabled,
isStopLossEnabled,
} = automationFlags

const isActiveOptimization = isAnyValueDefined(isAutoBuyEnabled, isPartialTakeProfitEnabled)
const isActiveProtection = isAnyValueDefined(
isAutoSellEnabled,
isTrailingStopLossEnabled,
isStopLossEnabled,
)
const protocolBoost = getProtocolBoost(positionRaysMultipliersData)

// In general all positions should have `netValue` field since all positions extend either Lending or Supply interface
const raysPerYear = 'netValue' in position ? getPointsPerYear(position.netValue.toNumber()) : 0

const stackedRaysPerYear = raysPerYear * protocolBoost

if (activeTriggersNumber === 0) {
const extraRays =
stackedRaysPerYear * RAYS_OPTIMIZATION_BOOST * RAYS_PROTECTION_BOOST - stackedRaysPerYear

return (
<RaysSidebarBanner
title={t('rays.sidebar.banner.boost.title', { rays: '12345' })}
title={t('rays.sidebar.banner.boost.title', {
rays: formatCryptoBalance(new BigNumber(extraRays)),
})}
description={t('rays.sidebar.banner.boost.description')}
/>
)
Expand All @@ -26,9 +76,39 @@ export const getOmniAutomationSidebarRaysBanner = ({
}

if (action === TriggerAction.Remove) {
let rays = 0

const { isLastOptimization, isLastProtection } = getIsLastAutomationOfKind({
automationFlags,
automationFeature,
action,
})

if (isLastOptimization && isActiveProtection) {
const baseRays = stackedRaysPerYear * RAYS_PROTECTION_BOOST * RAYS_OPTIMIZATION_BOOST
rays = baseRays - stackedRaysPerYear * RAYS_PROTECTION_BOOST
}

if (isLastProtection && isActiveOptimization) {
const baseRays = stackedRaysPerYear * RAYS_PROTECTION_BOOST * RAYS_OPTIMIZATION_BOOST
rays = baseRays - stackedRaysPerYear * RAYS_OPTIMIZATION_BOOST
}

if (isLastOptimization && !isActiveProtection) {
const baseRays = stackedRaysPerYear * RAYS_OPTIMIZATION_BOOST
rays = baseRays - stackedRaysPerYear
}

if (isLastProtection && !isActiveOptimization) {
const baseRays = stackedRaysPerYear * RAYS_PROTECTION_BOOST
rays = baseRays - stackedRaysPerYear
}

return (
<RaysSidebarBanner
title={t('rays.sidebar.banner.reduceAuto.title', { rays: '12345' })}
title={t('rays.sidebar.banner.reduceAuto.title', {
rays: formatCryptoBalance(new BigNumber(rays)),
})}
description={t('rays.sidebar.banner.reduceAuto.description')}
/>
)
Expand Down
2 changes: 2 additions & 0 deletions features/omni-kit/contexts/OmniGeneralContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
OmniSupportedNetworkIds,
} from 'features/omni-kit/types'
import { OmniSidebarAutomationStep } from 'features/omni-kit/types'
import type { PositionRaysMultipliersData } from 'features/rays/types'
import type { RefinanceContextInput } from 'features/refinance/contexts'
import type { TxDetails } from 'helpers/handleTransaction'
import { useAccount } from 'helpers/useAccount'
Expand Down Expand Up @@ -80,6 +81,7 @@ interface OmniGeneralContextProviderProps {
automationSteps: OmniSidebarAutomationStep[]
walletNetwork: NetworkConfig
refinanceContextInput?: RefinanceContextInput
positionRaysMultipliersData: PositionRaysMultipliersData
}

export enum OmniSlippageSourceSettings {
Expand Down
9 changes: 7 additions & 2 deletions features/omni-kit/controllers/OmniProductController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
} from 'features/omni-kit/types'
import { OmniProductType, OmniSidebarAutomationStep } from 'features/omni-kit/types'
import type { PositionHistoryEvent } from 'features/positionHistory/types'
import type { PositionRaysMultipliersData } from 'features/rays/types'
import { WithTermsOfService } from 'features/termsOfService/TermsOfService'
import { WithWalletAssociatedRisk } from 'features/walletAssociatedRisk/WalletAssociatedRisk'
import { INTERNAL_LINKS } from 'helpers/applicationLinks'
Expand Down Expand Up @@ -72,6 +73,7 @@ interface OmniProductControllerProps<Auction, History, Position> {
poolId?: string
positionData?: Position
protocolPricesData?: Tickers
positionRaysMultipliersData: PositionRaysMultipliersData
}
errors: string[]
}
Expand Down Expand Up @@ -106,7 +108,7 @@ export const OmniProductController = <Auction, History, Position>({
const { t } = useTranslation()

const { replace } = useRouter()
const { chainId, isConnected } = useAccount()
const { chainId, isConnected, walletAddress } = useAccount()

const network = getNetworkById(networkId)
const walletNetwork = getNetworkById(chainId || networkId)
Expand Down Expand Up @@ -144,7 +146,7 @@ export const OmniProductController = <Auction, History, Position>({
})

const {
data: { aggregatedData, poolId, positionData, protocolPricesData },
data: { aggregatedData, poolId, positionData, protocolPricesData, positionRaysMultipliersData },
errors: protocolDataErrors,
} = protocolHook({
collateralToken,
Expand All @@ -156,6 +158,8 @@ export const OmniProductController = <Auction, History, Position>({
quoteToken,
tokenPriceUSDData,
tokensPrecision,
isOpening,
walletAddress,
})

const {
Expand Down Expand Up @@ -305,6 +309,7 @@ export const OmniProductController = <Auction, History, Position>({
OmniSidebarAutomationStep.Manage,
OmniSidebarAutomationStep.Transaction,
]}
positionRaysMultipliersData={positionRaysMultipliersData}
>
{customState({
aggregatedData: _aggregatedData,
Expand Down
Loading
Loading