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

fix(HW-603): Incorrect amount on Preview sending for 1000 wISLM #2033

Merged
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
14 changes: 4 additions & 10 deletions src/components/transaction-sum/transaction-sum.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export type TransactionSumProps = {
to: string;
from: string;
contact: Contact | null;
onAmount: (amount: Balance) => void;
onPressPreview: (amount: Balance) => void;
onContact: () => void;
onToken: () => void;
onNetworkPress: () => void;
Expand All @@ -52,7 +52,7 @@ export const TransactionSum = observer(
balance,
fee,
contact,
onAmount,
onPressPreview,
onContact,
onToken,
onNetworkPress,
Expand Down Expand Up @@ -105,14 +105,8 @@ export const TransactionSum = observer(
);

const onDone = useCallback(() => {
onAmount(
new Balance(
parseFloat(amounts.amount),
undefined,
token.symbol ?? undefined,
),
);
}, [amounts, onAmount]);
onPressPreview(amounts.amountBalance);
}, [amounts, onPressPreview]);

const onPressMax = useCallback(() => {
vibrate(HapticEffects.impactLight);
Expand Down
4 changes: 3 additions & 1 deletion src/components/ui/sum-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ export const SumBlock = observer(
});
}

return new Balance(Number(value)).toFiat();
return new Balance(Number(value)).toFiat({
fixed: 'auto',
});
}, [token?.decimals, token?.symbol, value]);

return (
Expand Down
22 changes: 17 additions & 5 deletions src/hooks/use-sum-amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ export const useSumAmount = (
numericality: {
notValid: 'Invalid number',
greaterThanOrEqualTo: minAmountRef.current.toFloat(),
lessThanOrEqualTo: maxAmountRef.current.toFloat(),
notGreaterThan: getText(I18N.sumAmountTooLow, {
amount: maxAmountRef.current.toBalanceString('auto'),
notGreaterThanOrEqualTo: getText(I18N.sumAmountTooLow, {
amount: minAmountRef.current.toBalanceString('auto'),
}),
lessThanOrEqualTo: maxAmountRef.current.toFloat(),
notLessThanOrEqualTo: getText(I18N.sumAmountNotEnough, {
symbol: maxAmountRef.current.getSymbol(),
}),
Expand Down Expand Up @@ -73,6 +73,7 @@ export const useSumAmount = (
!error,
maxAmount: maxAmount,
amount: amountText,
amountBalance: amount,
error,
setMaxAmount(value = Balance.Empty) {
maxAmountRef.current = value;
Expand Down Expand Up @@ -104,7 +105,7 @@ export const useSumAmount = (
changed: _changed,
}));
},
setAmount(text: string, precision = app.provider.decimals) {
setAmount(text: string, precision?: number) {
if (text.match(/^[0-9].*/)) {
let i = 0;
const textFormatted = text
Expand All @@ -118,7 +119,18 @@ export const useSumAmount = (
.replace(/\D&[^.]/g, '')
.replace(/^0[0-9]/gm, '0');

const newAmount = new Balance(+textFormatted, precision);
const decimals =
precision ??
maxAmountRef.current?.getPrecission?.() ??
minAmountRef.current?.getPrecission?.() ??
app.provider.decimals;

const denom =
maxAmountRef.current?.getSymbol?.() ??
minAmountRef.current?.getSymbol?.() ??
app.provider.denom;

const newAmount = new Balance(+textFormatted, decimals, denom);
if (typeof onChange === 'function') {
onChange(newAmount, textFormatted);
}
Expand Down
16 changes: 10 additions & 6 deletions src/screens/HomeStack/TransactionStack/transaction-sum.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const TransactionSumScreen = memo(() => {
});
}
} catch (err) {
Logger.log('err onAmount', err);
Logger.log('tx sum err getFee', err);
return null;
}
},
Expand All @@ -87,8 +87,8 @@ export const TransactionSumScreen = memo(() => {
};
}, [event, onAddress]);

const onAmount = useCallback(
async (amount: Balance) => {
const onPressPreview = useCallback(
async (amount: Balance, repeated = false) => {
setLoading(true);
const estimate = await getFee(amount);

Expand All @@ -104,9 +104,13 @@ export const TransactionSumScreen = memo(() => {
showModal(ModalType.error, {
title: getText(I18N.feeCalculatingRpcErrorTitle),
description: getText(I18N.feeCalculatingRpcErrorDescription),
close: getText(I18N.feeCalculatingRpcErrorClose),
close: getText(
repeated ? I18N.cancel : I18N.feeCalculatingRpcErrorClose,
),
onClose: () => {
onAmount(amount);
if (!repeated) {
onPressPreview(amount, true);
}
},
});
}
Expand Down Expand Up @@ -156,7 +160,7 @@ export const TransactionSumScreen = memo(() => {
fee={fee}
to={to}
from={route.params.from}
onAmount={onAmount}
onPressPreview={onPressPreview}
onContact={onContact}
onToken={onToken}
onNetworkPress={onNetworkPress}
Expand Down
Loading