1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 03:12:42 +02:00

TransactionUtils: update determineTransactionType method / contractInteraction support (#18652)

* TransactionUtils: show contractInteraction if value
... exists when the transaction comes from a contract

* TransactionUtils: add contractInteraction test

* clean: restore empty line
This commit is contained in:
Ariella Vu 2023-04-21 10:30:44 -03:00 committed by GitHub
parent dc69ff017b
commit 9ade5d0eb1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 7 deletions

View File

@ -176,6 +176,8 @@ export async function determineTransactionType(txParams, query) {
contractCode = resultCode;
if (isContractAddress) {
const hasValue = txParams.value && txParams.value !== '0x0';
const tokenMethodName = [
TransactionType.tokenMethodApprove,
TransactionType.tokenMethodSetApprovalForAll,
@ -184,13 +186,8 @@ export async function determineTransactionType(txParams, query) {
TransactionType.tokenMethodSafeTransferFrom,
].find((methodName) => isEqualCaseInsensitive(methodName, name));
const isSendWithApprove =
txParams.value &&
txParams.value !== '0x0' &&
tokenMethodName === TransactionType.tokenMethodApprove;
result =
data && tokenMethodName && !isSendWithApprove
data && tokenMethodName && !hasValue
? tokenMethodName
: TransactionType.contractInteraction;
} else {

View File

@ -135,7 +135,7 @@ describe('Transaction.utils', function () {
});
});
it('should return a token transfer type when the recipient is a contract and data is for the respective method call', async function () {
it('should return a token transfer type when the recipient is a contract, there is no value passed, and data is for the respective method call', async function () {
const _providerResultStub = {
// 1 gwei
eth_gasPrice: '0x0de0b6b3a7640000',
@ -159,6 +159,48 @@ describe('Transaction.utils', function () {
});
});
it(
'should NOT return a token transfer type and instead return contract interaction' +
' when the recipient is a contract, the data matches the respective method call, but there is a value passed',
async function () {
const _providerResultStub = {
// 1 gwei
eth_gasPrice: '0x0de0b6b3a7640000',
// by default, all accounts are external accounts (not contracts)
eth_getCode: '0xab',
};
const _provider = createTestProviderTools({
scaffold: _providerResultStub,
}).provider;
const resultWithEmptyValue = await determineTransactionType(
{
value: '0x0',
to: '0x9e673399f795D01116e9A8B2dD2F156705131ee9',
data: '0xa9059cbb0000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C970000000000000000000000000000000000000000000000000000000000000000a',
},
new EthQuery(_provider),
);
expect(resultWithEmptyValue).toMatchObject({
type: TransactionType.tokenMethodTransfer,
getCodeResponse: '0xab',
});
const resultWithValue = await determineTransactionType(
{
value: '0x12345',
to: '0x9e673399f795D01116e9A8B2dD2F156705131ee9',
data: '0xa9059cbb0000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C970000000000000000000000000000000000000000000000000000000000000000a',
},
new EthQuery(_provider),
);
expect(resultWithValue).toMatchObject({
type: TransactionType.contractInteraction,
getCodeResponse: '0xab',
});
},
);
it('should NOT return a token transfer type when the recipient is not a contract but the data matches the respective method call', async function () {
const _providerResultStub = {
// 1 gwei