From c3684151bf1e10c62a7633222b75fd790956ba87 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Mon, 19 Jul 2021 20:49:54 -0300 Subject: [PATCH] Unified node-side tracing APIs --- src/Transaction.tsx | 7 +------ src/components/InternalOperation.tsx | 25 +++++++++++++++++++++++++ src/nodeFunctions.ts | 20 +------------------- src/transaction/Details.tsx | 14 +++++--------- src/types.ts | 11 ++++++----- src/useErigonHooks.ts | 25 +++++++++---------------- 6 files changed, 47 insertions(+), 55 deletions(-) create mode 100644 src/components/InternalOperation.tsx diff --git a/src/Transaction.tsx b/src/Transaction.tsx index 96b7bf9..ce564f9 100644 --- a/src/Transaction.tsx +++ b/src/Transaction.tsx @@ -111,12 +111,7 @@ const Transaction: React.FC = () => { return false; } - for (const t of intTransfers.transfers) { - if (t.to === txData.miner) { - return true; - } - } - for (const t of intTransfers.selfDestructs) { + for (const t of intTransfers) { if (t.to === txData.miner) { return true; } diff --git a/src/components/InternalOperation.tsx b/src/components/InternalOperation.tsx new file mode 100644 index 0000000..0ae0a38 --- /dev/null +++ b/src/components/InternalOperation.tsx @@ -0,0 +1,25 @@ +import React from "react"; +import InternalTransfer from "./InternalTransfer"; +import InternalSelfDestruct from "./InternalSelfDestruct"; +import { TransactionData, Transfer, TransferType } from "../types"; + +type InternalOperationProps = { + txData: TransactionData; + transfer: Transfer; +}; + +const InternalOperation: React.FC = ({ + txData, + transfer, +}) => ( + <> + {transfer.type === TransferType.TRANSFER && ( + + )} + {transfer.type === TransferType.SELF_DESTRUCT && ( + + )} + +); + +export default React.memo(InternalOperation); diff --git a/src/nodeFunctions.ts b/src/nodeFunctions.ts index b3ff583..2cb5558 100644 --- a/src/nodeFunctions.ts +++ b/src/nodeFunctions.ts @@ -12,25 +12,7 @@ export const getTransactionTransfers = async ( const _transfers: Transfer[] = []; for (const t of rawTransfers) { _transfers.push({ - from: ethers.utils.getAddress(t.from), - to: ethers.utils.getAddress(t.to), - value: t.value, - }); - } - return _transfers; -}; - -export const getTransactionSelfDestructs = async ( - provider: ethers.providers.JsonRpcProvider, - txData: TransactionData -) => { - const rawTransfers = await provider.send("ots_getTransactionSelfDestructs", [ - txData.transactionHash, - ]); - - const _transfers: Transfer[] = []; - for (const t of rawTransfers) { - _transfers.push({ + type: t.type, from: ethers.utils.getAddress(t.from), to: ethers.utils.getAddress(t.to), value: t.value, diff --git a/src/transaction/Details.tsx b/src/transaction/Details.tsx index cba7a9a..7d18098 100644 --- a/src/transaction/Details.tsx +++ b/src/transaction/Details.tsx @@ -12,17 +12,16 @@ import AddressHighlighter from "../components/AddressHighlighter"; import DecoratedAddressLink from "../components/DecoratedAddressLink"; import Copy from "../components/Copy"; import Timestamp from "../components/Timestamp"; -import InternalTransfer from "../components/InternalTransfer"; -import InternalSelfDestruct from "../components/InternalSelfDestruct"; +import InternalOperation from "../components/InternalOperation"; import MethodName from "../components/MethodName"; import GasValue from "../components/GasValue"; import FormattedBalance from "../components/FormattedBalance"; import TokenTransferItem from "../TokenTransferItem"; -import { InternalTransfers, TransactionData } from "../types"; +import { TransactionData, Transfer } from "../types"; type DetailsProps = { txData: TransactionData; - internalTransfers?: InternalTransfers; + internalTransfers?: Transfer[]; sendsEthToMiner: boolean; }; @@ -87,11 +86,8 @@ const Details: React.FC = ({ {internalTransfers && (
- {internalTransfers.transfers.map((t, i) => ( - - ))} - {internalTransfers.selfDestructs.map((t, i) => ( - + {internalTransfers.map((t, i) => ( + ))}
)} diff --git a/src/types.ts b/src/types.ts index 94301e5..29cc6bc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -70,17 +70,18 @@ export type From = { depth: number; }; +export enum TransferType { + TRANSFER = 0, + SELF_DESTRUCT = 1, +} + export type Transfer = { + type: TransferType; from: string; to: string; value: BigNumber; }; -export type InternalTransfers = { - transfers: Transfer[]; - selfDestructs: Transfer[]; -}; - export type TokenTransfer = { token: string; from: string; diff --git a/src/useErigonHooks.ts b/src/useErigonHooks.ts index 0ca4a06..447408c 100644 --- a/src/useErigonHooks.ts +++ b/src/useErigonHooks.ts @@ -1,16 +1,13 @@ import { ethers } from "ethers"; import { useState, useEffect } from "react"; -import { - getTransactionSelfDestructs, - getTransactionTransfers, -} from "./nodeFunctions"; -import { InternalTransfers, TransactionData } from "./types"; +import { getTransactionTransfers } from "./nodeFunctions"; +import { TransactionData, Transfer } from "./types"; export const useInternalTransfers = ( provider: ethers.providers.JsonRpcProvider | undefined, txData: TransactionData | undefined -): InternalTransfers | undefined => { - const [intTransfers, setIntTransfers] = useState(); +): Transfer[] | undefined => { + const [intTransfers, setIntTransfers] = useState(); useEffect(() => { const traceTransfers = async () => { @@ -19,16 +16,12 @@ export const useInternalTransfers = ( } const _transfers = await getTransactionTransfers(provider, txData); - const _selfDestructs = await getTransactionSelfDestructs( - provider, - txData - ); - for (const s of _selfDestructs) { - s.from = provider.formatter.address(s.from); - s.to = provider.formatter.address(s.to); - s.value = provider.formatter.bigNumber(s.value); + for (const t of _transfers) { + t.from = provider.formatter.address(t.from); + t.to = provider.formatter.address(t.to); + t.value = provider.formatter.bigNumber(t.value); } - setIntTransfers({ transfers: _transfers, selfDestructs: _selfDestructs }); + setIntTransfers(_transfers); }; traceTransfers(); }, [provider, txData]);