Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6075 from trufflesuite/new-route
Browse files Browse the repository at this point in the history
add new route in preparation for visual debugger PR
  • Loading branch information
eggplantzzz committed Jun 8, 2023
2 parents c37e1a6 + c05c8fe commit 7fa3b70
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/dashboard/lib/DashboardServer.ts
Expand Up @@ -2,13 +2,16 @@ import express, { Application, NextFunction, Request, Response } from "express";
import path from "path";
import getPort from "get-port";
import open from "open";
import { fetchAndCompile } from "@truffle/fetch-and-compile";
import { sha1 } from "object-hash";
import { v4 as uuid } from "uuid";
import Config from "@truffle/config";
import {
dashboardProviderMessageType,
LogMessage,
logMessageType
} from "@truffle/dashboard-message-bus-common";
import type { Compilation } from "@truffle/compile-common";
import { DashboardMessageBus } from "@truffle/dashboard-message-bus";
import { DashboardMessageBusClient } from "@truffle/dashboard-message-bus-client";
import cors from "cors";
Expand Down Expand Up @@ -95,6 +98,58 @@ export class DashboardServer {
this.expressApp.post("/rpc", this.postRpc.bind(this));
}

this.expressApp.get("/fetch-and-compile", async (req, res) => {
const { address, networkId, etherscanApiKey } = req.query as Record<
string,
string
>;
let config;
try {
config = Config.detect();
// we'll ignore errors as we only get the config for the api key
} catch {}

// a key provided in the browser takes precedence over on in the config
let etherscanKey: undefined | string;
if (etherscanApiKey) {
etherscanKey = etherscanApiKey;
} else if (config && config.etherscan !== undefined) {
etherscanKey = config.etherscan.apiKey;
}

config = Config.default().merge({
networks: {
custom: { network_id: networkId }
},
network: "custom",
etherscan: {
apiKey: etherscanKey
}
});

let result;
try {
result = (await fetchAndCompile(address, config)).compileResult;
} catch (error) {
if (!error.message.includes("No verified sources")) {
throw error;
}
}
if (result) {
// we calculate hashes on the server because it is at times too
// resource intensive for the browser and causes it to crash
const hashes = result.compilations.map((compilation: Compilation) => {
return sha1(compilation);
});
res.json({
hashes,
compilations: result.compilations
});
} else {
res.json({ compilations: [] });
}
});

this.expressApp.get("/analytics", (_req, res) => {
const userConfig = Config.getUserConfig();
res.json({
Expand Down
1 change: 1 addition & 0 deletions packages/dashboard/package.json
Expand Up @@ -39,6 +39,7 @@
"@mantine/prism": "^5.0.0",
"@truffle/codec": "^0.15.2",
"@truffle/config": "^1.3.57",
"@truffle/fetch-and-compile": "^0.5.48",
"@truffle/dashboard-message-bus": "^0.1.11",
"@truffle/dashboard-message-bus-client": "^0.1.11",
"@truffle/dashboard-message-bus-common": "^0.1.6",
Expand Down

0 comments on commit 7fa3b70

Please sign in to comment.