Merge branch 'feature/optimize-0x0fe2542079644e107cbf13690eb9c2c65963ccb79089ff96bfaf8dced2331c92' into feature/trace-tx

This commit is contained in:
Willian Mitsuda 2021-10-29 23:34:54 -03:00
commit 7b6cf499bc
8 changed files with 163 additions and 70 deletions

View File

@ -15,7 +15,7 @@ import {
type TokenTransferItemProps = { type TokenTransferItemProps = {
t: TokenTransfer; t: TokenTransfer;
txData: TransactionData; txData: TransactionData;
tokenMeta?: TokenMeta | undefined; tokenMeta: TokenMeta | null | undefined;
}; };
// TODO: handle partial // TODO: handle partial

View File

@ -20,7 +20,7 @@ type DecoratedAddressLinkProps = {
selfDestruct?: boolean; selfDestruct?: boolean;
txFrom?: boolean; txFrom?: boolean;
txTo?: boolean; txTo?: boolean;
tokenMeta?: TokenMeta; tokenMeta?: TokenMeta | null | undefined;
}; };
const DecoratedAddresssLink: React.FC<DecoratedAddressLinkProps> = ({ const DecoratedAddresssLink: React.FC<DecoratedAddressLinkProps> = ({

View File

@ -3,7 +3,8 @@ import AddressHighlighter from "../components/AddressHighlighter";
import DecoratedAddressLink from "../components/DecoratedAddressLink"; import DecoratedAddressLink from "../components/DecoratedAddressLink";
import ContentFrame from "../ContentFrame"; import ContentFrame from "../ContentFrame";
import { TransactionData } from "../types"; import { TransactionData } from "../types";
import { useTraceTransaction } from "../useErigonHooks"; import { useBatch4Bytes } from "../use4Bytes";
import { useTraceTransaction, useUniqueSignatures } from "../useErigonHooks";
import { RuntimeContext } from "../useRuntime"; import { RuntimeContext } from "../useRuntime";
import TraceItem from "./TraceItem"; import TraceItem from "./TraceItem";
@ -14,6 +15,8 @@ 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>
@ -37,6 +40,7 @@ const Trace: React.FC<TraceProps> = ({ txData }) => {
t={t} t={t}
txData={txData} txData={txData}
last={i === a.length - 1} last={i === a.length - 1}
fourBytesMap={sigMap}
/> />
))} ))}
</div> </div>

View File

