Skip to content

Commit

Permalink
Merge pull request #619 from scaffold-eth/cli-backmerge
Browse files Browse the repository at this point in the history
Weekly CLI backmerge
  • Loading branch information
carletex authored Nov 22, 2023
2 parents 621c9ac + efbe3fc commit 650d007
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
7 changes: 7 additions & 0 deletions .changeset/silver-baboons-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"create-eth": patch
---

- Add disableMultiplyBy1e18 flag to IntegerInput component (#609)
- Add pooling interval to EventHistory hook (#597)
- Fix AbiFunctionReturnType removing [0] (#610)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CommonInputProps, InputBase, IntegerVariant, isValidInteger } from "~~/

type IntegerInputProps = CommonInputProps<string | bigint> & {
variant?: IntegerVariant;
disableMultiplyBy1e18?: boolean;
};

export const IntegerInput = ({
Expand All @@ -12,6 +13,7 @@ export const IntegerInput = ({
placeholder,
disabled,
variant = IntegerVariant.UINT256,
disableMultiplyBy1e18 = false,
}: IntegerInputProps) => {
const [inputError, setInputError] = useState(false);
const multiplyBy1e18 = useCallback(() => {
Expand Down Expand Up @@ -41,7 +43,8 @@ export const IntegerInput = ({
onChange={onChange}
disabled={disabled}
suffix={
!inputError && (
!inputError &&
!disableMultiplyBy1e18 && (
<div
className="space-x-4 flex tooltip tooltip-top tooltip-secondary before:content-[attr(data-tip)] before:right-[-10px] before:left-auto before:transform-none"
data-tip="Multiply by 10^18 (wei)"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useEffect, useMemo, useState } from "react";
import { Abi, AbiEvent, ExtractAbiEventNames } from "abitype";
import { useInterval } from "usehooks-ts";
import { Hash } from "viem";
import * as chains from "viem/chains";
import { usePublicClient } from "wagmi";
import { useDeployedContractInfo } from "~~/hooks/scaffold-eth";
import scaffoldConfig from "~~/scaffold.config";
import { getTargetNetwork } from "~~/utils/scaffold-eth";
import { replacer } from "~~/utils/scaffold-eth/common";
import {
ContractAbi,
Expand All @@ -21,6 +25,7 @@ import {
* @param config.blockData - if set to true it will return the block data for each event (default: false)
* @param config.transactionData - if set to true it will return the transaction data for each event (default: false)
* @param config.receiptData - if set to true it will return the receipt data for each event (default: false)
* @param config.watch - if set to true, the events will be updated every pollingInterval milliseconds set at scaffoldConfig (default: false)
*/
export const useScaffoldEventHistory = <
TContractName extends ContractName,
Expand All @@ -36,30 +41,40 @@ export const useScaffoldEventHistory = <
blockData,
transactionData,
receiptData,
watch,
}: UseScaffoldEventHistoryConfig<TContractName, TEventName, TBlockData, TTransactionData, TReceiptData>) => {
const [events, setEvents] = useState<any[]>();
const [isLoading, setIsLoading] = useState(true);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string>();
const [fromBlockUpdated, setFromBlockUpdated] = useState<bigint>(fromBlock);

const { data: deployedContractData, isLoading: deployedContractLoading } = useDeployedContractInfo(contractName);
const publicClient = usePublicClient();
const configuredNetwork = getTargetNetwork();

useEffect(() => {
async function readEvents() {
try {
if (!deployedContractData) {
throw new Error("Contract not found");
}
const readEvents = async (fromBlock?: bigint) => {
setIsLoading(true);
try {
if (!deployedContractData) {
throw new Error("Contract not found");
}

const event = (deployedContractData.abi as Abi).find(
part => part.type === "event" && part.name === eventName,
) as AbiEvent;
const event = (deployedContractData.abi as Abi).find(
part => part.type === "event" && part.name === eventName,
) as AbiEvent;

const blockNumber = await publicClient.getBlockNumber({ cacheTime: 0 });

if ((fromBlock && blockNumber >= fromBlock) || blockNumber >= fromBlockUpdated) {
const logs = await publicClient.getLogs({
address: deployedContractData?.address,
event,
args: filters as any, // TODO: check if it works and fix type
fromBlock,
fromBlock: fromBlock || fromBlockUpdated,
toBlock: blockNumber,
});
setFromBlockUpdated(blockNumber + 1n);

const newEvents = [];
for (let i = logs.length - 1; i >= 0; i--) {
newEvents.push({
Expand All @@ -79,23 +94,34 @@ export const useScaffoldEventHistory = <
: null,
});
}
setEvents(newEvents);
if (events && typeof fromBlock === "undefined") {
setEvents([...events, ...newEvents]);
} else {
setEvents(newEvents);
}
setError(undefined);
} catch (e: any) {
console.error(e);
setEvents(undefined);
setError(e);
} finally {
setIsLoading(false);
}
} catch (e: any) {
console.error(e);
setEvents(undefined);
setError(e);
} finally {
setIsLoading(false);
}
};

useEffect(() => {
readEvents(fromBlock);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fromBlock]);

useEffect(() => {
if (!deployedContractLoading) {
readEvents();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
publicClient,
fromBlock,
contractName,
eventName,
deployedContractLoading,
Expand All @@ -108,6 +134,15 @@ export const useScaffoldEventHistory = <
receiptData,
]);

useInterval(
async () => {
if (!deployedContractLoading) {
readEvents();
}
},
watch ? (configuredNetwork.id !== chains.hardhat.id ? scaffoldConfig.pollingInterval : 4_000) : null,
);

const eventHistoryData = useMemo(
() =>
events?.map(addIndexedArgsToEvent) as UseScaffoldEventHistoryData<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export type AbiFunctionOutputs<TAbi extends Abi, TFunctionName extends string> =

export type AbiFunctionReturnType<TAbi extends Abi, TFunctionName extends string> = IsContractDeclarationMissing<
any,
AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>>[0]
AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>> extends readonly [any]
? AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>>[0]
: AbiParametersToPrimitiveTypes<AbiFunctionOutputs<TAbi, TFunctionName>>
>;

export type AbiEventInputs<TAbi extends Abi, TEventName extends ExtractAbiEventNames<TAbi>> = ExtractAbiEvent<
Expand Down Expand Up @@ -237,6 +239,7 @@ export type UseScaffoldEventHistoryConfig<
blockData?: TBlockData;
transactionData?: TTransactionData;
receiptData?: TReceiptData;
watch?: boolean;
};

export type UseScaffoldEventHistoryData<
Expand Down

0 comments on commit 650d007

Please sign in to comment.