Extract method formatter

This commit is contained in:
Willian Mitsuda 2021-12-16 19:12:55 -03:00
parent 478dd84710
commit e9f32e3530
2 changed files with 18 additions and 12 deletions

View File

@ -1,22 +1,12 @@
import React from "react"; import React from "react";
import { extract4Bytes, use4Bytes } from "../use4Bytes"; import { useMethodSelector } from "../use4Bytes";
type MethodNameProps = { type MethodNameProps = {
data: string; data: string;
}; };
const MethodName: React.FC<MethodNameProps> = ({ data }) => { const MethodName: React.FC<MethodNameProps> = ({ data }) => {
const rawFourBytes = extract4Bytes(data); const [isSimpleTransfer, methodName, methodTitle] = useMethodSelector(data);
const fourBytesEntry = use4Bytes(rawFourBytes);
const isSimpleTransfer = data === "0x";
const methodName = isSimpleTransfer
? "transfer"
: fourBytesEntry?.name ?? rawFourBytes ?? "-";
const methodTitle = isSimpleTransfer
? "ETH Transfer"
: methodName === rawFourBytes
? methodName
: `${methodName} [${rawFourBytes}]`;
return ( return (
<div <div

View File

@ -140,6 +140,22 @@ export const use4Bytes = (
return data; return data;
}; };
export const useMethodSelector = (data: string): [boolean, string, string] => {
const rawFourBytes = extract4Bytes(data);
const fourBytesEntry = use4Bytes(rawFourBytes);
const isSimpleTransfer = data === "0x";
const methodName = isSimpleTransfer
? "transfer"
: fourBytesEntry?.name ?? rawFourBytes ?? "-";
const methodTitle = isSimpleTransfer
? "ETH Transfer"
: methodName === rawFourBytes
? methodName
: `${methodName} [${rawFourBytes}]`;
return [isSimpleTransfer, methodName, methodTitle];
};
export const useTransactionDescription = ( export const useTransactionDescription = (
fourBytesEntry: FourBytesEntry | null | undefined, fourBytesEntry: FourBytesEntry | null | undefined,
data: string | undefined, data: string | undefined,