diff --git a/.env b/.env new file mode 100644 index 0000000..3292213 --- /dev/null +++ b/.env @@ -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= +MNEMONIC_PATH= + +# When loading wallet from SEED +SEED= \ No newline at end of file diff --git a/.mocharc.json b/.mocharc.json index bd97ae3..c0a6a66 100644 --- a/.mocharc.json +++ b/.mocharc.json @@ -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"] } diff --git a/Dockerfile b/Dockerfile index 50519cc..4d3fbe3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] diff --git a/package.json b/package.json index 2b784da..6744d0e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/commands/start.ts b/src/commands/start.ts index 32e289e..245d4e9 100644 --- a/src/commands/start.ts +++ b/src/commands/start.ts @@ -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", @@ -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, }) diff --git a/src/config.ts b/src/config.ts index ad1415b..0c1d81f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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(process.env.TIMEOUT); +export const RETRY_INTERVAL = parseInt(process.env.RETRY_INTERVAL); +export const POLLING_INTERVAL = parseInt(process.env.POLLING_INTERVAL); +export const CONFIRMATIONS = parseInt(process.env.CONFIRMATIONS); +export const CONFIRMATION_TIMEOUT = parseInt( + process.env.CONFIRMATION_TIMEOUT +); +export const GAS_LIMIT_MULTIPLIER = parseInt( + process.env.GAS_LIMIT_MULTIPLIER +); +export const GAS_PRICE_MULTIPLIER = parseInt( + process.env.GAS_PRICE_MULTIPLIER +); +export const BALANCE_THRESHOLD = parseEther( + process.env.BALANCE_THRESHOLD +); +export const GAS_STATION_API_CHAIN_ID = parseInt( + 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( + process.env.GAS_STATION_API_REQUEST_TIMEOUT_MS +); diff --git a/src/env.ts b/src/env.ts new file mode 100644 index 0000000..ece9fa5 --- /dev/null +++ b/src/env.ts @@ -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(); diff --git a/src/index.ts b/src/index.ts index c7d878e..ee31ed8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/test/setup.ts b/test/setup.ts new file mode 100644 index 0000000..3d56acb --- /dev/null +++ b/test/setup.ts @@ -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"; diff --git a/yarn.lock b/yarn.lock index 949058a..5e92caa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"