Skip to content

Commit

Permalink
Merge pull request #2146 from blockscout/name-tag-in-tx-interpretation
Browse files Browse the repository at this point in the history
Display address name and tag in tx interpretation
  • Loading branch information
tom2drum authored Aug 12, 2024
2 parents 2081c83 + d3db2e9 commit 45b93c5
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 39 deletions.
26 changes: 21 additions & 5 deletions mocks/txs/tx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable max-len */
import type { Transaction } from 'types/api/transaction';

import * as addressMock from 'mocks/address/address';
import { publicTag, privateTag, watchlistName } from 'mocks/address/tag';
import * as tokenTransferMock from 'mocks/tokens/tokenTransfer';
import * as decodedInputDataMock from 'mocks/txs/decodedInputData';
Expand Down Expand Up @@ -47,7 +48,7 @@ export const base: Transaction = {
status: 'ok',
timestamp: '2022-10-10T14:34:30.000000Z',
to: {
hash: '0xd789a607CEac2f0E14867de4EB15b15C9FFB5859',
hash: addressMock.hash,
implementations: null,
is_contract: false,
is_verified: true,
Expand Down Expand Up @@ -110,7 +111,7 @@ export const withTokenTransfer: Transaction = {
...base,
hash: '0x62d597ebcf3e8d60096dd0363bc2f0f5e2df27ba1dacd696c51aa7c9409f3196',
to: {
hash: '0xd789a607CEac2f0E14867de4EB15b15C9FFB5859',
hash: addressMock.hash,
implementations: null,
is_contract: true,
is_verified: true,
Expand Down Expand Up @@ -166,7 +167,7 @@ export const withRawRevertReason: Transaction = {
raw: '4f6e6c79206368616972706572736f6e2063616e206769766520726967687420746f20766f74652e',
},
to: {
hash: '0xd789a607CEac2f0E14867de4EB15b15C9FFB5859',
hash: addressMock.hash,
implementations: null,
is_verified: true,
is_contract: true,
Expand Down Expand Up @@ -344,7 +345,7 @@ export const base2 = {
hash: '0x02d597ebcf3e8d60096dd0363bc2f0f5e2df27ba1dacd696c51aa7c9409f3193',
from: {
...base.from,
hash: '0xd789a607CEac2f0E14867de4EB15b15C9FFB5859',
hash: addressMock.hash,
},
};

Expand All @@ -353,7 +354,7 @@ export const base3 = {
hash: '0x12d597ebcf3e8d60096dd0363bc2f0f5e2df27ba1dacd696c51aa7c9409f3193',
from: {
...base.from,
hash: '0xd789a607CEac2f0E14867de4EB15b15C9FFB5859',
hash: addressMock.hash,
},
};

Expand All @@ -375,3 +376,18 @@ export const withBlob = {
tx_types: [ 'blob_transaction' as const ],
type: 3,
};

export const withRecipientName = {
...base,
to: addressMock.withName,
};

export const withRecipientEns = {
...base,
to: addressMock.withEns,
};

export const withRecipientNameTag = {
...withRecipientEns,
to: addressMock.withNameTag,
};
4 changes: 3 additions & 1 deletion mocks/txs/txInterpretation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { TxInterpretationResponse } from 'types/api/txInterpretation';

import { hash } from 'mocks/address/address';

export const txInterpretation: TxInterpretationResponse = {
data: {
summaries: [ {
Expand All @@ -25,7 +27,7 @@ export const txInterpretation: TxInterpretationResponse = {
to_address: {
type: 'address',
value: {
hash: '0x48c04ed5691981C42154C6167398f95e8f38a7fF',
hash: hash,
implementations: null,
is_contract: false,
is_verified: false,
Expand Down
15 changes: 6 additions & 9 deletions ui/shared/tx/interpretation/TxInterpretation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Skeleton, Tooltip, chakra } from '@chakra-ui/react';
import BigNumber from 'bignumber.js';
import React from 'react';

import type { AddressParam } from 'types/api/addressParams';
import type {
TxInterpretationSummary,
TxInterpretationVariable,
Expand All @@ -23,14 +24,14 @@ import { extractVariables, getStringChunks, fillStringVariables, checkSummary, N
type Props = {
summary?: TxInterpretationSummary;
isLoading?: boolean;
ensDomainNames?: Record<string, string>;
addressDataMap?: Record<string, AddressParam>;
className?: string;
}

type NonStringTxInterpretationVariable = Exclude<TxInterpretationVariable, TxInterpretationVariableString>

const TxInterpretationElementByType = (
{ variable, ensDomainNames }: { variable?: NonStringTxInterpretationVariable; ensDomainNames?: Record<string, string> },
{ variable, addressDataMap }: { variable?: NonStringTxInterpretationVariable; addressDataMap?: Record<string, AddressParam> },
) => {
const onAddressClick = React.useCallback(() => {
mixpanel.logEvent(mixpanel.EventTypes.TX_INTERPRETATION_INTERACTION, { Type: 'Address click' });
Expand All @@ -51,14 +52,10 @@ const TxInterpretationElementByType = (
const { type, value } = variable;
switch (type) {
case 'address': {
let address = value;
if (!address.ens_domain_name && ensDomainNames?.[address.hash]) {
address = { ...address, ens_domain_name: ensDomainNames[address.hash] };
}
return (
<chakra.span display="inline-block" verticalAlign="top" _notFirst={{ marginLeft: 1 }}>
<AddressEntity
address={ address }
address={ addressDataMap?.[value.hash] || value }
truncation="constant"
onClick={ onAddressClick }
whiteSpace="initial"
Expand Down Expand Up @@ -129,7 +126,7 @@ const TxInterpretationElementByType = (
}
};

const TxInterpretation = ({ summary, isLoading, ensDomainNames, className }: Props) => {
const TxInterpretation = ({ summary, isLoading, addressDataMap, className }: Props) => {
if (!summary) {
return null;
}
Expand Down Expand Up @@ -161,7 +158,7 @@ const TxInterpretation = ({ summary, isLoading, ensDomainNames, className }: Pro
(
<TxInterpretationElementByType
variable={ variables[variablesNames[index]] as NonStringTxInterpretationVariable }
ensDomainNames={ ensDomainNames }
addressDataMap={ addressDataMap }
/>
)
) }
Expand Down
33 changes: 17 additions & 16 deletions ui/tx/TxSubHeading.pw.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';

import type { AddressMetadataInfo, AddressMetadataTagApi } from 'types/api/addressMetadata';
import type { AddressParam } from 'types/api/addressParams';

import config from 'configs/app';
import { protocolTagWithMeta } from 'mocks/metadata/address';
Expand Down Expand Up @@ -65,22 +64,24 @@ test.describe('blockscout provider', () => {
await expect(component).toHaveScreenshot();
});

test('with interpretation and recipient ENS domain', async({ render, mockApiResponse }) => {
const txData = {
...txMock.base,
to: {
...txMock.base.to,
hash: (txInterpretation.data.summaries[0].summary_template_variables.to_address.value as AddressParam).hash,
ens_domain_name: 'duckduck.eth',
},
};
const txWithEnsQuery = {
data: txData,
isPlaceholderData: false,
isError: false,
} as TxQuery;
test('with interpretation and recipient name +@mobile', async({ render, mockApiResponse }) => {
const newTxQuery = { ...txQuery, data: txMock.withRecipientName } as TxQuery;
await mockApiResponse('tx_interpretation', txInterpretation, { pathParams: { hash } });
const component = await render(<TxSubHeading hash={ hash } hasTag={ false } txQuery={ newTxQuery }/>);
await expect(component).toHaveScreenshot();
});

test('with interpretation and recipient ENS domain +@mobile', async({ render, mockApiResponse }) => {
const newTxQuery = { ...txQuery, data: txMock.withRecipientEns } as TxQuery;
await mockApiResponse('tx_interpretation', txInterpretation, { pathParams: { hash } });
const component = await render(<TxSubHeading hash={ hash } hasTag={ false } txQuery={ newTxQuery }/>);
await expect(component).toHaveScreenshot();
});

test('with interpretation and recipient name tag +@mobile', async({ render, mockApiResponse }) => {
const newTxQuery = { ...txQuery, data: txMock.withRecipientNameTag } as TxQuery;
await mockApiResponse('tx_interpretation', txInterpretation, { pathParams: { hash } });
const component = await render(<TxSubHeading hash={ hash } hasTag={ false } txQuery={ txWithEnsQuery }/>);
const component = await render(<TxSubHeading hash={ hash } hasTag={ false } txQuery={ newTxQuery }/>);
await expect(component).toHaveScreenshot();
});

Expand Down
18 changes: 10 additions & 8 deletions ui/tx/TxSubHeading.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Box, Flex, Link } from '@chakra-ui/react';
import React from 'react';

import type { AddressParam } from 'types/api/addressParams';

import config from 'configs/app';
import useApiQuery from 'lib/api/useApiQuery';
import { NOVES_TRANSLATE } from 'stubs/noves/NovesTranslate';
Expand Down Expand Up @@ -59,12 +61,12 @@ const TxSubHeading = ({ hash, hasTag, txQuery }: Props) => {
(hasNovesInterpretation && novesInterpretationQuery.data && !novesInterpretationQuery.isPlaceholderData) ||
(hasInternalInterpretation && !txInterpretationQuery.isPlaceholderData);

const ensDomainNames: Record<string, string> = {};
[ txQuery.data?.from, txQuery.data?.to ].forEach(data => {
if (data?.hash && data?.ens_domain_name) {
ensDomainNames[data.hash] = data.ens_domain_name;
}
});
const addressDataMap: Record<string, AddressParam> = {};
[ txQuery.data?.from, txQuery.data?.to ]
.filter((data): data is AddressParam => Boolean(data && data.hash))
.forEach(data => {
addressDataMap[data.hash] = data;
});

const content = (() => {
if (hasNovesInterpretation && novesInterpretationQuery.data) {
Expand All @@ -73,7 +75,7 @@ const TxSubHeading = ({ hash, hasTag, txQuery }: Props) => {
<TxInterpretation
summary={ novesSummary }
isLoading={ novesInterpretationQuery.isPlaceholderData || txQuery.isPlaceholderData }
ensDomainNames={ ensDomainNames }
addressDataMap={ addressDataMap }
fontSize="lg"
mr={{ base: 0, lg: 6 }}
/>
Expand All @@ -84,7 +86,7 @@ const TxSubHeading = ({ hash, hasTag, txQuery }: Props) => {
<TxInterpretation
summary={ txInterpretationQuery.data?.data.summaries[0] }
isLoading={ txInterpretationQuery.isPlaceholderData || txQuery.isPlaceholderData }
ensDomainNames={ ensDomainNames }
addressDataMap={ addressDataMap }
fontSize="lg"
mr={ hasViewAllInterpretationsLink ? 3 : 0 }
/>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 45b93c5

Please sign in to comment.