diff --git a/src/nodeFunctions.ts b/src/nodeFunctions.ts deleted file mode 100644 index 5a6f140..0000000 --- a/src/nodeFunctions.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { JsonRpcProvider } from "@ethersproject/providers"; -import { getAddress } from "@ethersproject/address"; -import { InternalOperation } from "./types"; - -export const getInternalOperations = async ( - provider: JsonRpcProvider, - txHash: string -) => { - const rawTransfers = await provider.send("ots_getInternalOperations", [ - txHash, - ]); - - const _transfers: InternalOperation[] = []; - for (const t of rawTransfers) { - _transfers.push({ - type: t.type, - from: getAddress(t.from), - to: getAddress(t.to), - value: t.value, - }); - } - return _transfers; -}; diff --git a/src/useErigonHooks.ts b/src/useErigonHooks.ts index f2b798c..75e8a32 100644 --- a/src/useErigonHooks.ts +++ b/src/useErigonHooks.ts @@ -1,4 +1,4 @@ -import { useState, useEffect } from "react"; +import { useState, useEffect, useMemo } from "react"; import { Block, BlockWithTransactions, @@ -12,7 +12,6 @@ import { BigNumber } from "@ethersproject/bignumber"; import { arrayify, hexDataSlice, isHexString } from "@ethersproject/bytes"; import useSWR from "swr"; import useSWRImmutable from "swr/immutable"; -import { getInternalOperations } from "./nodeFunctions"; import { TokenMetas, TokenTransfer, @@ -169,24 +168,25 @@ export const useBlockTransactions = ( return [totalTxs, txs]; }; +const blockDataFetcher = async ( + provider: JsonRpcProvider, + blockNumberOrHash: string +) => { + return await readBlock(provider, blockNumberOrHash); +}; + export const useBlockData = ( provider: JsonRpcProvider | undefined, blockNumberOrHash: string ): ExtendedBlock | null | undefined => { - const [block, setBlock] = useState(); - useEffect(() => { - if (!provider) { - return undefined; - } - - const _readBlock = async () => { - const extBlock = await readBlock(provider, blockNumberOrHash); - setBlock(extBlock); - }; - _readBlock(); - }, [provider, blockNumberOrHash]); - - return block; + const { data, error } = useSWRImmutable( + provider !== undefined ? [provider, blockNumberOrHash] : null, + blockDataFetcher + ); + if (error) { + return undefined; + } + return data; }; export const useTxData = ( @@ -308,33 +308,55 @@ export const useTxData = ( return txData; }; +// TODO: convert caller to hooks and remove this function +const getInternalOperations = async ( + provider: JsonRpcProvider, + txHash: string +) => { + const rawTransfers = await provider.send("ots_getInternalOperations", [ + txHash, + ]); + + const _transfers: InternalOperation[] = []; + for (const t of rawTransfers) { + _transfers.push({ + type: t.type, + from: getAddress(t.from), + to: getAddress(t.to), + value: t.value, + }); + } + return _transfers; +}; + export const useInternalOperations = ( provider: JsonRpcProvider | undefined, txData: TransactionData | undefined | null ): InternalOperation[] | undefined => { - const [intTransfers, setIntTransfers] = useState(); + const { data, error } = useSWRImmutable( + provider !== undefined && txData?.confirmedData + ? ["ots_getInternalOperations", txData.transactionHash] + : null, + providerFetcher(provider) + ); - useEffect(() => { - const traceTransfers = async () => { - if (!provider || !txData || !txData.confirmedData) { - return; - } + const _transfers = useMemo(() => { + if (provider === undefined || error || data === undefined) { + return undefined; + } - const _transfers = await getInternalOperations( - provider, - txData.transactionHash - ); - 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); - }; - traceTransfers(); - }, [provider, txData]); - - return intTransfers; + const _t: InternalOperation[] = []; + for (const t of data) { + _t.push({ + type: t.type, + from: provider.formatter.address(getAddress(t.from)), + to: provider.formatter.address(getAddress(t.to)), + value: provider.formatter.bigNumber(t.value), + }); + } + return _t; + }, [provider, data]); + return _transfers; }; export type TraceEntry = {