Skip to content

Commit

Permalink
feat: add switch for v2
Browse files Browse the repository at this point in the history
  • Loading branch information
w84april committed Oct 24, 2024
1 parent 4955503 commit 631dfdc
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion apps/vaults/components/details/VaultActionsTabsWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<TTabsOptions[]>([tabs[0], tabs[1]]);
Expand Down Expand Up @@ -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`) ||
Expand Down Expand Up @@ -302,8 +325,16 @@ export function VaultActionsTabsWrapper({currentVault}: {currentVault: TYDaemonV
<p className={'hidden text-base md:inline'}>&nbsp;</p>
<div>
<VaultDetailsQuickActionsButtons currentVault={currentVault} />
{!hasStakingRewardsLive && (
<div className={'mt-1 flex justify-between'}>
<button
className={'font-number text-xxs text-neutral-900/50'}
onClick={(): void => set_isAutoStakingEnabled(!isAutoStakingEnabled)}>
{isAutoStakingEnabled ? 'Deposit only' : 'Deposit and Stake'}
</button>
</div>
)}
</div>
<legend className={'hidden text-xs md:inline'}>&nbsp;</legend>
</div>
</div>
)}
Expand Down

0 comments on commit 631dfdc

Please sign in to comment.