WIP: convert block/txs reading in SWR; simplify code

This commit is contained in:
Willian Mitsuda 2022-08-23 15:17:53 -03:00
parent 90d25cdd8d
commit c365bde784
No known key found for this signature in database
2 changed files with 59 additions and 60 deletions

View File

@ -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;
};

View File

@ -1,4 +1,4 @@
import { useState, useEffect } from "react"; import { useState, useEffect, useMemo } from "react";
import { import {
Block, Block,
BlockWithTransactions, BlockWithTransactions,
@ -12,7 +12,6 @@ import { BigNumber } from "@ethersproject/bignumber";
import { arrayify, hexDataSlice, isHexString } from "@ethersproject/bytes"; import { arrayify, hexDataSlice, isHexString } from "@ethersproject/bytes";
import useSWR from "swr"; import useSWR from "swr";
import useSWRImmutable from "swr/immutable"; import useSWRImmutable from "swr/immutable";
import { getInternalOperations } from "./nodeFunctions";
import { import {
TokenMetas, TokenMetas,
TokenTransfer, TokenTransfer,
@ -169,24 +168,25 @@ export const useBlockTransactions = (
return [totalTxs, txs]; return [totalTxs, txs];
}; };
const blockDataFetcher = async (
provider: JsonRpcProvider,
blockNumberOrHash: string
) => {
return await readBlock(provider, blockNumberOrHash);
};
export const useBlockData = ( export const useBlockData = (
provider: JsonRpcProvider | undefined, provider: JsonRpcProvider | undefined,
blockNumberOrHash: string blockNumberOrHash: string
): ExtendedBlock | null | undefined => { ): ExtendedBlock | null | undefined => {
const [block, setBlock] = useState<ExtendedBlock | null | undefined>(); const { data, error } = useSWRImmutable(
useEffect(() => { provider !== undefined ? [provider, blockNumberOrHash] : null,
if (!provider) { blockDataFetcher
return undefined; );
} if (error) {
return undefined;
const _readBlock = async () => { }
const extBlock = await readBlock(provider, blockNumberOrHash); return data;
setBlock(extBlock);
};
_readBlock();
}, [provider, blockNumberOrHash]);
return block;
}; };
export const useTxData = ( export const useTxData = (
@ -308,33 +308,55 @@ export const useTxData = (
return txData; 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 = ( export const useInternalOperations = (
provider: JsonRpcProvider | undefined, provider: JsonRpcProvider | undefined,
txData: TransactionData | undefined | null txData: TransactionData | undefined | null
): InternalOperation[] | undefined => { ): InternalOperation[] | undefined => {
const [intTransfers, setIntTransfers] = useState<InternalOperation[]>(); const { data, error } = useSWRImmutable(
provider !== undefined && txData?.confirmedData
? ["ots_getInternalOperations", txData.transactionHash]
: null,
providerFetcher(provider)
);
useEffect(() => { const _transfers = useMemo(() => {
const traceTransfers = async () => { if (provider === undefined || error || data === undefined) {
if (!provider || !txData || !txData.confirmedData) { return undefined;
return; }
}
const _transfers = await getInternalOperations( const _t: InternalOperation[] = [];
provider, for (const t of data) {
txData.transactionHash _t.push({
); type: t.type,
for (const t of _transfers) { from: provider.formatter.address(getAddress(t.from)),
t.from = provider.formatter.address(t.from); to: provider.formatter.address(getAddress(t.to)),
t.to = provider.formatter.address(t.to); value: provider.formatter.bigNumber(t.value),
t.value = provider.formatter.bigNumber(t.value); });
} }
setIntTransfers(_transfers); return _t;
}; }, [provider, data]);
traceTransfers(); return _transfers;
}, [provider, txData]);
return intTransfers;
}; };
export type TraceEntry = { export type TraceEntry = {