-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide alert if token transfer recipient is internal account
- Loading branch information
1 parent
fde9fa1
commit b349f1f
Showing
5 changed files
with
113 additions
and
41 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
ui/pages/confirmations/components/confirm/info/hooks/useTransferRecipient.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,55 @@ | ||
import { TransactionMeta } from '@metamask/transaction-controller'; | ||
import { useTransferRecipient } from './useTransferRecipient'; | ||
import { renderHookWithConfirmContextProvider } from '../../../../../../../test/lib/confirmations/render-helpers'; | ||
import { getMockConfirmStateForTransaction } from '../../../../../../../test/data/confirmations/helper'; | ||
import { genUnapprovedContractInteractionConfirmation } from '../../../../../../../test/data/confirmations/contract-interaction'; | ||
import { genUnapprovedTokenTransferConfirmation } from '../../../../../../../test/data/confirmations/token-transfer'; | ||
|
||
const ADDRESS_MOCK = '0x2e0D7E8c45221FcA00d74a3609A0f7097035d09B'; | ||
|
||
const TRANSACTION_METADATA_MOCK = | ||
genUnapprovedContractInteractionConfirmation() as TransactionMeta; | ||
|
||
function runHook(transaction?: TransactionMeta) { | ||
const state = transaction | ||
? getMockConfirmStateForTransaction(transaction) | ||
: {}; | ||
|
||
const { result } = renderHookWithConfirmContextProvider( | ||
useTransferRecipient, | ||
state, | ||
); | ||
|
||
return result.current as string | undefined; | ||
} | ||
|
||
describe('useTransferRecipient', () => { | ||
it('returns undefined if no transaction', () => { | ||
expect(runHook()).toBeUndefined(); | ||
}); | ||
|
||
it('returns transaction to address if no token data', () => { | ||
expect( | ||
runHook({ | ||
...TRANSACTION_METADATA_MOCK, | ||
txParams: { | ||
...TRANSACTION_METADATA_MOCK.txParams, | ||
to: ADDRESS_MOCK, | ||
}, | ||
}), | ||
).toBe(ADDRESS_MOCK); | ||
}); | ||
|
||
it('returns transaction data to address if token transfer', () => { | ||
expect( | ||
runHook({ | ||
...TRANSACTION_METADATA_MOCK, | ||
txParams: { | ||
...TRANSACTION_METADATA_MOCK.txParams, | ||
to: '0x123', | ||
data: genUnapprovedTokenTransferConfirmation().txParams.data, | ||
}, | ||
}), | ||
).toBe(ADDRESS_MOCK); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
ui/pages/confirmations/components/confirm/info/hooks/useTransferRecipient.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,14 @@ | ||
import { TransactionMeta } from '@metamask/transaction-controller'; | ||
import { useTokenTransactionData } from './useTokenTransactionData'; | ||
import { useConfirmContext } from '../../../../context/confirm'; | ||
|
||
export function useTransferRecipient() { | ||
const { currentConfirmation: transactionMetadata } = | ||
useConfirmContext<TransactionMeta>(); | ||
|
||
const transactionData = useTokenTransactionData(); | ||
const transactionTo = transactionMetadata?.txParams?.to; | ||
const transferTo = transactionData?.args?._to as string | undefined; | ||
|
||
return transferTo || transactionTo; | ||
} |
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