|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: |
| 4 | +alwaysApply: true |
| 5 | +--- |
| 6 | +This codebase contains Scaffold-ETH 2 (SE-2), everything you need to build dApps on Ethereum. Its tech stack is NextJS, RainbowKit, Wagmi and Typescript. Supports Hardhat and Foundry. |
| 7 | + |
| 8 | +It's a yarn monorepo that contains two main packages: |
| 9 | + |
| 10 | +- Hardhat (`packages/hardhat`): The solidity framework to write, test and deploy EVM Smart Contracts. |
| 11 | +- NextJS (`packages/nextjs`): The UI framework extended with utilities to make interacting with Smart Contracts easy (using Next.js App Router, not Pages Router). |
| 12 | + |
| 13 | +The usual dev flow is: |
| 14 | + |
| 15 | +- Start SE-2 locally: |
| 16 | + - `yarn chain`: Starts a local blockchain network |
| 17 | + - `yarn deploy`: Deploys SE-2 default contract |
| 18 | + - `yarn start`: Starts the frontend |
| 19 | +- Write a Smart Contract (modify the deployment script in `packages/hardhat/deploy` if needed) |
| 20 | +- Deploy it locally (`yarn deploy`) |
| 21 | +- Go to the `http://locahost:3000/debug` page to interact with your contract with a nice UI |
| 22 | +- Iterate until you get the functionality you want in your contract |
| 23 | +- Write tests for the contract in `packages/hardhat/test` |
| 24 | +- Create your custom UI using all the SE-2 components, hooks, and utilities. |
| 25 | +- Deploy your Smart Contrac to a live network |
| 26 | +- Deploy your UI (`yarn vercel` or `yarn ipfs`) |
| 27 | + - You can tweak which network the frontend is poiting (and some other configurations) in `scaffold.config.ts` |
| 28 | + |
| 29 | +## Smart Contract UI interactions guidelines |
| 30 | + |
| 31 | +SE-2 provides a set of hooks that facilitates contract interactions from the UI. It reads the contract data from `deployedContracts.ts` and `externalContracts.ts`, located in `packages/nextjs/contracts`. |
| 32 | + |
| 33 | +### Reading data from a contract |
| 34 | +Use the `useScaffoldReadContract` (`packages/nextjs/hooks/scaffold-eth/useScaffoldReadContract.ts`) hook. Example: |
| 35 | + |
| 36 | +```typescript |
| 37 | +const { data: someData } = useScaffoldReadContract({ |
| 38 | + contractName: "YourContract", |
| 39 | + functionName: "functionName", |
| 40 | + args: [arg1, arg2], // optional |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +### Writing data to a contract |
| 45 | +Use the `useScaffoldWriteContract` (`packages/nextjs/hooks/scaffold-eth/useScaffoldWriteContract.ts`) hook. |
| 46 | +1. Initilize the hook with just the contract name |
| 47 | +2. Call the `writeContractAsync` function. |
| 48 | + |
| 49 | + Example: |
| 50 | + |
| 51 | +```typescript |
| 52 | +const { writeContractAsync: writeYourContractAsync } = useScaffoldWriteContract( |
| 53 | + { contractName: "YourContract" } |
| 54 | +); |
| 55 | + |
| 56 | +// Usage (this will send a write transaction to the contract) |
| 57 | +await writeContractAsync({ |
| 58 | + functionName: "functionName", |
| 59 | + args: [arg1, arg2], // optional |
| 60 | + value: parseEther("0.1"), // optional, for payable functions |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +Never use any other patterns for contract interaction. The hooks are: |
| 65 | + |
| 66 | +- useScaffoldReadContract (for reading) |
| 67 | +- useScaffoldWriteContract (for writing) |
| 68 | + |
| 69 | +### Other Hooks |
| 70 | +SE-2 also provides other hooks to interact with blockchain data: `useScaffoldWatchContractEvent`, `useScaffoldEventHistory`, `useDeployedContractInfo`, `useScaffoldContract`, `useTransactor`. They live under `packages/nextjs/hooks/scaffold-eth`. |
| 71 | + |
| 72 | +## Display Components guidelines |
| 73 | +SE-2 provides a set of pre-built React components for common Ethereum use cases: |
| 74 | +- `Address`: Always use this when displaying an ETH address |
| 75 | +- `AddressInput`: Always use this when users need to input an ETH address |
| 76 | +- `Balance`: Display the ETH/USDC balance of a given address |
| 77 | +- `EtherInput`: An extended number input with ETH/USD conversion. |
| 78 | + |
| 79 | +They live under `packages/nextjs/components/scaffold-eth`. |
| 80 | + |
| 81 | +Find the relevant information from the documentation and the codebase. Think step by step before answering the question. |
0 commit comments