Skip to content

Commit

Permalink
feat: update delegation statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
totraev committed Dec 3, 2024
1 parent c6cae45 commit 7796f4e
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 72 deletions.
68 changes: 53 additions & 15 deletions src/app/hooks/services/useDelegationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useMemo } from "react";

import { DELEGATION_ACTIONS as ACTIONS } from "@/app/constants";
import { useDelegationV2State } from "@/app/state/DelegationV2State";
import type { DelegationV2StakingState } from "@/app/types/delegationsV2";
import { DelegationV2StakingState as State } from "@/app/types/delegationsV2";

import { useTransactionService } from "./useTransactionService";

Expand All @@ -20,7 +20,7 @@ interface TxProps {
covenantBtcPkHex: string;
signatureHex: string;
}[];
state: DelegationV2StakingState;
state: State;
stakingInput: {
finalityProviderPkNoCoordHex: string;
stakingAmountSat: number;
Expand All @@ -39,35 +39,41 @@ export function useDelegationService() {
hasMoreDelegations,
isLoading,
findDelegationByTxHash,
addDelegation,
updateDelegationStatus,
} = useDelegationV2State();

const {
submitStakingTx,
submitUnbondingTx,
submitEarlyUnbondedWithdrawalTx,
submitTimelockUnbondedWithdrawalTx,
createDelegationEoi,
} = useTransactionService();

const COMMANDS: Record<ActionType, DelegationCommand> = useMemo(
() => ({
[ACTIONS.STAKE]: ({
[ACTIONS.STAKE]: async ({
stakingInput,
paramsVersion,
stakingTxHashHex,
stakingTxHex,
}: TxProps) =>
submitStakingTx(
}: TxProps) => {
await submitStakingTx(
stakingInput,
paramsVersion,
stakingTxHashHex,
stakingTxHex,
),
);

updateDelegationStatus(
stakingTxHashHex,
State.INTERMEDIATE_PENDING_BTC_CONFIRMATION,
);
},

[ACTIONS.UNBOUND]: async ({
stakingInput,
paramsVersion,
stakingTxHashHex,
stakingTxHex,
unbondingTxHex,
covenantUnbondingSignatures,
Expand All @@ -86,34 +92,54 @@ export function useDelegationService() {
sigHex: sig.signatureHex,
})),
);

updateDelegationStatus(
stakingTxHashHex,
State.INTERMEDIATE_UNBONDING_SUBMITTED,
);
},

[ACTIONS.WITHDRAW_ON_EARLY_UNBOUNDING]: ({
[ACTIONS.WITHDRAW_ON_EARLY_UNBOUNDING]: async ({
stakingTxHashHex,
stakingInput,
paramsVersion,
unbondingTxHex,
}: TxProps) =>
submitEarlyUnbondedWithdrawalTx(
}: TxProps) => {
await submitEarlyUnbondedWithdrawalTx(
stakingInput,
paramsVersion,
unbondingTxHex,
),
);

updateDelegationStatus(
stakingTxHashHex,
State.INTERMEDIATE_EARLY_UNBONDING_WITHDRAWAL_SUBMITTED,
);
},

[ACTIONS.WITHDRAW_ON_TIMELOCK]: async ({
stakingInput,
paramsVersion,
stakingTxHex,
}: TxProps) =>
submitTimelockUnbondedWithdrawalTx(
stakingTxHashHex,
}: TxProps) => {
await submitTimelockUnbondedWithdrawalTx(
stakingInput,
paramsVersion,
stakingTxHex,
),
);

updateDelegationStatus(
stakingTxHashHex,
State.INTERMEDIATE_TIMELOCK_WITHDRAWAL_SUBMITTED,
);
},

