Add next/last nonce navigation support

This commit is contained in:
Willian Mitsuda 2021-12-27 19:20:13 -03:00
parent e012165696
commit 0d2a1a593d
3 changed files with 38 additions and 15 deletions

View File

@ -254,11 +254,7 @@ const Details: React.FC<DetailsProps> = ({
</div> </div>
<div className="flex items-baseline pl-3"> <div className="flex items-baseline pl-3">
<Nonce value={txData.nonce} /> <Nonce value={txData.nonce} />
<NavNonce <NavNonce sender={txData.from} nonce={txData.nonce} />
sender={txData.from}
nonce={txData.nonce}
latestBlockNumber={undefined}
/>
</div> </div>
</div> </div>
</InfoRow> </InfoRow>

View File

@ -5,19 +5,17 @@ import { faChevronRight } from "@fortawesome/free-solid-svg-icons/faChevronRight
import NavButton from "./NavButton"; import NavButton from "./NavButton";
import { ChecksummedAddress } from "../types"; import { ChecksummedAddress } from "../types";
import { RuntimeContext } from "../useRuntime"; import { RuntimeContext } from "../useRuntime";
import { useTransactionBySenderAndNonce } from "../useErigonHooks"; import {
useTransactionBySenderAndNonce,
useTransactionCount,
} from "../useErigonHooks";
type NavNonceProps = { type NavNonceProps = {
sender: ChecksummedAddress; sender: ChecksummedAddress;
nonce: number; nonce: number;
latestBlockNumber: number | undefined;
}; };
const NavNonce: React.FC<NavNonceProps> = ({ const NavNonce: React.FC<NavNonceProps> = ({ sender, nonce }) => {
sender,
nonce,
latestBlockNumber,
}) => {
const { provider } = useContext(RuntimeContext); const { provider } = useContext(RuntimeContext);
const prevTxHash = useTransactionBySenderAndNonce( const prevTxHash = useTransactionBySenderAndNonce(
provider, provider,
@ -29,7 +27,12 @@ const NavNonce: React.FC<NavNonceProps> = ({
sender, sender,
nonce + 1 nonce + 1
); );
const lastTxHash = nextTxHash; const count = useTransactionCount(provider, sender);
const lastTxHash = useTransactionBySenderAndNonce(
provider,
sender,
count !== undefined ? count - 1 : undefined
);
return ( return (
<div className="pl-2 self-center flex space-x-1"> <div className="pl-2 self-center flex space-x-1">
@ -38,13 +41,13 @@ const NavNonce: React.FC<NavNonceProps> = ({
</NavButton> </NavButton>
<NavButton <NavButton
txHash={nextTxHash} txHash={nextTxHash}
disabled={latestBlockNumber === undefined || nonce >= latestBlockNumber} disabled={count === undefined || nonce >= count - 1}
> >
<FontAwesomeIcon icon={faChevronRight} /> <FontAwesomeIcon icon={faChevronRight} />
</NavButton> </NavButton>
<NavButton <NavButton
txHash={lastTxHash} txHash={lastTxHash}
disabled={latestBlockNumber === undefined || nonce >= latestBlockNumber} disabled={count === undefined || nonce >= count - 1}
> >
<FontAwesomeIcon icon={faChevronRight} /> <FontAwesomeIcon icon={faChevronRight} />
<FontAwesomeIcon icon={faChevronRight} /> <FontAwesomeIcon icon={faChevronRight} />

View File

@ -503,6 +503,30 @@ export const useTransactionError = (
return [errorMsg, data, isCustomError]; return [errorMsg, data, isCustomError];
}; };
export const useTransactionCount = (
provider: JsonRpcProvider | undefined,
sender: ChecksummedAddress | undefined
): number | undefined => {
const [count, setCount] = useState<number | undefined>();
useEffect(() => {
// Reset
setCount(undefined);
if (provider === undefined || sender === undefined) {
return;
}
const readCount = async () => {
const _count = await provider.getTransactionCount(sender);
setCount(_count);
};
readCount();
}, [provider, sender]);
return count;
};
export const useTransactionBySenderAndNonce = ( export const useTransactionBySenderAndNonce = (
provider: JsonRpcProvider | undefined, provider: JsonRpcProvider | undefined,
sender: ChecksummedAddress | undefined, sender: ChecksummedAddress | undefined,