Commit e5af9612 authored by tom's avatar tom

decoding for topics

parent f6caf9b7
export default function hexToAddress(hex: string) {
return hex.slice(0, 2) + hex.slice(26);
}
...@@ -22,7 +22,6 @@ const TxLogItem = ({ address, index, topics, data, decoded }: Props) => { ...@@ -22,7 +22,6 @@ const TxLogItem = ({ address, index, topics, data, decoded }: Props) => {
const borderColor = useColorModeValue('blackAlpha.200', 'whiteAlpha.200'); const borderColor = useColorModeValue('blackAlpha.200', 'whiteAlpha.200');
const dataBgColor = useColorModeValue('blackAlpha.50', 'whiteAlpha.50'); const dataBgColor = useColorModeValue('blackAlpha.50', 'whiteAlpha.50');
const decodedTopics = decoded?.parameters.filter(({ indexed }) => indexed);
return ( return (
<Grid <Grid
...@@ -69,8 +68,6 @@ const TxLogItem = ({ address, index, topics, data, decoded }: Props) => { ...@@ -69,8 +68,6 @@ const TxLogItem = ({ address, index, topics, data, decoded }: Props) => {
<TxLogTopic <TxLogTopic
key={ index } key={ index }
hex={ item } hex={ item }
decoded={ decodedTopics?.[index - 1]?.value }
type={ decodedTopics?.[index - 1]?.type }
index={ index } index={ index }
/> />
)) } )) }
......
import { Flex, Button, Select, Box } from '@chakra-ui/react'; import { Flex, Button, Select, Box } from '@chakra-ui/react';
import capitalize from 'lodash/capitalize';
import React from 'react'; import React from 'react';
import hexToAddress from 'lib/hexToAddress';
import hexToUtf8 from 'lib/hexToUtf8';
import AddressLink from 'ui/shared/address/AddressLink'; import AddressLink from 'ui/shared/address/AddressLink';
import CopyToClipboard from 'ui/shared/CopyToClipboard';
import HashStringShortenDynamic from 'ui/shared/HashStringShortenDynamic'; import HashStringShortenDynamic from 'ui/shared/HashStringShortenDynamic';
interface Props { interface Props {
hex: string; hex: string;
decoded?: string;
type?: string;
index: number; index: number;
} }
type DataType = 'Hex' | 'Dec'; type DataType = 'hex' | 'text' | 'address' | 'number';
const OPTIONS: Array<DataType> = [ 'Hex', 'Dec' ];
const TxLogTopic = ({ hex, index, decoded, type }: Props) => { const VALUE_CONVERTERS: Record<DataType, (hex: string) => string> = {
const [ selectedDataType, setSelectedDataType ] = React.useState<DataType>('Hex'); hex: (hex) => hex,
text: hexToUtf8,
address: hexToAddress,
number: (hex) => BigInt(hex).toString(),
};
const OPTIONS: Array<DataType> = [ 'hex', 'address', 'text', 'number' ];
const TxLogTopic = ({ hex, index }: Props) => {
const [ selectedDataType, setSelectedDataType ] = React.useState<DataType>('hex');
const handleSelectChange = React.useCallback((event: React.ChangeEvent<HTMLSelectElement>) => { const handleSelectChange = React.useCallback((event: React.ChangeEvent<HTMLSelectElement>) => {
setSelectedDataType(event.target.value as DataType); setSelectedDataType(event.target.value as DataType);
}, []); }, []);
const value = VALUE_CONVERTERS[selectedDataType.toLowerCase() as Lowercase<DataType>](hex);
const content = (() => { const content = (() => {
if (selectedDataType === 'Dec' && type === 'address' && decoded) { switch (selectedDataType) {
return ( case 'hex':
<AddressLink hash={ decoded }/> case 'number':
); case 'text': {
} return (
<>
<Box overflow="hidden" whiteSpace="nowrap">
<HashStringShortenDynamic hash={ value }/>
</Box>
<CopyToClipboard text={ value }/>
</>
);
}
const value = selectedDataType === 'Dec' && decoded ? decoded : hex; case 'address': {
return ( return (
<Box overflow="hidden" whiteSpace="nowrap"> <AddressLink hash={ hexToAddress(hex) }/>
<HashStringShortenDynamic hash={ value }/> );
</Box> }
); }
})(); })();
return ( return (
...@@ -41,18 +60,18 @@ const TxLogTopic = ({ hex, index, decoded, type }: Props) => { ...@@ -41,18 +60,18 @@ const TxLogTopic = ({ hex, index, decoded, type }: Props) => {
<Button variant="outline" colorScheme="gray" isActive size="xs" fontWeight={ 400 } mr={ 3 } w={ 6 }> <Button variant="outline" colorScheme="gray" isActive size="xs" fontWeight={ 400 } mr={ 3 } w={ 6 }>
{ index } { index }
</Button> </Button>
{ decoded && ( { index !== 0 && (
<Select <Select
size="sm" size="sm"
borderRadius="base" borderRadius="base"
value={ selectedDataType } value={ selectedDataType }
onChange={ handleSelectChange } onChange={ handleSelectChange }
focusBorderColor="none" focusBorderColor="none"
w="75px"
mr={ 3 } mr={ 3 }
flexShrink={ 0 } flexShrink={ 0 }
w="auto"
> >
{ OPTIONS.map((option) => <option key={ option } value={ option }>{ option }</option>) } { OPTIONS.map((option) => <option key={ option } value={ option }>{ capitalize(option) }</option>) }
</Select> </Select>
) } ) }
{ content } { content }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment