Skip to content

Commit

Permalink
Merge pull request #64 from bludnic/moon
Browse files Browse the repository at this point in the history
Migrate to Moonrepo
  • Loading branch information
bludnic authored Oct 5, 2024
2 parents bfb3279 + d0a7cec commit af07d14
Show file tree
Hide file tree
Showing 78 changed files with 1,383 additions and 445 deletions.
11 changes: 11 additions & 0 deletions .changeset/four-papayas-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"opentrader": minor
"frontend": minor
"processor": minor
---

- Migrated to Moonrepo
- Added DTS in the NPM package
- Use Docker Build Cloud
- Improve CONTRIBUTING.md
- Fixed missing return type in `exchange.placeMarketOrder` method
1 change: 1 addition & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"flat-moose-rush",
"flat-steaks-grow",
"fluffy-experts-raise",
"four-papayas-hear",
"friendly-kids-pay",
"gentle-peaches-destroy",
"healthy-ducks-listen",
Expand Down
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.next
.dist
.env
.moon/cache
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.idea
node_modules
.DS_Store
.turbo
playground
.env
.env.production
Expand All @@ -11,6 +10,7 @@ exchanges.json5

# Release build for publishing on NPM
/release
/dts

# Prisma SQLite database files
dev.db
Expand All @@ -20,3 +20,10 @@ dev.db-journal
/out

/k8s

# TS
tsconfig.tsbuildinfo

# moon
.moon/cache
.moon/docker
11 changes: 11 additions & 0 deletions .moon/toolchain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$schema: "https://moonrepo.dev/schemas/toolchain.json"

node:
version: "20.18.0"
packageManager: "pnpm"
pnpm:
version: "9.12.0"

typescript:
syncProjectReferences: true
syncProjectReferencesToPaths: true
30 changes: 30 additions & 0 deletions .moon/workspace.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# https://moonrepo.dev/docs/config/workspace
$schema: "https://moonrepo.dev/schemas/workspace.json"

# Require a specific version of moon while running commands, otherwise fail.
# versionConstraint: '>=1.0.0'

# Extend and inherit an external configuration file. Must be a valid HTTPS URL or file system path.
# extends: './shared/workspace.yml'

# REQUIRED: A map of all projects found within the workspace, or a list or file system globs.
# When using a map, each entry requires a unique project ID as the map key, and a file system
# path to the project folder as the map value. File paths are relative from the workspace root,
# and cannot reference projects located outside the workspace boundary.
projects:
- "apps/*"
- "packages/*"
- "pro/*"

# Configures the version control system to utilize within the workspace. A VCS
# is required for determining touched (added, modified, etc) files, calculating file hashes,
# computing affected files, and much more.
vcs:
# The client to use when managing the repository.
# Accepts "git". Defaults to "git".
manager: "git"

# The default branch (master/main/trunk) in the repository for comparing the
# local branch against. For git, this is is typically "master" or "main",
# and must include the remote prefix (before /).
defaultBranch: "dev"
27 changes: 9 additions & 18 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ node -v
# `pnpm` must be installed
pnpm -v

# Install Turborepo globally
pnpm install turbo --global
# `moonrepo` must be installed
moon --version

# Docker (optional)
docker -v
```

> [!WARNING]
> Due to a [bug](https://github.com/bludnic/opentrader/issues/53) in turbo that makes the Frontend app inaccessible in the browser, please use version `2.0.9` as a workaround.
```bash
pnpm i -g [email protected]
```

## Setup

1. Clone the repository:
Expand Down Expand Up @@ -58,27 +51,27 @@ pnpm install
2. Run database migrations

```bash
turbo run prisma:migrate
moon run prisma:migrate
```

3. Seed the database

```bash
turbo run prisma:seed
moon run prisma:seed
```

4. Build the project

```bash
turbo run build
moon run :build
```

# Development

## CLI

You can interact with the bot by using CLI.
If you made changes in the code, don't forget to rebuild the project `turbo run build`.
If you made changes in the code, don't forget to rebuild the project `moon run :build`.

```bash
# List of commands
Expand Down Expand Up @@ -108,23 +101,21 @@ The UI allows managing multiple bots and strategies, viewing backtest results, a
**Option 1**: Run both `frontend` and `backend` apps in a single terminal

