diff --git a/.github/workflows/docker-deploy-to-github.yml b/.github/workflows/docker-deploy-to-github.yml new file mode 100644 index 000000000..d87d5e55c --- /dev/null +++ b/.github/workflows/docker-deploy-to-github.yml @@ -0,0 +1,66 @@ +name: 'Docker deploy to GitHub' + +on: + push: + branches: + - dockerized + paths: + - 'api.dockerfile' + - 'worker.dockerfile' + - 'src/**' + - '.github/workflows/docker-deploy-to-github.yml' + +env: + # GitHub settings + REGISTRY: ghcr.io/liquality + + # AWS settings + AWS_REGION: 'us-east-1' + + # atomicagent-api + AGENT_API_IMAGE_NAME: 'atomicagent-api' + AGENT_API_IMAGE_TAG: 'latest' + AGENT_API_DOCKERFILE_PATH: 'api.dockerfile' + + # atomicagent-worker + AGENT_WORKER_IMAGE_NAME: 'atomicagent-worker' + AGENT_WORKER_IMAGE_TAG: 'latest' + AGENT_WORKER_DOCKERFILE_PATH: 'worker.dockerfile' + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Log in to the Container registry + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Build, tag, and push Docker image (agent api) + - name: Build, tag, push image to GitHub (agent api) + id: build-api-image + run: | + echo "Building and tagging Docker image ($AGENT_API_IMAGE_NAME)" + docker build -f $AGENT_API_DOCKERFILE_PATH -t $AGENT_API_IMAGE_NAME . --no-cache + docker tag $AGENT_API_IMAGE_NAME:$AGENT_API_IMAGE_TAG $REGISTRY/$AGENT_API_IMAGE_NAME:$AGENT_API_IMAGE_TAG + echo "Pushing image to registry as: $REGISTRY/$AGENT_API_IMAGE_NAME" + docker push $REGISTRY/$AGENT_API_IMAGE_NAME:$AGENT_API_IMAGE_TAG + + # Build, tag, and push Docker image (agent worker) + - name: Build, tag, push image to GitHub (agent worker) + id: build-worker-image + run: | + echo "Building and tagging Docker image ($AGENT_WORKER_IMAGE_NAME)" + docker build -f $AGENT_WORKER_DOCKERFILE_PATH -t $AGENT_WORKER_IMAGE_NAME . --no-cache + docker tag $AGENT_WORKER_IMAGE_NAME:$AGENT_WORKER_IMAGE_TAG $REGISTRY/$AGENT_WORKER_IMAGE_NAME:$AGENT_WORKER_IMAGE_TAG + echo "Pushing image to registry as: $REGISTRY/$AGENT_WORKER_IMAGE_NAME" + docker push $REGISTRY/$AGENT_WORKER_IMAGE_NAME:$AGENT_WORKER_IMAGE_TAG diff --git a/.github/workflows/docker-push-to-personal.yml b/.github/workflows/docker-push-to-personal.yml deleted file mode 100644 index bb714f673..000000000 --- a/.github/workflows/docker-push-to-personal.yml +++ /dev/null @@ -1,70 +0,0 @@ -name: 'deploy-to-personal' - -on: - push: - branches: - - dockerized - paths: - - 'env/personal/**' - - 'src/**' - - '.github/workflows/docker-push-to-personal.yml' - -env: - ENV_ALIAS: 'personal' - AWS_REGION: 'us-east-2' - IMAGE_NAME: 'atomicagent-personal' - IMAGE_TAG: 'latest' - DOCKERFILE_PATH: 'api.dockerfile' - -jobs: - docker-deploy: - name: 'Build & Deploy Image' - runs-on: ubuntu-latest - environment: personal - - defaults: - run: - shell: bash - - steps: - # Checkout the repository to the GitHub Actions runner - - name: Checkout - uses: actions/checkout@v2 - - # Load AWS credentials (this is the ecs-service service account) - - name: Load AWS creds - uses: aws-actions/configure-aws-credentials@v1 - with: - role-skip-session-tagging: true - aws-region: ${{ env.AWS_REGION }} - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} - role-duration-seconds: 1200 - - # Login to ECR - - name: Login to AWS ECR - id: login-ecr - uses: aws-actions/amazon-ecr-login@v1 - - # Build, tag, and push Docker image to ECR - - name: Build, tag, push image to ECR - id: build-image - env: - ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} - # IMAGE_TAG: ${{ github.sha }} - DB_PASSWORD: ${{ secrets.DB_PASSWORD }} - run: | - echo "Building and tagging Docker image ($IMAGE_NAME)" - docker build -f $DOCKERFILE_PATH --build-arg ENV_ALIAS=${ENV_ALIAS} --build-arg DB_PASSWORD=${DB_PASSWORD} -t $IMAGE_NAME . --no-cache - docker tag $IMAGE_NAME:$IMAGE_TAG $ECR_REGISTRY/$IMAGE_NAME:$IMAGE_TAG - echo "Pushing image to registry: $REGISTRY_URI/$IMAGE_NAME" - docker push $ECR_REGISTRY/$IMAGE_NAME:$IMAGE_TAG - - # Trigger update to ECS cluster - - name: Update ECS - id: update-ecs - run: | - echo "Assuming deployment role for ECS update" - source ./bin/aws-assume-role.sh "${AWS_REGION}" "${{ github.sha }}" "${{ secrets.AWS_ROLE_TO_ASSUME }}" - aws ecs update-service --cluster ${IMAGE_NAME} --service ${IMAGE_NAME} --force-new-deployment diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index eda30ec53..0d6bdeaeb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,7 +1,14 @@ # This actions runs against every Pull request(Include Draft) name: Test -on: push +on: + push: + paths: + - 'src/**' + - 'test/**' + - 'sample.config.toml' + - 'package.json' + - '.github/workflows/test.yml' jobs: test: diff --git a/.gitignore b/.gitignore index 8a7cf4376..eaabfcaf5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,29 @@ -.env -*config.toml +# ------------------------------------------------------------------------------ +# Local env and config files +# ------------------------------------------------------------------------------ +/*config.toml +/*config.docker.toml !sample.config.toml !heroku.config.toml -node_modules/ -marketdata/ test/docker/config/* !test/docker/config/bitcoin.conf -docker-commands.md -.DS_Store + +# ------------------------------------------------------------------------------ +# NodeJS artifacts +# ------------------------------------------------------------------------------ +# (yarn.lock is ignored because we are using npm) +node_modules/ +npm-debug.log* +.eslintcache +yarn.lock + +# ------------------------------------------------------------------------------ +# Custom assets +# ------------------------------------------------------------------------------ +marketdata/ + +# ------------------------------------------------------------------------------ +# OS junk files +# ------------------------------------------------------------------------------ +[Tt]humbs.db +*.DS_Store diff --git a/README.md b/README.md index 6d7acf1c0..f3148a2be 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,29 @@ # 💥 Atomic Agent ![Build status](https://github.com/liquality/agent/workflows/Test,%20publish%20&%20deploy/badge.svg) -## User <-> Agent Swap Workflow -![Workflow](diagram.png "Workflow") +## Table of Contents + +* [Introduction][section-introduction] +* [Prerequisites][section-prerequisites] +* [Installation][section-installation] +* [Usage][section-usage] +* [Test][section-test] +* [Liquality Hosted Agents][section-liquality-hosted-agents] +* [Liquality Nodes][section-liquality-nodes] +* [Run with Docker][section-run-with-docker] +* [User to Agent Swap Workflow][section-swap-workflow] +* [License][section-license] + + +## Introduction + +The Atomic Agent service contains three utilities: `migrate`, `api`, `worker`. The API and worker utilities work together to provide the running service, while the migrate utility is utilized to initialize the database with market data. + +The API and worker utilities are also distributed as Docker images: +* `atomicagent-api` +* `atomicagent-worker` + +> Found here: https://github.com/orgs/liquality/packages?repo_name=atomicagent ## Prerequisites @@ -13,28 +34,60 @@ 4. [RPC/API endpoints for the chains you want to support](#liquality-nodes) -## Setup +## Installation + +These instructions outline the standard installation process for the atomic agent: ```bash git clone git@github.com:liquality/atomicagent.git cd atomicagent -npm ci +npm install cp sample.config.toml config.toml # copy sample config -nano config.toml # configure your agent +nano config.toml # configure your agent settings nano src/migrate/data/assets.json # add/remove assets nano src/migrate/data/markets.json # add/remove markets npm run migrate # prepare agent with assets & markets ``` +## Usage + +Ensure you have configured all settings for your scenario within the `config.toml` file you created at the root of the repo. + +To run the utilities: + +```bash +npm run api # runs agent market maker api +npm run worker # runs the background process +``` + + +## Test + +### Configure + +```bash +cp sample.config.toml test.config.toml # copy sample config +nano config.toml # configure your agent as per your test environment +``` + +### Run Automated Tests + +```bash +chmod -R 777 test/docker/config +npm run docker:start +sleep 30 # let bitcoind[regtest] mine first 100 blocks +npm run test +``` + ## Liquality Hosted Agents -|Environment| Network | Endpoint | -|-|---------|--------------------------------------------------------| -|Production| Testnet | https://liquality.io/swap-testnet/agent | -|Production| Mainnet | https://liquality.io/swap/agent | -|Development| Testnet | https://liquality.io/swap-testnet-dev/agent | -|Development| Mainnet | https://liquality.io/swap-dev/agent | +|Environment | Network | Endpoint | +|------------|---------|---------------------------------------------| +|Production | Testnet | https://liquality.io/swap-testnet/agent | +|Production | Mainnet | https://liquality.io/swap/agent | +|Development | Testnet | https://liquality.io/swap-testnet-dev/agent | +|Development | Mainnet | https://liquality.io/swap-dev/agent | ## Liquality Testnet Nodes @@ -61,36 +114,85 @@ npm run migrate # prepare agent with assets & markets | Polygon Scraper | Mainnet | https://liquality.io/polygon-mainnet-api/ | +## Run with Docker -## Run! +The atomicagent service (which contains two utilities: api, worker) can each be dockerized for portability and convenience. -```bash -npm run api # runs agent market maker api -npm run worker # runs the background process -``` +### Run the Atomic Agent Utilities Locally +To run the utilities locally as Docker containers, make a copy of the `sample.config.toml` at the root of the repo and name it: `config.docker.toml`. The Docker run commands provided will use this file for its configuration. -## Test +> **NOTE:** This configuration requires you to have your own MongoDB running. +> **TIP:** You can use the `config/local/run-mongodb.yml` config to run a simple MongoDB locally. -### Configure +Ensure you have configured your desired markets and assets to run. The migrate utility will run automatically when the container starts, if the database is empty. + +You can configure the markets and assets in the following files: -```bash -cp sample.config.toml test.config.toml # copy sample config -nano config.toml # configure your agent as per your test environment +``` +src/migrate/data/markets.json +src/migrate/data/assets.json ``` +To run the API as a container locally: -### Test! +| Command | Description | +| ------------------------- | ------------------------------------- | +| `docker:build-api-local` | Builds the "atomicagent-api-local" image. | +| `docker:run-api-local` | Runs the "atomicagent-api-local" image as a container. | +| `docker:log-api-local` | Prints the standard out of the running "atomicagent-api-local" container. | +| `docker:stop-api-local` | Stops the running "atomicagent-api-local" container. | -```bash -chmod -R 777 test/docker/config -npm run docker:start -sleep 30 # let bitcoind[regtest] mine first 100 blocks -npm run test -``` +To run the worker as a container locally: + +| Command | Description | +| ---------------------------- | ------------------------------------- | +| `docker:build-worker-local` | Builds the "atomicagent-worker-local" image. | +| `docker:run-worker-local` | Runs the "atomicagent-worker-local" image as a container. | +| `docker:log-worker-local` | Prints the standard out of the running "atomicagent-worker-local" container. | +| `docker:stop-worker-local` | Stops the running "atomicagent-worker-local" container. | + + +### Docker Image Settings + +[TBC] + +### Run the Full Swap System + +The atomicagent service operates on multiple dependencies (for various chains/networks), as well as requiring a MongoDB to read/write data. + +To run the full swap system in a contained environment (for testing purposes): + +| Command | Description | +| ----------------------------- | ------------------------------------- | +| `docker:start-full-system` | Builds and runs the "atomicagent-full-system" image and runs a local simulation of the supported agent services. | +| `docker:log-full-system` | Prints the standard out of the running "atomicagent-full-system" container. | +| `docker:stop-full-system` | Stops the running "atomicagent-full-system" container and the agent services. | + +The config file is pre-configured for this scenario and is located at: `config/tester/config.tester.toml` + + +## User to Agent Swap Workflow + +![Workflow](diagram.png "Workflow") ## License [MIT](./LICENSE.md) + + + + + +[section-introduction]: #introduction +[section-prerequisites]: #prerequisites +[section-installation]: #installation +[section-usage]: #usage +[section-test]: #test +[section-liquality-hosted-agents]: #liquality-hosted-agents +[section-liquality-nodes]: #liquality-nodes +[section-run-with-docker]: #run-with-docker +[section-swap-workflow]: #user-to-agent-swap-workflow +[section-license]: #license diff --git a/api.dockerfile b/api.dockerfile index 9b0106848..de2e8c283 100644 --- a/api.dockerfile +++ b/api.dockerfile @@ -1,13 +1,5 @@ FROM node:15.7.0-alpine -# --------------- -# Load env params -# --------------- -ARG ENV_ALIAS -ENV ENV_ALIAS ${ENV_ALIAS} -ARG DB_PASSWORD -ENV DB_PASSWORD ${DB_PASSWORD} - # ------------------- # Build app directory # ------------------- @@ -22,12 +14,9 @@ COPY bin/ ./bin COPY src/ ./src COPY LICENSE.md ./ -# Load environment config -COPY env/${ENV_ALIAS}/config.${ENV_ALIAS}.toml ./config.toml - -# --------- -# Start API -# --------- +# ------------- +# Start Service +# ------------- EXPOSE 3030 CMD ["./bin/start-api.sh"] diff --git a/bin/start-api.sh b/bin/start-api.sh index ba4f9ed70..aad7e450e 100755 --- a/bin/start-api.sh +++ b/bin/start-api.sh @@ -1,10 +1,5 @@ #! /bin/sh -echo "--------------------------------" -echo "ENV_ALIAS: ${ENV_ALIAS}" -echo "--------------------------------" -echo "" +echo "Starting the Atomic Agent API..." -echo "Starting Atomic Agent API..." - -npm run api +npm run api-service diff --git a/bin/start-worker.sh b/bin/start-worker.sh new file mode 100755 index 000000000..e7261b835 --- /dev/null +++ b/bin/start-worker.sh @@ -0,0 +1,5 @@ +#! /bin/sh + +echo "Starting the Atomic Agent worker..." + +npm run worker-service diff --git a/env/dev/config.dev.toml b/config.docker.toml similarity index 94% rename from env/dev/config.dev.toml rename to config.docker.toml index fd0024856..5eb6c191b 100644 --- a/env/dev/config.dev.toml +++ b/config.docker.toml @@ -1,6 +1,7 @@ [database] -debug = true -uri = "" +debug = false +# uri = "mongodb://docker.for.mac.host.internal:27017/swapOrders" +uri = "mongodb://admin:admin-password@docker.for.mac.host.internal:27017/swapOrders?authSource=admin" [application] apiPort = 3030 diff --git a/env/local/run-mongodb.yml b/config/local/run-mongodb.yml similarity index 69% rename from env/local/run-mongodb.yml rename to config/local/run-mongodb.yml index dda4b3c41..62e50774e 100644 --- a/env/local/run-mongodb.yml +++ b/config/local/run-mongodb.yml @@ -9,10 +9,6 @@ services: environment: - PUID=1000 - PGID=1000 - - MONGO_INITDB_ROOT_USERNAME=root - - MONGO_INITDB_ROOT_PASSWORD=password - volumes: - - ~/mongodb/db:/data/db ports: - 27017:27017 restart: unless-stopped diff --git a/config/tester/config.tester.toml b/config/tester/config.tester.toml new file mode 100644 index 000000000..f2ef8eca7 --- /dev/null +++ b/config/tester/config.tester.toml @@ -0,0 +1,95 @@ +[database] +debug = false + +[application] +apiPort = 3030 +swapExpirationDurationInSeconds = 3600 +nodeSwapExpirationDurationInSeconds = 1800 + +[auth] +cookieSecret = '58da74ef560e5578cb46219b7818d7c2' +cookieMaxAgeMs = 86400000 +simplePassword = '25ec02267950f537347b4a7c02b00ced' + +[threshold] +manualAboveFromAmountUsd = 5000 + +[worker] +maxJobRetry = 5 +jobRetryDelay = "5 seconds" +backendJobRetryDelay = "5 seconds" +minConcurrentSwaps = 3 +defaultLockLifetimeInMs = 120000 # 2 min +killswitch = 'pm2 stop "Worker"' + +[assets] + [assets.BTC] + network = "bitcoin_regtest" + addressType = "bech32" + swapMode = "p2wsh" + feeNumberOfBlocks = 2 + blockTime = "5 seconds" + defaultFee = "average" + [assets.BTC.wallet] + type = "js" + mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" + [assets.BTC.api] + url = "http://10.10.0.12:3002/" # vulpemventures/electrs:latest (3rd party) + [assets.BTC.batchApi] + url = "http://10.10.0.14:9090/" # electrs-batch-server.dockerfile (inside docker dir) + [assets.BTC.rpc] + url = "http://10.10.0.10:18443/" # vulpemventures/bitcoin:latest (3rd party) + user = "admin1" + password = "123" + + [assets.ETH] + network="local" + blockTime = "5 seconds" + defaultFee = "average" + [assets.ETH.wallet] + type = "js" + mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" + [assets.ETH.scraper] + url = "http://10.10.0.20:8080" # ethereum-scraper + [assets.ETH.rpc] + url = "http://10.10.0.16:8545" # Infura (3rd party) + + [assets.DAI] + network="local" + type = "ERC20" + blockTime = "5 seconds" + contractAddress = "0x094cdd8e19b97505869c87a995a972c4df7f69a8" + defaultFee = "average" + [assets.DAI.wallet] + type = "js" + mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" + [assets.DAI.scraper] + url = "http://10.10.0.20:8080" # ethereum-scraper + [assets.DAI.rpc] + url = "http://10.10.0.16:8545" # Infura (3rd party) + + [assets.RBTC] + blockTime = "20 seconds" + network = "rsk_testnet" + defaultFee = "average" + pegWith = "BTC" + [assets.RBTC.rpc] + url = "https://public-node.testnet.rsk.co" # RSK service (3rd party) + [assets.RBTC.scraper] + url = "https://liquality.io/rsk-testnet-api/" # ethereum-scraper + [assets.RBTC.wallet] + type = "js" + mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" + + [assets.SOV] + blockTime = "20 seconds" + network = "rsk_testnet" + defaultFee = "average" + contractAddress = "0x6a9A07972D07E58f0daF5122D11e069288A375fB" + [assets.SOV.rpc] + url = "https://public-node.testnet.rsk.co" # Infura (3rd party) + [assets.SOV.scraper] + url="https://liquality.io/rsk-testnet-api/" # ethereum-scraper + [assets.SOV.wallet] + type = "js" + mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" diff --git a/config/tester/run-full-system.yml b/config/tester/run-full-system.yml new file mode 100644 index 000000000..5e941bc15 --- /dev/null +++ b/config/tester/run-full-system.yml @@ -0,0 +1,27 @@ +# Run with: docker-compose -f run-full-system.yml up -d --build +version: "3.8" + +services: + atomicagent-full-system: + build: + context: ../../ + dockerfile: api.dockerfile + container_name: atomicagent-full-system + environment: + - MONGO_URI=mongodb://10.10.0.8:27017/swapOrders + - CONFIG_PATH=/config/config.tester.toml + volumes: + - ./config/tester:/config + ports: + - 3030:3030 + restart: unless-stopped + networks: + local: + ipv4_address: 10.10.0.6 + +networks: + local: + driver: bridge + ipam: + config: + - subnet: 10.10.0.0/24 diff --git a/env/local/config.local.toml b/env/local/config.local.toml deleted file mode 100644 index 44378a729..000000000 --- a/env/local/config.local.toml +++ /dev/null @@ -1,126 +0,0 @@ -[database] -# ---------------------------------------------- Connection settings for MongoDB -# Provide either (uri will take precedence): -# -# uri - A full mongodb connection uri (see: https://docs.mongodb.com/manual/reference/connection-string/) -# e.g. -# mongodb://admin:password@127.0.0.1:27017/liquality?authSource=admin -# -# -or- -# -# host - The db hostname/IP -# port - The db port (optional) -# dbname - The db name (optional) -# username - The username for auth (optional, leave blank if no auth) -# password - The password for auth (optional, leave blank if no auth) -# authdbname - The auth db name (optional with auth, leave blank if no auth) -# ------------------------------------------------------------------------------ -# Other options: -# -# debug - true|false (Enables mongodb debug) -# ------------------------------------------------------------------------------ -debug = false -#uri = "mongodb://demo:demo-password@docker.for.mac.host.internal:27017/rickandmorty?authSource=test" -#uri = "mongodb://demo:demo-password@127.0.0.1:27017/rickandmorty?authSource=test" -uri = "mongodb://admin:admin-password@docker.for.mac.host.internal:27017/liquality?authSource=admin" -# uri = "mongodb://admin:admin-password@127.0.0.1:27017/liquality?authSource=admin" -#host = "docker.for.mac.host.internal" -host = "127.0.0.1" -port = "27017" -dbname = "rickandmorty" -username = "demo" -password = "demo-password" -authdbname = "test" - -[application] -apiPort = 3030 -swapExpirationDurationInSeconds = 3600 -nodeSwapExpirationDurationInSeconds = 1800 - -[auth] -cookieSecret = '58da74ef560e5578cb46219b7818d7c2' -cookieMaxAgeMs = 86400000 -simplePassword = '25ec02267950f537347b4a7c02b00ced' - -[threshold] -manualAboveFromAmountUsd = 5000 - -[worker] -maxJobRetry = 5 -jobRetryDelay = "5 seconds" -backendJobRetryDelay = "5 seconds" -minConcurrentSwaps = 3 -defaultLockLifetimeInMs = 120000 # 2 min -killswitch = 'pm2 stop "Worker"' - -[assets] - # [assets.BTC] - # network = "bitcoin_regtest" - # addressType = "bech32" - # swapMode = "p2wsh" - # feeNumberOfBlocks = 2 - # blockTime = "5 seconds" - # defaultFee = "average" - # [assets.BTC.wallet] - # type = "js" - # mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" - # [assets.BTC.api] - # url = "http://localhost:3002/" # vulpemventures/electrs:latest (3rd party) - # [assets.BTC.batchApi] - # url = "http://localhost:9090/" # electrs-batch-server.dockerfile (inside docker dir) - # [assets.BTC.rpc] - # url = "http://localhost:18443/" # vulpemventures/bitcoin:latest (3rd party) - # user = "admin1" - # password = "123" - - [assets.ETH] - network="local" - blockTime = "5 seconds" - defaultFee = "average" - [assets.ETH.wallet] - type = "js" - mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" - [assets.ETH.scraper] - url = "http://localhost:8080" # ethereum-scraper - [assets.ETH.rpc] - url = "http://localhost:8545" # Infura (3rd party) - - # [assets.DAI] - # network="local" - # type = "ERC20" - # blockTime = "5 seconds" - # contractAddress = "0x094cdd8e19b97505869c87a995a972c4df7f69a8" - # defaultFee = "average" - # [assets.DAI.wallet] - # type = "js" - # mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" - # [assets.DAI.scraper] - # url = "http://localhost:8080" # ethereum-scraper - # [assets.DAI.rpc] - # url = "http://localhost:8545" # Infura (3rd party) - - # [assets.RBTC] - # blockTime = "20 seconds" - # network = "rsk_testnet" - # defaultFee = "average" - # pegWith = "BTC" - # [assets.RBTC.rpc] - # url = "https://public-node.testnet.rsk.co" # RSK service (3rd party) - # [assets.RBTC.scraper] - # url = "https://liquality.io/rsk-testnet-api/" # ethereum-scraper - # [assets.RBTC.wallet] - # type = "js" - # mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" - - # [assets.SOV] - # blockTime = "20 seconds" - # network = "rsk_testnet" - # defaultFee = "average" - # contractAddress = "0x6a9A07972D07E58f0daF5122D11e069288A375fB" - # [assets.SOV.rpc] - # url = "https://public-node.testnet.rsk.co" # Infura (3rd party) - # [assets.SOV.scraper] - # url="https://liquality.io/rsk-testnet-api/" # ethereum-scraper - # [assets.SOV.wallet] - # type = "js" - # mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" diff --git a/env/personal/config.personal.toml b/env/personal/config.personal.toml deleted file mode 100644 index a111250b7..000000000 --- a/env/personal/config.personal.toml +++ /dev/null @@ -1,103 +0,0 @@ -[database] -debug = false -# uri = "mongodb://demo:demo-password@docker.for.mac.host.internal:27017/rickandmorty?authSource=test" -# uri = "mongodb://admin:admin-password@docker.for.mac.host.internal:27017/liquality?authSource=admin" -# uri = "mongodb://admin:admin-password@docker.for.mac.host.internal:27017/liquality" -host = "10.0.1.5" -port = "27017" -dbname = "liquality" -username = "liquality" -authdbname = "admin" - -[application] -apiPort = 3030 -swapExpirationDurationInSeconds = 3600 -nodeSwapExpirationDurationInSeconds = 1800 - -[auth] -cookieSecret = '58da74ef560e5578cb46219b7818d7c2' -cookieMaxAgeMs = 86400000 -simplePassword = '25ec02267950f537347b4a7c02b00ced' - -[threshold] -manualAboveFromAmountUsd = 5000 - -[worker] -maxJobRetry = 5 -jobRetryDelay = "5 seconds" -backendJobRetryDelay = "5 seconds" -minConcurrentSwaps = 3 -defaultLockLifetimeInMs = 120000 # 2 min -killswitch = 'pm2 stop "Worker"' - -[assets] - # [assets.BTC] - # network = "bitcoin_regtest" - # addressType = "bech32" - # swapMode = "p2wsh" - # feeNumberOfBlocks = 2 - # blockTime = "5 seconds" - # defaultFee = "average" - # [assets.BTC.wallet] - # type = "js" - # mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" - # [assets.BTC.api] - # url = "http://localhost:3002/" # vulpemventures/electrs:latest (3rd party) - # [assets.BTC.batchApi] - # url = "http://localhost:9090/" # electrs-batch-server.dockerfile (inside docker dir) - # [assets.BTC.rpc] - # url = "http://localhost:18443/" # vulpemventures/bitcoin:latest (3rd party) - # user = "admin1" - # password = "123" - - [assets.ETH] - network="local" - blockTime = "5 seconds" - defaultFee = "average" - [assets.ETH.wallet] - type = "js" - mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" - [assets.ETH.scraper] - url = "http://localhost:8080" # ethereum-scraper - [assets.ETH.rpc] - url = "http://localhost:8545" # Infura (3rd party) - - # [assets.DAI] - # network="local" - # type = "ERC20" - # blockTime = "5 seconds" - # contractAddress = "0x094cdd8e19b97505869c87a995a972c4df7f69a8" - # defaultFee = "average" - # [assets.DAI.wallet] - # type = "js" - # mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" - # [assets.DAI.scraper] - # url = "http://localhost:8080" # ethereum-scraper - # [assets.DAI.rpc] - # url = "http://localhost:8545" # Infura (3rd party) - - # [assets.RBTC] - # blockTime = "20 seconds" - # network = "rsk_testnet" - # defaultFee = "average" - # pegWith = "BTC" - # [assets.RBTC.rpc] - # url = "https://public-node.testnet.rsk.co" # RSK service (3rd party) - # [assets.RBTC.scraper] - # url = "https://liquality.io/rsk-testnet-api/" # ethereum-scraper - # [assets.RBTC.wallet] - # type = "js" - # mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" - - # [assets.SOV] - # blockTime = "20 seconds" - # network = "rsk_testnet" - # defaultFee = "average" - # contractAddress = "0x6a9A07972D07E58f0daF5122D11e069288A375fB" - # [assets.SOV.rpc] - # url = "https://public-node.testnet.rsk.co" # Infura (3rd party) - # [assets.SOV.scraper] - # url="https://liquality.io/rsk-testnet-api/" # ethereum-scraper - # [assets.SOV.wallet] - # type = "js" - # mnemonic = "piece effort bind that embrace enrich remind powder sudden patient legend group" diff --git a/original.Dockerfile b/original.Dockerfile deleted file mode 100644 index 35efc6313..000000000 --- a/original.Dockerfile +++ /dev/null @@ -1,9 +0,0 @@ -FROM node:slim - -WORKDIR /app - -ADD . /app - -RUN npm ci - -EXPOSE 3030 3031 diff --git a/package.json b/package.json index 6ac80d0c1..595366100 100644 --- a/package.json +++ b/package.json @@ -13,20 +13,28 @@ "node": ">=14.15.0" }, "scripts": { - "api": "PROCESS_TYPE=api nodemon src/index.js", - "worker": "PROCESS_TYPE=worker nodemon src/index.js", - "migrate": "PROCESS_TYPE=migrate node src/index.js", - "migrate-local": "CONFIG_PATH=env/local/config.local.toml PROCESS_TYPE=migrate node src/index.js", - "docker:build-local": "docker build -f api.dockerfile --build-arg ENV_ALIAS=local -t atomicagent-local . --no-cache", - "docker:run-local": "docker run --rm --name atomicagent -d -it atomicagent-local:latest", - "docker:stop-local": "docker stop atomicagent", - "docker:log-local": "docker container logs -t atomicagent", + "api": "PROCESS_TYPE=api CONFIG_PATH=config.toml nodemon src/index.js", + "worker": "PROCESS_TYPE=worker CONFIG_PATH=config.toml nodemon src/index.js", + "migrate": "PROCESS_TYPE=migrate CONFIG_PATH=config.toml FORCE_MIGRATE=true node src/index.js", + "api-service": "KEEP_ALIVE=true node src/index.js", + "worker-service": "PROCESS_TYPE=worker node src/index.js", + "docker:build-api-local": "docker build -f api.dockerfile -t atomicagent-api-local . --no-cache", + "docker:run-api-local": "docker run --rm --name atomicagent-api-local --env CONFIG_PATH=/config/config.docker.toml -v `pwd`:/config -p 3030:3030 -d -it atomicagent-api-local:latest", + "docker:stop-api-local": "docker stop atomicagent-api-local", + "docker:log-api-local": "docker container logs -t atomicagent-api-local", + "docker:build-worker-local": "docker build -f worker.dockerfile -t atomicagent-worker-local . --no-cache", + "docker:run-worker-local": "docker run --rm --name atomicagent-worker-local --env CONFIG_PATH=/config/config.docker.toml -v `pwd`:/config -d -it atomicagent-worker-local:latest", + "docker:stop-worker-local": "docker stop atomicagent-worker-local", + "docker:log-worker-local": "docker container logs -t atomicagent-worker-local", + "docker:start-full-system": "COMPOSE_PROJECT_NAME=tester npm run docker:start && docker-compose -f config/tester/run-full-system.yml up -d --build", + "docker:stop-full-system": "COMPOSE_PROJECT_NAME=tester docker stop atomicagent-full-system && COMPOSE_PROJECT_NAME=tester npm run docker:stop", + "docker:log-full-system": "docker container logs -t atomicagent-full-system", "docker:start": "docker-compose -f test/docker/docker-compose.yml up -d --force-recreate --renew-anon-volumes", "docker:stop": "docker-compose -f test/docker/docker-compose.yml down", "lint": "standard", "flint": "standard --fix", "precommit-message-lint": "printf '\nPre-commit checks (linting)...\n' && exit 0", - "test": "DEBUG=liquality:agent* NODE_ENV=test CONFIG_PATH=test.config.toml mocha --exit --bail ./test/*.test.js", + "test": "DEBUG=liquality:agent* NODE_ENV=test CONFIG_PATH=test.config.toml FORCE_MIGRATE=true mocha --exit --bail ./test/*.test.js", "test:unit": "NODE_ENV=test mocha test/unit/**" }, "pre-commit": [ diff --git a/sample.config.toml b/sample.config.toml index 5a464b3eb..12e4610ae 100644 --- a/sample.config.toml +++ b/sample.config.toml @@ -1,5 +1,6 @@ [database] -uri = "mongodb://localhost/liqualitytest" +# uri = "mongodb://docker.for.mac.host.internal:27017/swapOrders" +uri = "mongodb://localhost/swapOrders" debug = true [application] diff --git a/src/config.js b/src/config.js index 54422fc19..2a4387c43 100644 --- a/src/config.js +++ b/src/config.js @@ -3,7 +3,7 @@ const path = require('path') const toml = require('toml') if (!process.env.CONFIG_PATH) { - process.env.CONFIG_PATH = path.resolve(__dirname, '..', 'config.toml') + process.env.CONFIG_PATH = path.resolve(__dirname, '..', 'config/config.toml') } console.log(`Config file: ${process.env.CONFIG_PATH}`) diff --git a/src/index.js b/src/index.js index 4f2b9b23e..bf02e4a67 100644 --- a/src/index.js +++ b/src/index.js @@ -11,7 +11,9 @@ const mongoConnect = require('./utils/mongoConnect') const config = require('./config') // Load db settings and establish connection -mongoConnect.connect(config.database) +const dbConfig = config.database || {} +if (process.env.MONGO_URI) dbConfig.uri = process.env.MONGO_URI // override with env var +mongoConnect.connect(dbConfig) // Run service switch (process.env.PROCESS_TYPE) { @@ -28,5 +30,10 @@ switch (process.env.PROCESS_TYPE) { break default: - throw new Error('Unknown PROCESS_TYPE') + runApiService() +} + +async function runApiService () { + await require('./migrate').run() + require('./api').start() } diff --git a/src/migrate/data/assets.json b/src/migrate/data/assets.json index 68f5628ca..12465cad6 100644 --- a/src/migrate/data/assets.json +++ b/src/migrate/data/assets.json @@ -1,23 +1,66 @@ [ { "code": "BTC", - "actualBalance": 0, - "min": 80000, - "max": 45000000, - "minConf": 1 + "min": 200000, + "max": 0, + "actualBalance": 247886556, + "minConf": 1, + "updatedAt": "2021-12-14T14:06:22.182Z" }, { "code": "ETH", - "actualBalance": 0, - "min": 40000000000000000, - "max": 12000000000000000000, - "minConf": 1 + "actualBalance": 56427481491786380000, + "min": 50000000000000000, + "minConf": 1, + "createdAt": "2020-04-07T18:43:23.284Z", + "updatedAt": "2021-12-14T13:50:23.261Z" }, { "code": "DAI", + "actualBalance": 1.7089336621378284e+23, + "min": 100000000000000000000, + "minConf": 1, + "createdAt": "2020-04-07T18:43:23.284Z", + "updatedAt": "2021-12-10T18:15:03.712Z" + }, + { + "code": "USDC", + "actualBalance": 104467212573, + "min": 100000000, + "minConf": 1, + "createdAt": "2020-04-07T18:43:23.284Z", + "updatedAt": "2021-11-30T15:36:53.302Z" + }, + { + "code": "WBTC", + "min": 200000, + "max": 0, + "actualBalance": 9364900, + "minConf": 1, + "updatedAt": "2021-12-14T13:46:53.153Z" + }, + { + "code": "UNI", "actualBalance": 0, - "min": 5000000000000000000, - "max": 2e+21, - "minConf": 1 + "min": 25000000000000000000, + "minConf": 1, + "createdAt": "2020-09-29T20:30:22.975Z", + "updatedAt": "2021-06-28T01:40:28.582Z" + }, + { + "code": "RBTC", + "min": 2000000000000000, + "max": 0, + "actualBalance": 8555522522403779000, + "minConf": 5, + "updatedAt": "2021-12-14T14:08:53.015Z" + }, + { + "code": "MATIC", + "actualBalance": 1.6740307252607191e+22, + "min": 120000000000000000000, + "minConf": 5, + "createdAt": "2021-06-21T14:42:10.516Z", + "updatedAt": "2021-12-14T13:28:54.066Z" } ] diff --git a/src/migrate/data/markets.json b/src/migrate/data/markets.json index ea574b3d8..b283387c7 100644 --- a/src/migrate/data/markets.json +++ b/src/migrate/data/markets.json @@ -2,61 +2,553 @@ { "from": "BTC", "to": "ETH", - "rate": 42.63633734, - "spread": 0, + "rate": 12.3040467, "orderExpiresIn": 7200000, "status": "ACTIVE", - "max": 28163916, - "min": 80000 + "max": 437237434, + "min": 200000, + "createdAt": "2020-04-07T18:43:23.301Z", + "updatedAt": "2021-12-14T14:13:58.749Z", + "minConf": 1 }, { "from": "ETH", "to": "BTC", - "rate": 0.02345417, - "spread": 0, + "rate": 0.07981121, "orderExpiresIn": 7200000, "status": "ACTIVE", - "max": 12000000000000000000, - "min": 40000000000000000 + "max": 29611725838118110000, + "min": 50000000000000000, + "createdAt": "2020-04-07T18:43:23.301Z", + "updatedAt": "2021-12-14T14:13:58.752Z", + "minConf": 1 + }, + { + "from": "RBTC", + "to": "BTC", + "rate": 0.99, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "createdAt": "2020-12-17T10:18:03.884Z", + "max": 2382598936310679600, + "min": 2000000000000000, + "minConf": 5, + "updatedAt": "2021-12-14T14:13:58.754Z" + }, + { + "from": "BTC", + "to": "RBTC", + "rate": 0.99, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "createdAt": "2020-12-17T10:18:03.875Z", + "max": 822326922, + "min": 200000, + "minConf": 1, + "updatedAt": "2021-12-14T14:13:59.220Z" + }, + { + "from": "RBTC", + "to": "ETH", + "rate": 12.23359335, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "createdAt": "2020-12-17T10:18:03.881Z", + "max": 4389700868624741000, + "min": 2000000000000000, + "minConf": 5, + "updatedAt": "2021-12-14T14:13:59.217Z" + }, + { + "from": "ETH", + "to": "RBTC", + "rate": 0.08012748, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "createdAt": "2020-12-17T10:18:03.873Z", + "max": 101746057786217090000, + "min": 50000000000000000, + "minConf": 1, + "updatedAt": "2021-12-14T14:13:59.198Z" }, { "from": "DAI", "to": "BTC", - "rate": 0.00013726, - "spread": 0, + "rate": 0.000021, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 1.1255425453530648e+23, + "min": 100000000000000000000, + "createdAt": "2020-04-07T18:43:23.301Z", + "updatedAt": "2021-12-14T14:13:59.199Z", + "minConf": 1 + }, + { + "from": "BTC", + "to": "DAI", + "rate": 46767.7166693, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 348755199, + "min": 200000, + "createdAt": "2020-04-07T18:43:23.301Z", + "updatedAt": "2021-12-14T14:13:59.201Z", + "minConf": 1 + }, + { + "from": "ETH", + "to": "DAI", + "rate": 3763.51559203, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 43214452586448420000, + "min": 50000000000000000, + "createdAt": "2020-04-07T18:43:23.302Z", + "updatedAt": "2021-12-14T14:13:59.177Z", + "minConf": 1 + }, + { + "from": "DAI", + "to": "ETH", + "rate": 0.00026046, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 2.0616668958897742e+23, + "min": 100000000000000000000, + "createdAt": "2020-04-07T18:43:23.302Z", + "updatedAt": "2021-12-14T14:13:59.180Z", + "minConf": 1 + }, + { + "from": "USDC", + "to": "BTC", + "rate": 0.00002102, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 112546825954, + "min": 100000000, + "createdAt": "2020-04-07T18:43:23.302Z", + "updatedAt": "2021-12-14T14:13:59.181Z", + "minConf": 1 + }, + { + "from": "BTC", + "to": "USDC", + "rate": 46764.63, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 213194253, + "min": 200000, + "createdAt": "2020-04-07T18:43:23.302Z", + "updatedAt": "2021-12-14T14:13:59.153Z", + "minConf": 1 + }, + { + "from": "ETH", + "to": "USDC", + "rate": 3763.2672, "orderExpiresIn": 7200000, "status": "ACTIVE", - "max": 2e+21, - "min": 5000000000000000000 + "max": 26434261760098050000, + "min": 50000000000000000, + "createdAt": "2020-04-07T18:43:23.302Z", + "updatedAt": "2021-12-14T14:13:59.155Z", + "minConf": 1 + }, + { + "from": "USDC", + "to": "ETH", + "rate": 0.00026063, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 206166689588, + "min": 100000000, + "createdAt": "2020-04-07T18:43:23.302Z", + "updatedAt": "2021-12-14T14:13:59.157Z", + "minConf": 1 }, { "from": "BTC", + "to": "WBTC", + "rate": 0.99163744, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "min": 200000, + "max": 9003833, + "minConf": 1, + "createdAt": "2020-07-16T12:31:29.053Z", + "updatedAt": "2021-12-14T14:13:59.113Z" + }, + { + "from": "WBTC", + "to": "BTC", + "rate": 0.99028809, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "min": 200000, + "max": 238507303, + "minConf": 1, + "createdAt": "2020-07-16T12:31:29.048Z", + "updatedAt": "2021-12-14T14:13:59.115Z" + }, + { + "from": "WBTC", + "to": "ETH", + "rate": 12.28380021, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 437173556, + "min": 200000, + "minConf": 1, + "createdAt": "2020-07-16T12:31:29.048Z", + "updatedAt": "2021-12-14T14:13:59.084Z" + }, + { + "from": "ETH", + "to": "WBTC", + "rate": 0.07979955, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 1116675646670708700, + "min": 50000000000000000, + "minConf": 1, + "createdAt": "2020-07-17T08:15:30.095Z", + "updatedAt": "2021-12-14T14:13:59.086Z" + }, + { + "from": "DAI", + "to": "WBTC", + "rate": 0.00002099, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 4.245162956570324e+21, + "min": 100000000000000000000, + "minConf": 1, + "createdAt": "2020-07-17T08:15:30.095Z", + "updatedAt": "2021-12-14T14:13:59.087Z" + }, + { + "from": "USDC", + "to": "WBTC", + "rate": 0.00002099, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 4244882775, + "min": 100000000, + "minConf": 1, + "createdAt": "2020-07-17T08:15:30.089Z", + "updatedAt": "2021-12-14T14:13:59.067Z" + }, + { + "from": "USDT", + "to": "WBTC", + "rate": 0.00002099, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 4244882775, + "min": 100000000, + "minConf": 1, + "createdAt": "2020-07-17T08:15:30.089Z", + "updatedAt": "2021-12-14T14:13:59.069Z" + }, + { + "from": "WBTC", + "to": "DAI", + "rate": 46690.49157244, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 348091536, + "min": 200000, + "minConf": 1, + "createdAt": "2020-07-17T08:15:30.089Z", + "updatedAt": "2021-12-14T14:13:59.070Z" + }, + { + "from": "WBTC", + "to": "USDC", + "rate": 46687.41, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "max": 212788555, + "min": 200000, + "minConf": 1, + "createdAt": "2020-07-17T08:15:30.083Z", + "updatedAt": "2021-12-14T14:13:59.044Z" + }, + { + "from": "UNI", + "to": "BTC", + "rate": 0.00030606, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.222Z", + "max": 7.719261039394304e+21, + "min": 25000000000000000000, + "updatedAt": "2021-12-14T14:13:59.047Z" + }, + { + "from": "UNI", + "to": "ETH", + "rate": 0.0037958, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.220Z", + "max": 1.4140376515113715e+22, + "min": 25000000000000000000, + "updatedAt": "2021-12-14T14:13:59.017Z" + }, + { + "from": "UNI", "to": "DAI", - "rate": 7285.42544146, - "spread": 0, + "rate": 14.43515272, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.219Z", + "max": 1.1265149672306468e+22, + "min": 25000000000000000000, + "updatedAt": "2021-12-14T14:13:59.018Z" + }, + { + "from": "UNI", + "to": "USDC", + "rate": 14.4342, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.210Z", + "max": 6.886846823668121e+21, + "min": 25000000000000000000, + "updatedAt": "2021-12-14T14:13:59.005Z" + }, + { + "from": "UNI", + "to": "WBTC", + "rate": 0.00030608, "orderExpiresIn": 7200000, "status": "ACTIVE", - "max": 27452000, - "min": 80000 + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.207Z", + "max": 291144223307177200000, + "min": 25000000000000000000, + "updatedAt": "2021-12-14T14:13:58.998Z" + }, + { + "from": "BTC", + "to": "UNI", + "rate": 3207.45061728, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.199Z", + "max": 0, + "min": 200000, + "updatedAt": "2021-12-14T14:13:58.996Z" }, { "from": "ETH", + "to": "UNI", + "rate": 258.11160494, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.197Z", + "max": 0, + "min": 50000000000000000, + "updatedAt": "2021-12-14T14:13:58.965Z" + }, + { + "from": "DAI", + "to": "UNI", + "rate": 0.06789675, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.195Z", + "max": 0, + "min": 100000000000000000000, + "updatedAt": "2021-12-14T14:13:58.966Z" + }, + { + "from": "USDC", + "to": "UNI", + "rate": 0.06790123, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.187Z", + "max": 0, + "min": 100000000, + "updatedAt": "2021-12-14T14:13:58.953Z" + }, + { + "from": "WBTC", + "to": "UNI", + "rate": 3202.15432099, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.183Z", + "max": 0, + "min": 200000, + "updatedAt": "2021-12-14T14:13:58.941Z" + }, + { + "from": "RBTC", "to": "DAI", - "rate": 170.87362322, - "spread": 0, + "rate": 46499.40896099, "orderExpiresIn": 7200000, "status": "ACTIVE", - "max": 11704560000000000000, - "min": 40000000000000000 + "createdAt": "2020-12-17T10:18:03.882Z", + "max": 3497506951249070000, + "min": 2000000000000000, + "minConf": 5, + "updatedAt": "2021-12-14T14:13:58.943Z" }, { "from": "DAI", + "to": "RBTC", + "rate": 0.00002108, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "createdAt": "2020-12-17T10:18:03.871Z", + "max": 3.8644431375113084e+23, + "min": 100000000000000000000, + "minConf": 1, + "updatedAt": "2021-12-14T14:13:58.944Z" + }, + { + "from": "UNI", + "to": "RBTC", + "rate": 0.00030733, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2020-09-29T20:30:23.222Z", + "max": 2.70274055446038e+22, + "min": 25000000000000000000, + "updatedAt": "2021-12-14T14:13:58.929Z" + }, + { + "from": "MATIC", + "to": "BTC", + "rate": 0.00003861, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 5, + "createdAt": "2021-06-21T14:42:10.742Z", + "max": 6.116675323614856e+22, + "min": 120000000000000000000, + "updatedAt": "2021-12-14T14:13:58.847Z" + }, + { + "from": "MATIC", + "to": "DAI", + "rate": 1.82172023, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 5, + "createdAt": "2021-06-21T14:42:10.735Z", + "max": 8.926407091567367e+22, + "min": 120000000000000000000, + "updatedAt": "2021-12-14T14:13:58.849Z" + }, + { + "from": "MATIC", + "to": "USDC", + "rate": 1.8216, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 5, + "createdAt": "2021-06-21T14:42:10.741Z", + "max": 5.457077922201619e+22, + "min": 120000000000000000000, + "updatedAt": "2021-12-14T14:13:58.821Z" + }, + { + "from": "MATIC", + "to": "RBTC", + "rate": 0.00003879, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 5, + "createdAt": "2021-06-21T14:42:10.728Z", + "max": 2.098989468548669e+23, + "min": 120000000000000000000, + "updatedAt": "2021-12-14T14:13:58.825Z" + }, + { + "from": "MATIC", "to": "ETH", - "rate": 0.00585228, - "spread": 0, + "rate": 0.00047891, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 5, + "createdAt": "2021-06-21T14:42:10.724Z", + "max": 1.120471139068624e+23, + "min": 120000000000000000000, + "updatedAt": "2021-12-14T14:13:58.801Z" + }, + { + "from": "BTC", + "to": "MATIC", + "rate": 25415.55978261, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2021-06-21T14:42:10.711Z", + "max": 62751773, + "min": 200000, + "updatedAt": "2021-12-14T14:13:58.804Z" + }, + { + "from": "DAI", + "to": "MATIC", + "rate": 0.53800797, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2021-06-21T14:42:10.708Z", + "max": 2.9605964748882776e+22, + "min": 100000000000000000000, + "updatedAt": "2021-12-14T14:13:58.784Z" + }, + { + "from": "USDC", + "to": "MATIC", + "rate": 0.53804348, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 1, + "createdAt": "2021-06-21T14:42:10.710Z", + "max": 29605964748, + "min": 100000000, + "updatedAt": "2021-12-14T14:13:58.785Z" + }, + { + "from": "RBTC", + "to": "MATIC", + "rate": 25269.75, + "orderExpiresIn": 7200000, + "status": "ACTIVE", + "minConf": 5, + "createdAt": "2021-06-21T14:42:10.697Z", + "max": 629955639913645300, + "min": 2000000000000000, + "updatedAt": "2021-12-14T14:13:58.765Z" + }, + { + "from": "ETH", + "to": "MATIC", + "rate": 2045.25391304, "orderExpiresIn": 7200000, "status": "ACTIVE", - "max": 2e+21, - "min": 5000000000000000000 + "minConf": 1, + "createdAt": "2021-06-21T14:42:10.700Z", + "max": 7783592763442825000, + "min": 50000000000000000, + "updatedAt": "2021-12-14T14:13:58.767Z" } ] diff --git a/src/migrate/index.js b/src/migrate/index.js index 3eb5df962..6c5665dd5 100644 --- a/src/migrate/index.js +++ b/src/migrate/index.js @@ -1,19 +1,45 @@ +const DEBUG = require('debug')('worker') const Asset = require('../models/Asset') const Market = require('../models/Market') const assets = require('./data/assets.json') const markets = require('./data/markets.json') -const logHeader = '[MIGRATE]' +module.exports.run = async () => { + const force = process.env.FORCE_MIGRATE + console.log('Running migrate as force?', !!force) -module.exports.run = async (options = {}) => { - console.log(`${logHeader} Seeding data...`) - await Asset.deleteMany({}) - const newAssets = await Asset.insertMany(assets, { ordered: false }) - console.log(`${logHeader} ${newAssets.length} assets have been set`) + const keepAlive = process.env.KEEP_ALIVE || false + DEBUG(`keep alive? ${keepAlive}`) - await Market.deleteMany({}) - const newMarkets = await Market.insertMany(markets, { ordered: false }) - console.log(`${logHeader} ${newMarkets.length} markets have been set`) + if (!force && await hasData()) { + DEBUG('Data is already seeded.') + } else { + DEBUG('Seeding data...') + await Asset.deleteMany({}) + const newAssets = await Asset.insertMany(assets, { ordered: false }) + DEBUG(`${newAssets.length} assets have been set`) - process.exit() + await Market.deleteMany({}) + const newMarkets = await Market.insertMany(markets, { ordered: false }) + DEBUG(`${newMarkets.length} markets have been set`) + } + + if (!keepAlive) process.exit() +} + +async function hasData () { + let result = true + + DEBUG('Checking for existing assets') + + const existingAssets = await Asset.find({}).exec() + + if (!existingAssets || existingAssets.length === 0) { + DEBUG('No assets found.') + result = false + } else { + DEBUG('Existing assets found:', existingAssets) + } + + return result } diff --git a/worker.dockerfile b/worker.dockerfile new file mode 100644 index 000000000..0c4b8b3ba --- /dev/null +++ b/worker.dockerfile @@ -0,0 +1,20 @@ +FROM node:15.7.0-alpine + +# ------------------- +# Build app directory +# ------------------- +WORKDIR /app + +# Build dependencies +COPY package*.json ./ +RUN npm ci + +# Bundle app source +COPY bin/ ./bin +COPY src/ ./src +COPY LICENSE.md ./ + +# ------------- +# Start Service +# ------------- +CMD ["./bin/start-worker.sh"]