mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01: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:
parent
dc69ff017b
commit
9ade5d0eb1
@ -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 {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user