Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quick start example #25

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ dist-ssr
.cache
server/dist
public/dist
.idea
.idea
.yarn
.pnp.*js
37 changes: 37 additions & 0 deletions QuickStart.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Development Environment

Get a local development environment up and running with as few steps as possible.

## Required Variables

- **OPENAI_API_KEY:** An OpenAI API key needs to be obtained manually from [OpenAI](https://platform.openai.com/api-keys):

## Start

```shell
yarn set version classic
yarn install
yarn contracts:solo-up
yarn contracts:deploy:solo

OPENAI_API_KEY="" \
ADMIN_MNEMONIC="denial kitchen pet squirrel other broom bar gas better priority spoil cross" \
yarn dev
```

## Result

Link | Service
--- | ---
http://localhost:8082 | Frontend
http://localhost:3000 | Backend
http://localhost:8081 | Inspector
http://localhost:8669 | Solo Thor

Deployed contracts documented in: [packages/config-contract/config.ts](packages/config-contract/config.ts)

## Shutdown

```shell
yarn contracts:solo-down
```
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
##############
#########

> [!TIP]
> Impatient? Check out the [QuickStart](./QuickStart.md) to instantly run the app template.

Unlock the potential of decentralized application development on Vechain with our X-App template for VeBetterDAO. Designed for the Vechain Thor blockchain, this template integrates cutting-edge technologies such as React, TypeScript, Hardhat, and Express, ensuring a seamless and efficient DApp development experience. 🌟

Read more about the implementation and key features of this template in our [Developer Docs](https://docs.vebetterdao.org/developer-guides/integration-examples/pattern-2-use-smart-contracts-and-backend).
Expand Down Expand Up @@ -110,7 +113,7 @@ yarn contracts:deploy:solo
Once the deployment is completed successfully you can go ahead and start the frontend and backend:

> ⚠️ **Warning:**
> Remeber to set the OPENAI_API_KEY env variable in the backend .env.development.local file. Refer to the [Environment Variables](#environment-variables) section for more information.
> Remember to set the OPENAI_API_KEY env variable in the backend .env.development.local file. Refer to the [Environment Variables](#environment-variables) section for more information.

```bash
yarn dev
Expand Down
16 changes: 10 additions & 6 deletions apps/backend/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { config } from 'dotenv';
import { mnemonic } from '@vechain/sdk-core';
import { ValidateEnv } from '@utils/validateEnv';
config({ path: `.env.${process.env.NODE_ENV || 'development'}.local` });

const validatedEnv = ValidateEnv();

export const CREDENTIALS = process.env.CREDENTIALS === 'true';
export const { NODE_ENV, PORT, LOG_FORMAT, LOG_DIR, ORIGIN } = process.env;
export const { OPENAI_API_KEY } = process.env;
export const { MAX_FILE_SIZE } = process.env;
export const { ADMIN_MNEMONIC, ADMIN_ADDRESS } = process.env;
export const { NETWORK_URL, NETWORK_TYPE } = process.env;
export const { REWARD_AMOUNT } = process.env;
export const { NODE_ENV, PORT, LOG_FORMAT, LOG_DIR, ORIGIN } = validatedEnv;

export const { OPENAI_API_KEY } = validatedEnv;
export const { MAX_FILE_SIZE } = validatedEnv;
export const { ADMIN_MNEMONIC, ADMIN_ADDRESS } = validatedEnv;
export const { NETWORK_URL, NETWORK_TYPE } = validatedEnv;
export const { REWARD_AMOUNT } = validatedEnv;

export const ADMIN_PRIVATE_KEY = mnemonic.derivePrivateKey(ADMIN_MNEMONIC.split(' '));
3 changes: 0 additions & 3 deletions apps/backend/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { App } from '@/app';
import { ValidateEnv } from '@utils/validateEnv';
import { initializeOpenAI } from './utils/initializeOpenAI';
import { SubmissionRoute } from './routes/submission.route';

ValidateEnv();

export const openAIHelper = initializeOpenAI();

const app = new App([new SubmissionRoute()]);
Expand Down
24 changes: 21 additions & 3 deletions apps/backend/src/utils/validateEnv.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import { cleanEnv, port, str } from 'envalid';
import { makeValidator, cleanEnv, port, str } from 'envalid';

const openApiKey = makeValidator((apiKey: string) => {
if (/^sk-proj-.{100,}$/.test(apiKey)) {
return apiKey;
} else {
throw new Error('Please obtain a valid OpenAPI-Key from https://platform.openai.com/api-keys');
}
});

export const ValidateEnv = () => {
cleanEnv(process.env, {
return cleanEnv(process.env, {
NODE_ENV: str(),
PORT: port(),
PORT: port({ devDefault: 3000 }),
ORIGIN: str({ devDefault: '*' }),
LOG_FORMAT: str({ devDefault: 'prod' }),
LOG_DIR: str({ devDefault: '../logs' }),
REWARD_AMOUNT: str({ devDefault: '1' }),
ADMIN_MNEMONIC: str(),
NETWORK_URL: str({ devDefault: 'http://localhost:8669' }),
NETWORK_TYPE: str({ devDefault: 'solo' }),
OPENAI_API_KEY: openApiKey(),
MAX_FILE_SIZE: str({ devDefault: '10mb' }),
ADMIN_ADDRESS: str({ default: '' }),
});
};
Loading