Skip to content

Commit

Permalink
fix(wallet-dashboard): sending max amount doesn't send the correct am…
Browse files Browse the repository at this point in the history
…ount (#4252)

* fix(wallet-dashboard): sending max amount does not send the correct amount

* fix(wallet-dashboard): linter

* fix(wallet-dashboard): clean debris and improve the amount field in form data values

* feat(wallet): clean debris

---------

Co-authored-by: Marc Espin <[email protected]>
  • Loading branch information
cpl121 and marc2332 authored Dec 4, 2024
1 parent 2b70724 commit fdd1b8a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CoinBalance } from '@iota/iota-sdk/client';
import { useSendCoinTransaction, useNotifications } from '@/hooks';
import { useSignAndExecuteTransaction } from '@iota/dapp-kit';
import { NotificationType } from '@/stores/notificationStore';
import { useGetAllCoins } from '@iota/core';
import { CoinFormat, useFormatCoin, useGetAllCoins } from '@iota/core';
import { Dialog, DialogBody, DialogContent, DialogPosition, Header } from '@iota/apps-ui-kit';
import { FormDataValues } from './interfaces';
import { INITIAL_VALUES } from './constants';
Expand All @@ -33,19 +33,22 @@ function SendTokenDialogBody({
const [step, setStep] = useState<FormStep>(FormStep.EnterValues);
const [selectedCoin, setSelectedCoin] = useState<CoinBalance>(coin);
const [formData, setFormData] = useState<FormDataValues>(INITIAL_VALUES);
const [fullAmount] = useFormatCoin(formData.amount, selectedCoin.coinType, CoinFormat.FULL);
const { addNotification } = useNotifications();

const { data: coinsData } = useGetAllCoins(selectedCoin.coinType, activeAddress);

const { mutateAsync: signAndExecuteTransaction, isPending } = useSignAndExecuteTransaction();
const isPayAllIota =
selectedCoin.totalBalance === formData.amount && selectedCoin.coinType === IOTA_TYPE_ARG;

const { data: transaction } = useSendCoinTransaction(
coinsData || [],
selectedCoin?.coinType,
selectedCoin.coinType,
activeAddress,
formData.to,
formData.formattedAmount,
selectedCoin?.totalBalance === formData.amount && selectedCoin.coinType === IOTA_TYPE_ARG,
formData.amount,
isPayAllIota,
);

function handleTransfer() {
Expand Down Expand Up @@ -74,6 +77,11 @@ function SendTokenDialogBody({
}

function onBack(): void {
// The amount is formatted when submitting the enterValuesForm, so it is necessary to return to the previous value when backing out
setFormData({
...formData,
amount: fullAmount,
});
setStep(FormStep.EnterValues);
}

Expand Down Expand Up @@ -103,10 +111,7 @@ function SendTokenDialogBody({
senderAddress={activeAddress}
isPending={isPending}
coinType={selectedCoin.coinType}
isPayAllIota={
selectedCoin.totalBalance === formData.amount &&
selectedCoin.coinType === IOTA_TYPE_ARG
}
isPayAllIota={isPayAllIota}
/>
)}
</DialogBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ import { FormDataValues } from '../interfaces';
export const INITIAL_VALUES: FormDataValues = {
to: '',
amount: '',
formattedAmount: '',
gasBudgetEst: '',
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
export interface FormDataValues {
amount: string;
formattedAmount: string;
to: string;
gasBudgetEst: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,21 @@ function FormInputs({
coins,
queryResult,
}: FormInputsProps): React.JSX.Element {
const isPayAllIota =
parseAmount(values.amount, coinDecimals) === coinBalance && coinType === IOTA_TYPE_ARG;
const formattedAmount = parseAmount(values.amount, coinDecimals);
const isPayAllIota = formattedAmount === coinBalance && coinType === IOTA_TYPE_ARG;

const hasEnoughBalance =
isPayAllIota ||
iotaBalance >
BigInt(values.gasBudgetEst ?? '0') +
parseAmount(coinType === IOTA_TYPE_ARG ? values.amount : '0', coinDecimals);
(coinType === IOTA_TYPE_ARG ? formattedAmount : 0n);

async function onMaxTokenButtonClick() {
await setFieldValue('amount', formattedTokenBalance);
}

const isMaxActionDisabled =
parseAmount(values.amount, coinDecimals) === coinBalance ||
queryResult.isPending ||
!coinBalance;
formattedAmount === coinBalance || queryResult.isPending || !coinBalance;

return (
<div className="flex h-full w-full flex-col">
Expand Down Expand Up @@ -199,19 +197,10 @@ export function EnterValuesFormView({
}

async function handleFormSubmit({ to, amount, gasBudgetEst }: FormDataValues) {
if (!coins || !iotaCoins) return;
const coinsIDs = [...coins]
.sort((a, b) => Number(b.balance) - Number(a.balance))
.map(({ coinObjectId }) => coinObjectId);

const formattedAmount = parseAmount(amount, coinDecimals).toString();

const data = {
to,
amount,
formattedAmount,
coins,
coinIds: coinsIDs,
amount: formattedAmount,
gasBudgetEst,
};
setFormData(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ButtonType,
} from '@iota/apps-ui-kit';
import { formatAddress, IOTA_TYPE_ARG } from '@iota/iota-sdk/utils';
import { CoinIcon, ImageIconSize, useFormatCoin, ExplorerLinkType } from '@iota/core';
import { CoinIcon, ImageIconSize, useFormatCoin, ExplorerLinkType, CoinFormat } from '@iota/core';
import { Loader } from '@iota/ui-icons';
import { ExplorerLink } from '@/components';

Expand All @@ -32,15 +32,16 @@ interface ReviewValuesFormProps {
}

export function ReviewValuesFormView({
formData: { amount, to, formattedAmount, gasBudgetEst },
formData: { amount, to, gasBudgetEst },
senderAddress,
isPending,
executeTransfer,
coinType,
isPayAllIota,
}: ReviewValuesFormProps): JSX.Element {
const [formatAmount, symbol] = useFormatCoin(formattedAmount, coinType);
const [roundedAmount, symbol] = useFormatCoin(amount, coinType, CoinFormat.ROUNDED);
const [gasEstimated, gasSymbol] = useFormatCoin(gasBudgetEst, IOTA_TYPE_ARG);

return (
<div className="flex h-full flex-col">
<div className="flex h-full w-full flex-col gap-md">
Expand All @@ -58,7 +59,7 @@ export function ReviewValuesFormView({
/>
</CardImage>
<CardBody
title={`${isPayAllIota ? '~' : ''}${formatAmount} ${symbol}`}
title={`${isPayAllIota ? '~' : ''}${roundedAmount} ${symbol}`}
subtitle="Amount"
/>
<CardAction type={CardActionType.SupportingText} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export type FormValues = typeof INITIAL_VALUES;
export type SubmitProps = {
to: string;
amount: string;
coinIds: string[];
coins: CoinStruct[];
gasBudgetEst: string;
};
Expand Down Expand Up @@ -100,16 +99,12 @@ export function SendTokenForm({
const formattedTokenBalance = tokenBalance.replace(/,/g, '');

async function handleFormSubmit({ to, amount, gasBudgetEst }: FormValues) {
if (!coins || !iotaCoins) return;
const coinsIDs = [...coins]
.sort((a, b) => Number(b.balance) - Number(a.balance))
.map(({ coinObjectId }) => coinObjectId);
if (!coins) return;

const data = {
to,
amount,
coins,
coinIds: coinsIDs,
gasBudgetEst,
};
onSubmit(data);
Expand Down

0 comments on commit fdd1b8a

Please sign in to comment.