|
| 1 | +import { evmAddress, useStkGhoMigrate } from '@aave/react'; |
| 2 | +import { useSendTransaction } from '@aave/react/viem'; |
| 3 | +import { Trans } from '@lingui/macro'; |
| 4 | +import { BoxProps } from '@mui/material'; |
| 5 | +import { useQueryClient } from '@tanstack/react-query'; |
| 6 | +import { errAsync } from 'neverthrow'; |
| 7 | +import React, { useEffect } from 'react'; |
| 8 | +import { oracles, stakedTokens } from 'src/hooks/stake/common'; |
| 9 | +import { useModalContext } from 'src/hooks/useModal'; |
| 10 | +import { useSavingsMarketData } from 'src/hooks/useSavingsMarketData'; |
| 11 | +import { useWeb3Context } from 'src/libs/hooks/useWeb3Context'; |
| 12 | +import { useSGhoVaultContext } from 'src/modules/sGho/SGhoVaultContext'; |
| 13 | +import { useRootStore } from 'src/store/root'; |
| 14 | +import { queryKeysFactory } from 'src/ui-config/queries'; |
| 15 | +import { wagmiConfig } from 'src/ui-config/wagmiConfig'; |
| 16 | +import { useWalletClient } from 'wagmi'; |
| 17 | +import { waitForTransactionReceipt } from 'wagmi/actions'; |
| 18 | +import { useShallow } from 'zustand/shallow'; |
| 19 | + |
| 20 | +import { TxActionsWrapper } from '../TxActionsWrapper'; |
| 21 | + |
| 22 | +// Static recommendation: migrate redeems the full stkGHO position and deposits |
| 23 | +// it into the sGHO vault. No approval step exists (the migrator holds the |
| 24 | +// stkGHO claim-helper role and redeems on the user's behalf), so this is a safe |
| 25 | +// upper bound for the single migrate() call. |
| 26 | +const STK_GHO_MIGRATE_GAS_LIMIT = 250_000; |
| 27 | + |
| 28 | +export interface StkGhoMigrateActionsProps extends BoxProps { |
| 29 | + isWrongNetwork: boolean; |
| 30 | + blocked: boolean; |
| 31 | +} |
| 32 | + |
| 33 | +export const StkGhoMigrateActions = React.memo( |
| 34 | + ({ isWrongNetwork, blocked, sx, ...props }: StkGhoMigrateActionsProps) => { |
| 35 | + const { currentAccount } = useWeb3Context(); |
| 36 | + const { chainId: targetChainId, sdkChainId } = useSavingsMarketData(); |
| 37 | + const { mainTxState, setMainTxState, setTxError, setGasLimit } = useModalContext(); |
| 38 | + const { refresh } = useSGhoVaultContext(); |
| 39 | + const queryClient = useQueryClient(); |
| 40 | + const [user, marketData] = useRootStore( |
| 41 | + useShallow((state) => [state.account, state.currentMarketData]) |
| 42 | + ); |
| 43 | + |
| 44 | + const { data: walletClient } = useWalletClient(); |
| 45 | + const [migrate] = useStkGhoMigrate(); |
| 46 | + const [sendTransaction] = useSendTransaction(walletClient); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + setGasLimit(STK_GHO_MIGRATE_GAS_LIMIT.toString()); |
| 50 | + }, [setGasLimit]); |
| 51 | + |
| 52 | + const action = async () => { |
| 53 | + if (!currentAccount || !walletClient) return; |
| 54 | + setMainTxState({ loading: true }); |
| 55 | + setTxError(undefined); |
| 56 | + |
| 57 | + const result = await migrate({ |
| 58 | + user: evmAddress(currentAccount), |
| 59 | + chainId: sdkChainId, |
| 60 | + }).andThen((plan) => { |
| 61 | + switch (plan.__typename) { |
| 62 | + case 'TransactionRequest': |
| 63 | + return sendTransaction(plan); |
| 64 | + case 'InsufficientBalanceError': |
| 65 | + return errAsync(new Error('No stkGHO balance available to migrate.')); |
| 66 | + case 'ApprovalRequired': |
| 67 | + return errAsync( |
| 68 | + new Error('Unexpected migration plan; expected a transaction request.') |
| 69 | + ); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + if (result.isErr()) { |
| 74 | + setMainTxState({ loading: false }); |
| 75 | + setTxError({ |
| 76 | + blocking: true, |
| 77 | + actionBlocked: true, |
| 78 | + rawError: result.error as Error, |
| 79 | + error: <span>{(result.error as Error).message}</span>, |
| 80 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 81 | + txAction: 0 as any, |
| 82 | + }); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + const submittedTxHash = result.value; |
| 87 | + setMainTxState({ loading: true, txHash: submittedTxHash }); |
| 88 | + |
| 89 | + // Wait for the receipt on the connected chain (works on forks too). |
| 90 | + try { |
| 91 | + await waitForTransactionReceipt(wagmiConfig, { |
| 92 | + hash: submittedTxHash as `0x${string}`, |
| 93 | + chainId: targetChainId, |
| 94 | + }); |
| 95 | + } catch (e) { |
| 96 | + console.warn('waitForTransactionReceipt failed', e); |
| 97 | + } |
| 98 | + |
| 99 | + // Refresh the sGHO vault cache (new shares) and invalidate the stkGHO |
| 100 | + // position + pool balances so both panels reflect the migration. |
| 101 | + refresh(); |
| 102 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 103 | + |
| 104 | + queryClient.invalidateQueries({ queryKey: queryKeysFactory.pool }); |
| 105 | + queryClient.invalidateQueries({ |
| 106 | + queryKey: queryKeysFactory.userStakeUiData(user, marketData, stakedTokens, oracles), |
| 107 | + }); |
| 108 | + |
| 109 | + setMainTxState({ loading: false, success: true, txHash: submittedTxHash }); |
| 110 | + }; |
| 111 | + |
| 112 | + return ( |
| 113 | + <TxActionsWrapper |
| 114 | + requiresApproval={false} |
| 115 | + preparingTransactions={false} |
| 116 | + mainTxState={mainTxState} |
| 117 | + isWrongNetwork={isWrongNetwork} |
| 118 | + handleAction={action} |
| 119 | + symbol="stkGHO" |
| 120 | + actionText={<Trans>Proceed with migration</Trans>} |
| 121 | + actionInProgressText={<Trans>Migrating</Trans>} |
| 122 | + sx={sx} |
| 123 | + blocked={blocked} |
| 124 | + {...props} |
| 125 | + /> |
| 126 | + ); |
| 127 | + } |
| 128 | +); |
| 129 | + |
| 130 | +StkGhoMigrateActions.displayName = 'StkGhoMigrateActions'; |
0 commit comments