- Setup Scaffold-ETH 2
- Demo of Scaffold-ETH 2
- Solidity by Example
- Install Metamask
- SpeedRunEthereum
- 🚩 Challenge 0: 🎟 Simple NFT Example
- Setup Scaffold-ETH 2 branch with The Graph Integration
- Demo of The Graph integration
- Extra Credit - Adding Additional Events
- Extra Extra Credit - Querying from an Application
- Extra Credit - Build out a UI with Scaffold-ETH 2
Before you begin, you need to install the following tools:
- Node (v18 LTS)
- Yarn (v1 or v2+)
- Git
- Docker
To get started with Scaffold-ETH 2, follow the steps below:
- Clone this repo & install dependencies
git clone https://github.com/scaffold-eth/scaffold-eth-2.git
cd scaffold-eth-2
yarn install
- Run a local network in the first terminal:
yarn chain
This command starts a local Ethereum network using Hardhat. The network runs on your local machine and can be used for testing and development. You can customize the network configuration in
hardhat.config.ts
.
- On a second terminal, deploy the test contract:
yarn deploy
This command deploys a test smart contract to the local network. The contract is located in
packages/hardhat/contracts
and can be modified to suit your needs. Theyarn deploy
command uses the deploy script located inpackages/hardhat/deploy
to deploy the contract to the network. You can also customize the deploy script.
- On a third terminal, start your NextJS app:
yarn start
Visit your app on:
http://localhost:3000
. You can interact with your smart contract using the contract component or the example ui in the frontend. You can tweak the app config inpackages/nextjs/scaffold.config.ts
.
Run smart contract test with
yarn hardhat:test
- Edit your smart contract
YourContract.sol
inpackages/hardhat/contracts
- Edit your frontend in
packages/nextjs/pages
- Edit your deployment scripts in
packages/hardhat/deploy
Steps will be added later here...
- Incorporate the following variables and functions into your smart contract.
uint public count;
// Function to get the current count
function get() public view returns (uint) {
return count;
}
// Function to increment count by 1
function inc() public {
count += 1;
}
// Function to decrement count by 1
function dec() public {
// This function will fail if count = 0
count -= 1;
}
- Redploy and test.
First, we will start out with a special build of Scaffold-ETH 2 written by Simon from Edge and Node… Thanks Simon! 🫡
git clone -b subgraph-package \
https://github.com/scaffold-eth/scaffold-eth-2.git \
scaffold-eth-2-subgraph-package
Once you have this checked out on your machine, navigate into the directory and install all of the dependencies using yarn.
cd scaffold-eth-2-subgraph-package && \
yarn install
Next, we will want to start up our local blockchain so that we can eventually deploy and test our smart contracts. Scaffold-ETH 2 comes with Hardhat by default. To spin up the chain just type the following yarn command…
yarn chain
You will keep this window up and available so that you can see any output from hardhat console. 🖥️
Next we are going to spin up our frontend application. Scaffold-ETH 2 comes with NextJS by default and also can be started with a simple yarn command. You will need to open up a new command line and type the following…
yarn start
You will also want to keep this window up at all times so that you can debug any code changes you make to NextJS, debug performance or just check that the server is running properly.
Next, you will want to open up a third window where you can deploy your smart contract, along with some other useful commands found in Scaffold-ETH. To do a deploy you can simply run the following…
yarn deploy
You should get a tx along with an address and amount of gas spent on the deploy. ⛽
If you navigate to http://localhost:3000 you should see the NextJS application. Explore the menus and features of Scaffold-ETH 2! Someone call in an emergency, cause hot damn that is fire! 🔥
Now that we have spun up our blockchain, started our frontend application and deployed our smart contract, we can start setting up our subgraph and utilize The Graph!
- First, navigate into the subgraph directory. ⌨️
cd packages/subgraph
- Next, you will want to run the following to clean up any old data. Do this if you need to reset everything.
yarn clean-node
- We can now spin up a graph node by running the following command… 🧑🚀
yarn run-node
This will spin up all the containers for The Graph using docker-compose. You will want to keep this window open at all times so that you can see log output from Docker.
Note: If you are running Docker Desktop on Mac you might encounter the following errors… If so please checkout this fix below.
CRIT Database does not use C locale. Please check the graph-node documentation for how to set up the database locale, error: database collation is `en_US.utf8` but must be `C`, pool: main, shard: primary, component: ConnectionPool thread 'tokio-runtime-worker' panicked at 'Database does not use C locale. Please check the graph-node documentation for how to set up the database locale: database collation is `en_US.utf8` but must be `C`', store/postgres/src/connection_pool.rs:976:13 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: JoinError::Panic(...)', /graph-node/store/postgres/src/connection_pool.rs:484:10
To fix this, stop the existing node and clean up the data. 🧹
yarn stop-node && yarn clean-node
Then add the following to the environment section of your docker-compose.yml file.
# workaround for https://github.com/docker/for-mac/issues/6270?
PGDATA: "/var/lib/postgresql/data"
POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C"
Then go ahead and start it up again.
yarn run-node
As stated before, be sure to keep this window open so that you can see any log output from Docker. 🔎
Now we can open up a fourth window to finish setting up The Graph. 😅
- In this forth window we will create our local subgraph! You will only need to do this once.
yarn local-create
You should see some output stating your Subgraph has been created along with a log output on your graph-node inside docker.
- Next we will ship our subgraph! You will need to give your subgraph a version after executing this command. (e.g. 0.0.1).
yarn local-ship
If you get an error ts-node is missing run
npm install -g ts-node
This command does the following all in one… 🚀🚀🚀
- Copies the contracts ABI from the hardhat/deployments folder
- Generates the networks.json file
- Generates AssemblyScript types from the subgraph schema and the contract ABIs.
- Compiles and checks the mapping functions.
- … and deploy a local subgraph!
You should get a build completed output along with the address of your Subgraph endpoint.
Build completed: QmYdGWsVSUYTd1dJnqn84kJkDggc2GD9RZWK5xLVEMB9iP
Deployed to http://localhost:8000/subgraphs/name/scaffold-eth/your-contract/graphql
Subgraph endpoints:
Queries (HTTP): http://localhost:8000/subgraphs/name/scaffold-eth/your-contract
Go ahead and head over to your subgraph endpoint and take a look!
Here is an example query…
{
greetings(first: 25, orderBy: createdAt, orderDirection: desc) {
id
greeting
premium
value
createdAt
sender {
address
greetingCount
}
}
}
If all is well and you’ve sent a transaction to your smart contract then you will see a similar data output!
Next up we will dive into a bit more detail on how The Graph works so that as you start adding events to your smart contract you can start indexing and parsing the data you need for your front end application.
Scroll down to "Adding Additional Events"
Scroll down to "Querying from an Application"
Open up main index.tsx file located in packages/nextjs