From 631dfdcb21e1cda60bc2d94e45f010a65967f6a3 Mon Sep 17 00:00:00 2001 From: Daniil Polienko Date: Thu, 24 Oct 2024 17:39:27 +0300 Subject: [PATCH] feat: add switch for v2 --- .../details/VaultActionsTabsWrapper.tsx | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/apps/vaults/components/details/VaultActionsTabsWrapper.tsx b/apps/vaults/components/details/VaultActionsTabsWrapper.tsx index 5d7637d07..26e9e9a95 100755 --- a/apps/vaults/components/details/VaultActionsTabsWrapper.tsx +++ b/apps/vaults/components/details/VaultActionsTabsWrapper.tsx @@ -25,6 +25,7 @@ import { } from '@vaults-v3/components/details/VaultActionsTabsWrapper'; import {readContracts} from '@wagmi/core'; import {parseMarkdown} from '@yearn-finance/web-lib/utils/helpers'; +import {useYearn} from '@common/contexts/useYearn'; import {IconChevron} from '@common/icons/IconChevron'; import type {ReactElement} from 'react'; @@ -39,6 +40,7 @@ import type {TTabsOptions} from '@vaults-v3/components/details/VaultActionsTabsW *************************************************************************************************/ export function VaultActionsTabsWrapper({currentVault}: {currentVault: TYDaemonVault}): ReactElement { const router = useRouter(); + const {isAutoStakingEnabled, set_isAutoStakingEnabled} = useYearn(); const {address} = useWeb3(); const {onSwitchSelectedOptions, isDepositing, actionParams} = useActionFlow(); const [possibleTabs, set_possibleTabs] = useState([tabs[0], tabs[1]]); @@ -136,6 +138,27 @@ export function VaultActionsTabsWrapper({currentVault}: {currentVault: TYDaemonV } }, [currentVault?.migration?.available, currentVault?.info?.isRetired, actionParams.isReady, hasStakingRewards]); + /************************************************************************************************ + * This effect manages the auto-staking feature based on staking rewards availability. + * It disables auto-staking if there are no staking rewards and the last reward ended over a week ago. + * Otherwise, it enables auto-staking. + * + * The check for rewards ending over a week ago helps prevent unnecessary auto-staking + * for vaults with expired or long-inactive staking programs. + ************************************************************************************************/ + useEffect(() => { + if ( + !hasStakingRewards && + currentVault.staking.rewards?.some( + el => Math.floor(Date.now() / 1000) - (el.finishedAt ?? 0) > 60 * 60 * 24 * 7 + ) + ) { + set_isAutoStakingEnabled(false); + return; + } + set_isAutoStakingEnabled(true); + }, [currentVault.staking.rewards, hasStakingRewards, hasStakingRewardsLive, set_isAutoStakingEnabled]); + const isSonneRetiredVault = toAddress(currentVault.address) === toAddress(`0x5b977577eb8a480f63e11fc615d6753adb8652ae`) || toAddress(currentVault.address) === toAddress(`0xad17a225074191d5c8a37b50fda1ae278a2ee6a2`) || @@ -302,8 +325,16 @@ export function VaultActionsTabsWrapper({currentVault}: {currentVault: TYDaemonV

 

+ {!hasStakingRewardsLive && ( +
+ +
+ )}
-   )}