diff --git a/.dockerignore b/.dockerignore index 3ec281a15..504d71996 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,16 @@ +.circleci +.drone.yml +.editorconfig +.env* +.git* +.github/ +.prettie* +*.md +*.MD +*ignore +0x_mesh_test/ +docker-compose* +Dockerfile node_modules postgres 0x_mesh -.circleci \ No newline at end of file diff --git a/.env_prod b/.env_prod new file mode 100644 index 000000000..dad7a4380 --- /dev/null +++ b/.env_prod @@ -0,0 +1,37 @@ +# Required +CHAIN_ID= +ETHEREUM_RPC_URL= + +# Optional +MESH_WEBSOCKET_URI= +MESH_HTTP_URI= +POSTGRES_URI= +FEE_RECIPIENT_ADDRESS= +MAKER_FEE_ASSET_DATA= +MAKER_FEE_UNIT_AMOUNT= +MAKER_FEE_ASSET_DATA= +TAKER_FEE_UNIT_AMOUNT= +# POSTGRES_URI=postgresql://api:api@qa-pgsql/api +WHITELIST_ALL_TOKENS=true +LIQUIDITY_POOL_REGISTRY_ADDRESS= +MESH_IGNORED_ADDRESSES= +SWAP_IGNORED_ADDRESSES= +HTTP_KEEP_ALIVE_TIMEOUT= +HTTP_HEADERS_TIMEOUT= +SRA_ORDER_EXPIRATION_BUFFER_SECONDS= +MAX_EXPIRATION_ALERT_THRESHOLD_SECONDS= +PINNED_POOL_IDS= +PINNED_MM_ADDRESSES= +WHITELISTED_API_KEYS_META_TXN_SUBMIT= +META_TXN_RELAY_PRIVATE_KEYS= +META_TXN_RELAY_EXPECTED_MINED_SEC= +ENABLE_PROMETHEUS_METRICS= +PROMETHEUS_PORT= +RFQT_API_KEY_WHITELIST= +PLP_API_KEY_WHITELIST=* + +# For release +GITHUB_TOKEN= + +INDICATIVE_PRICE_AWARE_RFQ_ENABLED= +FIRM_PRICE_AWARE_RFQ_ENABLED= diff --git a/Dockerfile b/Dockerfile index cc65cadab..d8427e179 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,28 +1,16 @@ -# Stage 1 -FROM node:12-alpine as yarn-install +FROM node:14-alpine as yarn-install WORKDIR /usr/src/app # Install app dependencies COPY package.json yarn.lock ./ -RUN apk update && \ - apk upgrade && \ - apk add --no-cache --virtual build-dependencies bash git openssh python make g++ && \ - yarn --frozen-lockfile --no-cache && \ - apk del build-dependencies && \ - yarn cache clean - +RUN apk add -U --virtual build-dependencies bash git openssh python make g++ +RUN yarn --frozen-lockfile --no-cache # Runtime container with minimal dependencies -FROM node:12-alpine - -RUN apk update && \ - apk upgrade && \ - apk add ca-certificates - +FROM node:14-alpine WORKDIR /usr/src/app COPY --from=yarn-install /usr/src/app/node_modules /usr/src/app/node_modules # Bundle app source COPY . . - RUN yarn build EXPOSE 3000 -CMD [ "node", "lib/src/index.js" ] +CMD [ "yarn", "start:db_migrate"] diff --git a/README.md b/README.md index a76346010..a8c24e646 100644 --- a/README.md +++ b/README.md @@ -136,9 +136,25 @@ ETHEREUM_RPC_URL=http://ganache:8545 Then run ``` -$ docker-compose up +$ docker-compose -f docker-compose-test-ganache.yml $ yarn dev ``` +#### Developing on Ropsten + +export env vars to point to Chain id and ethereum rpc url: +```sh +export CHAIN_ID=123 +export ETHEREUM_RPC_URL=https://example.com/id +``` + +Run deployment with existing `0xapi:latest` docker image +```sh +docker-compose -f docker-compose.yml up -d +``` +Alternatively it is possible to specify `--build` argument to rebuild the image from local foler and then run/update the deployment +```sh +docker-compose -f docker-compose.yml up -d --build +``` ## Testing @@ -152,6 +168,7 @@ Tip: Test suites set up and teardown sandboxed environments, so using `.only` on - `yarn test` - Test the code - `yarn lint` - Lint the code - `yarn start` - Starts the API +- `yarn start:db_migrate` - to be used in containers, runs migrations and starts the API - `yarn dev` - Starts the API in dev-mode - `yarn watch` - Watch the source code and rebuild on change - `yarn prettier` - Auto-format the code diff --git a/docker-compose-services.yml b/docker-compose-services.yml new file mode 100644 index 000000000..c97c4c111 --- /dev/null +++ b/docker-compose-services.yml @@ -0,0 +1,45 @@ +version: '3' +services: + ganache: + image: '0xorg/ganache-cli:6.5.10' + ports: + - '8545:8545' + environment: + VERSION: 8.0.1 + CHAIN_ID: 1337 + NETWORK_ID: 1337 + postgres: + image: postgres:9.6 + environment: + - POSTGRES_USER=api + - POSTGRES_PASSWORD=api + - POSTGRES_DB=api + # persist the postgres data to disk so we don't lose it + # on rebuilds. + volumes: + - ./postgres:/var/lib/postgresql/data + ports: + - '5432:5432' + mesh: + image: 0xorg/mesh:11.0.1 + depends_on: + - ganache + restart: on-failure + environment: + ETHEREUM_RPC_URL: '${ETHEREUM_RPC_URL}' + ETHEREUM_CHAIN_ID: '${CHAIN_ID}' + USE_BOOTSTRAP_LIST: 'false' + VERBOSITY: 4 + PRIVATE_KEY_PATH: '' + ENABLE_GRAPHQL_SERVER: 'true' + ENABLE_GRAPHQL_PLAYGROUND: 'true' + BLOCK_POLLING_INTERVAL: '2s' + ETHEREUM_RPC_MAX_REQUESTS_PER_24_HR_UTC: '150000' + volumes: + - ./0x_mesh_test:/usr/mesh/0x_mesh + ports: + - '60557:60557' + - '60558:60558' + - '60559:60559' + command: | + sh -c "waitForGanache () { until printf 'POST /\r\nContent-Length: 26\r\n\r\n{\"method\":\"net_listening\"}' | nc localhost 8545 | grep true; do continue; done }; waitForGanache && ./mesh" diff --git a/docker-compose-test.yml b/docker-compose-test-ganache.yml similarity index 100% rename from docker-compose-test.yml rename to docker-compose-test-ganache.yml diff --git a/docker-compose.yml b/docker-compose.yml index c97c4c111..ad72b242e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,45 +1,45 @@ version: '3' services: - ganache: - image: '0xorg/ganache-cli:6.5.10' - ports: - - '8545:8545' - environment: - VERSION: 8.0.1 - CHAIN_ID: 1337 - NETWORK_ID: 1337 postgres: image: postgres:9.6 environment: - POSTGRES_USER=api - POSTGRES_PASSWORD=api - POSTGRES_DB=api - # persist the postgres data to disk so we don't lose it - # on rebuilds. - volumes: - - ./postgres:/var/lib/postgresql/data ports: - '5432:5432' mesh: - image: 0xorg/mesh:11.0.1 - depends_on: - - ganache + image: 0xorg/mesh:11.0.3 restart: on-failure environment: ETHEREUM_RPC_URL: '${ETHEREUM_RPC_URL}' ETHEREUM_CHAIN_ID: '${CHAIN_ID}' - USE_BOOTSTRAP_LIST: 'false' - VERBOSITY: 4 - PRIVATE_KEY_PATH: '' ENABLE_GRAPHQL_SERVER: 'true' - ENABLE_GRAPHQL_PLAYGROUND: 'true' - BLOCK_POLLING_INTERVAL: '2s' - ETHEREUM_RPC_MAX_REQUESTS_PER_24_HR_UTC: '150000' + VERBOSITY: 3 volumes: - ./0x_mesh_test:/usr/mesh/0x_mesh ports: - - '60557:60557' - - '60558:60558' - - '60559:60559' - command: | - sh -c "waitForGanache () { until printf 'POST /\r\nContent-Length: 26\r\n\r\n{\"method\":\"net_listening\"}' | nc localhost 8545 | grep true; do continue; done }; waitForGanache && ./mesh" + - '127.0.0.1:60557:60557' + - '127.0.0.1:60558:60558' + - '127.0.0.1:60559:60559' + 0xapi: + image: 0xapi:latest + build: + context: ./ + dockerfile: Dockerfile + command: ["yarn", "start:db_migrate"] + depends_on: + - postgres + - mesh + labels: + - "com.centurylinklabs.watchtower.enable=false" + volumes: + - .env_prod:/usr/src/app/.env + ports: + - 3000:3000 + environment: + ETHEREUM_RPC_URL: '${ETHEREUM_RPC_URL}' + ETHEREUM_CHAIN_ID: '${CHAIN_ID}' + POSTGRES_URI: 'postgresql://api:api@postgres/api' + MESH_WEBSOCKET_URI: http://mesh:60557 + MESH_HTTP_URI: http://mesh:60557 diff --git a/package.json b/package.json index a26a983da..ee2a65c31 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "prettier": "prettier --write ${npm_package_config_prettier_target} --config .prettierrc", "prettier:ci": "prettier --list-different ${npm_package_config_prettier_target} --config .prettierrc", "start": "node -r dotenv/config lib/src/index.js", + "start:db_migrate": "yarn db:migrate && node -r dotenv/config lib/src/index.js", "start:service:http": "node -r dotenv/config lib/src/runners/http_service_runner.js", "start:service:sra_http": "node -r dotenv/config lib/src/runners/http_sra_service_runner.js", "start:service:swap_http": "node -r dotenv/config lib/src/runners/http_swap_service_runner.js",