Rework use4Bytes hook to use swr; fix handling of 0x00 data

This commit is contained in:
Willian Mitsuda 2021-12-16 18:35:49 -03:00
parent d590521683
commit d5ea295eec

View File

@ -4,9 +4,10 @@ import {
Interface, Interface,
TransactionDescription, TransactionDescription,
} from "@ethersproject/abi"; } from "@ethersproject/abi";
import { BigNumberish } from "@ethersproject/bignumber";
import useSWR from "swr";
import { RuntimeContext } from "./useRuntime"; import { RuntimeContext } from "./useRuntime";
import { fourBytesURL } from "./url"; import { fourBytesURL } from "./url";
import { BigNumberish } from "@ethersproject/bignumber";
export type FourBytesEntry = { export type FourBytesEntry = {
name: string; name: string;
@ -20,8 +21,6 @@ const simpleTransfer: FourBytesEntry = {
signature: undefined, signature: undefined,
}; };
const fullCache = new Map<string, FourBytesEntry | null>();
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;
@ -100,54 +99,45 @@ export const useBatch4Bytes = (
export const use4Bytes = ( export const use4Bytes = (
rawFourBytes: string rawFourBytes: string
): FourBytesEntry | null | undefined => { ): FourBytesEntry | null | undefined => {
if (rawFourBytes !== "0x") { if (!rawFourBytes.startsWith("0x")) {
if (rawFourBytes.length !== 10 || !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 4 bytes hex method signature starting with 0x; received value: "${rawFourBytes}"` );
);
}
} }
const runtime = useContext(RuntimeContext); const { config } = useContext(RuntimeContext);
const assetsURLPrefix = runtime.config?.assetsURLPrefix; const assetsURLPrefix = config?.assetsURLPrefix;
const fourBytes = rawFourBytes.slice(2); const fourBytesFetcher = (key: string) => {
const [entry, setEntry] = useState<FourBytesEntry | null | undefined>( // TODO: throw error?
fullCache.get(fourBytes)
);
useEffect(() => {
if (assetsURLPrefix === undefined) { if (assetsURLPrefix === undefined) {
return; return undefined;
} }
if (fourBytes === "") { if (key === "0x") {
return; return undefined;
} }
const loadSig = async () => { // Handle simple transfers with invalid selector like tx:
const entry = await fetch4Bytes(assetsURLPrefix, fourBytes); // 0x8bcbdcc1589b5c34c1e55909c8269a411f0267a4fed59a73dd4348cc71addbb9,
fullCache.set(fourBytes, entry); // which contains 0x00 as data
setEntry(entry); if (key.length !== 10) {
}; return undefined;
loadSig(); }
}, [fourBytes, assetsURLPrefix]);
return fetch4Bytes(assetsURLPrefix, key.slice(2));
};
const { data, error } = useSWR<FourBytesEntry | null | undefined>(
rawFourBytes,
fourBytesFetcher
);
if (rawFourBytes === "0x") { if (rawFourBytes === "0x") {
return simpleTransfer; return simpleTransfer;
} }
if (assetsURLPrefix === undefined) { if (error) {
return undefined; return undefined;
} }
return data;
// Try to resolve 4bytes name
if (entry === null || entry === undefined) {
return entry;
}
// Simulates LRU
// TODO: implement LRU purging
fullCache.delete(fourBytes);
fullCache.set(fourBytes, entry);
return entry;
}; };
export const useTransactionDescription = ( export const useTransactionDescription = (