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) #202

Merged
merged 2 commits into from
Oct 9, 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",
jrwbabylonlab marked this conversation as resolved.
Show resolved Hide resolved
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
54 changes: 29 additions & 25 deletions src/app/components/Delegations/Delegations.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { networks } from "bitcoinjs-lib";
import { useEffect, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import InfiniteScroll from "react-infinite-scroll-component";
import { useLocalStorage } from "usehooks-ts";

Expand Down Expand Up @@ -104,16 +104,19 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
const { showError } = useError();
const { isApiNormal, isGeoBlocked } = useHealthCheck();
const [awaitingWalletResponse, setAwaitingWalletResponse] = useState(false);
const [selectedDelegationHeight, setSelectedDelegationHeight] = useState<
number | undefined
>();
const {
delegations = [],
fetchMoreDelegations,
hasMoreDelegations,
isLoading,
} = useDelegationState();

const delegation = useMemo(
() =>
delegationsAPI.find((delegation) => delegation.stakingTxHashHex === txID),
[delegationsAPI, txID],
);

const shouldShowPoints =
isApiNormal && !isGeoBlocked && shouldDisplayPoints();
// Local storage state for intermediate delegations (withdrawing, unbonding)
Expand Down Expand Up @@ -184,15 +187,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 @@ -221,23 +222,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 @@ -285,6 +283,21 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
});
}, [delegationsAPI, setIntermediateDelegationsLocalStorage]);

useEffect(() => {
if (modalOpen && !delegation) {
showError({
error: {
message: "Delegation not found",
errorState: ErrorState.SERVER_ERROR,
},
noCancel: false,
});
setModalOpen(false);
setTxID("");
setModalMode(undefined);
}
}, [modalOpen, delegation, showError]);

// combine delegations from the API and local storage, prioritizing API data
const combinedDelegationsData = delegationsAPI
? [...delegations, ...delegationsAPI]
Expand Down Expand Up @@ -343,19 +356,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 @@ -367,7 +370,7 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
</div>
</>
)}
{modalMode && txID && (
{modalMode && txID && delegation && (
<UnbondWithdrawModal
open={modalOpen}
onClose={() => setModalOpen(false)}
Expand All @@ -378,6 +381,7 @@ const DelegationsContent: React.FC<DelegationsContentProps> = ({
}}
mode={modalMode}
awaitingWalletResponse={awaitingWalletResponse}
delegation={delegation}
/>
)}
</div>
Expand Down
14 changes: 10 additions & 4 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 { useAppState } from "@/app/state";
import { useVersionByHeight } from "@/app/hooks/useVersions";
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 @@ -20,6 +21,7 @@ interface PreviewModalProps {
onProceed: () => void;
mode: MODE;
awaitingWalletResponse: boolean;
delegation: DelegationInterface;
}

export const UnbondWithdrawModal: React.FC<PreviewModalProps> = ({
Expand All @@ -28,12 +30,16 @@ export const UnbondWithdrawModal: React.FC<PreviewModalProps> = ({
onProceed,
mode,
awaitingWalletResponse,
delegation,
}) => {
const { coinName, networkName } = getNetworkConfig();
const { currentVersion: globalParams } = useAppState();

const unbondingFeeSat = globalParams?.unbondingFeeSat || 0;
const unbondingTimeBlocks = globalParams?.unbondingTime || 0;
const { currentVersion: delegationGlobalParams } = useVersionByHeight(
delegation.stakingTx.startHeight ?? 0,
);

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

const unbondTitle = "Unbond";

Expand Down
11 changes: 11 additions & 0 deletions src/app/hooks/useVersions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { useMemo } from "react";

import { getGlobalParams } from "@/app/api/getGlobalParams";
import { useAPIQuery } from "@/app/hooks/useApi";
import { getCurrentGlobalParamsVersion } from "@/utils/globalParams";

export const VERSIONS_KEY = "VERSIONS";

Expand All @@ -12,3 +15,11 @@ export function useVersions({ enabled = true }: { enabled?: boolean } = {}) {

return data;
}

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