Skip to content

Commit

Permalink
Merge pull request #15 from scaffold-eth/backmerge-upstream-main
Browse files Browse the repository at this point in the history
Backmerge upstream main
  • Loading branch information
carletex authored May 1, 2024
2 parents c41a3bb + d25d38f commit 35e6ae6
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 285 deletions.
7 changes: 7 additions & 0 deletions .changeset/shiny-bugs-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"create-eth": patch
---

- use burner-connector package
- Update useScaffoldEventHistory hook
- fix: types typo
10 changes: 4 additions & 6 deletions templates/base/packages/nextjs/components/SwitchTheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ export const SwitchTheme = ({ className }: { className?: string }) => {
onChange={handleToggle}
checked={isDarkMode}
/>
{
<label htmlFor="theme-toggle" className={`swap swap-rotate ${!isDarkMode ? "swap-active" : ""}`}>
<SunIcon className="swap-on h-5 w-5" />
<MoonIcon className="swap-off h-5 w-5" />
</label>
}
<label htmlFor="theme-toggle" className={`swap swap-rotate ${!isDarkMode ? "swap-active" : ""}`}>
<SunIcon className="swap-on h-5 w-5" />
<MoonIcon className="swap-off h-5 w-5" />
</label>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useTargetNetwork } from "./useTargetNetwork";
import { Abi, AbiEvent, ExtractAbiEventNames } from "abitype";
import { useInterval } from "usehooks-ts";
Expand Down Expand Up @@ -45,7 +45,7 @@ export const useScaffoldEventHistory = <
watch,
enabled = true,
}: UseScaffoldEventHistoryConfig<TContractName, TEventName, TBlockData, TTransactionData, TReceiptData>) => {
const [events, setEvents] = useState<any[]>();
const [events, setEvents] = useState<any[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string>();
const [fromBlockUpdated, setFromBlockUpdated] = useState<bigint>(fromBlock);
Expand All @@ -56,95 +56,91 @@ export const useScaffoldEventHistory = <
chainId: targetNetwork.id,
});

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

if (!enabled) {
throw new Error("Hook disabled");
}
if (!enabled) {
throw new Error("Hook disabled");
}

if (!publicClient) {
throw new Error("Public client not found");
}
if (!publicClient) {
throw new Error("Public client 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,
fromBlock: fromBlock || fromBlockUpdated,
toBlock: blockNumber,
});
setFromBlockUpdated(blockNumber + 1n);

const newEvents = [];
for (let i = logs.length - 1; i >= 0; i--) {
newEvents.push({
log: logs[i],
args: logs[i].args,
block:
blockData && logs[i].blockHash === null
? null
: await publicClient.getBlock({ blockHash: logs[i].blockHash as Hash }),
transaction:
transactionData && logs[i].transactionHash !== null
? await publicClient.getTransaction({ hash: logs[i].transactionHash as Hash })
: null,
receipt:
receiptData && logs[i].transactionHash !== null
? await publicClient.getTransactionReceipt({ hash: logs[i].transactionHash as Hash })
: null,
const blockNumber = await publicClient.getBlockNumber({ cacheTime: 0 });

if (blockNumber >= fromBlockUpdated) {
const logs = await publicClient.getLogs({
address: deployedContractData?.address,
event,
args: filters as any,
fromBlock: fromBlockUpdated,
toBlock: blockNumber,
});
}
if (events && typeof fromBlock === "undefined") {
setFromBlockUpdated(blockNumber + 1n);

const newEvents = [];
for (let i = logs.length - 1; i >= 0; i--) {
newEvents.push({
log: logs[i],
args: logs[i].args,
block:
blockData && logs[i].blockHash === null
? null
: await publicClient.getBlock({ blockHash: logs[i].blockHash as Hash }),
transaction:
transactionData && logs[i].transactionHash !== null
? await publicClient.getTransaction({ hash: logs[i].transactionHash as Hash })
: null,
receipt:
receiptData && logs[i].transactionHash !== null
? await publicClient.getTransactionReceipt({ hash: logs[i].transactionHash as Hash })
: null,
});
}
setEvents([...newEvents, ...events]);
} else {
setEvents(newEvents);
setError(undefined);
}
setError(undefined);
} catch (e: any) {
if (events.length > 0) {
setEvents([]);
}
setError(e);
console.error(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, enabled]);
[
blockData,
deployedContractData,
enabled,
eventName,
events,
// eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(filters, replacer),
fromBlockUpdated,
publicClient,
receiptData,
transactionData,
],
);

useEffect(() => {
if (!deployedContractLoading) {
readEvents();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
publicClient,
contractName,
eventName,
deployedContractLoading,
deployedContractData?.address,
deployedContractData,
// eslint-disable-next-line react-hooks/exhaustive-deps
JSON.stringify(filters, replacer),
blockData,
transactionData,
receiptData,
]);
}, [readEvents, deployedContractLoading]);

useEffect(() => {
// Reset the internal state when target network or fromBlock changed
Expand All @@ -159,7 +155,7 @@ export const useScaffoldEventHistory = <
readEvents();
}
},
watch ? (targetNetwork.id !== chains.hardhat.id ? scaffoldConfig.pollingInterval : 4_000) : null,
watch && enabled ? (targetNetwork.id !== chains.hardhat.id ? scaffoldConfig.pollingInterval : 4_000) : null,
);

const eventHistoryData = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { notification } from "~~/utils/scaffold-eth";
import {
ContractAbi,
ContractName,
scaffoldWriteContractOptions,
scaffoldWriteContractVariables,
ScaffoldWriteContractOptions,
ScaffoldWriteContractVariables,
} from "~~/utils/scaffold-eth/contract";

/**
Expand All @@ -36,8 +36,8 @@ export const useScaffoldWriteContract = <TContractName extends ContractName>(
const sendContractWriteAsyncTx = async <
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, "nonpayable" | "payable">,
>(
variables: scaffoldWriteContractVariables<TContractName, TFunctionName>,
options?: scaffoldWriteContractOptions,
variables: ScaffoldWriteContractVariables<TContractName, TFunctionName>,
options?: ScaffoldWriteContractOptions,
) => {
if (!deployedContractData) {
notification.error("Target Contract is not deployed, did you forget to run `yarn deploy`?");
Expand Down Expand Up @@ -86,8 +86,8 @@ export const useScaffoldWriteContract = <TContractName extends ContractName>(
TContractName extends ContractName,
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, "nonpayable" | "payable">,
>(
variables: scaffoldWriteContractVariables<TContractName, TFunctionName>,
options?: Omit<scaffoldWriteContractOptions, "onBlockConfirmation" | "blockConfirmations">,
variables: ScaffoldWriteContractVariables<TContractName, TFunctionName>,
options?: Omit<ScaffoldWriteContractOptions, "onBlockConfirmation" | "blockConfirmations">,
) => {
if (!deployedContractData) {
notification.error("Target Contract is not deployed, did you forget to run `yarn deploy`?");
Expand Down
1 change: 1 addition & 0 deletions templates/base/packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@uniswap/sdk-core": "~4.0.1",
"@uniswap/v2-sdk": "~3.0.1",
"blo": "~1.0.1",
"burner-connector": "~0.0.3",
"daisyui": "4.5.0",
"next": "~14.0.4",
"next-themes": "~0.2.1",
Expand Down

This file was deleted.

Loading

0 comments on commit 35e6ae6

Please sign in to comment.