@ -4,29 +4,36 @@ import DecoratedAddressLink from "../components/DecoratedAddressLink";
import FormattedBalance from "../components/FormattedBalance"; import FormattedBalance from "../components/FormattedBalance";
import FunctionSignature from "./FunctionSignature"; import FunctionSignature from "./FunctionSignature";
import { TransactionData } from "../types"; import { TransactionData } from "../types";
import { rawInputTo4Bytes, use4Bytes } from "../use4Bytes"; import { extract4Bytes, FourBytesEntry } from "../use4Bytes";
import { TraceGroup } from "../useErigonHooks"; import { TraceGroup } from "../useErigonHooks";
type TraceItemProps = { type TraceItemProps = {
t: TraceGroup; t: TraceGroup;
txData: TransactionData; txData: TransactionData;
last: boolean; last: boolean;
fourBytesMap: Record<string, FourBytesEntry | null | undefined>;
}; };
const TraceItem: React.FC<TraceItemProps> = ({ t, txData, last }) => { const TraceItem: React.FC<TraceItemProps> = ({
const raw4Bytes = rawInputTo4Bytes(t.input); t,
const fourBytesEntry = use4Bytes(raw4Bytes); txData,
last,
fourBytesMap,
}) => {
const raw4Bytes = extract4Bytes(t.input);
const sigText =
raw4Bytes === null
? "<fallback>"
: fourBytesMap[raw4Bytes]?.name ?? raw4Bytes;
return ( return (
<> <>
<div className="flex"> <div className="flex relative">
<div className="relative w-5"> <div className="absolute border-l border-b w-5 h-full transform -translate-y-1/2"></div>
<div className="absolute border-l border-b w-full h-full transform -translate-y-1/2"></div> {!last && (
{!last && ( <div className="absolute left-0 border-l w-5 h-full transform translate-y-1/2"></div>
<div className="absolute left-0 border-l w-full h-full transform translate-y-1/2"></div> )}
)} <div className="ml-5 flex items-baseline border rounded px-1 py-px">
</div>
<div className="flex items-baseline border rounded px-1 py-px">
<span className="text-xs text-gray-400 lowercase">{t.type}</span> <span className="text-xs text-gray-400 lowercase">{t.type}</span>
<span> <span>
<AddressHighlighter address={t.to}> <AddressHighlighter address={t.to}>
@ -39,37 +46,28 @@ const TraceItem: React.FC<TraceItemProps> = ({ t, txData, last }) => {
</AddressHighlighter> </AddressHighlighter>
</span> </span>
<span>.</span> <span>.</span>
<FunctionSignature <FunctionSignature callType={t.type} sig={sigText} />
callType={t.type}
sig={fourBytesEntry ? fourBytesEntry.name : raw4Bytes}
/>
{t.value && !t.value.isZero() && ( {t.value && !t.value.isZero() && (
<span className="text-red-700 whitespace-nowrap"> <span className="text-red-700 whitespace-nowrap">
{"{"}value: <FormattedBalance value={t.value} /> ETH{"}"} {"{"}value: <FormattedBalance value={t.value} /> ETH{"}"}
</span> </span>
)} )}
<span>(</span> <span className="whitespace-nowrap">
{t.input.length > 10 && ( ({t.input.length > 10 && <>input=[0x{t.input.slice(10)}]</>})
<span className="whitespace-nowrap"> </span>
input=[0x{t.input.slice(10)}]
</span>
)}
<span>)</span>
</div> </div>
</div> </div>
{t.children && ( {t.children && (
<div className="flex"> <div className={`pl-10 ${last ? "" : "border-l"} space-y-3`}>
<div className={`w-10 ${last ? "" : "border-l"}`}></div> {t.children.map((tc, i, a) => (
<div className="space-y-3"> <TraceItem
{t.children.map((tc, i, a) => ( key={i}
<TraceItem t={tc}
key={i} txData={txData}
t={tc} last={i === a.length - 1}
txData={txData} fourBytesMap={fourBytesMap}
last={i === a.length - 1} />
/> ))}
))}
</div>
</div> </div>
)} )}
</> </>

View File

@ -108,4 +108,4 @@ export type TokenMeta = {
decimals: number; decimals: number;
}; };
export type TokenMetas = Record<string, TokenMeta>; export type TokenMetas = Record<string, TokenMeta | null | undefined>;

View File

@ -7,6 +7,8 @@ export type FourBytesEntry = {
signature: string | undefined; signature: string | undefined;
}; };
export type FourBytesMap = Record<string, FourBytesEntry | null | undefined>;
const simpleTransfer: FourBytesEntry = { const simpleTransfer: FourBytesEntry = {
name: "Transfer", name: "Transfer",
signature: undefined, signature: undefined,
@ -14,8 +16,76 @@ const simpleTransfer: FourBytesEntry = {
const fullCache = new Map<string, FourBytesEntry | null>(); const fullCache = new Map<string, FourBytesEntry | null>();
export const extract4Bytes = (rawInput: string): string | null => {
if (rawInput.length < 10) {
return null;
}
return rawInput.slice(0, 10);
};
export const rawInputTo4Bytes = (rawInput: string) => rawInput.slice(0, 10); export const rawInputTo4Bytes = (rawInput: string) => rawInput.slice(0, 10);
const fetch4Bytes = async (
assetsURLPrefix: string,
fourBytes: string
): Promise<FourBytesEntry | null> => {
const signatureURL = fourBytesURL(assetsURLPrefix, fourBytes);
try {
const res = await fetch(signatureURL);
if (!res.ok) {
console.error(`Signature does not exist in 4bytes DB: ${fourBytes}`);
return null;
}
// Get only the first occurrence, for now ignore alternative param names
const sigs = await res.text();
const sig = sigs.split(";")[0];
const cut = sig.indexOf("(");
const method = sig.slice(0, cut);
const entry: FourBytesEntry = {
name: method,
signature: sig,
};
return entry;
} catch (err) {
console.error(`Couldn't fetch signature URL ${signatureURL}`, err);
return null;
}
};
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) {
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
* *
@ -47,34 +117,12 @@ export const use4Bytes = (
return; return;
} }
const signatureURL = fourBytesURL(assetsURLPrefix, fourBytes); const loadSig = async () => {
fetch(signatureURL) const entry = await fetch4Bytes(assetsURLPrefix, fourBytes);
.then(async (res) => { fullCache.set(fourBytes, entry);
if (!res.ok) { setEntry(entry);
console.error(`Signature does not exist in 4bytes DB: ${fourBytes}`); };
fullCache.set(fourBytes, null); loadSig();
setEntry(null);
return;
}
// Get only the first occurrence, for now ignore alternative param names
const sigs = await res.text();
const sig = sigs.split(";")[0];
const cut = sig.indexOf("(");
const method = sig.slice(0, cut);
const entry: FourBytesEntry = {
name: method,
signature: sig,
};
setEntry(entry);
fullCache.set(fourBytes, entry);
})
.catch((err) => {
console.error(`Couldn't fetch signature URL ${signatureURL}`, err);
setEntry(null);
fullCache.set(fourBytes, null);
});
}, [fourBytes, assetsURLPrefix]); }, [fourBytes, assetsURLPrefix]);
if (rawFourBytes === "0x") { if (rawFourBytes === "0x") {

View File

@ -1,10 +1,11 @@
import { useState, useEffect } from "react"; import { useState, useEffect, useMemo } 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";
import { Contract } from "@ethersproject/contracts"; import { Contract } from "@ethersproject/contracts";
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,
@ -221,7 +222,7 @@ export const useTxData = (
// Extract token meta // Extract token meta
const tokenMetas: TokenMetas = {}; const tokenMetas: TokenMetas = {};
for (const t of tokenTransfers) { for (const t of tokenTransfers) {
if (tokenMetas[t.token]) { if (tokenMetas[t.token] !== undefined) {
continue; continue;
} }
const erc20Contract = new Contract(t.token, erc20, provider); const erc20Contract = new Contract(t.token, erc20, provider);
@ -237,6 +238,7 @@ export const useTxData = (
decimals, decimals,
}; };
} catch (err) { } catch (err) {
tokenMetas[t.token] = null;
console.warn(`Couldn't get token ${t.token} metadata; ignoring`, err); console.warn(`Couldn't get token ${t.token} metadata; ignoring`, err);
} }
} }
@ -398,3 +400,43 @@ 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;
};

View File

@ -14,7 +14,8 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"noEmit": true, "noEmit": true,
"jsx": "react-jsx" "jsx": "react-jsx",
"downlevelIteration": true
}, },
"include": ["src"] "include": ["src"]
} }