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

chore: make config overridable by environment variables #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# URL of the Ethereum node
URL=http://localhost:8545
# Default to 24 hours
TIMEOUT=86400000
RETRY_INTERVAL=10000
POLLING_INTERVAL=30000
CONFIRMATIONS=1
# Default to 10 minutes
CONFIRMATION_TIMEOUT=600000
GAS_LIMIT_MULTIPLIER=160
GAS_PRICE_MULTIPLIER=160
BALANCE_THRESHOLD=0.05
# Gas price predictor strategy. One of < eth-provider | fast | fastest | safeLow | average >
GAS_PRICE_PROVIDER=eth-provider

# ETH Gas Station API parameters
GAS_STATION_API_CHAIN_ID=1
GAS_STATION_API_URL=https://ethgasstation.info/json/ethgasAPI.json
GAS_STATION_API_KEY=
GAS_STATION_API_REQUEST_TIMEOUT_MS=10000

# When loading wallet from MNEMONIC
MNEMONIC=

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a good idea to have the option to set the MNEMONIC here, some users might set this and it's definitely very unsafe.

Copy link
Contributor Author

@alexandre-abrioux alexandre-abrioux Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is very unsafe indeed but the only way as far as I know to start the node without a TTY, in a CI pipeline for instance. We could provide an option to use Docker Swarm Secrets or Kubernetes File Secrets with an environment variables like MNEMONIC_FILE targeting the path of a mounted file in the container, but it probably is worth opening a whole new issue for this. The MNEMONIC env var is an undocumented feature but it could be nice to reference the variable here for advanced users who are able to add other layers of security to prevent leaks until a better solution is offered. We could even write a warning in the comment like so:

# [UNSAFE, DO NOT USE] When loading wallet from MNEMONIC

What do you think?

MNEMONIC_PATH=

# When loading wallet from SEED
SEED=

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem as MNEMONIC

2 changes: 1 addition & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extension": ["ts"],
"timeout": 10000,
"require": "ts-node/register",
"require": ["ts-node/register", "test/setup.ts"],
"spec": "test/**/*.spec.ts",
"watch-files": ["test/**/*.ts"]
}
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ COPY package.json ./
COPY yarn.lock ./
RUN yarn install --frozen-lockfile

## We just need the build to execute the command
# We just need the build to execute the command
COPY --from=builder /usr/src/app/dist ./dist
# And the default environment variables
COPY .env ./
ENTRYPOINT ["node", "/app/dist/index.js"]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@cartesi/util": "^1.0.1",
"axios": "^0.21.4",
"chalk": "^4.1.2",
"dotenv": "^10.0.0",
"ethers": "^5.4.7",
"humanize-duration": "^3.27.0",
"loglevel": "^1.7.1",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const builder = (yargs: Argv) => {
return yargs
.option("url", {
describe: "URL of the Ethereum node",
default: process.env.URL || "http://localhost:8545",
default: process.env.URL,
})
.option("wallet", {
describe: "Filename of JSON wallet file",
Expand All @@ -48,7 +48,7 @@ export const builder = (yargs: Argv) => {
})
.option("gasPrice", {
describe: "Gas price predictor strategy",
default: process.env.GAS_PRICE_PROVIDER || "eth-provider",
default: process.env.GAS_PRICE_PROVIDER,
demandOption: true,
choices: gasPriceProviderTypes,
})
Expand Down
35 changes: 23 additions & 12 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,26 @@

import { parseEther } from "@ethersproject/units";

export const TIMEOUT = 24 * 60 * 60 * 1000;
export const RETRY_INTERVAL = 10000;
export const POLLING_INTERVAL = 30000;
export const CONFIRMATIONS = 1;
export const CONFIRMATION_TIMEOUT = 10 * 60 * 1000; // 10 minutes
export const GAS_LIMIT_MULTIPLIER = 160;
export const GAS_PRICE_MULTIPLIER = 160;
export const BALANCE_THRESHOLD = parseEther("0.05");
export const GAS_STATION_API_CHAIN_ID = 1;
export const GAS_STATION_API_URL =
"https://ethgasstation.info/json/ethgasAPI.json";
export const GAS_STATION_API_REQUEST_TIMEOUT_MS = 10000;
export const TIMEOUT = parseInt(<string>process.env.TIMEOUT);
export const RETRY_INTERVAL = parseInt(<string>process.env.RETRY_INTERVAL);
export const POLLING_INTERVAL = parseInt(<string>process.env.POLLING_INTERVAL);
export const CONFIRMATIONS = parseInt(<string>process.env.CONFIRMATIONS);
export const CONFIRMATION_TIMEOUT = parseInt(
<string>process.env.CONFIRMATION_TIMEOUT
);
export const GAS_LIMIT_MULTIPLIER = parseInt(
<string>process.env.GAS_LIMIT_MULTIPLIER
);
export const GAS_PRICE_MULTIPLIER = parseInt(
<string>process.env.GAS_PRICE_MULTIPLIER
);
export const BALANCE_THRESHOLD = parseEther(
<string>process.env.BALANCE_THRESHOLD
);
export const GAS_STATION_API_CHAIN_ID = parseInt(
<string>process.env.GAS_STATION_API_CHAIN_ID
);
export const GAS_STATION_API_URL = process.env.GAS_STATION_API_URL;
export const GAS_STATION_API_REQUEST_TIMEOUT_MS = parseInt(
<string>process.env.GAS_STATION_API_REQUEST_TIMEOUT_MS
);
13 changes: 13 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2020 Cartesi Pte. Ltd.

// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

import { config } from "dotenv";
config();
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

import "./env";
import log from "loglevel";
import chalk from "chalk";
import prefix from "loglevel-plugin-prefix";
Expand Down
12 changes: 12 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2020 Cartesi Pte. Ltd.

// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

import "../src/env";
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3548,6 +3548,11 @@ domutils@^2.5.2, domutils@^2.6.0, domutils@^2.7.0:
domelementtype "^2.2.0"
domhandler "^4.2.0"

dotenv@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81"
integrity sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==

dotignore@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/dotignore/-/dotignore-0.1.2.tgz#f942f2200d28c3a76fbdd6f0ee9f3257c8a2e905"
Expand Down