Skip to content

Commit

Permalink
fix: minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
totraev committed Jan 2, 2025
1 parent 36da1bb commit 6a709d9
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 46 deletions.
27 changes: 16 additions & 11 deletions src/app/api/getDelegationsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,27 @@ interface DelegationV2API {

export const getDelegationV2 = async (
stakingTxHashHex: string,
): Promise<DelegationV2> => {
): Promise<DelegationV2 | null> => {
if (!stakingTxHashHex) {
throw new Error("No staking tx hash provided");
}
const params = {
staking_tx_hash_hex: stakingTxHashHex,
};

const { data: delegationAPIResponse } = await apiWrapper(
"GET",
"/v2/delegation",
"Error getting delegation v2",
{ query: params },
);
try {
const params = {
staking_tx_hash_hex: stakingTxHashHex,
};

return apiToDelegationV2(delegationAPIResponse);
const { data: delegationAPIResponse } = await apiWrapper(
"GET",
"/v2/delegation",
"Error getting delegation v2",
{ query: params },
);

return apiToDelegationV2(delegationAPIResponse.data);
} catch {
return null;
}
};

export const getDelegationsV2 = async (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { Heading, Text } from "@babylonlabs-io/bbn-core-ui";
import { twMerge } from "tailwind-merge";

interface StatusViewProps {
icon: React.ReactNode | string;
title: string;
description?: React.ReactNode;
className?: string;
}

export const StatusView = ({
icon,
title,
description,
}: {
icon: React.ReactNode | string;
title: string;
description?: React.ReactNode;
}) => (
<div className="flex items-center justify-center h-[21rem]">
className,
}: StatusViewProps) => (
<div
className={twMerge("flex items-center justify-center h-[21rem]", className)}
>
<div className="flex flex-col items-center gap-4">
<div className="bg-primary-contrast relative w-[5.5rem] h-[5.5rem]">
<div className="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2">
Expand Down
4 changes: 2 additions & 2 deletions src/app/hooks/services/useStakingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ export function useStakingService() {

const delegation = await retry(
() => getDelegationV2(stakingTxHashHex),
(delegation) => delegation.state === DelegationState.VERIFIED,
(delegation) => delegation?.state === DelegationState.VERIFIED,
5 * ONE_SECOND,
);

setVerifiedDelegation(delegation);
setVerifiedDelegation(delegation as DelegationV2);
goToStep("verified");
setProcessing(false);
} catch (error: any) {
Expand Down
3 changes: 2 additions & 1 deletion src/app/state/StakingState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type StakingStep =
| "eoi-staking-slashing"
| "eoi-unbonding-slashing"
| "eoi-proof-of-possession"
| "eoi-sign-bbn"
| "eoi-send-bbn"
| "verifying"
| "verified"
Expand Down Expand Up @@ -237,7 +238,7 @@ export function StakingState({ children }: PropsWithChildren) {
stakingInfo?.maxFeeRate ?? 0,
"Selected fee rate is higher than the hour fee",
)
.moreThan(
.min(
stakingInfo?.defaultFeeRate ?? 0,
"Fees are low, inclusion is not guaranteed",
),
Expand Down
14 changes: 14 additions & 0 deletions src/components/staking/StakingForm/components/FeeSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useFormState } from "@babylonlabs-io/bbn-core-ui";
import { PropsWithChildren } from "react";

export const FeeSection = ({ children }: PropsWithChildren) => {
const { errors } = useFormState({
name: ["finalityProvider", "term", "amount"],
});

if (errors.finalityProvider || errors.term || errors.amount) {
return null;
}

return <>{children}</>;
};
6 changes: 4 additions & 2 deletions src/components/staking/StakingForm/components/Overlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function FormOverlay({
className,
children,
}: PropsWithChildren<OverlayProps>) {
const { isValid } = useFormState({
const { errors } = useFormState({
name: "finalityProvider",
});

Expand All @@ -19,7 +19,9 @@ export function FormOverlay({
<div
className={twJoin(
`absolute inset-0 bg-secondary-contrast z-10 transition-opacity duration-300`,
isValid ? "opacity-0 pointer-events-none" : "opacity-75",
!errors.finalityProvider
? "opacity-0 pointer-events-none"
: "opacity-75",
)}
/>
{children}
Expand Down
36 changes: 17 additions & 19 deletions src/components/staking/StakingForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Heading, Text, useFormState } from "@babylonlabs-io/bbn-core-ui";
import { Heading, Loader, Text } from "@babylonlabs-io/bbn-core-ui";
import { useState } from "react";

import { LoadingView } from "@/app/components/Loading/Loading";
import { StatusView } from "@/app/components/Staking/FinalityProviders/FinalityProviderTableStatusView";
import apiNotAvailable from "@/app/components/Staking/Form/States/api-not-available.svg";
import { Message } from "@/app/components/Staking/Form/States/Message";
import { WalletNotConnected } from "@/app/components/Staking/Form/States/WalletNotConnected";
Expand All @@ -11,6 +11,7 @@ import { AmountField } from "./components/AmountField";
import { FeeAmountField } from "./components/FeeAmountField";
import { FeeInfo } from "./components/FeeInfo";
import { FeeRateField } from "./components/FeeRateField";
import { FeeSection } from "./components/FeeSection";
import { InfoAlert } from "./components/InfoAlert";
import { FormOverlay } from "./components/Overlay";
import { SubmitButton } from "./components/SubmitButton";
Expand Down Expand Up @@ -38,12 +39,11 @@ export function DelegationForm({
stakingInfo,
}: DelegationFormProps) {
const [isCustomFee, setIsCustomFee] = useState(false);
const { isValid } = useFormState({
name: ["finalityProvider", "term", "amount"],
});

if (loading) {
return <LoadingView />;
return (
<StatusView className="flex-1" icon={<Loader />} title="Please wait..." />
);
}

if (disabled) {
Expand Down Expand Up @@ -79,21 +79,19 @@ export function DelegationForm({
max={stakingInfo?.maxStakingAmountSat}
/>

{isValid && (
<>
<FeeInfo custom={isCustomFee} />
<FeeSection>
<FeeInfo custom={isCustomFee} />

<FeeRateField
expanded={isCustomFee}
defaultRate={stakingInfo?.defaultFeeRate}
min={stakingInfo?.minFeeRate}
max={stakingInfo?.maxFeeRate}
onExpand={() => void setIsCustomFee(true)}
/>
<FeeRateField
expanded={isCustomFee}
defaultRate={stakingInfo?.defaultFeeRate}
min={stakingInfo?.minFeeRate}
max={stakingInfo?.maxFeeRate}
onExpand={() => void setIsCustomFee(true)}
/>

<FeeAmountField />
</>
)}
<FeeAmountField />
</FeeSection>
</FormOverlay>

<SubmitButton />
Expand Down
11 changes: 6 additions & 5 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ export function wait(ms: number): Promise<undefined> {
export async function retry<R>(
fn: () => Promise<R>,
finished: (value: R) => boolean,
every: number | ((index: number, value: R) => number),
delay: number | ((index: number, value: R) => number),
attempts = Infinity,
) {
const delay = typeof every === "number" ? () => every : every;

const getDelay = typeof delay === "number" ? () => delay : delay;
let value = await fn();
let index = 1;
while (!finished(value)) {
await wait(delay(index, value));

while (!finished(value) && index <= attempts) {
await wait(getDelay(index, value));
value = await fn();
index++;
}
Expand Down

0 comments on commit 6a709d9

Please sign in to comment.