Skip to content

Commit

Permalink
Merge pull request #25 from vechain-energy/main
Browse files Browse the repository at this point in the history
Add quick start example
  • Loading branch information
pierobassa authored Sep 13, 2024
2 parents f3aac60 + bae693a commit da3504a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 13 deletions.
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
```
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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
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: '' }),
});
};

0 comments on commit da3504a

Please sign in to comment.