[ACTIONS.WITHDRAW_ON_EARLY_UNBOUNDING_SLASHING]: async ({
stakingInput,
paramsVersion,
unbondingSlashingTxHex,
stakingTxHashHex,
}) => {
if (!unbondingSlashingTxHex) {
throw new Error(
Expand All @@ -125,12 +151,18 @@ export function useDelegationService() {
paramsVersion,
unbondingSlashingTxHex,
);

updateDelegationStatus(
stakingTxHashHex,
State.INTERMEDIATE_EARLY_UNBONDING_SLASHING_WITHDRAWAL_SUBMITTED,
);
},

[ACTIONS.WITHDRAW_ON_TIMELOCK_SLASHING]: async ({
stakingInput,
paramsVersion,
slashingTxHex,
stakingTxHashHex,
}) => {
if (!slashingTxHex) {
throw new Error("Slashing tx not found, can't submit withdrawal");
Expand All @@ -140,9 +172,15 @@ export function useDelegationService() {
paramsVersion,
slashingTxHex,
);

updateDelegationStatus(
stakingTxHashHex,
State.INTERMEDIATE_TIMELOCK_WITHDRAWAL_SUBMITTED,
);
},
}),
[
updateDelegationStatus,
submitStakingTx,
submitUnbondingTx,
submitEarlyUnbondedWithdrawalTx,
Expand Down
14 changes: 6 additions & 8 deletions src/app/hooks/storage/useDelegationStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,14 @@ export function useDelegationStorage(
function syncPendingDelegations() {
if (!key) return;

setPendingDelegations((delegations) => {
const result = Object.values(delegations)
setPendingDelegations((delegations) =>
Object.values(delegations)
.filter((d) => !delegationMap[d.stakingTxHashHex])
.reduce(
(acc, d) => ({ ...acc, [d.stakingTxHashHex]: d }),
{} as Record<string, DelegationLike>,
);

return result;
});
),
);
},
[key, delegationMap, setPendingDelegations],
);
Expand All @@ -86,8 +84,8 @@ export function useDelegationStorage(
Object.entries(statuses)
.filter(
([hash, status]) =>
DELEGATION_STATUSES[status] <
DELEGATION_STATUSES[delegationMap[hash].state],
DELEGATION_STATUSES[delegationMap[hash].state] <
DELEGATION_STATUSES[status],
)
.reduce(
(acc, [hash, status]) => ({ ...acc, [hash]: status }),
Expand Down
50 changes: 23 additions & 27 deletions src/app/types/delegationsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export enum DelegationV2StakingState {

// Slashed states
SLASHED = "SLASHED",
EARLY_UNBONDING_SLASHED = "EARLY_UNBONDING_SLASHED",
TIMELOCK_SLASHED = "TIMELOCK_SLASHED",

// Intermediate states
INTERMEDIATE_PENDING_VERIFICATION = "INTERMEDIATE_PENDING_VERIFICATION",
Expand All @@ -65,32 +63,30 @@ export enum DelegationV2StakingState {

export const DELEGATION_STATUSES = {
[DelegationV2StakingState.PENDING]: 0,
[DelegationV2StakingState.INTERMEDIATE_PENDING_VERIFICATION]: 0,
[DelegationV2StakingState.INTERMEDIATE_PENDING_VERIFICATION]: 0.5,
[DelegationV2StakingState.VERIFIED]: 1,
[DelegationV2StakingState.INTERMEDIATE_PENDING_BTC_CONFIRMATION]: 2,
[DelegationV2StakingState.ACTIVE]: 3,

[DelegationV2StakingState.INTERMEDIATE_UNBONDING_SUBMITTED]: 4,
[DelegationV2StakingState.EARLY_UNBONDING]: 5,
[DelegationV2StakingState.EARLY_UNBONDING_WITHDRAWABLE]: 6,
[DelegationV2StakingState.INTERMEDIATE_EARLY_UNBONDING_WITHDRAWAL_SUBMITTED]: 7,
[DelegationV2StakingState.EARLY_UNBONDING_WITHDRAWN]: 8,

[DelegationV2StakingState.SLASHED]: 9,
[DelegationV2StakingState.EARLY_UNBONDING_SLASHED]: 9,
[DelegationV2StakingState.EARLY_UNBONDING_SLASHING_WITHDRAWABLE]: 10,
[DelegationV2StakingState.INTERMEDIATE_EARLY_UNBONDING_SLASHING_WITHDRAWAL_SUBMITTED]: 11,
[DelegationV2StakingState.EARLY_UNBONDING_SLASHING_WITHDRAWN]: 12,

[DelegationV2StakingState.TIMELOCK_UNBONDING]: 13,
[DelegationV2StakingState.TIMELOCK_WITHDRAWABLE]: 14,
[DelegationV2StakingState.INTERMEDIATE_TIMELOCK_WITHDRAWAL_SUBMITTED]: 15,
[DelegationV2StakingState.TIMELOCK_WITHDRAWN]: 16,

[DelegationV2StakingState.TIMELOCK_SLASHED]: 17,
[DelegationV2StakingState.TIMELOCK_SLASHING_WITHDRAWABLE]: 18,
[DelegationV2StakingState.INTERMEDIATE_TIMELOCK_SLASHING_WITHDRAWAL_SUBMITTED]: 19,
[DelegationV2StakingState.TIMELOCK_SLASHING_WITHDRAWN]: 20,
[DelegationV2StakingState.INTERMEDIATE_PENDING_BTC_CONFIRMATION]: 1.5,
[DelegationV2StakingState.ACTIVE]: 2,

[DelegationV2StakingState.INTERMEDIATE_UNBONDING_SUBMITTED]: 2.5,
[DelegationV2StakingState.EARLY_UNBONDING]: 3,
[DelegationV2StakingState.EARLY_UNBONDING_WITHDRAWABLE]: 4,
[DelegationV2StakingState.INTERMEDIATE_EARLY_UNBONDING_WITHDRAWAL_SUBMITTED]: 4.5,
[DelegationV2StakingState.EARLY_UNBONDING_WITHDRAWN]: 5,

[DelegationV2StakingState.SLASHED]: 4,
[DelegationV2StakingState.EARLY_UNBONDING_SLASHING_WITHDRAWABLE]: 5,
[DelegationV2StakingState.INTERMEDIATE_EARLY_UNBONDING_SLASHING_WITHDRAWAL_SUBMITTED]: 5.5,
[DelegationV2StakingState.EARLY_UNBONDING_SLASHING_WITHDRAWN]: 6,

[DelegationV2StakingState.TIMELOCK_UNBONDING]: 3,
[DelegationV2StakingState.TIMELOCK_WITHDRAWABLE]: 4,
[DelegationV2StakingState.INTERMEDIATE_TIMELOCK_WITHDRAWAL_SUBMITTED]: 4.5,
[DelegationV2StakingState.TIMELOCK_WITHDRAWN]: 5,

[DelegationV2StakingState.TIMELOCK_SLASHING_WITHDRAWABLE]: 5,
[DelegationV2StakingState.INTERMEDIATE_TIMELOCK_SLASHING_WITHDRAWAL_SUBMITTED]: 5.5,
[DelegationV2StakingState.TIMELOCK_SLASHING_WITHDRAWN]: 6,
} as const;

export const getDelegationV2StakingState = (
Expand Down
44 changes: 22 additions & 22 deletions src/components/delegations/DelegationList/components/Status.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useAppState } from "@/app/state";
import { DelegationV2StakingState as state } from "@/app/types/delegationsV2";
import { DelegationV2StakingState as State } from "@/app/types/delegationsV2";
import { BbnStakingParamsVersion } from "@/app/types/networkInfo";
import { Hint } from "@/components/common/Hint";
import { blocksToDisplayTime } from "@/utils/time";
Expand All @@ -12,88 +12,88 @@ const STATUSES: Record<
string,
(param?: BbnStakingParamsVersion) => { label: string; tooltip: string }
> = {
[state.PENDING]: () => ({
[State.PENDING]: () => ({
label: "Pending",
tooltip: "Stake is pending verification",
}),
[state.VERIFIED]: () => ({
[State.VERIFIED]: () => ({
label: "Verified",
tooltip: "Stake is verified, you can start staking",
}),
[state.ACTIVE]: () => ({
[State.ACTIVE]: () => ({
label: "Active",
tooltip: "Stake is active",
}),
[state.TIMELOCK_UNBONDING]: (param) => ({
[State.TIMELOCK_UNBONDING]: (param) => ({
label: "Unbonding",
tooltip:
"Stake is about to be unbonded as it's reaching the timelock period",
}),
[state.EARLY_UNBONDING]: (param) => ({
[State.EARLY_UNBONDING]: (param) => ({
label: "Unbonding",
tooltip: `Unbonding process of ${blocksToDisplayTime(param?.unbondingTime)} has started`,
}),
[state.TIMELOCK_WITHDRAWABLE]: () => ({
[State.TIMELOCK_WITHDRAWABLE]: () => ({
label: "Withdrawable",
tooltip: "Stake is withdrawable as it's reached the timelock period",
}),
[state.EARLY_UNBONDING_WITHDRAWABLE]: () => ({
[State.EARLY_UNBONDING_WITHDRAWABLE]: () => ({
label: "Withdrawable",
tooltip: "Stake is withdrawable after the unbonding period",
}),
[state.TIMELOCK_SLASHING_WITHDRAWABLE]: () => ({
[State.TIMELOCK_SLASHING_WITHDRAWABLE]: () => ({
label: "Withdrawable",
tooltip: "Slashed Stake is now withdrawable",
}),
[state.EARLY_UNBONDING_SLASHING_WITHDRAWABLE]: () => ({
[State.EARLY_UNBONDING_SLASHING_WITHDRAWABLE]: () => ({
label: "Withdrawable",
tooltip: "Slashed Stake is now withdrawable",
}),
[state.SLASHED]: () => ({
[State.SLASHED]: () => ({
label: "Slashed",
tooltip: "Stake has been slashed",
}),
[state.TIMELOCK_WITHDRAWN]: () => ({
[State.TIMELOCK_WITHDRAWN]: () => ({
label: "Withdrawn",
tooltip: "Stake has been withdrawn",
}),
[state.EARLY_UNBONDING_WITHDRAWN]: () => ({
[State.EARLY_UNBONDING_WITHDRAWN]: () => ({
label: "Withdrawn",
tooltip: "Stake has been withdrawn",
}),
[state.EARLY_UNBONDING_SLASHING_WITHDRAWN]: () => ({
[State.EARLY_UNBONDING_SLASHING_WITHDRAWN]: () => ({
label: "Withdrawn",
tooltip: "Slashed Stake has been withdrawn",
}),
[state.TIMELOCK_SLASHING_WITHDRAWN]: () => ({
[State.TIMELOCK_SLASHING_WITHDRAWN]: () => ({
label: "Withdrawn",
tooltip: "Slashed Stake has been withdrawn",
}),
[state.INTERMEDIATE_PENDING_VERIFICATION]: () => ({
[State.INTERMEDIATE_PENDING_VERIFICATION]: () => ({
label: "Pending",
tooltip: "Stake is pending verification",
}),
[state.INTERMEDIATE_PENDING_BTC_CONFIRMATION]: () => ({
[State.INTERMEDIATE_PENDING_BTC_CONFIRMATION]: () => ({
label: "Pending",
tooltip: "Stake is pending 10 BTC confirmations",
}),
[state.INTERMEDIATE_UNBONDING_SUBMITTED]: () => ({
[State.INTERMEDIATE_UNBONDING_SUBMITTED]: () => ({
label: "Unbonding",
tooltip: "Stake is requesting unbonding",
}),
[state.INTERMEDIATE_EARLY_UNBONDING_WITHDRAWAL_SUBMITTED]: () => ({
[State.INTERMEDIATE_EARLY_UNBONDING_WITHDRAWAL_SUBMITTED]: () => ({
label: "Withdrawal",
tooltip: "Withdrawal transaction pending confirmation on Bitcoin",
}),
[state.INTERMEDIATE_EARLY_UNBONDING_SLASHING_WITHDRAWAL_SUBMITTED]: () => ({
[State.INTERMEDIATE_EARLY_UNBONDING_SLASHING_WITHDRAWAL_SUBMITTED]: () => ({
label: "Withdrawal",
tooltip: "Withdrawal transaction pending confirmation on Bitcoin",
}),
[state.INTERMEDIATE_TIMELOCK_WITHDRAWAL_SUBMITTED]: () => ({
[State.INTERMEDIATE_TIMELOCK_WITHDRAWAL_SUBMITTED]: () => ({
label: "Withdrawal",
tooltip: "Withdrawal transaction pending confirmation on Bitcoin",
}),
[state.INTERMEDIATE_TIMELOCK_SLASHING_WITHDRAWAL_SUBMITTED]: () => ({
[State.INTERMEDIATE_TIMELOCK_SLASHING_WITHDRAWAL_SUBMITTED]: () => ({
label: "Withdrawal",
tooltip: "Withdrawal transaction pending confirmation on Bitcoin",
}),
Expand Down

0 comments on commit 7796f4e

Please sign in to comment.