-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
218 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
ui/pages/confirmations/components/confirm/info/hooks/useMaxValueRefresher.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { TransactionType } from '@metamask/transaction-controller'; | ||
import { updateEditableParams } from '../../../../../../store/actions'; | ||
import { useConfirmContext } from '../../../../context/confirm'; | ||
import { useFeeCalculations } from './useFeeCalculations'; | ||
import { useMaxValueRefresher } from './useMaxValueRefresher'; | ||
|
||
jest.mock('react-redux', () => ({ | ||
useDispatch: jest.fn(), | ||
useSelector: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../../../../store/actions', () => ({ | ||
updateEditableParams: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../../context/confirm', () => ({ | ||
useConfirmContext: jest.fn(), | ||
})); | ||
|
||
jest.mock('../hooks/useFeeCalculations', () => ({ | ||
useFeeCalculations: jest.fn(), | ||
})); | ||
|
||
describe('useMaxValueRefresher', () => { | ||
const dispatchMock = jest.fn(); | ||
const simpleSendTransactionMetaMock = { | ||
id: '1', | ||
type: TransactionType.simpleSend, | ||
}; | ||
|
||
beforeEach(() => { | ||
(useDispatch as jest.Mock).mockReturnValue(dispatchMock); | ||
(useConfirmContext as jest.Mock).mockReturnValue({ | ||
currentConfirmation: simpleSendTransactionMetaMock, | ||
}); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('updates transaction value in max amount mode for simpleSend', () => { | ||
const balance = '0x111'; | ||
const preciseNativeFeeInHex = '0x001'; | ||
const newValue = '0x110'; | ||
|
||
(useSelector as jest.Mock) | ||
.mockReturnValueOnce(balance) | ||
.mockReturnValueOnce(true); | ||
|
||
(useFeeCalculations as jest.Mock).mockReturnValue({ | ||
preciseNativeFeeInHex, | ||
}); | ||
|
||
renderHook(() => useMaxValueRefresher()); | ||
|
||
expect(dispatchMock).toHaveBeenCalledWith( | ||
updateEditableParams(simpleSendTransactionMetaMock.id, { | ||
value: newValue, | ||
}), | ||
); | ||
}); | ||
|
||
it('does not update transaction value if not in max amount mode', () => { | ||
(useSelector as jest.Mock) | ||
.mockReturnValueOnce('0x111') | ||
.mockReturnValueOnce(false); | ||
|
||
renderHook(() => useMaxValueRefresher()); | ||
|
||
expect(dispatchMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not update transaction value if transaction type is not simpleSend', () => { | ||
const transactionMeta = { id: '1', type: 'otherType' }; | ||
(useConfirmContext as jest.Mock).mockReturnValue({ | ||
currentConfirmation: transactionMeta, | ||
}); | ||
|
||
renderHook(() => useMaxValueRefresher()); | ||
|
||
expect(dispatchMock).not.toHaveBeenCalled(); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
ui/pages/confirmations/components/confirm/info/hooks/useMaxValueRefresher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useEffect } from 'react'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import { | ||
TransactionType, | ||
type TransactionMeta, | ||
} from '@metamask/transaction-controller'; | ||
import { add0x } from '@metamask/utils'; | ||
|
||
import { | ||
getSelectedAccountCachedBalance, | ||
selectMaxValueModeForTransaction, | ||
} from '../../../../../../selectors'; | ||
import { subtractHexes } from '../../../../../../../shared/modules/conversion.utils'; | ||
import { updateEditableParams } from '../../../../../../store/actions'; | ||
import { useConfirmContext } from '../../../../context/confirm'; | ||
import { useFeeCalculations } from './useFeeCalculations'; | ||
|
||
// This hook is used to refresh the max value of the transaction | ||
// when the user is in max amount mode only for the transaction type simpleSend | ||
// It subtracts the native fee from the balance and updates the value of the transaction | ||
export const useMaxValueRefresher = () => { | ||
const { currentConfirmation: transactionMeta } = | ||
useConfirmContext<TransactionMeta>(); | ||
const dispatch = useDispatch(); | ||
const { preciseNativeFeeInHex } = useFeeCalculations(transactionMeta); | ||
const balance = useSelector(getSelectedAccountCachedBalance); | ||
const isMaxAmountMode = useSelector((state) => | ||
selectMaxValueModeForTransaction(state, transactionMeta?.id), | ||
); | ||
|
||
useEffect(() => { | ||
if ( | ||
!isMaxAmountMode || | ||
transactionMeta.type !== TransactionType.simpleSend | ||
) { | ||
return; | ||
} | ||
|
||
const newValue = subtractHexes(balance, preciseNativeFeeInHex); | ||
const newValueInHex = add0x(newValue); | ||
|
||
dispatch( | ||
updateEditableParams(transactionMeta.id, { value: newValueInHex }), | ||
); | ||
}, [isMaxAmountMode, balance, preciseNativeFeeInHex]); | ||
}; |
4 changes: 3 additions & 1 deletion
4
ui/pages/confirmations/components/confirm/info/native-transfer/native-transfer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.