-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix: update required assets correctly for money account deposits #29806
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
Changes from 4 commits
e230d66
48052f4
fa6c62a
5150e0f
4aeddf6
c14d618
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,25 @@ | ||
| import { useCallback } from 'react'; | ||
| import { BigNumber } from 'bignumber.js'; | ||
| import { toHex } from '@metamask/controller-utils'; | ||
| import { | ||
| TransactionMeta, | ||
| TransactionType, | ||
| } from '@metamask/transaction-controller'; | ||
| import { Hex } from '@metamask/utils'; | ||
| import { useTransactionMetadataRequest } from '../transactions/useTransactionMetadataRequest'; | ||
| import { useUpdateTokenAmount } from '../transactions/useUpdateTokenAmount'; | ||
| import { updateAtomicBatchData } from '../../../../../util/transaction-controller'; | ||
| import { | ||
| updateAtomicBatchData, | ||
| updateTransaction, | ||
| } from '../../../../../util/transaction-controller'; | ||
| import { | ||
| updateMoneyAccountDepositTokenAmount, | ||
| updateMoneyAccountWithdrawTokenAmount, | ||
| } from '../../../../UI/Money/utils/moneyAccountTransactions'; | ||
| import { UpdateTransactionPayAmountCall } from '../../types/transactions'; | ||
| import { hasTransactionType } from '../../utils/transaction'; | ||
| import Logger from '../../../../../util/Logger'; | ||
| import { useTransactionPayRequiredTokens } from './useTransactionPayData'; | ||
|
|
||
| type MoneyAccountAmountUpdater = ( | ||
| transactionMeta: TransactionMeta, | ||
|
|
@@ -22,6 +29,7 @@ type MoneyAccountAmountUpdater = ( | |
| export function useUpdateTransactionPayAmount() { | ||
| const transactionMeta = useTransactionMetadataRequest(); | ||
| const { updateTokenAmount } = useUpdateTokenAmount(); | ||
| const requiredTokens = useTransactionPayRequiredTokens(); | ||
|
|
||
| const applyMoneyAccountAmountUpdates = useCallback( | ||
| async ( | ||
|
|
@@ -72,6 +80,11 @@ export function useUpdateTransactionPayAmount() { | |
| TransactionType.moneyAccountDeposit, | ||
| ]) | ||
| ) { | ||
| syncMoneyAccountDepositRequiredAssets( | ||
| transactionMeta, | ||
| amountHuman, | ||
| requiredTokens?.[0]?.decimals, | ||
| ); | ||
| await applyMoneyAccountAmountUpdates( | ||
| amountHuman, | ||
| updateMoneyAccountDepositTokenAmount, | ||
|
|
@@ -95,8 +108,45 @@ export function useUpdateTransactionPayAmount() { | |
|
|
||
| updateTokenAmount(amountHuman); | ||
| }, | ||
| [transactionMeta, applyMoneyAccountAmountUpdates, updateTokenAmount], | ||
| [ | ||
| transactionMeta, | ||
| applyMoneyAccountAmountUpdates, | ||
| updateTokenAmount, | ||
| requiredTokens, | ||
| ], | ||
| ); | ||
|
|
||
| return { updateTransactionPayAmount }; | ||
| } | ||
|
|
||
| function syncMoneyAccountDepositRequiredAssets( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, this is a more general |
||
| transactionMeta: TransactionMeta, | ||
| amountHuman: string, | ||
| decimals: number | undefined, | ||
| ): void { | ||
| const existing = transactionMeta.requiredAssets; | ||
| if (!existing?.length || decimals === undefined) return; | ||
|
|
||
| try { | ||
| const amount = toHex( | ||
| new BigNumber(amountHuman) | ||
| .shiftedBy(decimals) | ||
| .decimalPlaces(0, BigNumber.ROUND_UP) | ||
| .toFixed(0), | ||
| ) as Hex; | ||
| if (existing[0].amount === amount) return; | ||
|
|
||
| updateTransaction( | ||
| { | ||
| ...transactionMeta, | ||
| requiredAssets: [{ ...existing[0], amount }, ...existing.slice(1)], | ||
| }, | ||
| 'Money Account deposit: sync requiredAssets amount', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, |
||
| ); | ||
| } catch (error) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, not sure how this would throw as it's a synchronous state update. |
||
| Logger.error( | ||
| error as Error, | ||
| 'Failed to sync Money Account deposit requiredAssets amount', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, also can be generic. |
||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,11 @@ export function getTokenAddress( | |
| return nestedCall.to; | ||
| } | ||
|
|
||
| const requiredAssetAddress = transactionMeta?.requiredAssets?.[0]?.address; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, if we have |
||
| if (requiredAssetAddress) { | ||
| return requiredAssetAddress; | ||
| } | ||
|
jpuri marked this conversation as resolved.
|
||
|
|
||
| return transactionMeta?.txParams?.to as Hex; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, this isn't an exported function, so should be something like
updates requires assets amount?