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

use correct height for unbonding model #196

Merged
merged 3 commits into from
Oct 4, 2024
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
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 @@ -100,9 +100,6 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
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 @@ -174,15 +171,13 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
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 @@ -211,23 +206,20 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
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 @@ -333,19 +325,9 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
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 @@ -357,9 +339,8 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
</div>
</>
)}
{modalMode && txID && selectedDelegationHeight !== undefined && (
{modalMode && txID && (
<UnbondWithdrawModal
delegationHeight={selectedDelegationHeight}
jeremy-babylonlabs marked this conversation as resolved.
Show resolved Hide resolved
open={modalOpen}
onClose={() => setModalOpen(false)}
onProceed={() => {
Expand All @@ -369,6 +350,8 @@ const DelegationsContent: React.FC<DelegationsProps> = ({
}}
mode={modalMode}
awaitingWalletResponse={awaitingWalletResponse}
delegationsAPI={delegationsAPI}
txID={txID}
/>
)}
</div>
Expand Down
18 changes: 15 additions & 3 deletions src/app/components/Modals/UnbondWithdrawModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IoMdClose } from "react-icons/io";

import { useGlobalParams } from "@/app/context/api/GlobalParamsProvider";
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 @@ -16,21 +17,23 @@ 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 { data: allGlobalParamsVersions } = useGlobalParams();
Expand All @@ -43,7 +46,16 @@ export const UnbondWithdrawModal: React.FC<PreviewModalProps> = ({
return currentVersion;
};

const globalParams = getGlobalParamsForDelegation(delegationHeight);
const delegation = delegationsAPI.find(
(delegation) => delegation.stakingTxHashHex === txID,
);
if (!delegation) {
throw new Error("Delegation not found");
}

const globalParams = getGlobalParamsForDelegation(
delegation.stakingTx.startHeight,
);
const unbondingFeeSat = globalParams?.unbondingFeeSat || 0;
const unbondingTimeBlocks = globalParams?.unbondingTime || 0;

Expand Down