From 850f7f8c71a3d843e3babcaf0fabdcaf23ebfbaa Mon Sep 17 00:00:00 2001 From: jeremy-babylonchain Date: Thu, 3 Oct 2024 19:12:36 +0700 Subject: [PATCH 1/3] use correct height for unbonding model --- .../components/Delegations/Delegations.tsx | 33 +++++-------------- .../components/Modals/UnbondWithdrawModal.tsx | 12 +++++-- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/src/app/components/Delegations/Delegations.tsx b/src/app/components/Delegations/Delegations.tsx index 59f2bec0..d7bbecf3 100644 --- a/src/app/components/Delegations/Delegations.tsx +++ b/src/app/components/Delegations/Delegations.tsx @@ -100,9 +100,6 @@ const DelegationsContent: React.FC = ({ const { showError } = useError(); const { isApiNormal, isGeoBlocked } = useHealthCheck(); const [awaitingWalletResponse, setAwaitingWalletResponse] = useState(false); - const [selectedDelegationHeight, setSelectedDelegationHeight] = useState< - number | undefined - >(); const shouldShowPoints = isApiNormal && !isGeoBlocked && shouldDisplayPoints(); @@ -174,15 +171,13 @@ const DelegationsContent: React.FC = ({ 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); } }; @@ -211,23 +206,20 @@ const DelegationsContent: React.FC = ({ 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(() => { @@ -333,19 +325,9 @@ const DelegationsContent: React.FC = ({ 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} @@ -357,9 +339,8 @@ const DelegationsContent: React.FC = ({ )} - {modalMode && txID && selectedDelegationHeight !== undefined && ( + {modalMode && txID && ( setModalOpen(false)} onProceed={() => { @@ -369,6 +350,8 @@ const DelegationsContent: React.FC = ({ }} mode={modalMode} awaitingWalletResponse={awaitingWalletResponse} + delegationsAPI={delegationsAPI} + txID={txID} /> )} diff --git a/src/app/components/Modals/UnbondWithdrawModal.tsx b/src/app/components/Modals/UnbondWithdrawModal.tsx index d77cfc64..0aa4dcd4 100644 --- a/src/app/components/Modals/UnbondWithdrawModal.tsx +++ b/src/app/components/Modals/UnbondWithdrawModal.tsx @@ -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"; @@ -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 = ({ - delegationHeight, open, onClose, onProceed, mode, awaitingWalletResponse, + delegationsAPI, + txID, }) => { const { coinName, networkName } = getNetworkConfig(); const { data: allGlobalParamsVersions } = useGlobalParams(); @@ -43,6 +46,11 @@ export const UnbondWithdrawModal: React.FC = ({ return currentVersion; }; + const delegation = delegationsAPI.find( + (delegation) => delegation.stakingTxHashHex === txID, + ); + const delegationHeight = delegation?.stakingTx.startHeight || 0; + const globalParams = getGlobalParamsForDelegation(delegationHeight); const unbondingFeeSat = globalParams?.unbondingFeeSat || 0; const unbondingTimeBlocks = globalParams?.unbondingTime || 0; From d82e211b3f861e701a48ed8ecb351f72253eb6d7 Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Fri, 4 Oct 2024 10:24:33 +1000 Subject: [PATCH 2/3] chore: update to 0.3.5 --- package-lock.json | 4 ++-- package.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index afb67f82..e6bebe45 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "simple-staking", - "version": "0.3.4", + "version": "0.3.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "simple-staking", - "version": "0.3.4", + "version": "0.3.5", "dependencies": { "@babylonlabs-io/btc-staking-ts": "0.3.0", "@bitcoin-js/tiny-secp256k1-asmjs": "2.2.3", diff --git a/package.json b/package.json index f744a64e..2823a52e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simple-staking", - "version": "0.3.4", + "version": "0.3.5", "private": true, "scripts": { "dev": "next dev", @@ -91,4 +91,4 @@ "ts-node": "^10.9.2", "typescript": "^5" } -} +} \ No newline at end of file From e21c1a8d844f75a9e73445b29227c7c1a27ecb3a Mon Sep 17 00:00:00 2001 From: wjrjerome Date: Fri, 4 Oct 2024 10:29:10 +1000 Subject: [PATCH 3/3] fix: should throw if no delegtion api found --- package.json | 2 +- src/app/components/Modals/UnbondWithdrawModal.tsx | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 2823a52e..c78586e2 100644 --- a/package.json +++ b/package.json @@ -91,4 +91,4 @@ "ts-node": "^10.9.2", "typescript": "^5" } -} \ No newline at end of file +} diff --git a/src/app/components/Modals/UnbondWithdrawModal.tsx b/src/app/components/Modals/UnbondWithdrawModal.tsx index 0aa4dcd4..34e1ff07 100644 --- a/src/app/components/Modals/UnbondWithdrawModal.tsx +++ b/src/app/components/Modals/UnbondWithdrawModal.tsx @@ -49,9 +49,13 @@ export const UnbondWithdrawModal: React.FC = ({ const delegation = delegationsAPI.find( (delegation) => delegation.stakingTxHashHex === txID, ); - const delegationHeight = delegation?.stakingTx.startHeight || 0; + if (!delegation) { + throw new Error("Delegation not found"); + } - const globalParams = getGlobalParamsForDelegation(delegationHeight); + const globalParams = getGlobalParamsForDelegation( + delegation.stakingTx.startHeight, + ); const unbondingFeeSat = globalParams?.unbondingFeeSat || 0; const unbondingTimeBlocks = globalParams?.unbondingTime || 0;