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

feat: add eligibility for transition in delegations and update UI acc… #406

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/app/api/getDelegations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface DelegationAPI {
unbonding_tx?: UnbondingTxAPI;
is_overflow: boolean;
transitioned: boolean;
is_eligible_for_transition: boolean;
}

interface StakingTxAPI {
Expand Down Expand Up @@ -56,6 +57,7 @@ export const getDelegations = async (
// "pagination_reverse": reverse,
// "pagination_limit": limit,
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
staker_btc_pk: encode(publicKeyNoCoord),
state: ["active", "unbonded"],
};

const response = await apiWrapper(
Expand Down Expand Up @@ -89,6 +91,7 @@ export const getDelegations = async (
}
: undefined,
transitioned: apiDelegation.transitioned,
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
isEligibleForTransition: apiDelegation.is_eligible_for_transition,
}),
);

Expand Down
58 changes: 38 additions & 20 deletions src/app/components/Delegations/Delegation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,21 @@ export const Delegation: React.FC<DelegationProps> = ({
};

const generateActionButton = () => {
// This function generates the transition or withdraw button
// based on the state of the delegation
// It also disables the button if the delegation
// is in an intermediate state (local storage)
if (state === DelegationState.ACTIVE) {
if (delegation.isEligibleForTransition) {
return (
<div className="flex justify-end lg:justify-start">
<button
className="btn btn-outline btn-xs inline-flex text-sm font-normal text-primary-dark"
onClick={onTransition}
disabled={
intermediateState === DelegationState.INTERMEDIATE_TRANSITIONING
}
>
Transition
</button>
</div>
);
} else if (state === DelegationState.ACTIVE) {
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
return (
<div className="flex justify-end lg:justify-start">
<button
Expand Down Expand Up @@ -143,6 +153,10 @@ export const Delegation: React.FC<DelegationProps> = ({

const { coinName, mempoolApiUrl } = getNetworkConfig();

const renderActionRequired = () => {
return <p className="text-error-main text-sm">Action Required</p>;
};

return (
<div
className={`relative rounded bg-secondary-contrast odd:bg-[#F9F9F9] p-4 text-base text-primary-dark`}
Expand Down Expand Up @@ -180,21 +194,25 @@ export const Delegation: React.FC<DelegationProps> = ({
add its size 12px and gap 4px, 16/2 = 8px
*/}
<div className="relative flex justify-start lg:justify-start order-4">
<div className="flex items-center justify-start gap-1">
<p>{renderState()}</p>
<span
className="cursor-pointer text-xs"
data-tooltip-id={`tooltip-${stakingTxHashHex}`}
data-tooltip-content={renderStateTooltip()}
data-tooltip-place="top"
>
<AiOutlineInfoCircle />
</span>
<Tooltip
id={`tooltip-${stakingTxHashHex}`}
className="tooltip-wrap"
/>
</div>
{delegation.isEligibleForTransition ? (
renderActionRequired()
) : (
<div className="flex items-center justify-start gap-1">
<p>{renderState()}</p>
<span
className="cursor-pointer text-xs"
data-tooltip-id={`tooltip-${stakingTxHashHex}`}
data-tooltip-content={renderStateTooltip()}
data-tooltip-place="top"
>
<AiOutlineInfoCircle />
</span>
<Tooltip
id={`tooltip-${stakingTxHashHex}`}
className="tooltip-wrap"
/>
</div>
)}
</div>
<DelegationPoints
stakingTxHashHex={stakingTxHashHex}
Expand Down
17 changes: 10 additions & 7 deletions src/app/components/Delegations/Delegations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ export const Delegations = () => {
isWalletConnected={connected}
address={address}
>
<DelegationsContent
delegationsAPI={delegationsAPI.delegations}
address={address}
btcWalletNetwork={network}
publicKeyNoCoord={publicKeyNoCoord}
isWalletConnected={connected}
/>
{/* If there are no delegations, don't render the content */}
{delegationsAPI.delegations.length > 0 && (
<DelegationsContent
delegationsAPI={delegationsAPI.delegations}
address={address}
btcWalletNetwork={network}
publicKeyNoCoord={publicKeyNoCoord}
isWalletConnected={connected}
/>
)}
</DelegationsPointsProvider>
);
};
Expand Down
8 changes: 7 additions & 1 deletion src/app/components/Staking/Staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const Staking = () => {
const { createDelegationEoi, estimateStakingFee } = useTransactionService();
const { networkInfo } = useAppState();
const latestParam = networkInfo?.params.bbnStakingParams?.latestParam;
const stakingStatus = networkInfo?.stakingStatus;

const [pendingVerificationOpen, setPendingVerificationOpen] = useState(false);
const [stakingTxHashHex, setStakingTxHashHex] = useState<
Expand Down Expand Up @@ -390,7 +391,12 @@ export const Staking = () => {
const renderStakingForm = () => {
// States of the staking form:
// Health check failed
if (!isApiNormal || isGeoBlocked || hasError) {
if (
!isApiNormal ||
isGeoBlocked ||
hasError ||
!stakingStatus?.isStakingOpen
) {
return (
<Message
title="Staking is not available"
Expand Down
3 changes: 3 additions & 0 deletions src/app/types/delegations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Delegation {
unbondingTx: UnbondingTx | undefined;
isOverflow: boolean;
transitioned: boolean;
isEligibleForTransition: boolean;
}

export interface StakingTx {
Expand All @@ -30,6 +31,7 @@ export const UNBONDED = "unbonded";
export const WITHDRAWN = "withdrawn";
export const PENDING = "pending";
export const OVERFLOW = "overflow";
export const SLASHED = "slashed";
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
export const EXPIRED = "expired";
export const INTERMEDIATE_UNBONDING = "intermediate_unbonding";
export const INTERMEDIATE_WITHDRAWAL = "intermediate_withdrawal";
Expand All @@ -45,6 +47,7 @@ export const DelegationState = {
WITHDRAWN,
PENDING,
OVERFLOW,
SLASHED,
EXPIRED,
INTERMEDIATE_UNBONDING,
INTERMEDIATE_WITHDRAWAL,
Expand Down
4 changes: 4 additions & 0 deletions src/utils/getState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export const getState = (state: string) => {
return "Transitioning";
case DelegationState.TRANSITIONED:
return "Transitioned";
case DelegationState.SLASHED:
return "Slashed";
default:
return "Unknown";
}
Expand Down Expand Up @@ -55,6 +57,8 @@ export const getStateTooltip = (state: string) => {
return "Stake is transitioning to the Babylon chain network";
case DelegationState.TRANSITIONED:
return "Stake has been transitioned to the Babylon chain network";
case DelegationState.SLASHED:
return "Stake has been slashed";
default:
return "Unknown";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const toLocalStorageIntermediateDelegation = (
timelock,
},
isOverflow: false,
isEligibleForTransition: false,
unbondingTx: undefined,
transitioned: false,
});
1 change: 1 addition & 0 deletions tests/helper/generateMockDelegation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function generateMockDelegation(
timelock: 3600,
},
isOverflow: false,
isEligibleForTransition: false,
unbondingTx: undefined,
transitioned: false,
};
Expand Down
Loading