From ca4b8d958aa71dce73e18caf3595453747ec22cf Mon Sep 17 00:00:00 2001 From: thibautbremand Date: Fri, 24 Nov 2023 13:42:31 +0100 Subject: [PATCH] Refactor custom fees logic --- .../constants/src/network/network.constant.ts | 9 +++ .../BaseTransaction/BaseTransaction.tsx | 70 ++++++++++++++++--- .../TransactionDetails/TransactionDetails.tsx | 5 +- .../ConfirmPayment/ConfirmPayment.tsx | 22 +++++- 4 files changed, 94 insertions(+), 12 deletions(-) diff --git a/packages/constants/src/network/network.constant.ts b/packages/constants/src/network/network.constant.ts index f8036ed04..3e91f69e8 100644 --- a/packages/constants/src/network/network.constant.ts +++ b/packages/constants/src/network/network.constant.ts @@ -1,3 +1,5 @@ +import { xrpToDrops } from 'xrpl'; + export enum Chain { XRPL = 'XRPL' } @@ -101,3 +103,10 @@ export function getNetwork(chain: Chain, network: Network): NetworkNode { throw new Error(`Network ${network} is not valid for chain ${chain}`); } + +export const getMaxFeeInDrops = (chain: Chain): number => { + switch (chain) { + default: + return Number(xrpToDrops(1)); + } +}; diff --git a/packages/extension/src/components/organisms/BaseTransaction/BaseTransaction.tsx b/packages/extension/src/components/organisms/BaseTransaction/BaseTransaction.tsx index 44d3d98f4..eee32b593 100644 --- a/packages/extension/src/components/organisms/BaseTransaction/BaseTransaction.tsx +++ b/packages/extension/src/components/organisms/BaseTransaction/BaseTransaction.tsx @@ -1,7 +1,9 @@ -import { FC } from 'react'; +import { FC, useState } from 'react'; import ErrorIcon from '@mui/icons-material/Error'; import { IconButton, Paper, Tooltip, Typography } from '@mui/material'; +import MuiInput from '@mui/material/Input'; +import { dropsToXrp, xrpToDrops } from 'xrpl'; import { CreateNFTOfferFlags, @@ -10,10 +12,12 @@ import { MintNFTFlags, PaymentFlags, SetAccountFlags, - TrustSetFlags + TrustSetFlags, + getMaxFeeInDrops } from '@gemwallet/constants'; import { ERROR_RED } from '../../../constants'; +import { useNetwork } from '../../../contexts'; import { formatAmount, formatFlags, formatToken } from '../../../utils'; import { TileLoader } from '../../atoms'; @@ -39,6 +43,7 @@ type FeeProps = { errorFees: string | undefined; estimatedFees: string; isBulk?: boolean; + onFeeChange?: (newFee: number) => void; useLegacy?: boolean; }; @@ -82,7 +87,34 @@ export const BaseTransaction: FC = ({ ); -export const Fee: FC = ({ errorFees, estimatedFees, fee, isBulk, useLegacy = true }) => { +export const Fee: FC = ({ + errorFees, + estimatedFees, + fee, + isBulk, + onFeeChange, + useLegacy = true +}) => { + const { chainName } = useNetwork(); + const [isEditing, setIsEditing] = useState(false); + + const handleFeeClick = () => { + if (onFeeChange !== undefined) { + setIsEditing(true); + } + }; + + const handleFeeChange = (event: React.ChangeEvent) => { + const newFee = Number(event.target.value); + if (onFeeChange) { + onFeeChange(Number(xrpToDrops(newFee))); + } + }; + + const handleBlur = () => { + setIsEditing(false); + }; + if (useLegacy) { return ( @@ -134,16 +166,38 @@ export const Fee: FC = ({ errorFees, estimatedFees, fee, isBulk, useLe {isBulk ? 'Total network fees' : 'Network fees'} - {errorFees ? ( - + {isEditing ? ( + + ) : errorFees ? ( + {errorFees} ) : estimatedFees === DEFAULT_FEES ? ( - ) : fee ? ( - formatToken(fee, 'XRP (manual)', true) + ) : fee !== null ? ( + + {formatToken(fee, 'XRP (manual)', true)} + ) : ( - formatAmount(estimatedFees) + + {formatAmount(estimatedFees)} + )} diff --git a/packages/extension/src/components/organisms/TransactionDetails/TransactionDetails.tsx b/packages/extension/src/components/organisms/TransactionDetails/TransactionDetails.tsx index 066ce701c..38891fe33 100644 --- a/packages/extension/src/components/organisms/TransactionDetails/TransactionDetails.tsx +++ b/packages/extension/src/components/organisms/TransactionDetails/TransactionDetails.tsx @@ -12,6 +12,7 @@ interface TransactionDetailsProps { errorFees?: string; isConnectionFailed?: boolean; displayTransactionType?: boolean; + onFeeChange?: (newFee: number) => void; } export const TransactionDetails: FC = ({ @@ -19,7 +20,8 @@ export const TransactionDetails: FC = ({ errorFees, estimatedFees, isConnectionFailed, - displayTransactionType + displayTransactionType, + onFeeChange }) => { const [isTxExpanded, setIsTxExpanded] = useState(false); const [isRawTxExpanded, setIsRawTxExpanded] = useState(false); @@ -65,6 +67,7 @@ export const TransactionDetails: FC = ({ estimatedFees={estimatedFees} fee={txParam?.Fee ? Number(txParam?.Fee) : null} useLegacy={false} + onFeeChange={onFeeChange} /> } isExpanded={isFeeExpanded} diff --git a/packages/extension/src/components/pages/SendPayment/ConfirmPayment/ConfirmPayment.tsx b/packages/extension/src/components/pages/SendPayment/ConfirmPayment/ConfirmPayment.tsx index 27e94a522..0587835e4 100644 --- a/packages/extension/src/components/pages/SendPayment/ConfirmPayment/ConfirmPayment.tsx +++ b/packages/extension/src/components/pages/SendPayment/ConfirmPayment/ConfirmPayment.tsx @@ -106,9 +106,10 @@ export const ConfirmPayment: FC = ({ }, []); const handleConfirm = useCallback(() => { - const [currency, issuer] = token.split('-'); setTransaction(TransactionStatus.Pending); - sendPayment(buildPaymentFromProps({ value, currency, issuer, address, memos, destinationTag })) + // Amount and Destination will be present because if not, + // we won't be able to go to the confirm transaction state + sendPayment(params.transaction as Payment) .then(() => { setTransaction(TransactionStatus.Success); }) @@ -117,12 +118,26 @@ export const ConfirmPayment: FC = ({ setTransaction(TransactionStatus.Rejected); Sentry.captureException(e); }); - }, [address, buildPaymentFromProps, destinationTag, memos, sendPayment, token, value]); + }, [params.transaction, sendPayment]); const handleTransactionClick = useCallback(() => { navigate(HOME_PATH); }, [navigate]); + const handleFeeChange = useCallback( + (fee: number) => { + if (params.transaction) { + setParams({ + transaction: { + ...params.transaction, + Fee: fee.toString() + } + }); + } + }, + [params.transaction] + ); + if (transaction === TransactionStatus.Success || transaction === TransactionStatus.Pending) { return ( = ({ estimatedFees={estimatedFees} errorFees={errorFees} displayTransactionType={false} + onFeeChange={handleFeeChange} /> );