Skip to content

Commit

Permalink
use correct height for unbonding model (#196)
Browse files Browse the repository at this point in the history
* fix: improvement on using the correct height for unbonding model
---------

Co-authored-by: wjrjerome <[email protected]>
  • Loading branch information
jeremy-babylonlabs and jrwbabylonlab committed Oct 4, 2024
1 parent 2b54b34 commit 597c2ed
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-staking",
"version": "0.3.4",
"version": "0.3.5",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
33 changes: 8 additions & 25 deletions src/app/components/Delegations/Delegations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
const { showError } = useError();
const { isApiNormal, isGeoBlocked } = useHealthCheck();
const [awaitingWalletResponse, setAwaitingWalletResponse] = useState(false);
const [selectedDelegationHeight, setSelectedDelegationHeight] = useState<
number | undefined
>();

const shouldShowPoints =
isApiNormal && !isGeoBlocked && shouldDisplayPoints();
Expand Down Expand Up @@ -185,15 +182,13 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
message: error.message,
errorState: ErrorState.UNBONDING,
},
retryAction: () =>
handleModal(id, MODE_UNBOND, selectedDelegationHeight!),
retryAction: () => handleModal(id, MODE_UNBOND),
});
} finally {
setModalOpen(false);
setTxID("");
setModalMode(undefined);
setAwaitingWalletResponse(false);
setSelectedDelegationHeight(undefined);
}
};

Expand Down Expand Up @@ -222,23 +217,20 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
message: error.message,
errorState: ErrorState.WITHDRAW,
},
retryAction: () =>
handleModal(id, MODE_WITHDRAW, selectedDelegationHeight!),
retryAction: () => handleModal(id, MODE_WITHDRAW),
});
} finally {
setModalOpen(false);
setTxID("");
setModalMode(undefined);
setAwaitingWalletResponse(false);
setSelectedDelegationHeight(undefined);
}
};

const handleModal = (txID: string, mode: MODE, delegationHeight: number) => {
const handleModal = (txID: string, mode: MODE) => {
setModalOpen(true);
setTxID(txID);
setModalMode(mode);
setSelectedDelegationHeight(delegationHeight);
};

useEffect(() => {
Expand Down Expand Up @@ -344,19 +336,9 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
stakingValueSat={stakingValueSat}
stakingTxHash={stakingTxHashHex}
state={state}
onUnbond={() =>
handleModal(
stakingTxHashHex,
MODE_UNBOND,
stakingTx.startHeight,
)
}
onUnbond={() => handleModal(stakingTxHashHex, MODE_UNBOND)}
onWithdraw={() =>
handleModal(
stakingTxHashHex,
MODE_WITHDRAW,
stakingTx.startHeight,
)
handleModal(stakingTxHashHex, MODE_WITHDRAW)
}
intermediateState={intermediateDelegation?.state}
isOverflow={isOverflow}
Expand All @@ -368,9 +350,8 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
</div>
</>
)}
{modalMode && txID && selectedDelegationHeight !== undefined && (
{modalMode && txID && (
<UnbondWithdrawModal
delegationHeight={selectedDelegationHeight}
open={modalOpen}
onClose={() => setModalOpen(false)}
onProceed={() => {
Expand All @@ -380,6 +361,8 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
}}
mode={modalMode}
awaitingWalletResponse={awaitingWalletResponse}
delegationsAPI={delegationsAPI}
txID={txID}
/>
)}
</div>
Expand Down
26 changes: 19 additions & 7 deletions src/app/components/Modals/UnbondWithdrawModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useMemo } from "react";
import { IoMdClose } from "react-icons/io";

import { useVersionInfo } from "@/app/context/api/VersionInfo";
import { useVersionByHeight } from "@/app/context/api/VersionInfo";
import { Delegation as DelegationInterface } from "@/app/types/delegations";
import { getNetworkConfig } from "@/config/network.config";
import { blocksToDisplayTime } from "@/utils/blocksToDisplayTime";
import { satoshiToBtc } from "@/utils/btcConversions";
Expand All @@ -15,28 +17,38 @@ export const MODE_WITHDRAW = "withdraw";
export type MODE = typeof MODE_UNBOND | typeof MODE_WITHDRAW;

interface PreviewModalProps {
delegationHeight: number;
open: boolean;
onClose: (value: boolean) => void;
onProceed: () => void;
mode: MODE;
awaitingWalletResponse: boolean;
delegationsAPI: DelegationInterface[];
txID: string;
}

export const UnbondWithdrawModal: React.FC<PreviewModalProps> = ({
delegationHeight,
open,
onClose,
onProceed,
mode,
awaitingWalletResponse,
delegationsAPI,
txID,
}) => {
const { coinName, networkName } = getNetworkConfig();
const versionInfo = useVersionInfo();

const globalParams = versionInfo?.currentVersion;
const unbondingFeeSat = globalParams?.unbondingFeeSat || 0;
const unbondingTimeBlocks = globalParams?.unbondingTime || 0;
const delegation = useMemo(
() =>
delegationsAPI.find((delegation) => delegation.stakingTxHashHex === txID),
[delegationsAPI, txID],
);

const { currentVersion: delegationGlobalParams } = useVersionByHeight(
delegation?.stakingTx?.startHeight ?? 0,
);

const unbondingFeeSat = delegationGlobalParams?.unbondingFeeSat ?? 0;
const unbondingTimeBlocks = delegationGlobalParams?.unbondingTime ?? 0;

const unbondTitle = "Unbond";

Expand Down
15 changes: 14 additions & 1 deletion src/app/context/api/VersionInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export function useVersionInfo() {
return useContext(VersionInfoContext);
}

export function useVersionByHeight(height: number) {
const { data: versions } = useVersions();
return useMemo(
() => getCurrentGlobalParamsVersion(height, versions ?? []),
[versions, height],
);
}

export function VersionInfoProvider({ children }: PropsWithChildren) {
const {
data: versions,
Expand All @@ -45,11 +53,16 @@ export function VersionInfoProvider({ children }: PropsWithChildren) {
isLoading: isVersionLoading || isHeightLoading,
};

const currentVersionInfo = getCurrentGlobalParamsVersion(
height + 1,
versions,
);

return {
currentHeight: height,
isError: isHeightError || isVersionError,
isLoading: isVersionLoading || isHeightLoading,
...getCurrentGlobalParamsVersion(height + 1, versions),
...currentVersionInfo,
};
}, [
versions,
Expand Down

0 comments on commit 597c2ed

Please sign in to comment.