Skip to content

Commit

Permalink
trigger all event from smartcontract
Browse files Browse the repository at this point in the history
  • Loading branch information
olivmath committed Sep 23, 2024
1 parent fa62a1b commit 4f056fe
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Fullstack Web3 Template v2.2.1
# Fullstack Web3 Template v2.3.0

## QuickStart

Expand Down
30 changes: 30 additions & 0 deletions ui/app/debug/_components/DebugContracts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,43 @@ import { BarsArrowUpIcon } from "@heroicons/react/20/solid";
import { ContractUI } from "~~/app/debug/_components/contract";
import { ContractName, GenericContract } from "~~/utils/fwt/contract";
import { useAllContracts } from "~~/utils/fwt/contractsData";
import { useScaffoldWatchContractEvent } from "~~/hooks/scaffold-eth/useScaffoldWatchContractEvent";
import { notification } from "~~/utils/fwt/notification";

const selectedContractStorageKey = "scaffoldEth2.selectedContract";

export function DebugContracts() {
const contractsData = useAllContracts();
const contractNames = useMemo(() => Object.keys(contractsData) as ContractName[], [contractsData]);

contractNames.forEach(contractName => {
const contract = contractsData[contractName];
if (contract && contract.abi) {
contract.abi.forEach(item => {
if (item.type === "event") {
useScaffoldWatchContractEvent({
contractName: contractName,
eventName: item.name,
onLogs: logs => {
logs.forEach(log => {
const args = Object.entries(log.args)
.filter(([key]) => isNaN(Number(key)))
.map(([key, value]) => `${key}: ${value}`)
.join("\n");

notification.info(
`Event "${item.name}" triggered on ${contractName}:
${args}`,
{ duration: 6000 },
);
});
},
});
}
});
}
});

const [selectedContract, setSelectedContract] = useLocalStorage<ContractName>(
selectedContractStorageKey,
contractNames[0],
Expand Down
17 changes: 0 additions & 17 deletions ui/app/debug/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,8 @@

import { DebugContracts } from "./_components/DebugContracts";
import type { NextPage } from "next";
import { useScaffoldWatchContractEvent } from "~~/hooks/scaffold-eth/useScaffoldWatchContractEvent";
import { notification } from "~~/utils/fwt/notification";

const Debug: NextPage = () => {
useScaffoldWatchContractEvent({
contractName: "Counter",
eventName: "YouCall",
onLogs: logs => {
logs.forEach(log => {
notification.info(
`Event YouCall triggered:
Caller: ${log.args.caller}
Amount: ${log.args.amount} wei`,
{ duration: 6000 }
);
});
},
});

return (
<>
<DebugContracts />
Expand Down
2 changes: 1 addition & 1 deletion ui/contracts/deployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { GenericContractsDeclaration } from "~~/utils/fwt/contract";

const deployedContracts = {
31337: {"Counter": {"address": "0x610178da211fef7d417bc0e6fed39f05609ad788", "abi": [{"type": "function", "name": "getError", "inputs": [], "outputs": [], "stateMutability": "pure"}, {"type": "function", "name": "increment", "inputs": [], "outputs": [], "stateMutability": "nonpayable"}, {"type": "function", "name": "number", "inputs": [], "outputs": [{"name": "", "type": "uint256", "internalType": "uint256"}], "stateMutability": "view"}, {"type": "function", "name": "setNumber", "inputs": [{"name": "newNumber", "type": "uint256", "internalType": "uint256"}], "outputs": [], "stateMutability": "nonpayable"}, {"type": "function", "name": "withdraw", "inputs": [{"name": "amount", "type": "uint256", "internalType": "uint256"}], "outputs": [], "stateMutability": "pure"}, {"type": "error", "name": "CustomError", "inputs": [{"name": "message", "type": "string", "internalType": "string"}]}, {"type": "error", "name": "InsufficientBalance", "inputs": [{"name": "available", "type": "uint256", "internalType": "uint256"}, {"name": "required", "type": "uint256", "internalType": "uint256"}]}]}}
31337: {"Counter": {"address": "0x9e545e3c0baab3e08cdfd552c960a1050f373042", "abi": [{"type": "function", "name": "getError", "inputs": [{"name": "yourAmount", "type": "uint256", "internalType": "uint256"}], "outputs": [{"name": "", "type": "string", "internalType": "string"}], "stateMutability": "pure"}, {"type": "function", "name": "increment", "inputs": [], "outputs": [], "stateMutability": "nonpayable"}, {"type": "function", "name": "launchEvent", "inputs": [], "outputs": [], "stateMutability": "payable"}, {"type": "function", "name": "number", "inputs": [], "outputs": [{"name": "", "type": "uint256", "internalType": "uint256"}], "stateMutability": "view"}, {"type": "function", "name": "setNumber", "inputs": [{"name": "newNumber", "type": "uint256", "internalType": "uint256"}], "outputs": [], "stateMutability": "nonpayable"}, {"type": "function", "name": "youETHBalance", "inputs": [], "outputs": [], "stateMutability": "nonpayable"}, {"type": "event", "name": "YouCall", "inputs": [{"name": "caller", "type": "address", "indexed": false, "internalType": "address"}, {"name": "amount", "type": "uint256", "indexed": false, "internalType": "uint256"}], "anonymous": false}, {"type": "event", "name": "YourBalance", "inputs": [{"name": "caller", "type": "address", "indexed": false, "internalType": "address"}, {"name": "amount", "type": "uint256", "indexed": false, "internalType": "uint256"}], "anonymous": false}, {"type": "error", "name": "CustomError", "inputs": [{"name": "message", "type": "string", "internalType": "string"}, {"name": "yourAmount", "type": "uint256", "internalType": "uint256"}]}]}}
} as const;

export default deployedContracts satisfies GenericContractsDeclaration;

0 comments on commit 4f056fe

Please sign in to comment.