diff --git a/.changeset/curvy-mangos-push.md b/.changeset/curvy-mangos-push.md new file mode 100644 index 000000000..c26fd67e9 --- /dev/null +++ b/.changeset/curvy-mangos-push.md @@ -0,0 +1,5 @@ +--- +"@nocturne-xyz/offchain-utils": minor +--- + +Dry up ethers, defender config code and add to offchain utils diff --git a/.changeset/spicy-tigers-collect.md b/.changeset/spicy-tigers-collect.md new file mode 100644 index 000000000..1b745a73f --- /dev/null +++ b/.changeset/spicy-tigers-collect.md @@ -0,0 +1,9 @@ +--- +"@nocturne-xyz/deposit-screener": patch +"@nocturne-xyz/insertion-writer": patch +"@nocturne-xyz/subtree-updater": patch +"@nocturne-xyz/test-actor": patch +"@nocturne-xyz/bundler": patch +--- + +Change log level flag to just --log-level diff --git a/Dockerfile b/Dockerfile index 6f091376f..a5f7f4fc6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -15,7 +15,7 @@ COPY . . RUN yarn install -RUN for component in bundler deposit-screener insertion-writer subtree-updater test-actor; do \ +RUN for component in bundler deposit-screener insertion-writer subtree-updater test-actor balance-monitor; do \ yarn turbo run build --filter="@nocturne-xyz/$component" \ # setup CLI using loop && cd /app/actors/$component \ diff --git a/actors/bundler/src/cli/commands/run/batcher.ts b/actors/bundler/src/cli/commands/run/batcher.ts index 58fbf0657..285b47a4a 100644 --- a/actors/bundler/src/cli/commands/run/batcher.ts +++ b/actors/bundler/src/cli/commands/run/batcher.ts @@ -22,11 +22,11 @@ const runBatcher = new Command("batcher") "./logs/bundler-server" ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(async (options) => { - const { configNameOrPath, maxLatency, batchSize, logDir, stdoutLogLevel } = + const { configNameOrPath, maxLatency, batchSize, logDir, logLevel } = options; // TODO: consolidate batcher and submitter into one component (batcher doesn't need config name) @@ -35,7 +35,7 @@ const runBatcher = new Command("batcher") logDir, `${configName}-bundler`, "batcher", - stdoutLogLevel + logLevel ); const batcher = new BundlerBatcher( getRedis(), diff --git a/actors/bundler/src/cli/commands/run/server.ts b/actors/bundler/src/cli/commands/run/server.ts index 97b58351c..821c8a1a9 100644 --- a/actors/bundler/src/cli/commands/run/server.ts +++ b/actors/bundler/src/cli/commands/run/server.ts @@ -22,11 +22,11 @@ const runServer = new Command("server") "./logs/bundler-server" ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(async (options) => { - const { configNameOrPath, port, bundlerAddress, logDir, stdoutLogLevel } = + const { configNameOrPath, port, bundlerAddress, logDir, logLevel } = options; const config = loadNocturneConfig(configNameOrPath); @@ -41,7 +41,7 @@ const runServer = new Command("server") logDir, `${configName}-bundler`, "server", - stdoutLogLevel + logLevel ); const server = new BundlerServer( bundlerAddress, diff --git a/actors/bundler/src/cli/commands/run/submitter.ts b/actors/bundler/src/cli/commands/run/submitter.ts index 715a905ab..f63a27d83 100644 --- a/actors/bundler/src/cli/commands/run/submitter.ts +++ b/actors/bundler/src/cli/commands/run/submitter.ts @@ -1,14 +1,11 @@ import { Command } from "commander"; -import { ethers } from "ethers"; import { BundlerSubmitter } from "../../../submitter"; -import { makeLogger } from "@nocturne-xyz/offchain-utils"; +import { + makeLogger, + getEthersProviderAndSignerFromEnvConfiguration, +} from "@nocturne-xyz/offchain-utils"; import { getRedis } from "./utils"; import { extractConfigName, loadNocturneConfig } from "@nocturne-xyz/config"; -import { - DefenderRelayProvider, - DefenderRelaySigner, -} from "@openzeppelin/defender-relay-client/lib/ethers"; -import { Speed } from "@openzeppelin/defender-relay-client"; const runSubmitter = new Command("submitter") .summary("run bundler submitter") @@ -25,7 +22,7 @@ const runSubmitter = new Command("submitter") "./logs/bundler-submitter" ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .option( @@ -34,42 +31,17 @@ const runSubmitter = new Command("submitter") parseInt ) .action(async (options) => { - const { configNameOrPath, logDir, stdoutLogLevel, finalityBlocks } = - options; + const { configNameOrPath, logDir, logLevel, finalityBlocks } = options; const config = loadNocturneConfig(configNameOrPath); - const relayerApiKey = process.env.OZ_RELAYER_API_KEY; - const relayerApiSecret = process.env.OZ_RELAYER_API_SECRET; - const relayerSpeed = process.env.OZ_RELAYER_SPEED; - - const privateKey = process.env.TX_SIGNER_KEY; - const rpcUrl = process.env.RPC_URL; - - let signer: ethers.Signer; - if (relayerApiKey && relayerApiSecret) { - const credentials = { - apiKey: relayerApiKey, - apiSecret: relayerApiSecret, - }; - const provider = new DefenderRelayProvider(credentials); - signer = new DefenderRelaySigner(credentials, provider, { - speed: (relayerSpeed as Speed) ?? "safeLow", - }); - } else if (rpcUrl && privateKey) { - const provider = new ethers.providers.JsonRpcProvider(rpcUrl); - signer = new ethers.Wallet(privateKey, provider); - } else { - throw new Error( - "missing RPC_URL/PRIVATE_KEY or OZ_RELAYER_API_KEY/OZ_RELAYER_API_SECRET" - ); - } + const { signer } = getEthersProviderAndSignerFromEnvConfiguration(); const configName = extractConfigName(configNameOrPath); const logger = makeLogger( logDir, `${configName}-bundler`, "submitter", - stdoutLogLevel + logLevel ); const submitter = new BundlerSubmitter( config.tellerAddress, diff --git a/actors/deposit-screener/src/cli/commands/inspect/check.ts b/actors/deposit-screener/src/cli/commands/inspect/check.ts index 665047e05..aca371869 100644 --- a/actors/deposit-screener/src/cli/commands/inspect/check.ts +++ b/actors/deposit-screener/src/cli/commands/inspect/check.ts @@ -17,7 +17,7 @@ import path from "path"; /** * Example - * yarn deposit-screener-cli inspect check --snapshot-json-path ./snapshot/addresses.json --output-dir output --stdout-log-level=info + * yarn deposit-screener-cli inspect check --snapshot-json-path ./snapshot/addresses.json --output-dir output --log-level=info */ const runChecker = new Command("check") .summary("inspect and analyze addresses from a snapshot JSON file") @@ -38,7 +38,7 @@ const runChecker = new Command("check") "./logs/address-checker" ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(main); @@ -61,18 +61,13 @@ function showReasonCounts( } async function main(options: any): Promise { - const { snapshotJsonPath, outputDir, logDir, stdoutLogLevel } = options; + const { snapshotJsonPath, outputDir, logDir, logLevel } = options; ensureExists(snapshotJsonPath, { path: outputDir, type: "DIRECTORY", }); - const logger = makeLogger( - logDir, - "address-checker", - "checker", - stdoutLogLevel - ); + const logger = makeLogger(logDir, "address-checker", "checker", logLevel); const redis = await getLocalRedis(); const ruleset = RULESET_V1(redis); diff --git a/actors/deposit-screener/src/cli/commands/inspect/snapshot.ts b/actors/deposit-screener/src/cli/commands/inspect/snapshot.ts index 971f1f777..87ce8c55b 100644 --- a/actors/deposit-screener/src/cli/commands/inspect/snapshot.ts +++ b/actors/deposit-screener/src/cli/commands/inspect/snapshot.ts @@ -16,7 +16,7 @@ import { /** * Example - * yarn deposit-screener-cli inspect snapshot --input-csv ./data/addresses.csv --output-data ./data/addresses.json --delay-ms 3000 --stdout-log-level info + * yarn deposit-screener-cli inspect snapshot --input-csv ./data/addresses.csv --output-data ./data/addresses.json --delay-ms 3000 --log-level info */ const runSnapshot = new Command("snapshot") .summary("create data snapshot for CSV or JSON file of addresses") @@ -42,7 +42,7 @@ const runSnapshot = new Command("snapshot") "500" ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(main); @@ -65,15 +65,10 @@ async function parseAndFilterCsvOfAddresses(path: string): Promise { async function main(options: any): Promise { requireApiKeys(); - const { inputCsv, outputData, logDir, stdoutLogLevel, delayMs } = options; + const { inputCsv, outputData, logDir, logLevel, delayMs } = options; ensureExists(inputCsv, { path: outputData, type: "FILE" }); - const logger = makeLogger( - logDir, - "address-checker", - "checker", - stdoutLogLevel - ); + const logger = makeLogger(logDir, "address-checker", "checker", logLevel); logger.info(`Starting snapshot for addresses from ${inputCsv}`); const dedupedAddresses = await parseAndFilterCsvOfAddresses(inputCsv); diff --git a/actors/deposit-screener/src/cli/commands/inspect/trmTxMonitor.ts b/actors/deposit-screener/src/cli/commands/inspect/trmTxMonitor.ts index 245d0f9bc..d004fe577 100644 --- a/actors/deposit-screener/src/cli/commands/inspect/trmTxMonitor.ts +++ b/actors/deposit-screener/src/cli/commands/inspect/trmTxMonitor.ts @@ -16,7 +16,7 @@ import { getCoinMarketCapPriceConversion } from "./helpers"; /** * Example - * yarn deposit-screener-cli inspect trmTxMonitor --token-address 0x6b175474e89094c44da98b954eedeac495271d0f --eth-transfer-style indirect --from-address 0xd90e2f925DA726b50C4Ed8D0Fb90Ad053324F31b --start-block 0 --end-block 27025780 --stdout-log-level=info + * yarn deposit-screener-cli inspect trmTxMonitor --token-address 0x6b175474e89094c44da98b954eedeac495271d0f --eth-transfer-style indirect --from-address 0xd90e2f925DA726b50C4Ed8D0Fb90Ad053324F31b --start-block 0 --end-block 27025780 --log-level=info */ const runTrmTxMonitor = new Command("trmTxMonitor") .summary( @@ -45,7 +45,7 @@ const runTrmTxMonitor = new Command("trmTxMonitor") "./logs/address-checker" ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(main); @@ -58,15 +58,10 @@ async function main(options: any): Promise { startBlock, endBlock, logDir, - stdoutLogLevel, + logLevel, } = options; - const logger = makeLogger( - logDir, - "trm-tx-monitor", - "monitor", - stdoutLogLevel - ); + const logger = makeLogger(logDir, "trm-tx-monitor", "monitor", logLevel); const redis = await getLocalRedis(); diff --git a/actors/deposit-screener/src/cli/commands/run/processor.ts b/actors/deposit-screener/src/cli/commands/run/processor.ts index 7d2d0a61a..30b6cfd04 100644 --- a/actors/deposit-screener/src/cli/commands/run/processor.ts +++ b/actors/deposit-screener/src/cli/commands/run/processor.ts @@ -1,9 +1,8 @@ import { extractConfigName, loadNocturneConfig } from "@nocturne-xyz/config"; -import { makeLogger } from "@nocturne-xyz/offchain-utils"; import { - DefenderRelayProvider, - DefenderRelaySigner, -} from "@openzeppelin/defender-relay-client/lib/ethers"; + makeLogger, + getEthersProviderAndSignerFromEnvConfiguration, +} from "@nocturne-xyz/offchain-utils"; import { Command } from "commander"; import { ethers } from "ethers"; import { DepositScreenerFulfiller } from "../../../fulfiller"; @@ -15,7 +14,6 @@ import { } from "../../../screening"; import { SubgraphDepositEventSyncAdapter } from "@nocturne-xyz/subgraph-sync-adapters"; import { getRedis } from "./utils"; -import { Speed } from "@openzeppelin/defender-relay-client"; const runProcess = new Command("processor") .summary("process deposit requests") @@ -46,7 +44,7 @@ const runProcess = new Command("processor") parseInt ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(async (options) => { @@ -58,14 +56,14 @@ const runProcess = new Command("processor") throw new Error(`ENVIRONMENT env var set to invalid value: ${env}`); } - const { configNameOrPath, logDir, throttleMs, stdoutLogLevel } = options; + const { configNameOrPath, logDir, throttleMs, logLevel } = options; const configName = extractConfigName(configNameOrPath); const logger = makeLogger( logDir, `${configName}-deposit-screener`, "processor", - stdoutLogLevel + logLevel ); const config = loadNocturneConfig(configNameOrPath); @@ -81,32 +79,8 @@ const runProcess = new Command("processor") logger.child({ function: "SubgraphDepositEventSyncAdapter" }) ); - const relayerApiKey = process.env.OZ_RELAYER_API_KEY; - const relayerApiSecret = process.env.OZ_RELAYER_API_SECRET; - const relayerSpeed = process.env.OZ_RELAYER_SPEED; - - const privateKey = process.env.TX_SIGNER_KEY; - const rpcUrl = process.env.RPC_URL; - - let provider: ethers.providers.Provider; - let signer: ethers.Signer; - if (relayerApiKey && relayerApiSecret) { - const credentials = { - apiKey: relayerApiKey, - apiSecret: relayerApiSecret, - }; - provider = new DefenderRelayProvider(credentials); - signer = new DefenderRelaySigner(credentials, provider, { - speed: (relayerSpeed as Speed) ?? "safeLow", - }); - } else if (rpcUrl && privateKey) { - provider = new ethers.providers.JsonRpcProvider(rpcUrl); - signer = new ethers.Wallet(privateKey, provider); - } else { - throw new Error( - "missing RPC_URL/PRIVATE_KEY or OZ_RELAYER_API_KEY/OZ_RELAYER_API_SECRET" - ); - } + const { signer, provider } = + getEthersProviderAndSignerFromEnvConfiguration(); const attestationSignerKey = process.env.ATTESTATION_SIGNER_KEY; if (!attestationSignerKey) { diff --git a/actors/deposit-screener/src/cli/commands/run/server.ts b/actors/deposit-screener/src/cli/commands/run/server.ts index 76b25a10b..90407c093 100644 --- a/actors/deposit-screener/src/cli/commands/run/server.ts +++ b/actors/deposit-screener/src/cli/commands/run/server.ts @@ -29,7 +29,7 @@ const runServer = new Command("server") "./logs/deposit-screener" ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(async (options) => { @@ -41,7 +41,7 @@ const runServer = new Command("server") throw new Error(`ENVIRONMENT env var set to invalid value: ${env}`); } - const { configNameOrPath, port, logDir, stdoutLogLevel } = options; + const { configNameOrPath, port, logDir, logLevel } = options; const config = loadNocturneConfig(configNameOrPath); @@ -67,7 +67,7 @@ const runServer = new Command("server") logDir, `${configName}-deposit-screener`, "server", - stdoutLogLevel + logLevel ); const server = new DepositScreenerServer( logger, diff --git a/actors/deposit-screener/test/scripts/generateRulesetV1CallsSnapshot.ts b/actors/deposit-screener/test/scripts/generateRulesetV1CallsSnapshot.ts index c63e62dc2..7d1c42b23 100644 --- a/actors/deposit-screener/test/scripts/generateRulesetV1CallsSnapshot.ts +++ b/actors/deposit-screener/test/scripts/generateRulesetV1CallsSnapshot.ts @@ -45,7 +45,7 @@ async function run() { const outputPath = getNewSnapshotFolderPath(); await execAsync( - `yarn build && yarn deposit-screener-cli inspect snapshot --input-csv ./snapshotTestCases.csv --output-data ${outputPath}/snapshot.json --delay-ms ${800} --stdout-log-level debug` + `yarn build && yarn deposit-screener-cli inspect snapshot --input-csv ./snapshotTestCases.csv --output-data ${outputPath}/snapshot.json --delay-ms ${800} --log-level debug` ); process.exit(0); diff --git a/actors/insertion-writer/src/cli/commands/run/runInsertionWriter.ts b/actors/insertion-writer/src/cli/commands/run/runInsertionWriter.ts index 2273294dd..c6fd88a07 100644 --- a/actors/insertion-writer/src/cli/commands/run/runInsertionWriter.ts +++ b/actors/insertion-writer/src/cli/commands/run/runInsertionWriter.ts @@ -33,7 +33,7 @@ export const runInsertionWriter = new Command("insertion-writer") parseInt ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(async (options) => { @@ -42,7 +42,7 @@ export const runInsertionWriter = new Command("insertion-writer") logDir, throttleMs, throttleOnEmptyMs, - stdoutLogLevel, + logLevel, finalityBlocks, } = options; @@ -51,7 +51,7 @@ export const runInsertionWriter = new Command("insertion-writer") logDir, `${configName}-insertion-writer`, "insertion-writer", - stdoutLogLevel + logLevel ); const config = loadNocturneConfig(configNameOrPath); diff --git a/actors/subtree-updater/Dockerfile b/actors/subtree-updater/Dockerfile index 11f1b1297..b76a20f29 100644 --- a/actors/subtree-updater/Dockerfile +++ b/actors/subtree-updater/Dockerfile @@ -26,8 +26,8 @@ COPY --from=circuit-artifacts ./subtreeupdate ./circuit-artifacts/subtreeupdate RUN yarn install # build witness generator -WORKDIR /app/packages/circuits -RUN ./scripts/build_witness_generator.sh +WORKDIR /app/actors/subtree-updater +RUN ./build_witness_generator.sh # build diff --git a/actors/subtree-updater/build_witness_generator.sh b/actors/subtree-updater/build_witness_generator.sh index 938e7731e..f9c8bea0a 100755 --- a/actors/subtree-updater/build_witness_generator.sh +++ b/actors/subtree-updater/build_witness_generator.sh @@ -1,15 +1,16 @@ #!/bin/bash SCRIPT_DIR=$(dirname "$0") -ROOT_DIR="$SCRIPT_DIR/../../../" +ROOT_DIR="$SCRIPT_DIR/../../" CIRCUIT_NAME="subtreeupdate" CIRCUIT_ARTIFACTS_DIR="$ROOT_DIR/circuit-artifacts/" pushd "$CIRCUIT_ARTIFACTS_DIR"/"$CIRCUIT_NAME"/"$CIRCUIT_NAME"_cpp + # clean rm *.o rm "$CIRCUIT_NAME" # build make -popd +popd \ No newline at end of file diff --git a/actors/subtree-updater/src/cli/commands/run/runSubtreeUpdater.ts b/actors/subtree-updater/src/cli/commands/run/runSubtreeUpdater.ts index a109039cb..cdd8689c9 100644 --- a/actors/subtree-updater/src/cli/commands/run/runSubtreeUpdater.ts +++ b/actors/subtree-updater/src/cli/commands/run/runSubtreeUpdater.ts @@ -1,8 +1,10 @@ import { Command } from "commander"; -import { ethers } from "ethers"; import { SubtreeUpdater } from "../../../subtreeUpdater"; import { getRedis } from "../utils"; -import { makeLogger } from "@nocturne-xyz/offchain-utils"; +import { + makeLogger, + getEthersProviderAndSignerFromEnvConfiguration, +} from "@nocturne-xyz/offchain-utils"; import { extractConfigName, loadNocturneConfig } from "@nocturne-xyz/config"; import { Handler__factory } from "@nocturne-xyz/contracts"; import { @@ -11,11 +13,6 @@ import { assertOrErr, } from "@nocturne-xyz/core"; import { RapidsnarkSubtreeUpdateProver } from "../../../rapidsnarkProver"; -import { - DefenderRelayProvider, - DefenderRelaySigner, -} from "@openzeppelin/defender-relay-client/lib/ethers"; -import { Speed } from "@openzeppelin/defender-relay-client"; export const runSubtreeUpdater = new Command("subtree-updater") .summary("run subtree updater service") @@ -53,7 +50,7 @@ export const runSubtreeUpdater = new Command("subtree-updater") "./logs/subtree-updater" ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(async (options) => { @@ -66,7 +63,7 @@ export const runSubtreeUpdater = new Command("subtree-updater") witnessGeneratorPath, zkeyPath, vkeyPath, - stdoutLogLevel, + logLevel, } = options; const configName = extractConfigName(configNameOrPath); @@ -74,7 +71,7 @@ export const runSubtreeUpdater = new Command("subtree-updater") logDir, `${configName}-subtree-updater`, "subtree-updater", - stdoutLogLevel + logLevel ); const config = loadNocturneConfig(configNameOrPath); @@ -86,31 +83,7 @@ export const runSubtreeUpdater = new Command("subtree-updater") throw new Error("missing SUBGRAPH_URL"); } - const relayerApiKey = process.env.OZ_RELAYER_API_KEY; - const relayerApiSecret = process.env.OZ_RELAYER_API_SECRET; - const relayerSpeed = process.env.OZ_RELAYER_SPEED; - - const privateKey = process.env.TX_SIGNER_KEY; - const rpcUrl = process.env.RPC_URL; - - let signer: ethers.Signer; - if (relayerApiKey && relayerApiSecret) { - const credentials = { - apiKey: relayerApiKey, - apiSecret: relayerApiSecret, - }; - const provider = new DefenderRelayProvider(credentials); - signer = new DefenderRelaySigner(credentials, provider, { - speed: (relayerSpeed as Speed) ?? "safeLow", - }); - } else if (rpcUrl && privateKey) { - const provider = new ethers.providers.JsonRpcProvider(rpcUrl); - signer = new ethers.Wallet(privateKey, provider); - } else { - throw new Error( - "missing RPC_URL/PRIVATE_KEY or OZ_RELAYER_API_KEY/OZ_RELAYER_API_SECRET" - ); - } + const { signer } = getEthersProviderAndSignerFromEnvConfiguration(); const handlerContract = Handler__factory.connect( config.handlerAddress, diff --git a/actors/test-actor/src/cli/run.ts b/actors/test-actor/src/cli/run.ts index abfbbb977..49de651ac 100644 --- a/actors/test-actor/src/cli/run.ts +++ b/actors/test-actor/src/cli/run.ts @@ -16,13 +16,11 @@ import { Command } from "commander"; import { ethers } from "ethers"; import { TestActor } from "../actor"; import * as fs from "fs"; -import { makeLogger } from "@nocturne-xyz/offchain-utils"; -import { LMDBKVStore } from "../lmdb"; import { - DefenderRelayProvider, - DefenderRelaySigner, -} from "@openzeppelin/defender-relay-client/lib/ethers"; -import { Speed } from "@openzeppelin/defender-relay-client"; + makeLogger, + getEthersProviderAndSignerFromEnvConfiguration, +} from "@nocturne-xyz/offchain-utils"; +import { LMDBKVStore } from "../lmdb"; export const run = new Command("run") .summary("run test actor") @@ -67,7 +65,7 @@ export const run = new Command("run") "./logs/test-actor" ) .option( - "--stdout-log-level ", + "--log-level ", "min log importance to log to stdout. if not given, logs will not be emitted to stdout" ) .action(async (options) => { @@ -83,7 +81,7 @@ export const run = new Command("run") onlyDeposits, onlyOperations, logDir, - stdoutLogLevel, + logLevel, finalityBlocks, } = options; @@ -92,7 +90,7 @@ export const run = new Command("run") logDir, `${configName}-test-actor`, "actor", - stdoutLogLevel + logLevel ); const config = loadNocturneConfig(configNameOrPath); @@ -120,32 +118,8 @@ export const run = new Command("run") throw new Error("NOCTURNE_SPENDING_KEY must be 32 bytes"); } - const relayerApiKey = process.env.OZ_RELAYER_API_KEY; - const relayerApiSecret = process.env.OZ_RELAYER_API_SECRET; - const relayerSpeed = process.env.OZ_RELAYER_SPEED; - - const privateKey = process.env.TX_SIGNER_KEY; - const rpcUrl = process.env.RPC_URL; - - let provider: ethers.providers.JsonRpcProvider; - let signer: ethers.Signer; - if (relayerApiKey && relayerApiSecret) { - const credentials = { - apiKey: relayerApiKey, - apiSecret: relayerApiSecret, - }; - provider = new DefenderRelayProvider(credentials); - signer = new DefenderRelaySigner(credentials, provider, { - speed: (relayerSpeed as Speed) ?? "safeLow", - }); - } else if (rpcUrl && privateKey) { - provider = new ethers.providers.JsonRpcProvider(rpcUrl); - signer = new ethers.Wallet(privateKey, provider); - } else { - throw new Error( - "missing RPC_URL/PRIVATE_KEY or OZ_RELAYER_API_KEY/OZ_RELAYER_API_SECRET" - ); - } + const { signer, provider } = + getEthersProviderAndSignerFromEnvConfiguration(); const teller = Teller__factory.connect(config.tellerAddress, signer); const depositManager = DepositManager__factory.connect( diff --git a/package.json b/package.json index 8b8bd5423..75f622df9 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "scripts": { "copy-circuit-artifacts": "cp -r ../protocol/circuit-artifacts .", "build": "yarn turbo run --no-daemon build", - "build:docker": "scripts/build_docker.sh", + "build:docker-actors": "scripts/build_docker_actors.sh", + "build:docker-updater": "scripts/build_docker_updater.sh", "build:nocache": "yarn turbo run --no-daemon build --force", "clean": "yarn workspaces foreach run clean && rm -rf ./node_modules/.cache/turbo", "deploy-and-deposit": "cd packages/e2e-tests && ts-node ./scripts/deployAndDeposit.ts", @@ -21,7 +22,8 @@ "prettier:check": "npx prettier --check ./sql/**/*.sql && yarn workspaces foreach run prettier:check", "prettier:write": "npx prettier --write ./sql/**/*.sql && yarn workspaces foreach run prettier:write", "publish-packages": "yarn clean && yarn build && changeset version && yarn workspaces foreach --no-private --from '@nocturne-xyz/*' npm publish --access public --tolerate-republish && yarn changeset tag", - "push:docker": "scripts/push_docker.sh", + "push:docker-actors": "scripts/push_docker_actors.sh", + "push:docker-updater": "scripts/push_docker_updater.sh", "rebuild-circuits-and-test-cases": "yarn circuits:build && yarn local-prover:gen-test-cases", "test": "yarn turbo run --no-daemon test", "test:e2e": "yarn turbo run --no-daemon test:e2e --filter=@nocturne-xyz/e2e-tests", diff --git a/packages/offchain-utils/package.json b/packages/offchain-utils/package.json index f8ce6b5eb..c8ca8cbc1 100644 --- a/packages/offchain-utils/package.json +++ b/packages/offchain-utils/package.json @@ -26,8 +26,10 @@ "@opentelemetry/resources": "^1.14.0", "@opentelemetry/sdk-metrics": "^1.14.0", "@opentelemetry/semantic-conventions": "^1.14.0", + "@openzeppelin/defender-relay-client": "1.47.1", "ajv": "^8.12.0", "bigint-json-serialization": "^1.0.1", + "ethers": "^5.7.2", "json-stable-stringify": "^1.0.2", "knex": "^3.0.1", "pg": "^8.11.3", diff --git a/packages/offchain-utils/src/ethersHelpers.ts b/packages/offchain-utils/src/ethersHelpers.ts new file mode 100644 index 000000000..5798d0072 --- /dev/null +++ b/packages/offchain-utils/src/ethersHelpers.ts @@ -0,0 +1,66 @@ +// import ethers +import { ethers } from "ethers"; +import { + DefenderRelayProvider, + DefenderRelaySigner, +} from "@openzeppelin/defender-relay-client/lib/ethers"; +import { Speed } from "@openzeppelin/defender-relay-client"; +import { intFromEnv } from "./configuration"; +import * as https from "https"; + +const DEFAULT_RPC_TIMEOUT_MS = 3000; + +const DEFAULT_SPEED: Speed = "safeLow"; + +/** + * Get ethers provider and signer from environment variables. + */ +export function getEthersProviderAndSignerFromEnvConfiguration(): { + provider: ethers.providers.JsonRpcProvider; + signer: ethers.Signer; +} { + const relayerApiKey = process.env.OZ_RELAYER_API_KEY; + const relayerApiSecret = process.env.OZ_RELAYER_API_SECRET; + const relayerSpeed = process.env.OZ_RELAYER_SPEED; + + const privateKey = process.env.TX_SIGNER_KEY; + const rpcUrl = process.env.RPC_URL; + + const timeout = intFromEnv("RPC_TIMEOUT_MS") ?? DEFAULT_RPC_TIMEOUT_MS; + + let signer: ethers.Signer; + let provider: ethers.providers.JsonRpcProvider; + + if (relayerApiKey && relayerApiSecret) { + const credentials = { + apiKey: relayerApiKey, + apiSecret: relayerApiSecret, + }; + if (rpcUrl) { + provider = new ethers.providers.JsonRpcProvider({ + url: rpcUrl, + timeout: timeout, + }); + } else { + provider = new DefenderRelayProvider({ + ...credentials, + httpsAgent: new https.Agent({ + timeout: timeout, + keepAlive: true, + }), + }); + } + signer = new DefenderRelaySigner(credentials, provider, { + speed: (relayerSpeed as Speed) ?? DEFAULT_SPEED, + }); + } else if (rpcUrl && privateKey) { + provider = new ethers.providers.JsonRpcProvider(rpcUrl); + signer = new ethers.Wallet(privateKey, provider); + } else { + throw new Error( + "missing RPC_URL/PRIVATE_KEY or OZ_RELAYER_API_KEY/OZ_RELAYER_API_SECRET" + ); + } + + return { provider, signer }; +} diff --git a/packages/offchain-utils/src/index.ts b/packages/offchain-utils/src/index.ts index 312ac6289..2ead423c5 100644 --- a/packages/offchain-utils/src/index.ts +++ b/packages/offchain-utils/src/index.ts @@ -3,3 +3,4 @@ export * from "./request"; export * from "./types"; export * from "./instrumentation"; export * from "./geoMiddleware"; +export * from "./ethersHelpers"; diff --git a/scripts/build_docker.sh b/scripts/build_docker.sh deleted file mode 100755 index e2830c2d5..000000000 --- a/scripts/build_docker.sh +++ /dev/null @@ -1,130 +0,0 @@ -#!/usr/bin/env bash - -SCRIPT_DIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" -ROOT_DIR="$SCRIPT_DIR/../" - -cd $ROOT_DIR - -COMMIT_HASH=$(git rev-parse --short HEAD) - -# builds docker containers for offchain actors -# takes two options: -# -p (optional): prover mode for subtree updater, 'mock' or 'rapidsnark'. If not given, defaults to 'mock'. -# -o (optional): build only the indiciated actor. can be one of `subtree-updater`, `bundler`, `deposit-screener`, or `test-actor` - -usage() { echo "usage: $0 [-p <'mock' | 'rapidsnark'> -o <'subtree-updater' | 'bundler' | 'deposit-screener' | 'test-actor' | 'insertion-writer'>]" 1>&2; } - -while getopts ":p:o:" o; do - case "${o}" in - p) - SUBTREE_UPDATER_PROVER_MODE=${OPTARG} - ((SUBTREE_UPDATE_PROVER_MODE == "mock" || SUBTREE_UPDATE_PROVER_MODE == "rapidsnark")) || (usage && exit 1) - ;; - o) - BUILD_ONLY=${OPTARG} - ((BUILD_ONLY == "subtree-updater" || BUILD_ONLY == "bundler" || BUILD_ONLY == "deposit-screener" || BUILD_ONLY == "test-actor")) || (usage && exit 1) - ;; - *) - SUBTREE_UPDATER_PROVER_MODE="mock" - ;; - esac -done - -build_subtree_updater() { - if [ "$SUBTREE_UPDATER_PROVER_MODE" == "rapidsnark" ]; then - pushd rapidsnark - git submodule init - git submodule update - popd - - if [ ! -d "$ROOT_DIR/circuit-artifacts/subtreeupdate/" ]; then - pushd packages/circuits - yarn download-big-ptau - yarn build:subtreeupdate - yarn local-prover:gen-test-cases - popd - fi - - if [[ $(uname -m) == 'arm64' ]]; then - echo "dected arm64, building for x86..." - - docker build --platform linux/amd64 -t rapidsnark ./rapidsnark - docker buildx build --build-context circuit-artifacts=circuit-artifacts -f actors/subtree-updater/Dockerfile --platform linux/amd64 -t nocturnelabs/subtree-updater:$COMMIT_HASH . - else - echo "deceting x86, building for x86..." - docker build -t rapidsnark ./rapidsnark - docker buildx build --build-context circuit-artifacts=circuit-artifacts -f actors/subtree-updater/Dockerfile -t nocturnelabs/subtree-updater:$COMMIT_HASH . - fi - else - docker build -f actors/subtree-updater/Mock.Dockerfile -t nocturnelabs/subtree-updater:$COMMIT_HASH . - fi -} - - -if [ "$BUILD_ONLY" == "subtree-updater" ]; then - BUILD_SUBTREE_UPDATER=true -elif [ "$BUILD_ONLY" == "bundler" ]; then - BUILD_BUNDLER=true -elif [ "$BUILD_ONLY" == "deposit-screener" ]; then - BUILD_DEPOSIT_SCREENER=true -elif [ "$BUILD_ONLY" == "test-actor" ]; then - BUILD_TEST_ACTOR=true -elif [ "$BUILD_ONLY" == "insertion-writer" ]; then - BUILD_INSERTION_WRITER=true -elif [ "$BUILD_ONLY" == "balance-monitor" ]; then - BUILD_BALANCE_MONITOR=true -else - BUILD_SUBTREE_UPDATER=true - BUILD_BUNDLER=true - BUILD_DEPOSIT_SCREENER=true - BUILD_TEST_ACTOR=true - BUILD_INSERTION_WRITER=true - BUILD_BALANCE_MONITOR=true -fi - -# build subtree updater -if [ "$BUILD_SUBTREE_UPDATER" == "true" ]; then - build_subtree_updater -else - echo "skipping subtree updater..." -fi - -# build bundler -if [ "$BUILD_BUNDLER" == "true" ]; then - echo "building bundler..." - docker build -f actors/bundler/Dockerfile -t nocturnelabs/bundler:$COMMIT_HASH . -else - echo "skipping bundler..." -fi - -# build deposit-screener -if [ "$BUILD_DEPOSIT_SCREENER" == "true" ]; then - echo "building deposit-screener..." - docker build -f actors/deposit-screener/Dockerfile -t nocturnelabs/deposit-screener:$COMMIT_HASH . -else - echo "skipping deposit-screener..." -fi - -# build test-actor -if [ "$BUILD_TEST_ACTOR" == "true" ]; then - echo "building test-actor..." - docker build -f actors/test-actor/Dockerfile -t nocturnelabs/test-actor:$COMMIT_HASH . -else - echo "skipping test-actor..." -fi - -# build insertion-writer -if [ "$BUILD_INSERTION_WRITER" == "true" ]; then - echo "building insertion-writer..." - docker build -f actors/insertion-writer/Dockerfile -t nocturnelabs/insertion-writer:$COMMIT_HASH . -else - echo "skipping insertion-writer..." -fi - -# build balance-monitor -if [ "$BUILD_BALANCE_MONITOR" == "true" ]; then - echo "building balance-monitor..." - docker build -f actors/balance-monitor/Dockerfile -t nocturnelabs/balance-monitor:$COMMIT_HASH . -else - echo "skipping balance-monitor..." -fi diff --git a/scripts/build_docker_actors.sh b/scripts/build_docker_actors.sh new file mode 100755 index 000000000..6ac17fb40 --- /dev/null +++ b/scripts/build_docker_actors.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +SCRIPT_DIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" +ROOT_DIR="$SCRIPT_DIR/../" + +cd $ROOT_DIR + +COMMIT_HASH=$(git rev-parse --short HEAD) + +docker build -t nocturne-actors:$COMMIT_HASH . +docker tag nocturne-actors:$COMMIT_HASH 714567495486.dkr.ecr.us-east-2.amazonaws.com/nocturne-actors:$COMMIT_HASH diff --git a/scripts/build_docker_updater.sh b/scripts/build_docker_updater.sh new file mode 100755 index 000000000..20c9f855e --- /dev/null +++ b/scripts/build_docker_updater.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash + +SCRIPT_DIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" +ROOT_DIR="$SCRIPT_DIR/../" + +cd $ROOT_DIR + +COMMIT_HASH=$(git rev-parse --short HEAD) + +usage() { echo "usage: $0 [-p <'mock' | 'rapidsnark'>" 1>&2; } + +while getopts ":p:o:" o; do + case "${o}" in + p) + SUBTREE_UPDATER_PROVER_MODE=${OPTARG} + ((SUBTREE_UPDATE_PROVER_MODE == "mock" || SUBTREE_UPDATE_PROVER_MODE == "rapidsnark")) || (usage && exit 1) + ;; + o) + BUILD_ONLY=${OPTARG} + ((BUILD_ONLY == "subtree-updater" || BUILD_ONLY == "bundler" || BUILD_ONLY == "deposit-screener" || BUILD_ONLY == "test-actor")) || (usage && exit 1) + ;; + *) + SUBTREE_UPDATER_PROVER_MODE="mock" + ;; + esac +done + +if [ "$SUBTREE_UPDATER_PROVER_MODE" == "rapidsnark" ]; then + pushd rapidsnark + git submodule init + git submodule update + popd + + if [ ! -d "$ROOT_DIR/circuit-artifacts/subtreeupdate/" ]; then + pushd packages/circuits + yarn download-big-ptau + yarn build:subtreeupdate + yarn local-prover:gen-test-cases + popd + fi + + if [[ $(uname -m) == 'arm64' ]]; then + echo "dected arm64, building for x86..." + + docker build --platform linux/amd64 -t rapidsnark ./rapidsnark + docker buildx build --build-context circuit-artifacts=circuit-artifacts -f actors/subtree-updater/Dockerfile --platform linux/amd64 -t subtree-updater:$COMMIT_HASH . + else + echo "deceting x86, building for x86..." + docker build -t rapidsnark ./rapidsnark + docker buildx build --build-context circuit-artifacts=circuit-artifacts -f actors/subtree-updater/Dockerfile -t subtree-updater:$COMMIT_HASH . + fi +else + docker build -f actors/subtree-updater/Mock.Dockerfile -t subtree-updater:$COMMIT_HASH . +fi + +docker tag subtree-updater:$COMMIT_HASH 714567495486.dkr.ecr.us-east-2.amazonaws.com/subtree-updater:$COMMIT_HASH diff --git a/scripts/push_docker.sh b/scripts/push_docker.sh deleted file mode 100755 index 98beef33f..000000000 --- a/scripts/push_docker.sh +++ /dev/null @@ -1,81 +0,0 @@ -#!/usr/bin/env bash - -COMMIT_HASH=$(git rev-parse --short HEAD) - -# pushes docker containers for offchain actors -# takes one option: -# -o (optional): push only the indiciated actor. can be one of `subtree-updater`, `bundler`, `deposit-screener`, or `test-actor` - -usage() { echo "usage: $0 [-o <'subtree-updater' | 'bundler' | 'deposit-screener' | 'test-actor' | 'insertion-writer'>]" 1>&2; } - -while getopts ":p:o:" o; do - case "${o}" in - o) - PUSH_ONLY=${OPTARG} - ((PUSH_ONLY == "subtree-updater" || PUSH_ONLY == "bundler" || PUSH_ONLY == "deposit-screener" || PUSH_ONLY == "test-actor" || PUSH_ONLY == "insertion-writer")) || (usage && exit 1) - ;; - *) - SUBTREE_UPDATER_PROVER_MODE="mock" - ;; - esac -done - - -if [ "$PUSH_ONLY" == "subtree-updater" ]; then - PUSH_SUBTREE_UPDATER=true -elif [ "$PUSH_ONLY" == "bundler" ]; then - PUSH_BUNDLER=true -elif [ "$PUSH_ONLY" == "deposit-screener" ]; then - PUSH_DEPOSIT_SCREENER=true -elif [ "$PUSH_ONLY" == "test-actor" ]; then - PUSH_TEST_ACTOR=true -elif [ "$PUSH_ONLY" == "insertion-writer" ]; then - PUSH_INSERTION_WRITER=true -elif [ "$PUSH_ONLY" == "balance-monitor" ]; then - PUSH_BALANCE_MONITOR=true -else - PUSH_SUBTREE_UPDATER=true - PUSH_BUNDLER=true - PUSH_DEPOSIT_SCREENER=true - PUSH_TEST_ACTOR=true - PUSH_INSERTION_WRITER=true - PUSH_BALANCE_MONITOR=true -fi - - -if [ "$PUSH_BUNDLER" == "true" ]; then - echo "pushing bundler..." - docker push "nocturnelabs/bundler:$COMMIT_HASH" -else - echo "skipping bundler..." -fi - -if [ "$PUSH_DEPOSIT_SCREENER" == "true" ]; then - docker push "nocturnelabs/deposit-screener:$COMMIT_HASH" -else - echo "skipping deposit-screener..." -fi - -if [ "$PUSH_TEST_ACTOR" == "true" ]; then - docker push "nocturnelabs/test-actor:$COMMIT_HASH" -else - echo "skipping test-actor..." -fi - -if [ "$PUSH_SUBTREE_UPDATER" == "true" ]; then - docker push "nocturnelabs/subtree-updater:$COMMIT_HASH" -else - echo "skipping subtree-updater..." -fi - -if [ "$PUSH_INSERTION_WRITER" == "true" ]; then - docker push "nocturnelabs/insertion-writer:$COMMIT_HASH" -else - echo "skipping insertion-writer..." -fi - -if [ "$PUSH_BALANCE_MONITOR" == "true" ]; then - docker push "nocturnelabs/balance-monitor:$COMMIT_HASH" -else - echo "skipping balance-monitor..." -fi diff --git a/scripts/push_docker_actors.sh b/scripts/push_docker_actors.sh new file mode 100755 index 000000000..8de021dc9 --- /dev/null +++ b/scripts/push_docker_actors.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +COMMIT_HASH=$(git rev-parse --short HEAD) +docker push 714567495486.dkr.ecr.us-east-2.amazonaws.com/nocturne-actors:$COMMIT_HASH \ No newline at end of file diff --git a/scripts/push_docker_updater.sh b/scripts/push_docker_updater.sh new file mode 100755 index 000000000..53b8ec5b5 --- /dev/null +++ b/scripts/push_docker_updater.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +COMMIT_HASH=$(git rev-parse --short HEAD) +docker push 714567495486.dkr.ecr.us-east-2.amazonaws.com/subtree-updater:$COMMIT_HASH \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 6345e003f..526332bc5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4277,6 +4277,7 @@ __metadata: "@opentelemetry/resources": ^1.14.0 "@opentelemetry/sdk-metrics": ^1.14.0 "@opentelemetry/semantic-conventions": ^1.14.0 + "@openzeppelin/defender-relay-client": 1.47.1 "@types/chai": ^4.3.3 "@types/chai-as-promised": ^7.1.5 "@types/mocha": ^10.0.0 @@ -4290,6 +4291,7 @@ __metadata: env-cmd: ^10.1.0 eslint: ^7.32.0 eslint-config-prettier: ^8.8.0 + ethers: ^5.7.2 json-stable-stringify: ^1.0.2 knex: ^3.0.1 mocha: ^10.1.0