Eliminate simple transfer singleton entry

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

View File

@ -1,15 +1,17 @@
import React from "react"; import React from "react";
import { rawInputTo4Bytes, use4Bytes } from "../use4Bytes"; import { extract4Bytes, use4Bytes } from "../use4Bytes";
type MethodNameProps = { type MethodNameProps = {
data: string; data: string;
}; };
const MethodName: React.FC<MethodNameProps> = ({ data }) => { const MethodName: React.FC<MethodNameProps> = ({ data }) => {
const rawFourBytes = rawInputTo4Bytes(data); const rawFourBytes = extract4Bytes(data);
const fourBytesEntry = use4Bytes(rawFourBytes); const fourBytesEntry = use4Bytes(rawFourBytes);
const methodName = fourBytesEntry?.name ?? rawFourBytes; const isSimpleTransfer = data === "0x";
const isSimpleTransfer = rawFourBytes === "0x"; const methodName = isSimpleTransfer
? "transfer"
: fourBytesEntry?.name ?? rawFourBytes ?? "-";
const methodTitle = isSimpleTransfer const methodTitle = isSimpleTransfer
? "ETH Transfer" ? "ETH Transfer"
: methodName === rawFourBytes : methodName === rawFourBytes

View File

@ -16,11 +16,13 @@ export type FourBytesEntry = {
export type FourBytesMap = Record<string, FourBytesEntry | null | undefined>; export type FourBytesMap = Record<string, FourBytesEntry | null | undefined>;
const simpleTransfer: FourBytesEntry = { /**
name: "transfer", * Given a hex input data; extract the method selector
signature: undefined, *
}; * @param rawInput Raw tx input including the "0x"
* @returns the first 4 bytes, including the "0x" or null if the input
* contains an invalid selector, e.g., txs with 0x00 data
*/
export const extract4Bytes = (rawInput: string): string | null => { export const extract4Bytes = (rawInput: string): string | null => {
if (rawInput.length < 10) { if (rawInput.length < 10) {
return null; return null;
@ -60,6 +62,7 @@ const fetch4Bytes = async (
} }
}; };
// TODO: migrate to swr and merge with use4Bytes
export const useBatch4Bytes = ( export const useBatch4Bytes = (
rawFourByteSigs: string[] | undefined rawFourByteSigs: string[] | undefined
): FourBytesMap => { ): FourBytesMap => {
@ -97,9 +100,9 @@ export const useBatch4Bytes = (
* @param rawFourBytes an hex string containing the 4bytes signature in the "0xXXXXXXXX" format. * @param rawFourBytes an hex string containing the 4bytes signature in the "0xXXXXXXXX" format.
*/ */
export const use4Bytes = ( export const use4Bytes = (
rawFourBytes: string rawFourBytes: string | null
): FourBytesEntry | null | undefined => { ): FourBytesEntry | null | undefined => {
if (!rawFourBytes.startsWith("0x")) { if (rawFourBytes !== null && !rawFourBytes.startsWith("0x")) {
throw new Error( throw new Error(
`rawFourBytes must contain a bytes hex string starting with 0x; received value: "${rawFourBytes}"` `rawFourBytes must contain a bytes hex string starting with 0x; received value: "${rawFourBytes}"`
); );
@ -108,12 +111,12 @@ export const use4Bytes = (
const { config } = useContext(RuntimeContext); const { config } = useContext(RuntimeContext);
const assetsURLPrefix = config?.assetsURLPrefix; const assetsURLPrefix = config?.assetsURLPrefix;
const fourBytesFetcher = (key: string) => { const fourBytesFetcher = (key: string | null) => {
// TODO: throw error? // TODO: throw error?
if (assetsURLPrefix === undefined) { if (assetsURLPrefix === undefined) {
return undefined; return undefined;
} }
if (key === "0x") { if (key === null || key === "0x") {
return undefined; return undefined;
} }
@ -131,9 +134,6 @@ export const use4Bytes = (
rawFourBytes, rawFourBytes,
fourBytesFetcher fourBytesFetcher
); );
if (rawFourBytes === "0x") {
return simpleTransfer;
}
if (error) { if (error) {
return undefined; return undefined;
} }