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

fix: default term value #612

Merged
merged 2 commits into from
Jan 10, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/app/components/Staking/StakingForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function StakingForm() {
<FinalityProviders />
</div>

<div className="flex items-center justify-center lg:w-2/5 xl:w-1/3 p-6 rounded border bg-secondary-contrast border-primary-light/20">
<div className="flex lg:w-2/5 xl:w-1/3 p-6 rounded border bg-secondary-contrast border-primary-light/20">
<DelegationForm
loading={loading}
disabled={hasError}
Expand Down
2 changes: 1 addition & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const Home = () => {

<Container
as="main"
className="-mt-[10rem] md:-mt-[6.25rem] flex flex-col gap-12 md:gap-16 pb-16"
className="-mt-[10rem] md:-mt-[6.5rem] flex flex-col gap-12 md:gap-16 pb-16"
>
<Stats />
<PersonalBalance />
Expand Down
7 changes: 7 additions & 0 deletions src/app/state/StakingState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useNetworkFees } from "@/app/hooks/client/api/useNetworkFees";
import { useHealthCheck } from "@/app/hooks/useHealthCheck";
import { useAppState } from "@/app/state";
import type { DelegationV2 } from "@/app/types/delegationsV2";
import { IS_FIXED_TERM_FIELD } from "@/config";
import { getNetworkConfigBTC } from "@/config/network/btc";
import { btcToSatoshi, satoshiToBtc } from "@/utils/btc";
import { createStateUtils } from "@/utils/createStateUtils";
Expand Down Expand Up @@ -53,6 +54,7 @@ export interface StakingState {
defaultFeeRate: number;
minStakingTimeBlocks: number;
maxStakingTimeBlocks: number;
defaultStakingTimeBlocks?: number;
minStakingAmountSat: number;
maxStakingAmountSat: number;
unbondingFeeSat: number;
Expand Down Expand Up @@ -161,6 +163,10 @@ export function StakingState({ children }: PropsWithChildren) {

const { minFeeRate, defaultFeeRate, maxFeeRate } =
getFeeRateFromMempool(mempoolFeeRates);
const defaultStakingTimeBlocks =
IS_FIXED_TERM_FIELD || minStakingTimeBlocks === maxStakingTimeBlocks
? maxStakingTimeBlocks
: undefined;

return {
defaultFeeRate,
Expand All @@ -170,6 +176,7 @@ export function StakingState({ children }: PropsWithChildren) {
maxStakingAmountSat,
minStakingTimeBlocks,
maxStakingTimeBlocks,
defaultStakingTimeBlocks,
unbondingFeeSat,
unbondingTime,
};
Expand Down
7 changes: 4 additions & 3 deletions src/components/staking/StakingForm/components/TermField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { HiddenField, NumberField } from "@babylonlabs-io/bbn-core-ui";
interface TermFieldProps {
min?: number;
max?: number;
defaultValue?: number;
}

export function TermField({ min = 0, max = 0 }: TermFieldProps) {
if (min === max) {
return <HiddenField name="term" defaultValue={max.toString()} />;
export function TermField({ min = 0, defaultValue }: TermFieldProps) {
if (defaultValue) {
return <HiddenField name="term" defaultValue={defaultValue.toString()} />;
}

const label = (
Expand Down
8 changes: 7 additions & 1 deletion src/components/staking/StakingForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface DelegationFormProps {
maxStakingTimeBlocks: number;
minStakingAmountSat: number;
maxStakingAmountSat: number;
defaultStakingTimeBlocks?: number;
};
}

Expand All @@ -45,7 +46,11 @@ export function DelegationForm({

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

Expand Down Expand Up @@ -85,6 +90,7 @@ export function DelegationForm({
<div className="flex flex-1 flex-col">
<FormOverlay>
<TermField
defaultValue={stakingInfo?.defaultStakingTimeBlocks}
min={stakingInfo?.minStakingTimeBlocks}
max={stakingInfo?.maxStakingTimeBlocks}
/>
Expand Down
16 changes: 13 additions & 3 deletions src/components/staking/StakingModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ export function StakingModal() {
} = useStakingState();
const { getFinalityProvider } = useFinalityProviderState();
const { createEOI, stakeDelegation } = useStakingService();
const { reset: resetForm, trigger: revalidateForm } = useFormContext();
const {
reset: resetForm,
trigger: revalidateForm,
setValue: setFieldValue,
} = useFormContext();

const fp = useMemo(
() => getFinalityProvider(formData?.finalityProvider ?? ""),
Expand Down Expand Up @@ -65,9 +69,15 @@ export function StakingModal() {
finalityProvider: "",
term: "",
amount: "",
feeRate: stakingInfo?.defaultFeeRate ?? 0,
feeAmount: 0,
feeRate: stakingInfo?.defaultFeeRate?.toString() ?? "0",
feeAmount: "0",
});
if (stakingInfo?.defaultStakingTimeBlocks) {
setFieldValue("term", stakingInfo?.defaultStakingTimeBlocks, {
shouldDirty: true,
shouldTouch: true,
});
}
revalidateForm();
}}
/>
Expand Down
3 changes: 3 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ export const getNetworkAppUrl = (): string => {
export const getBtcNetwork = (): Network => {
return network;
};

export const IS_FIXED_TERM_FIELD =
process.env.NEXT_PUBLIC_FIXED_STAKING_TERM === "true";