Skip to content

Commit

Permalink
feat(rpc-provider): alchemy provider + defender (#535)
Browse files Browse the repository at this point in the history
* feat(rpc-provider): alchemy provider + defender

* update updater dockerfile

* add new docker scripts

* fix updater docker scripts

* separate build and push

* chmod

* fix wtns gen updater

* update package json scripts

* fix stdout log level bug

* fix ethers helper import

---------

Co-authored-by: Luke Tchang <[email protected]>
Co-authored-by: Ubuntu <[email protected]>
  • Loading branch information
3 people authored Oct 25, 2023
1 parent 9cac082 commit 891de7e
Show file tree
Hide file tree
Showing 28 changed files with 228 additions and 398 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-mangos-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nocturne-xyz/offchain-utils": minor
---

Dry up ethers, defender config code and add to offchain utils
9 changes: 9 additions & 0 deletions .changeset/spicy-tigers-collect.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
6 changes: 3 additions & 3 deletions actors/bundler/src/cli/commands/run/batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ const runBatcher = new Command("batcher")
"./logs/bundler-server"
)
.option(
"--stdout-log-level <string>",
"--log-level <string>",
"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)
Expand All @@ -35,7 +35,7 @@ const runBatcher = new Command("batcher")
logDir,
`${configName}-bundler`,
"batcher",
stdoutLogLevel
logLevel
);
const batcher = new BundlerBatcher(
getRedis(),
Expand Down
6 changes: 3 additions & 3 deletions actors/bundler/src/cli/commands/run/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ const runServer = new Command("server")
"./logs/bundler-server"
)
.option(
"--stdout-log-level <string>",
"--log-level <string>",
"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);

Expand All @@ -41,7 +41,7 @@ const runServer = new Command("server")
logDir,
`${configName}-bundler`,
"server",
stdoutLogLevel
logLevel
);
const server = new BundlerServer(
bundlerAddress,
Expand Down
44 changes: 8 additions & 36 deletions actors/bundler/src/cli/commands/run/submitter.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -25,7 +22,7 @@ const runSubmitter = new Command("submitter")
"./logs/bundler-submitter"
)
.option(
"--stdout-log-level <string>",
"--log-level <string>",
"min log importance to log to stdout. if not given, logs will not be emitted to stdout"
)
.option(
Expand All @@ -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,
Expand Down
13 changes: 4 additions & 9 deletions actors/deposit-screener/src/cli/commands/inspect/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -38,7 +38,7 @@ const runChecker = new Command("check")
"./logs/address-checker"
)
.option(
"--stdout-log-level <string>",
"--log-level <string>",
"min log importance to log to stdout. if not given, logs will not be emitted to stdout"
)
.action(main);
Expand All @@ -61,18 +61,13 @@ function showReasonCounts(
}

async function main(options: any): Promise<void> {
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);
Expand Down
13 changes: 4 additions & 9 deletions actors/deposit-screener/src/cli/commands/inspect/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -42,7 +42,7 @@ const runSnapshot = new Command("snapshot")
"500"
)
.option(
"--stdout-log-level <string>",
"--log-level <string>",
"min log importance to log to stdout. if not given, logs will not be emitted to stdout"
)
.action(main);
Expand All @@ -65,15 +65,10 @@ async function parseAndFilterCsvOfAddresses(path: string): Promise<Address[]> {
async function main(options: any): Promise<void> {
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);
Expand Down
13 changes: 4 additions & 9 deletions actors/deposit-screener/src/cli/commands/inspect/trmTxMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -45,7 +45,7 @@ const runTrmTxMonitor = new Command("trmTxMonitor")
"./logs/address-checker"
)
.option(
"--stdout-log-level <string>",
"--log-level <string>",
"min log importance to log to stdout. if not given, logs will not be emitted to stdout"
)
.action(main);
Expand All @@ -58,15 +58,10 @@ async function main(options: any): Promise<void> {
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();

Expand Down
42 changes: 8 additions & 34 deletions actors/deposit-screener/src/cli/commands/run/processor.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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")
Expand Down Expand Up @@ -46,7 +44,7 @@ const runProcess = new Command("processor")
parseInt
)
.option(
"--stdout-log-level <string>",
"--log-level <string>",
"min log importance to log to stdout. if not given, logs will not be emitted to stdout"
)
.action(async (options) => {
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions actors/deposit-screener/src/cli/commands/run/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const runServer = new Command("server")
"./logs/deposit-screener"
)
.option(
"--stdout-log-level <string>",
"--log-level <string>",
"min log importance to log to stdout. if not given, logs will not be emitted to stdout"
)
.action(async (options) => {
Expand All @@ -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);

Expand All @@ -67,7 +67,7 @@ const runServer = new Command("server")
logDir,
`${configName}-deposit-screener`,
"server",
stdoutLogLevel
logLevel
);
const server = new DepositScreenerServer(
logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 891de7e

Please sign in to comment.