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
1 changed files with 28 additions and 38 deletions

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 4 bytes hex method signature starting with 0x; received value: "${rawFourBytes}"` `rawFourBytes must contain a bytes hex string 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) {
return;
}
if (fourBytes === "") {
return;
}
const loadSig = async () => {
const entry = await fetch4Bytes(assetsURLPrefix, fourBytes);
fullCache.set(fourBytes, entry);
setEntry(entry);
};
loadSig();
}, [fourBytes, assetsURLPrefix]);
if (rawFourBytes === "0x") {
return simpleTransfer;
}
if (assetsURLPrefix === undefined) { if (assetsURLPrefix === undefined) {
return undefined; return undefined;
} }
if (key === "0x") {
// Try to resolve 4bytes name return undefined;
if (entry === null || entry === undefined) {
return entry;
} }
// Simulates LRU // Handle simple transfers with invalid selector like tx:
// TODO: implement LRU purging // 0x8bcbdcc1589b5c34c1e55909c8269a411f0267a4fed59a73dd4348cc71addbb9,
fullCache.delete(fourBytes); // which contains 0x00 as data
fullCache.set(fourBytes, entry); if (key.length !== 10) {
return entry; return undefined;
}
return fetch4Bytes(assetsURLPrefix, key.slice(2));
};
const { data, error } = useSWR<FourBytesEntry | null | undefined>(
rawFourBytes,
fourBytesFetcher
);
if (rawFourBytes === "0x") {
return simpleTransfer;
}
if (error) {
return undefined;
}
return data;
}; };
export const useTransactionDescription = ( export const useTransactionDescription = (