Remove fourBytesMap usage; replace everything by swr

This commit is contained in:
Willian Mitsuda 2021-12-18 21:13:35 -03:00
parent d67cd52ceb
commit a934282ab0
5 changed files with 19 additions and 113 deletions

View File

@ -3,8 +3,7 @@ import ContentFrame from "../ContentFrame";
import TransactionAddress from "../components/TransactionAddress"; import TransactionAddress from "../components/TransactionAddress";
import TraceItem from "./TraceItem"; import TraceItem from "./TraceItem";
import { TransactionData } from "../types"; import { TransactionData } from "../types";
import { useBatch4Bytes } from "../use4Bytes"; import { useTraceTransaction } from "../useErigonHooks";
import { useTraceTransaction, useUniqueSignatures } from "../useErigonHooks";
import { RuntimeContext } from "../useRuntime"; import { RuntimeContext } from "../useRuntime";
type TraceProps = { type TraceProps = {
@ -14,8 +13,6 @@ type TraceProps = {
const Trace: React.FC<TraceProps> = ({ txData }) => { const Trace: React.FC<TraceProps> = ({ txData }) => {
const { provider } = useContext(RuntimeContext); const { provider } = useContext(RuntimeContext);
const traces = useTraceTransaction(provider, txData.transactionHash); const traces = useTraceTransaction(provider, txData.transactionHash);
const uniqueSignatures = useUniqueSignatures(traces);
const sigMap = useBatch4Bytes(uniqueSignatures);
return ( return (
<ContentFrame tabs> <ContentFrame tabs>
@ -27,12 +24,7 @@ const Trace: React.FC<TraceProps> = ({ txData }) => {
</div> </div>
<div className="ml-5 space-y-3 self-stretch"> <div className="ml-5 space-y-3 self-stretch">
{traces.map((t, i, a) => ( {traces.map((t, i, a) => (
<TraceItem <TraceItem key={i} t={t} last={i === a.length - 1} />
key={i}
t={t}
last={i === a.length - 1}
fourBytesMap={sigMap}
/>
))} ))}
</div> </div>
</> </>

View File

@ -9,18 +9,17 @@ import ExpanderSwitch from "../components/ExpanderSwitch";
import { TraceEntry } from "../useErigonHooks"; import { TraceEntry } from "../useErigonHooks";
import { import {
extract4Bytes, extract4Bytes,
FourBytesEntry, use4Bytes,
useTransactionDescription, useTransactionDescription,
} from "../use4Bytes"; } from "../use4Bytes";
type TraceInputProps = { type TraceInputProps = {
t: TraceEntry; t: TraceEntry;
fourBytesMap: Record<string, FourBytesEntry | null | undefined>;
}; };
const TraceInput: React.FC<TraceInputProps> = ({ t, fourBytesMap }) => { const TraceInput: React.FC<TraceInputProps> = ({ t }) => {
const raw4Bytes = extract4Bytes(t.input); const raw4Bytes = extract4Bytes(t.input);
const fourBytes = raw4Bytes !== null ? fourBytesMap[raw4Bytes] : null; const fourBytes = use4Bytes(raw4Bytes);
const sigText = const sigText =
raw4Bytes === null ? "<fallback>" : fourBytes?.name ?? raw4Bytes; raw4Bytes === null ? "<fallback>" : fourBytes?.name ?? raw4Bytes;
const hasParams = t.input.length > 10; const hasParams = t.input.length > 10;

View File

@ -3,17 +3,15 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPlusSquare } from "@fortawesome/free-regular-svg-icons/faPlusSquare"; import { faPlusSquare } from "@fortawesome/free-regular-svg-icons/faPlusSquare";
import { faMinusSquare } from "@fortawesome/free-regular-svg-icons/faMinusSquare"; import { faMinusSquare } from "@fortawesome/free-regular-svg-icons/faMinusSquare";
import { Switch } from "@headlessui/react"; import { Switch } from "@headlessui/react";
import { FourBytesEntry } from "../use4Bytes";
import { TraceGroup } from "../useErigonHooks"; import { TraceGroup } from "../useErigonHooks";
import TraceInput from "./TraceInput"; import TraceInput from "./TraceInput";
type TraceItemProps = { type TraceItemProps = {
t: TraceGroup; t: TraceGroup;
last: boolean; last: boolean;
fourBytesMap: Record<string, FourBytesEntry | null | undefined>;
}; };
const TraceItem: React.FC<TraceItemProps> = ({ t, last, fourBytesMap }) => { const TraceItem: React.FC<TraceItemProps> = ({ t, last }) => {
const [expanded, setExpanded] = useState<boolean>(true); const [expanded, setExpanded] = useState<boolean>(true);
return ( return (
@ -35,7 +33,7 @@ const TraceItem: React.FC<TraceItemProps> = ({ t, last, fourBytesMap }) => {
/> />
</Switch> </Switch>
)} )}
<TraceInput t={t} fourBytesMap={fourBytesMap} /> <TraceInput t={t} />
</div> </div>
{t.children && ( {t.children && (
<div <div
@ -43,7 +41,7 @@ const TraceItem: React.FC<TraceItemProps> = ({ t, last, fourBytesMap }) => {
expanded ? "" : "hidden" expanded ? "" : "hidden"
}`} }`}
> >
<TraceChildren c={t.children} fourBytesMap={fourBytesMap} /> <TraceChildren c={t.children} />
</div> </div>
)} )}
</> </>
@ -52,24 +50,16 @@ const TraceItem: React.FC<TraceItemProps> = ({ t, last, fourBytesMap }) => {
type TraceChildrenProps = { type TraceChildrenProps = {
c: TraceGroup[]; c: TraceGroup[];
fourBytesMap: Record<string, FourBytesEntry | null | undefined>;
}; };
const TraceChildren: React.FC<TraceChildrenProps> = React.memo( const TraceChildren: React.FC<TraceChildrenProps> = React.memo(({ c }) => {
({ c, fourBytesMap }) => { return (
return ( <>
<> {c.map((tc, i, a) => (
{c.map((tc, i, a) => ( <TraceItem key={i} t={tc} last={i === a.length - 1} />
<TraceItem ))}
key={i} </>
t={tc} );
last={i === a.length - 1} });
fourBytesMap={fourBytesMap}
/>
))}
</>
);
}
);
export default TraceItem; export default TraceItem;

View File

@ -1,4 +1,4 @@
import { useState, useEffect, useContext, useMemo } from "react"; import { useContext, useMemo } from "react";
import { import {
Fragment, Fragment,
Interface, Interface,
@ -14,8 +14,6 @@ export type FourBytesEntry = {
signature: string | undefined; signature: string | undefined;
}; };
export type FourBytesMap = Record<string, FourBytesEntry | null | undefined>;
/** /**
* Given a hex input data; extract the method selector * Given a hex input data; extract the method selector
* *
@ -61,38 +59,6 @@ const fetch4Bytes = async (
} }
}; };
// TODO: migrate to swr and merge with use4Bytes
export const useBatch4Bytes = (
rawFourByteSigs: string[] | undefined
): FourBytesMap => {
const runtime = useContext(RuntimeContext);
const assetsURLPrefix = runtime.config?.assetsURLPrefix;
const [fourBytesMap, setFourBytesMap] = useState<FourBytesMap>({});
useEffect(() => {
if (!rawFourByteSigs || assetsURLPrefix === undefined) {
setFourBytesMap({});
return;
}
const loadSigs = async () => {
const promises = rawFourByteSigs.map((s) =>
fetch4Bytes(assetsURLPrefix, s.slice(2))
);
const results = await Promise.all(promises);
const _fourBytesMap: Record<string, FourBytesEntry | null> = {};
for (let i = 0; i < rawFourByteSigs.length; i++) {
_fourBytesMap[rawFourByteSigs[i]] = results[i];
}
setFourBytesMap(_fourBytesMap);
};
loadSigs();
}, [assetsURLPrefix, rawFourByteSigs]);
return fourBytesMap;
};
/** /**
* Extract 4bytes DB info * Extract 4bytes DB info
* *

View File

@ -1,4 +1,4 @@
import { useState, useEffect, useMemo } from "react"; import { useState, useEffect } from "react";
import { Block, BlockWithTransactions } from "@ethersproject/abstract-provider"; import { Block, BlockWithTransactions } from "@ethersproject/abstract-provider";
import { JsonRpcProvider } from "@ethersproject/providers"; import { JsonRpcProvider } from "@ethersproject/providers";
import { getAddress } from "@ethersproject/address"; import { getAddress } from "@ethersproject/address";
@ -6,7 +6,6 @@ import { Contract } from "@ethersproject/contracts";
import { defaultAbiCoder } from "@ethersproject/abi"; import { defaultAbiCoder } from "@ethersproject/abi";
import { BigNumber } from "@ethersproject/bignumber"; import { BigNumber } from "@ethersproject/bignumber";
import { arrayify, hexDataSlice, isHexString } from "@ethersproject/bytes"; import { arrayify, hexDataSlice, isHexString } from "@ethersproject/bytes";
import { extract4Bytes } from "./use4Bytes";
import { getInternalOperations } from "./nodeFunctions"; import { getInternalOperations } from "./nodeFunctions";
import { import {
TokenMetas, TokenMetas,
@ -408,46 +407,6 @@ export const useTraceTransaction = (
return traceGroups; return traceGroups;
}; };
/**
* Flatten a trace tree and extract and dedup 4byte function signatures
*/
export const useUniqueSignatures = (traces: TraceGroup[] | undefined) => {
const uniqueSignatures = useMemo(() => {
if (!traces) {
return undefined;
}
const sigs = new Set<string>();
let nextTraces: TraceGroup[] = [...traces];
while (nextTraces.length > 0) {
const traces = nextTraces;
nextTraces = [];
for (const t of traces) {
if (
t.type === "CALL" ||
t.type === "DELEGATECALL" ||
t.type === "STATICCALL" ||
t.type === "CALLCODE"
) {
const fourBytes = extract4Bytes(t.input);
if (fourBytes) {
sigs.add(fourBytes);
}
}
if (t.children) {
nextTraces.push(...t.children);
}
}
}
return [...sigs];
}, [traces]);
return uniqueSignatures;
};
const hasCode = async ( const hasCode = async (
provider: JsonRpcProvider, provider: JsonRpcProvider,
address: ChecksummedAddress address: ChecksummedAddress