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 { extract4Bytes, use4Bytes } from "../use4Bytes";
import { useMethodSelector } from "../use4Bytes";
type MethodNameProps = {
data: string;
};
const MethodName: React.FC<MethodNameProps> = ({ data }) => {
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}]`;
const [isSimpleTransfer, methodName, methodTitle] = useMethodSelector(data);
return (
<div

View File

@ -140,6 +140,22 @@ export const use4Bytes = (
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 = (
fourBytesEntry: FourBytesEntry | null | undefined,
data: string | undefined,