```bash
turbo run dev
moon run :dev
```

**Option 2**: Run each app in a separate terminal

First Terminal

```bash
cd pro/frontend
pnpm run dev
moon run frontend:dev
```

Second Terminal

```bash
cd pro/backend
pnpm run dev
moon run backend:dev
```

# Apps
Expand Down
121 changes: 34 additions & 87 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,107 +1,54 @@
FROM node:20-alpine AS base

FROM base AS builder
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
RUN apk update
# Install pnpm
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
# Set working directory
#### BASE
FROM node:latest AS base
WORKDIR /app
RUN pnpm add turbo -g
COPY . .

RUN turbo prune --scope=processor --scope=frontend --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app

# Install pnpm & turbo
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
RUN pnpm add turbo -g

# First install dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
# Copy Prisma Schema as it is not included in `/json` dir
COPY --from=builder /app/out/full/packages/prisma/src/schema.prisma ./packages/prisma/src/schema.prisma
RUN --mount=type=cache,id=pnpm,target=/root/.local/share/pnpm/store pnpm fetch
# Overriding the lockfile may not be necessary because the current image doesn't include additional dependencies from the pro module
RUN pnpm install --prefer-offline --lockfile

# Build the project and its dependencies
COPY --from=builder /app/out/full/ .
COPY turbo.json turbo.json
# Install moon binary
RUN npm install -g @moonrepo/cli

# ENV vars
ARG DATABASE_URL
ENV DATABASE_URL=$DATABASE_URL
#### SKELETON
FROM base AS skeleton

ARG NEXT_PUBLIC_PROCESSOR_URL
ENV NEXT_PUBLIC_PROCESSOR_URL=$NEXT_PUBLIC_PROCESSOR_URL

ARG NEXT_PUBLIC_PROCESSOR_ENABLE_TRPC
ENV NEXT_PUBLIC_PROCESSOR_ENABLE_TRPC=$NEXT_PUBLIC_PROCESSOR_ENABLE_TRPC

ARG NEXT_PUBLIC_STATIC
ENV NEXT_PUBLIC_STATIC=$NEXT_PUBLIC_STATIC

ARG ADMIN_PASSWORD
ENV ADMIN_PASSWORD=$ADMIN_PASSWORD

# RUN --mount=type=cache,id=turbo-cache,target=/app/node_modules/.cache turbo run build
RUN turbo run build
# Copy entire repository and scaffold
COPY . .

FROM base AS optimizer
# Intall only production deps
# Copy the minimum of files necessary for installing dependencies
RUN moon docker scaffold frontend processor

RUN apk add --no-cache libc6-compat
RUN apk update
# Install pnpm
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
#### BUILD
FROM base AS build

WORKDIR /app
# Copy toolchain
COPY --from=skeleton /root/.proto /root/.proto

COPY --from=installer /app/pro/frontend/dist ./pro/frontend/dist
COPY --from=installer /app/pro/processor ./pro/processor
COPY --from=installer /app/package.json ./package.json
COPY --from=installer /app/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=installer /app/pnpm-workspace.yaml ./pnpm-workspace.yaml
COPY --from=installer /app/packages ./packages
COPY --from=installer /app/.npmrc ./.npmrc
# Copy workspace skeleton
COPY --from=skeleton /app/.moon/docker/workspace .
# Copy Prisma schema
COPY --from=skeleton /app/.moon/docker/sources/packages/prisma/src/schema.prisma ./packages/prisma/src/schema.prisma

RUN pnpm i --prod
# Install toolchain and dependencies
RUN moon docker setup

# Copy source files
COPY --from=skeleton /app/.moon/docker/sources .

FROM base AS runner
# Build something (optional)
RUN moon run frontend:build processor:build

# Cannot create DB file when running Prisma migrations
# due lack of permissions when running as non-root user
# RUN addgroup --system --gid 1001 expressjs
# RUN adduser --system --uid 1001 expressjs
# USER expressjs
# Remove unneeded files and folders
RUN moon docker prune

# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
##### RUNNER
FROM node:20-alpine AS runner
WORKDIR /app

