From 7e706db0403b0486417d31d387d01dea7930164d Mon Sep 17 00:00:00 2001 From: filip mertens Date: Wed, 5 Oct 2022 15:37:13 +0200 Subject: [PATCH] fixes contracts from client --- .../run-tab/src/lib/actions/events.ts | 3 +- libs/remixd/src/services/hardhatClient.ts | 46 +++++++++++-------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/libs/remix-ui/run-tab/src/lib/actions/events.ts b/libs/remix-ui/run-tab/src/lib/actions/events.ts index 8c4068d896..37f1e0ab6a 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/events.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/events.ts @@ -2,7 +2,7 @@ import { envChangeNotification } from "@remix-ui/helper" import { RunTab } from "../types/run-tab" import { setExecutionContext, setFinalContext, updateAccountBalances } from "./account" import { addExternalProvider, addInstance, removeExternalProvider, setNetworkNameFromProvider } from "./actions" -import { addDeployOption, clearAllInstances, clearRecorderCount, fetchContractListSuccess, resetUdapp, setCompilationSource, setCurrentContract, setCurrentFile, setLoadType, setProxyEnvAddress, setRecorderCount, setRemixDActivated, setSendValue } from "./payload" +import { addDeployOption, clearAllInstances, clearRecorderCount, fetchContractListSuccess, resetUdapp, setCurrentContract, setCurrentFile, setLoadType, setProxyEnvAddress, setRecorderCount, setRemixDActivated, setSendValue } from "./payload" import { CompilerAbstract } from '@remix-project/remix-solidity' import * as ethJSUtil from 'ethereumjs-util' import Web3 from 'web3' @@ -118,7 +118,6 @@ export const setupEvents = (plugin: RunTab, dispatch: React.Dispatch) => { const broadcastCompilationResult = async (compilerName: string, plugin: RunTab, dispatch: React.Dispatch, file, source, languageVersion, data, input?) => { _paq.push(['trackEvent', 'udapp', 'broadcastCompilationResult', compilerName]) // TODO check whether the tab is configured - console.log('compilation finished', compilerName, file) const compiler = new CompilerAbstract(languageVersion, data, source, input) plugin.compilersArtefacts[languageVersion] = compiler plugin.compilersArtefacts.__last = compiler diff --git a/libs/remixd/src/services/hardhatClient.ts b/libs/remixd/src/services/hardhatClient.ts index 5468700dcb..c19479766e 100644 --- a/libs/remixd/src/services/hardhatClient.ts +++ b/libs/remixd/src/services/hardhatClient.ts @@ -3,7 +3,7 @@ import { PluginClient } from '@remixproject/plugin' import * as chokidar from 'chokidar' import * as utils from '../utils' import * as fs from 'fs-extra' -import { basename, join } from 'path' +import { join } from 'path' const { spawn } = require('child_process') // eslint-disable-line export class HardhatClient extends PluginClient { @@ -14,12 +14,12 @@ export class HardhatClient extends PluginClient { warnLog: boolean buildPath: string - constructor (private readOnly = false) { + constructor(private readOnly = false) { super() this.methods = ['compile', 'sync'] } - setWebSocket (websocket: WS): void { + setWebSocket(websocket: WS): void { this.websocket = websocket this.websocket.addEventListener('close', () => { this.warnLog = false @@ -27,13 +27,13 @@ export class HardhatClient extends PluginClient { }) } - sharedFolder (currentSharedFolder: string): void { + sharedFolder(currentSharedFolder: string): void { this.currentSharedFolder = currentSharedFolder this.buildPath = utils.absolutePath('artifacts/contracts', this.currentSharedFolder) this.listenOnHardhatCompilation() } - compile (configPath: string) { + compile(configPath: string) { return new Promise((resolve, reject) => { if (this.readOnly) { const errMsg = '[Hardhat Compilation]: Cannot compile in read-only mode' @@ -60,9 +60,10 @@ export class HardhatClient extends PluginClient { }) } - private async processArtifact () { + private async processArtifact() { // resolving the files const folderFiles = await fs.readdir(this.buildPath) + const targetsSynced = [] // name of folders are file names for (const file of folderFiles) { // ["artifacts/contracts/Greeter.sol/"] const contractFilePath = join(this.buildPath, file) @@ -87,15 +88,19 @@ export class HardhatClient extends PluginClient { const jsonStd = JSON.parse(contentStd) compilationResult.target = jsonStd.sourceName - // this is the full compilation result - console.log('Processing Hardhat artifact for file: ', file) + targetsSynced.push(compilationResult.target) const path = join(contractFilePath, jsonDbg.buildInfo) const content = await fs.readFile(path, { encoding: 'utf-8' }) - + await this.feedContractArtifactFile(content, compilationResult) } if (compilationResult.target) { - this.emit('compilationFinished', compilationResult.target, { sources: compilationResult.input }, 'soljson', compilationResult.output, compilationResult.solcVersion) + // we are only interested in the contracts that are in the target of the compilation + compilationResult.output = { + ...compilationResult.output, + contracts: { [compilationResult.target]: compilationResult.output.contracts[compilationResult.target] } + } + this.emit('compilationFinished', compilationResult.target, { sources: compilationResult.input }, 'soljson', compilationResult.output, compilationResult.solcVersion) } } } @@ -104,29 +109,32 @@ export class HardhatClient extends PluginClient { this.call('terminal', 'log', 'receiving compilation result from Hardhat') this.warnLog = true } + if (targetsSynced.length) { + console.log(`Processing artifacts for files: ${[...new Set(targetsSynced)].join(', ')}`) + // @ts-ignore + this.call('terminal', 'log', `synced with Hardhat: ${[...new Set(targetsSynced)].join(', ')}`) + } } - listenOnHardhatCompilation () { + listenOnHardhatCompilation() { try { this.watcher = chokidar.watch(this.buildPath, { depth: 1, ignorePermissionErrors: true, ignoreInitial: true }) - + this.watcher.on('change', () => this.processArtifact()) this.watcher.on('add', () => this.processArtifact()) // process the artifact on activation setTimeout(() => this.processArtifact(), 1000) } catch (e) { console.log(e) - } + } } - async sync () { + async sync() { console.log('syncing from Hardhat') this.processArtifact() - // @ts-ignore - this.call('terminal', 'log', 'synced with Hardhat') } - async feedContractArtifactFile (artifactContent, compilationResultPart) { + async feedContractArtifactFile(artifactContent, compilationResultPart) { const contentJSON = JSON.parse(artifactContent) compilationResultPart.solcVersion = contentJSON.solcVersion for (const file in contentJSON.input.sources) { @@ -139,10 +147,10 @@ export class HardhatClient extends PluginClient { compilationResultPart.output['sources'][file] = contentJSON.output.sources[file] compilationResultPart.output['contracts'][file] = contentJSON.output.contracts[file] if (contentJSON.output.errors && contentJSON.output.errors.length) { - compilationResultPart.output['errors'] = contentJSON.output.errors.filter(error => error.sourceLocation.file === file) + compilationResultPart.output['errors'] = contentJSON.output.errors.filter(error => error.sourceLocation.file === file) } } } - } + } } }