Skip to content

Commit

Permalink
fix(send): max amount when switching payment method
Browse files Browse the repository at this point in the history
  • Loading branch information
pwltr committed Nov 4, 2024
1 parent 13028ad commit e707b3f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 49 deletions.
14 changes: 13 additions & 1 deletion e2e/send.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,23 @@ d('Send', () => {
const unified4 = encode(onchainAddress, { lightning: lnInvoice4 });

await enterAddress(unified4);

// max amount (lightning)
await element(by.id('AvailableAmount')).tap();
await element(by.id('ContinueAmount')).tap();
await expect(element(by.text('18 900'))).toBeVisible();
await element(by.id('AssetButton-switch')).tap();
await element(by.id('NavigationBack')).atIndex(0).tap();

// max amount (onchain)
await element(by.id('AssetButton-switch')).tap();
await element(by.id('AvailableAmount')).tap();
await element(by.id('ContinueAmount')).tap();
await expect(element(by.text('78 838'))).toBeVisible();
await element(by.id('NavigationBack')).atIndex(0).tap();

await element(
by.id('NRemove').withAncestor(by.id('SendAmountNumberPad')),
).multiTap(5);
await element(by.id('N1').withAncestor(by.id('SendAmountNumberPad'))).tap();
await element(
by.id('N0').withAncestor(by.id('SendAmountNumberPad')),
Expand Down
33 changes: 7 additions & 26 deletions src/screens/Wallets/Send/Amount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,19 @@ const Amount = ({ navigation }: SendScreenProps<'Amount'>): ReactElement => {
};

const onMaxAmount = useCallback((): void => {
if (!usesLightning) {
const result = getNumberPadText(availableAmount, denomination, unit);
setText(result);
} else {
const result = getNumberPadText(availableAmount, denomination, unit);
setText(result);

if (usesLightning) {
showToast({
type: 'warning',
title: t('send_max_spending.title'),
description: t('send_max_spending.description'),
});
}

sendMax({ selectedWallet, selectedNetwork });
}, [
availableAmount,
selectedNetwork,
selectedWallet,
usesLightning,
denomination,
unit,
t,
]);
sendMax();
}, [availableAmount, usesLightning, denomination, unit, t]);

const onError = (): void => {
setError(true);
Expand All @@ -189,8 +181,6 @@ const Amount = ({ navigation }: SendScreenProps<'Amount'>): ReactElement => {
const onContinue = useCallback((): void => {
const result = updateSendAmount({
amount,
selectedWallet,
selectedNetwork,
transaction,
});
if (result.isErr()) {
Expand Down Expand Up @@ -219,16 +209,7 @@ const Amount = ({ navigation }: SendScreenProps<'Amount'>): ReactElement => {
}
navigation.navigate('ReviewAndSend');
}
}, [
amount,
selectedWallet,
selectedNetwork,
coinSelectAuto,
transaction,
usesLightning,
navigation,
t,
]);
}, [amount, coinSelectAuto, transaction, usesLightning, navigation, t]);

const isValid = useMemo(() => {
if (amount === 0) {
Expand Down
37 changes: 15 additions & 22 deletions src/utils/wallet/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import {
addBoostedTransaction,
updateSendTransaction,
} from '../../store/actions/wallet';
import { dispatch, getFeesStore, getSettingsStore } from '../../store/helpers';
import {
dispatch,
getFeesStore,
getSettingsStore,
getUiStore,
} from '../../store/helpers';
import { removeActivityItem } from '../../store/slices/activity';
import { initialFeesState } from '../../store/slices/fees';
import {
Expand Down Expand Up @@ -418,7 +423,7 @@ export const getEstimatedRoutingFee = (amount: number): number => {
/**
* Calculates the max amount able to send for onchain/lightning
* @param {ISendTransaction} [transaction]
* @param {number} [index]
* @param {'onchain' | 'lightning'} [method
*/
export const getMaxSendAmount = ({
transaction,
Expand Down Expand Up @@ -483,25 +488,20 @@ export const getMaxSendAmount = ({
export const sendMax = async ({
address,
index = 0,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: {
address?: string;
index?: number;
selectedWallet?: TWalletName;
selectedNetwork?: EAvailableNetwork;
} = {}): Promise<Result<string>> => {
const { paymentMethod } = getUiStore();

try {
const tx = getOnChainWalletTransaction();
const transaction = tx.data;

// TODO: Re-work lightning transaction invoices once beignet migration is complete.
// Handle max toggle for lightning invoice
if (transaction.lightningInvoice) {
const { spendingBalance } = getBalance({
selectedWallet,
selectedNetwork,
});
if (paymentMethod === 'lightning') {
const { spendingBalance } = getBalance();

const fee = getEstimatedRoutingFee(spendingBalance);
const amount = spendingBalance - fee;
Expand Down Expand Up @@ -587,20 +587,16 @@ export const adjustFee = ({
* Updates the amount to send for the currently selected output.
* @param {number} amount
* @param {ISendTransaction} [transaction]
* @param {EAvailableNetwork} [selectedNetwork]
* @param {TWalletName} [selectedWallet]
*/
export const updateSendAmount = ({
amount,
transaction,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: {
amount: number;
transaction?: ISendTransaction;
selectedNetwork?: EAvailableNetwork;
selectedWallet?: TWalletName;
}): Result<string> => {
const { paymentMethod } = getUiStore();

if (!transaction) {
transaction = getOnchainTransactionData();
}
Expand All @@ -609,12 +605,9 @@ export const updateSendAmount = ({
const currentOutput = transaction.outputs[0];
let max = false;

if (transaction.lightningInvoice) {
if (paymentMethod === 'lightning') {
// lightning transaction
const { spendingBalance } = getBalance({
selectedWallet,
selectedNetwork,
});
const { spendingBalance } = getBalance();

if (amount > spendingBalance) {
return err(i18n.t('wallet:send_amount_error_balance'));
Expand Down

0 comments on commit e707b3f

Please sign in to comment.