diff --git a/components/admin/StakePoolUpdate.tsx b/components/admin/StakePoolUpdate.tsx index db7f33ad4..72086ec79 100644 --- a/components/admin/StakePoolUpdate.tsx +++ b/components/admin/StakePoolUpdate.tsx @@ -3,9 +3,12 @@ import { AsyncButton } from 'common/Button' import { FormFieldTitleInput } from 'common/FormFieldInput' import { LoadingSpinner } from 'common/LoadingSpinner' import { SelectorBoolean } from 'common/SelectorBoolean' +import { Tooltip } from 'common/Tooltip' import { useFormik } from 'formik' import { useHandleStakePoolCreate } from 'handlers/useHandleStakePoolCreate' +import { useHandleStakePoolRemove } from 'handlers/useHandleStakePoolRemove' import { useHandleStakePoolUpdate } from 'handlers/useHandleStakePoolUpdate' +import { useRewardDistributorData } from 'hooks/useRewardDistributorData' import { useStakePoolData } from 'hooks/useStakePoolData' import { useStakePoolId } from 'hooks/useStakePoolId' import { useWalletId } from 'hooks/useWalletId' @@ -74,6 +77,8 @@ export function StakePoolUpdate({ const stakePool = useStakePoolData() const handleStakePoolUpdate = useHandleStakePoolUpdate() const handleStakePoolCreate = useHandleStakePoolCreate() + const handleStakePoolRemove = useHandleStakePoolRemove() + const rewardDistributor = useRewardDistributorData() const initialValues = defaultValues(stakePool.data) const formState = useFormik({ initialValues, @@ -87,6 +92,9 @@ export function StakePoolUpdate({ }, [JSON.stringify(stakePool)]) if (stakePooldId && !stakePool.isFetched) return + + if (stakePool.isFetched && !stakePool.data) + return
No stake pool found
return (
@@ -288,6 +296,31 @@ export function StakePoolUpdate({ > {stakePool.data ? 'Update' : 'Get Started'} + {stakePool.data && ( + +
+ { + handleStakePoolRemove.mutate() + }} + loading={handleStakePoolRemove.isLoading} + inlineLoader + className="flex w-full items-center justify-center bg-red-500 text-center hover:bg-red-600" + > + Remove + +
+
+ )}
) } diff --git a/handlers/useHandleStakePoolRemove.ts b/handlers/useHandleStakePoolRemove.ts new file mode 100644 index 000000000..361685e69 --- /dev/null +++ b/handlers/useHandleStakePoolRemove.ts @@ -0,0 +1,63 @@ +import { rewardsCenterProgram } from '@cardinal/rewards-center' +import { executeTransaction } from '@cardinal/staking' +import { withCloseStakePool } from '@cardinal/staking/dist/cjs/programs/stakePool/transaction' +import { useWallet } from '@solana/wallet-adapter-react' +import { Transaction } from '@solana/web3.js' +import { notify } from 'common/Notification' +import { asWallet } from 'common/Wallets' +import { useRewardDistributorData } from 'hooks/useRewardDistributorData' +import { useMutation } from 'react-query' + +import { isStakePoolV2, useStakePoolData } from '../hooks/useStakePoolData' +import { useEnvironmentCtx } from '../providers/EnvironmentProvider' + +export const useHandleStakePoolRemove = () => { + const wallet = asWallet(useWallet()) + const { connection } = useEnvironmentCtx() + const stakePool = useStakePoolData() + const rewardDistributor = useRewardDistributorData() + + return useMutation( + async (): Promise => { + if (!wallet) throw 'Wallet not found' + if (!stakePool.data || !stakePool.data.parsed) throw 'No stake pool found' + if (rewardDistributor.data?.parsed) + throw 'Reward distributor must be removed first' + + const transaction = new Transaction() + if (isStakePoolV2(stakePool.data.parsed)) { + const program = rewardsCenterProgram(connection, wallet) + const ix = await program.methods + .closeStakePool() + .accounts({ + stakePool: stakePool.data.pubkey, + authority: wallet.publicKey, + }) + .instruction() + transaction.add(ix) + } else { + await withCloseStakePool(transaction, connection, wallet, { + stakePoolId: stakePool.data.pubkey, + }) + } + return executeTransaction(connection, wallet, transaction, {}) + }, + { + onSuccess: (txid) => { + notify({ + message: `Successfully closed stake pool`, + txid, + type: 'success', + }) + rewardDistributor.refetch() + stakePool.refetch() + }, + onError: (e) => { + notify({ + message: 'Failed to close stake pool', + description: `${e}`, + }) + }, + } + ) +}