Skip to content

Commit

Permalink
Seed data script and example (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Feb 7, 2024
1 parent 2d0bea5 commit e0556e7
Show file tree
Hide file tree
Showing 12 changed files with 706 additions and 33 deletions.
63 changes: 37 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,52 @@ To get started follow the steps below:

1. Clone this repo & install dependencies

```
git clone https://github.com/BuidlGuidl/grants.buidlguidl.com.git
cd grants.buidlguidl.com
yarn install
```
```
git clone https://github.com/BuidlGuidl/grants.buidlguidl.com.git
cd grants.buidlguidl.com
yarn install
```

2. Set up your environment variables (and optionally, a local Firebase instance):
Copy the `packages/nextjs/.env.example` file to `packages/nextjs/.env.local` and fill in the required environment variables.
Copy the `packages/nextjs/.env.example` file to `packages/nextjs/.env.local` and fill in the required environment variables.

(Optional) Start the firebase emulators (vs set up a live Firebase instance). You will need to install the [firebase CLI](https://firebase.google.com/docs/cli#install_the_firebase_cli) and run the following command:
```bash
# You might need to add a real "--project <projectName>" (run firebase projects:list)
firebase emulators:start
```
(Optional) Start the firebase emulators (vs set up a live Firebase instance). You will need to install the [firebase CLI](https://firebase.google.com/docs/cli#install_the_firebase_cli) and run the following command:

3. Run a local network in the first terminal:
```bash
# You might need to add a real "--project <projectName>" (run firebase projects:list)
firebase emulators:start
```

```
yarn chain
```
3. Seed data in your local Firebase instance:

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`.
Copy the `packages/local_db/seed.sample.json` to `packages/local_db/seed.json` and tweak the data as you see fit. Then run the following command:

4. On a second terminal, deploy the test contract:
```bash
yarn seed
```

```
yarn deploy
```
To seed it to empty _*live*_ firestore instance you can use `yarn seed --force-prod`. If there is data in the live instance, it will not seed it again to bypass it use `yarn seed --reset --force-prod`

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. Run a local network in the first terminal:

4. On a third terminal, start your NextJS app:
```bash
yarn chain
```

```
yarn start
```
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`.

Visit your app on: `http://localhost:3000`. You can interact with your smart contract using the `/debug` page. You can tweak the app config in `packages/nextjs/scaffold.config.ts`.
5. 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. 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.

6. 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 `/debug` page. You can tweak the app config in `packages/nextjs/scaffold.config.ts`.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"workspaces": {
"packages": [
"packages/hardhat",
"packages/nextjs"
"packages/nextjs",
"packages/local_db"
]
},
"scripts": {
Expand All @@ -28,7 +29,8 @@
"postinstall": "husky install",
"precommit": "lint-staged",
"vercel": "yarn workspace @se-2/nextjs vercel",
"vercel:yolo": "yarn workspace @se-2/nextjs vercel:yolo"
"vercel:yolo": "yarn workspace @se-2/nextjs vercel:yolo",
"seed": "yarn workspace @se-2/local_db seed"
},
"packageManager": "[email protected]",
"devDependencies": {
Expand Down
21 changes: 21 additions & 0 deletions packages/local_db/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# dependencies
/node_modules

# build
/dist

# misc
.DS_Store

# local env files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# typescript
*.tsbuildinfo

# seed
seed.json
24 changes: 24 additions & 0 deletions packages/local_db/firestoreDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { applicationDefault, getApps, initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";

const getFirestoreConnector = () => {
if (getApps().length > 0) {
return getFirestore();
}

if (process.env.GOOGLE_APPLICATION_CREDENTIALS) {
console.log("Initializing LIVE Firestore");
initializeApp({
credential: applicationDefault(),
});
} else {
console.log("Initializing local Firestore instance");
initializeApp({
projectId: process.env.FIREBASE_PROJECT_ID,
});
}

return getFirestore();
};

export { getFirestoreConnector };
34 changes: 34 additions & 0 deletions packages/local_db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as dotenv from "dotenv";
dotenv.config({ path: "../nextjs/.env.local" });

import { getFirestoreConnector } from "./firestoreDB.js";
import { importSeed } from "./utils.js";

const seedData = async () => {
const firestoreDB = getFirestoreConnector();

const args = process.argv.slice(2);
const flags = args.filter((arg) => arg.startsWith("--"));
const isForceProd = flags.includes("--force-prod");

if (process.env.GOOGLE_APPLICATION_CREDENTIALS) {
console.log("Connected to LIVE Firestore");
if (!isForceProd) {
console.log("To update Live firestore use `yarn seed --force-prod`");
console.log("Exiting...");
return;
}
}

try {
await importSeed(firestoreDB);
} catch (error) {
console.error("Error seeding the data:", error);
}
};

try {
seedData();
} catch (error) {
console.error("Error initializing seed data:", error);
}
23 changes: 23 additions & 0 deletions packages/local_db/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@se-2/local_db",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"seed": "tsx index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.4.1",
"firebase-admin": "^11.11.1"
},
"devDependencies": {
"@types/node": "^20.11.16",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
}
}
173 changes: 173 additions & 0 deletions packages/local_db/seed.sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"version": "1.1.1",
"users": {
"0x60583563D5879C2E59973E5718c7DE2147971807": {
"creationTimestamp": 1633088553221,
"role": "admin",
"ens": "carletex.eth",
"function": "damageDealer",
"status": {
"text": "My Status",
"timestamp": 1657208443047
},
"builderCohort": [
{
"id": "0x2eA63c9C9C114ae85b1027697A906420a23e8572",
"name": "Sand Garden",
"url": "https://sandgarden.buidlguidl.com"
}
],
"stream": {
"streamAddress": "0xCC756617C97a05aC472fD8Ab92C88F2A9cb681EC",
"streamAddress_rinkeby": "0xCC756617C97a05aC472fD8Ab92C88F2A9cb681EC",
"streamAddress_localhost": "0x5FbDB2315678afecb367f032d93F642f64180aa3",
"cap": "0.5",
"frequency": 2592000,
"lastContract": 1654720543,
"lastIndexedBlock": 10983432,
"balance": "0.21"
},
"builds": [
{
"id": "06bb3c9d-784b-44d1-b7e2-1ae5affdfa8b",
"submittedTimestamp": 1658587661752
}
]
},
"0x70997970C51812dc3A010C7d01b50e0d17dc79C8": {
"creationTimestamp": 1633088553223,
"role": "builder",
"function": "damageDealer",
"socialLinks": {
"telegram": "test",
"twitter": "test"
},
"builderCohort": [
{
"id": "0x2eA63c9C9C114ae85b1027697A906420a23e8572",
"name": "Sand Garden",
"url": "https://sandgarden.buidlguidl.com"
},
{
"id": "0xaCc9Cc4983D57cea0748B8CD1Adb87Ada5b1a67c",
"name": "Not Just Notfellows",
"url": "https://not-just-notfellows.buidlguidl.com/"
}
],
"stream": {
"streamAddress": "0x5FbDB2315678afecb367f032d93F642f64180aa3",
"cap": "0.5",
"frequency": 2592000,
"lastContract": 1654720543,
"lastIndexedBlock": 10983432,
"balance": "0.5"
}
},
"0x34aA3F359A9D614239015126635CE7732c18fDF3": {
"creationTimestamp": 1648653481405,
"role": "builder",
"function": "knight",
"ens": "austingriffith.eth"
},
"0x72fC8b63692B516D0D8748d469faB65C7e4e7389": {
"creationTimestamp": 1648653501156,
"role": "builder",
"function": "cadets",
"ensClaimData": {
"submittedTimestamp": 1648736241913,
"provided": true
}
}
},
"builds": {
"06bb3c9d-784b-44d1-b7e2-1ae5affdfa8b": {
"branch": "https://github.com/moonshotcollective/scaffold-directory",
"demoUrl": "https://speedrunethereum.com/",
"videoUrl": "",
"desc": "👩‍🏫 Learn how to build on Ethereum completing the scaffold-eth challenges.",
"image": "https://storage.googleapis.com/download/storage/v1/b/buidlguidl-v3.appspot.com/o/builds%2F0032da28a08871cbcb2922b00.png?generation=1647284971917513&alt=media",
"name": "🏃‍♀️ SpeedRunEthereum.com",
"builder": "0x60583563D5879C2E59973E5718c7DE2147971807",
"featured": false,
"submittedTimestamp": 1658587661752,
"likes": ["0x60583563D5879C2E59973E5718c7DE2147971807"]
}
},
"events": [
{
"type": "build.submit",
"timestamp": 1658587661754,
"signature": "0x206e5f766fb427599acacb41d45dfb794d95076f3af8519008328cef9c6bf3b4137a87ba73368f1e16b19674edddb6860093e601e6576c1b427fcc8e7ffba4cb1b",
"payload": {
"userAddress": "0x60583563D5879C2E59973E5718c7DE2147971807",
"buildUrl": "https://github.com/moonshotcollective/scaffold-directory",
"name": "🏃‍♀️ SpeedRunEthereum.com",
"buildId": "06bb3c9d-784b-44d1-b7e2-1ae5affdfa8b"
}
},
{
"type": "stream.withdraw",
"timestamp": 1660983774000,
"payload": {
"userAddress": "0x60583563D5879C2E59973E5718c7DE2147971807",
"amount": "0.5",
"block": "13845820",
"reason": "Test withdraw",
"streamAddress": "0xCC756617C97a05aC472fD8Ab92C88F2A9cb681EC",
"tx": "0x6be7d0065eb1ec0f5e9296b82b527e4b1b8fec91d890066d43857044443a9969"
}
}
],
"cohorts": {
"0x2eA63c9C9C114ae85b1027697A906420a23e8572": {
"chainId": 10,
"name": "Sand Garden",
"url": "https://sandgarden.buidlguidl.com"
},
"0x2Be18e07C7be0a2CC408C9E02C90203B2052D7DE": {
"chainId": 1,
"name": "Jessy's Hacker House",
"url": "https://hackerhouse.buidlguidl.com/"
}
},
"config": {
"streams": {
"lastIndexedBlock": 10616289
}
},
"notifications": [
{
"criteria": {
"minBuilds": 1,
"daysJoinedBefore": 30,
"hasStream": true
},
"title": "New cohort coming",
"content": "Some 'New cohort coming' **markdown** content with [links](https://buidlguidl.com)",
"active": true
},
{
"criteria": {
"daysJoinedAfter": 2
},
"title": "Onboarding Batch!",
"content": "Some 'Onboarding Batch' **markdown** content with [links](https://buidlguidl.com)",
"active": true
},
{
"criteria": {
"hasStream": true
},
"component": "OnboardingBatch",
"active": true
},
{
"criteria": {
"hasStream": true
},
"title": "Announment for stremead builders",
"content": "Some 'Announment for stremead builders' **markdown** content with [links](https://buidlguidl.com)",
"active": false
}
]
}
17 changes: 17 additions & 0 deletions packages/local_db/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"skipLibCheck": true,
"target": "es2022",
"allowJs": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"isolatedModules": true,
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"module": "NodeNext",
"outDir": "dist",
"exclude": ["node_modules", "dist"]
}
}
Loading

0 comments on commit e0556e7

Please sign in to comment.