Skip to content

Commit 109f67b

Browse files
committed
fixed calculation spam
1 parent 40da2cc commit 109f67b

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

src/components/nft-dashboard/unconfirmed-transactions/components/ReplaceTransaction/ReplaceTransaction.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface ReplaceTransactionProps {
2121
const ReplaceTransaction: React.FC<ReplaceTransactionProps> = ({ onCancel, onReplace, transaction }) => {
2222
const { isDesktop, isTablet } = useResponsive();
2323
const { balanceData, isLoading: isBalanceLoading } = useBalanceData(); // Fetch balance data using the hook
24-
const { isAuthenticated, login, token } = useWalletAuth(); // Use wallet authentication
24+
const { isAuthenticated, login, token} = useWalletAuth(); // Use wallet authentication
2525

2626
const [isLoadingSize, setIsLoadingSize] = useState(false); //tx size fetching states
2727

@@ -34,7 +34,7 @@ const ReplaceTransaction: React.FC<ReplaceTransactionProps> = ({ onCancel, onRep
3434
const [newFee, setNewFee] = useState<number>(0); // State to store the new fee
3535
const [totalCost, setTotalCost] = useState<number>(parseInt(transaction.amount)); // State to store total cost
3636
// const [enableRBF, setEnableRBF] = useState(false); // State to store RBF status
37-
37+
const memoizedLogin = useCallback(login, []);
3838
const [result, setResult] = useState<{ isSuccess: boolean; message: string; txid: string }>({
3939
isSuccess: false,
4040
message: '',
@@ -48,6 +48,7 @@ const ReplaceTransaction: React.FC<ReplaceTransactionProps> = ({ onCancel, onRep
4848
setIsLoadingSize(true);
4949
try {
5050
if (!isAuthenticated) {
51+
console.log("not auth")
5152
await login(); // Ensure user is logged in
5253
}
5354
if (!token) {
@@ -88,7 +89,7 @@ const ReplaceTransaction: React.FC<ReplaceTransactionProps> = ({ onCancel, onRep
8889
};
8990

9091
fetchTransactionSize();
91-
}, [transaction.txid, transaction.amount, isAuthenticated, login, token]);
92+
}, [transaction.txid, transaction.amount, isAuthenticated, memoizedLogin, token]);
9293

9394
// Calculate the total transaction cost (Amount + Calculated Fee)
9495

@@ -97,11 +98,17 @@ const ReplaceTransaction: React.FC<ReplaceTransactionProps> = ({ onCancel, onRep
9798
setTotalCost(totalCost);
9899
}, [txSize, newFee, transaction.amount]);
99100

101+
useEffect(() => {
102+
if (totalCost <= 0 || (balanceData && totalCost > balanceData.latest_balance)) {
103+
setInvalidAmount(true);
104+
} else {
105+
setInvalidAmount(false);
106+
}
107+
}, [totalCost]);
108+
100109
useEffect(() => {
101110
setNewFee(newFeeRate * (txSize || 0)); // Update the new fee based on the fee rate and transaction size
102111
}, [newFeeRate, txSize]);
103-
// Check if the total cost exceeds the user's balance
104-
const isBalanceInsufficient = balanceData?.latest_balance !== undefined && totalCost > balanceData.latest_balance;
105112

106113
const handleFeeRateChange = useCallback((fee: number) => {
107114
setNewFeeRate(fee); // Update the new fee when it changes
@@ -214,7 +221,7 @@ const ReplaceTransaction: React.FC<ReplaceTransactionProps> = ({ onCancel, onRep
214221
<S.ContentWrapper>
215222
<S.FieldDisplay>
216223
<S.FieldLabel>Transaction ID</S.FieldLabel>
217-
<S.ValueWrapper isMobile={!isDesktop || !isTablet}>
224+
<S.ValueWrapper $isMobile={!isDesktop || !isTablet}>
218225
<S.FieldValue>{transaction.txid}
219226
{" "}
220227
<ClipboardCopy textToCopy={transaction.txid} />
@@ -223,7 +230,7 @@ const ReplaceTransaction: React.FC<ReplaceTransactionProps> = ({ onCancel, onRep
223230
</S.FieldDisplay>
224231
<S.FieldDisplay>
225232
<S.FieldLabel>Amount</S.FieldLabel>
226-
<S.ValueWrapper isMobile={!isDesktop || !isTablet}>
233+
<S.ValueWrapper $isMobile={!isDesktop || !isTablet}>
227234
<S.FieldValue>{transaction.amount}</S.FieldValue>
228235
</S.ValueWrapper>
229236
</S.FieldDisplay>
@@ -238,9 +245,9 @@ const ReplaceTransaction: React.FC<ReplaceTransactionProps> = ({ onCancel, onRep
238245
<TieredFees inValidAmount={inValidAmount} handleFeeChange={handleFeeRateChange} txSize={txSize} />
239246
<S.FieldDisplay>
240247
<S.FieldLabel>New Fee Rate</S.FieldLabel>
241-
<S.ValueWrapper isMobile={!isDesktop || !isTablet}>
248+
<S.ValueWrapper $isMobile={!isDesktop || !isTablet}>
242249
<S.FeeInput
243-
isMobile={!isTablet}
250+
$isMobile={!isTablet}
244251
type="number" // Use 'number' input type to allow numerical values
245252
value={newFeeRate} // Bind the input value to the newFee state
246253
onChange={(e) => {
@@ -257,27 +264,27 @@ const ReplaceTransaction: React.FC<ReplaceTransactionProps> = ({ onCancel, onRep
257264

258265
<S.FieldDisplay>
259266
<S.FieldLabel>New Fee</S.FieldLabel>
260-
<S.ValueWrapper isMobile={!isDesktop || !isTablet}>
267+
<S.ValueWrapper $isMobile={!isDesktop || !isTablet}>
261268
<S.FieldValue>{newFee}</S.FieldValue>
262269
</S.ValueWrapper>
263270
</S.FieldDisplay>
264271

265272
<S.FieldDisplay>
266273
<S.FieldLabel>Total</S.FieldLabel>
267-
<S.ValueWrapper isMobile={!isDesktop || !isTablet}>
274+
<S.ValueWrapper $isMobile={!isDesktop || !isTablet}>
268275
{/* Calculate total amount (Amount + Fee based on transaction size) */}
269276
<S.FieldValue>{totalCost}</S.FieldValue>
270277
</S.ValueWrapper>
271278
<S.BalanceInfo>{`Balance: ${balanceData ? balanceData.latest_balance : 0}`}</S.BalanceInfo>
272279
</S.FieldDisplay>
273280

274281
{/* Show error message if balance is insufficient */}
275-
{isBalanceInsufficient && <S.ErrorMessage>Insufficient balance to complete the transaction.</S.ErrorMessage>}
282+
{inValidAmount && <S.ErrorMessage>Insufficient balance to complete the transaction.</S.ErrorMessage>}
276283

277284
<S.ButtonRow>
278285
<S.Button onClick={onCancel}>Cancel</S.Button>
279286
{/* Disable replace button if total cost exceeds balance */}
280-
<S.Button onClick={handleReplace} disabled={isBalanceInsufficient}>
287+
<S.Button onClick={handleReplace} disabled={inValidAmount}>
281288
Replace
282289
</S.Button>
283290
</S.ButtonRow>

0 commit comments

Comments
 (0)