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

Remove stake pools #317

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
33 changes: 33 additions & 0 deletions components/admin/StakePoolUpdate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -87,6 +92,9 @@ export function StakePoolUpdate({
}, [JSON.stringify(stakePool)])

if (stakePooldId && !stakePool.isFetched) return <LoadingSpinner />

if (stakePool.isFetched && !stakePool.data)
return <div className="text-gray-400">No stake pool found</div>
return (
<div className="flex w-full flex-col gap-4">
<div className="flex flex-wrap">
Expand Down Expand Up @@ -288,6 +296,31 @@ export function StakePoolUpdate({
>
{stakePool.data ? 'Update' : 'Get Started'}
</AsyncButton>
{stakePool.data && (
<Tooltip title="Only can remove when 0 tokens are staked and reward distributor is closed">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Only can remove when 0 tokens are staked and reward distributor is closed" ->
"Can only be removed when 0 tokens are staked and reward distributor is closed"

<div>
<AsyncButton
disabled={
!stakePool.data ||
stakePool.data.parsed.totalStaked !== 0 ||
!rewardDistributor.isFetched ||
!!rewardDistributor.data ||
walletId?.toString() !==
stakePool?.data.parsed.authority.toString()
}
colorized={false}
onClick={async () => {
handleStakePoolRemove.mutate()
}}
loading={handleStakePoolRemove.isLoading}
inlineLoader
className="flex w-full items-center justify-center bg-red-500 text-center hover:bg-red-600"
>
Remove
</AsyncButton>
</div>
</Tooltip>
)}
</div>
)
}
63 changes: 63 additions & 0 deletions handlers/useHandleStakePoolRemove.ts
Original file line number Diff line number Diff line change
@@ -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<string> => {
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}`,
})
},
}
)
}