Skip to content

Commit

Permalink
Database set up
Browse files Browse the repository at this point in the history
  • Loading branch information
carletex committed Dec 23, 2024
1 parent ed8b6f8 commit 32cf3c0
Show file tree
Hide file tree
Showing 14 changed files with 1,409 additions and 56 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ node_modules
.idea

# cli
dist
dist

data/db
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
73 changes: 27 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,61 @@
# 🏗 Scaffold-ETH 2

<h4 align="center">
<a href="https://docs.scaffoldeth.io">Documentation</a> |
<a href="https://scaffoldeth.io">Website</a>
</h4>

🧪 An open-source, up-to-date toolkit for building decentralized applications (dapps) on the Ethereum blockchain. It's designed to make it easier for developers to create and deploy smart contracts and build user interfaces that interact with those contracts.

⚙️ Built using NextJS, RainbowKit, Hardhat, Wagmi, Viem, and Typescript.

-**Contract Hot Reload**: Your frontend auto-adapts to your smart contract as you edit it.
- 🪝 **[Custom hooks](https://docs.scaffoldeth.io/hooks/)**: Collection of React hooks wrapper around [wagmi](https://wagmi.sh/) to simplify interactions with smart contracts with typescript autocompletion.
- 🧱 [**Components**](https://docs.scaffoldeth.io/components/): Collection of common web3 components to quickly build your frontend.
- 🔥 **Burner Wallet & Local Faucet**: Quickly test your application with a burner wallet and local faucet.
- 🔐 **Integration with Wallet Providers**: Connect to different wallet providers and interact with the Ethereum network.

![Debug Contracts tab](https://github.com/scaffold-eth/scaffold-eth-2/assets/55535804/b237af0c-5027-4849-a5c1-2e31495cccb1)
# Start Ethereum

## Requirements

Before you begin, you need to install the following tools:

- [Node (>= v18.18)](https://nodejs.org/en/download/)
- [Node (>= v18.17)](https://nodejs.org/en/download/)
- Yarn ([v1](https://classic.yarnpkg.com/en/docs/install/) or [v2+](https://yarnpkg.com/getting-started/install))
- [Git](https://git-scm.com/downloads)
- [Docker Engine](https://docs.docker.com/engine/install/)

## Quickstart
## Development Quickstart

To get started with Scaffold-ETH 2, follow the steps below:

1. Install dependencies if it was skipped in CLI:
1. Install dependencies

```
cd my-dapp-example
yarn install
```

2. Run a local network in the first terminal:
2. Spin up the Postgres database service + create database + seed

```
yarn chain
docker compose up -d
yarn drizzle-kit push
yarn db:seed
```

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 `packages/hardhat/hardhat.config.ts`.
See more info about the database in the section below

3. On a second terminal, deploy the test contract:
3. Run a local network in the first terminal:

```
yarn deploy
yarn chain
```

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. The `yarn deploy` command uses the deploy script located in `packages/hardhat/deploy` to deploy the contract to the network. You can also customize the deploy script.

4. On a third terminal, start your NextJS app:
4. On a second terminal, deploy the contract:

```
yarn start
yarn deploy
```

Visit your app on: `http://localhost:3000`. You can interact with your smart contract using the `Debug Contracts` page. You can tweak the app config in `packages/nextjs/scaffold.config.ts`.

Run smart contract test with `yarn hardhat:test`

- Edit your smart contracts in `packages/hardhat/contracts`
- Edit your frontend homepage at `packages/nextjs/app/page.tsx`. For guidance on [routing](https://nextjs.org/docs/app/building-your-application/routing/defining-routes) and configuring [pages/layouts](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts) checkout the Next.js documentation.
- Edit your deployment scripts in `packages/hardhat/deploy`
5. On a third terminal, start your NextJS app:

```
yarn start
```

## Documentation
Visit your app on: `http://localhost:3000`.

Visit our [docs](https://docs.scaffoldeth.io) to learn how to start building with Scaffold-ETH 2.
## Database (dev info)

To know more about its features, check out our [website](https://scaffoldeth.io).
We are using Drizzle with Postgres. You can run `drizzle-kit` from the root with `yarn drizzle-kit`

## Contributing to Scaffold-ETH 2
To iterate on the database:

We welcome contributions to Scaffold-ETH 2!
- Tweak the schema in `schema.ts`
- Run `yarn drizzle-kit push` to apply the changes.
- Tweak `seed.js` if needed + run `yarn seed` (will delete existing data)
- You can explore the database with `yarn drizzle-kit studio`

Please see [CONTRIBUTING.MD](https://github.com/scaffold-eth/scaffold-eth-2/blob/main/CONTRIBUTING.md) for more information and guidelines for contributing to Scaffold-ETH 2.
After the initial iterations, we can start using migrations (`yarn drizzle-kit generate` & `yarn drizzle-kit migrate`)
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3"
services:
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: mysecretpassword
ports:
- "5432:5432"
volumes:
- ./data/db:/var/lib/postgresql/data
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"account:import": "yarn workspace @se-2/hardhat account:import",
"chain": "yarn hardhat:chain",
"compile": "yarn hardhat:compile",
"db:seed": "yarn workspace @se-2/nextjs db:seed",
"deploy": "yarn hardhat:deploy",
"drizzle-kit": "yarn workspace @se-2/nextjs drizzle-kit",
"fork": "yarn hardhat:fork",
"format": "yarn next:format && yarn hardhat:format",
"generate": "yarn account:generate",
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_URL="postgresql://postgres:mysecretpassword@localhost:5432/postgres"
148 changes: 147 additions & 1 deletion packages/nextjs/contracts/deployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,152 @@
*/
import { GenericContractsDeclaration } from "~~/utils/scaffold-eth/contract";

const deployedContracts = {} as const;
const deployedContracts = {
31337: {
YourContract: {
address: "0x5FbDB2315678afecb367f032d93F642f64180aa3",
abi: [
{
inputs: [
{
internalType: "address",
name: "_owner",
type: "address",
},
],
stateMutability: "nonpayable",
type: "constructor",
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "greetingSetter",
type: "address",
},
{
indexed: false,
internalType: "string",
name: "newGreeting",
type: "string",
},
{
indexed: false,
internalType: "bool",
name: "premium",
type: "bool",
},
{
indexed: false,
internalType: "uint256",
name: "value",
type: "uint256",
},
],
name: "GreetingChange",
type: "event",
},
{
inputs: [],
name: "greeting",
outputs: [
{
internalType: "string",
name: "",
type: "string",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "owner",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "premium",
outputs: [
{
internalType: "bool",
name: "",
type: "bool",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "string",
name: "_newGreeting",
type: "string",
},
],
name: "setGreeting",
outputs: [],
stateMutability: "payable",
type: "function",
},
{
inputs: [],
name: "totalCounter",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
name: "userGreetingCounter",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "withdraw",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
stateMutability: "payable",
type: "receive",
},
],
inheritedFunctions: {},
},
},
} as const;

export default deployedContracts satisfies GenericContractsDeclaration;
13 changes: 13 additions & 0 deletions packages/nextjs/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as dotenv from "dotenv";
import { defineConfig } from "drizzle-kit";

dotenv.config({ path: ".env.development" });

export default defineConfig({
schema: "./services/database/config/schema.ts",
out: "./services/database/migrations",
dialect: "postgresql",
dbCredentials: {
url: process.env.POSTGRES_URL as string,
},
});
9 changes: 9 additions & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"scripts": {
"build": "next build",
"check-types": "tsc --noEmit --incremental",
"db:seed": "tsx services/database/seed.ts",
"dev": "next dev",
"drizzle-kit": "drizzle-kit",
"format": "prettier --write . '!(node_modules|.next|contracts)/**/*'",
"lint": "next lint",
"serve": "next start",
Expand All @@ -19,12 +21,16 @@
"@tanstack/react-query": "~5.59.15",
"@uniswap/sdk-core": "~5.8.2",
"@uniswap/v2-sdk": "~4.6.1",
"@vercel/postgres": "^0.10.0",
"blo": "~1.2.0",
"burner-connector": "~0.0.8",
"daisyui": "4.12.10",
"dotenv": "^16.4.7",
"drizzle-orm": "^0.38.2",
"next": "~14.2.11",
"next-nprogress-bar": "~2.3.13",
"next-themes": "~0.3.0",
"pg": "^8.13.1",
"qrcode.react": "~4.0.1",
"react": "~18.3.1",
"react-copy-to-clipboard": "~5.1.0",
Expand All @@ -38,17 +44,20 @@
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "~4.3.0",
"@types/node": "~18.19.50",
"@types/pg": "^8.11.10",
"@types/react": "~18.3.5",
"@typescript-eslint/eslint-plugin": "~5.40.0",
"abitype": "1.0.6",
"autoprefixer": "~10.4.20",
"drizzle-kit": "^0.30.1",
"eslint": "~8.57.1",
"eslint-config-next": "~14.2.15",
"eslint-config-prettier": "~8.10.0",
"eslint-plugin-prettier": "~5.2.1",
"postcss": "~8.4.45",
"prettier": "~3.3.3",
"tailwindcss": "~3.4.11",
"tsx": "^4.19.2",
"type-fest": "~4.26.1",
"typescript": "<5.6.0",
"vercel": "~39.1.3"
Expand Down
26 changes: 26 additions & 0 deletions packages/nextjs/services/database/config/postgresClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as schema from "./schema";
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/node-postgres";
import { drizzle as drizzleVercel } from "drizzle-orm/vercel-postgres";
import { Pool } from "pg";

let db: ReturnType<typeof drizzle<typeof schema>> | ReturnType<typeof drizzleVercel<typeof schema>>;

const VERCEL_DB_STRING = "verceldb";

if (process.env.POSTGRES_URL?.includes(VERCEL_DB_STRING)) {
db = drizzleVercel(sql, { schema });
} else {
const pool = new Pool({
connectionString: process.env.POSTGRES_URL,
});

db = drizzle(pool, { schema });

pool.on("error", err => {
console.error("Unexpected error on idle client", err);
process.exit(-1);
});
}

export { db };
8 changes: 8 additions & 0 deletions packages/nextjs/services/database/config/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { sql } from "drizzle-orm";
import { pgTable, timestamp, varchar } from "drizzle-orm/pg-core";

// TODO: Define the right schema.
export const users = pgTable("users", {
id: varchar("id", { length: 42 }).primaryKey(),
creationTimestamp: timestamp("creation_timestamp").default(sql`now()`),
});
Loading

0 comments on commit 32cf3c0

Please sign in to comment.