COPY --from=optimizer /app/pro/frontend/dist ./pro/frontend/dist
COPY --from=optimizer /app/pro/processor ./pro/processor
COPY --from=optimizer /app/node_modules ./node_modules
COPY --from=build /app/pro/frontend/dist ./pro/frontend/dist
COPY --from=build /app/pro/processor ./pro/processor
COPY --from=build /app/node_modules ./node_modules

# Copy Prisma schema, migrations, and seed script
COPY --from=optimizer /app/packages/prisma/src/schema.prisma ./packages/prisma/src/schema.prisma
COPY --from=optimizer /app/packages/prisma/src/migrations ./packages/prisma/src/migrations
COPY --from=optimizer /app/packages/prisma/seed.mjs ./packages/prisma/seed.mjs
COPY --from=build /app/packages/prisma/src/schema.prisma ./packages/prisma/src/schema.prisma
COPY --from=build /app/packages/prisma/src/migrations ./packages/prisma/src/migrations
COPY --from=build /app/packages/prisma/seed.mjs ./packages/prisma/seed.mjs

# Copy the entrypoint script to run migrations before starting the app
COPY bin/docker-entry.sh /app/bin/docker-entry.sh
Expand Down
40 changes: 40 additions & 0 deletions apps/cli/moon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
tasks:
build:
command: tsup
platform: node
inputs:
- 'src/**/*'
- '/packages/*/src/**'
outputs:
- 'dist'
dts:
command: |
rm -rf ../../dts &&
tsc --build --emitDeclarationOnly &&
resolve-tspaths --src . &&
resolve-tspaths --project packages/backtesting/tsconfig.json --src . &&
resolve-tspaths --project packages/bot/tsconfig.json --src . &&
resolve-tspaths --project packages/bot-processor/tsconfig.json --src . &&
resolve-tspaths --project packages/bot-store/tsconfig.json --src . &&
resolve-tspaths --project packages/bot-templates/tsconfig.json --src . &&
resolve-tspaths --project packages/daemon/tsconfig.json --src . &&
resolve-tspaths --project packages/db/tsconfig.json --src . &&
resolve-tspaths --project packages/event-bus/tsconfig.json --src . &&
resolve-tspaths --project packages/exchanges/tsconfig.json --src . &&
resolve-tspaths --project packages/indicators/tsconfig.json --src . &&
resolve-tspaths --project packages/logger/tsconfig.json --src . &&
resolve-tspaths --project packages/prisma/tsconfig.json --src . &&
resolve-tspaths --project packages/processing/tsconfig.json --src . &&
resolve-tspaths --project packages/tools/tsconfig.json --src . &&
resolve-tspaths --project packages/trpc/tsconfig.json --src . &&
resolve-tspaths --project packages/types/tsconfig.json --src .
inputs:
- 'src/**/*'
outputs:
- '/dts'
lint:
command: eslint . --quiet
lint-fix:
command: eslint . --fix
typecheck:
command: tsc --noEmit
13 changes: 3 additions & 10 deletions apps/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "opentrader",
"version": "1.0.0-beta.18",
"version": "1.0.0-beta.19",
"description": "OpenTrader is a powerful open-source crypto trading bot designated to automate your trading strategies on various cryptocurrency exchanges.",
"keywords": [
"bot",
Expand All @@ -20,12 +20,8 @@
".": "./dist/main.mjs",
"./effects": "./dist/effects.mjs"
},
"scripts": {
"build": "tsup",
"build:release": "rimraf ./release && tsup -d release/dist && cp -r ./bin ./release/bin && cp -r ./scripts ./release/scripts",
"lint": "eslint . --quiet",
"lint:fix": "eslint . --fix",
"typecheck": "tsc --noEmit"
"bin": {
"opentrader": "./bin/opentrader.mjs"
},
"author": "bludnic",
"license": "Apache-2.0",
Expand Down Expand Up @@ -76,8 +72,5 @@
"random-words": "^2.0.1",
"superjson": "^2.2.1",
"zod": "3.23.8"
},
"bin": {
"opentrader": "./bin/opentrader.mjs"
}
}
Loading

0 comments on commit af07d14

Please sign in to comment.