From 099b4a00534d33416efa935ab5110a8ed3d13df7 Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Tue, 15 Mar 2022 17:58:56 +0530 Subject: [PATCH 1/9] truffle integration --- libs/remixd/src/bin/remixd.ts | 9 ++++++- libs/remixd/src/index.ts | 2 ++ libs/remixd/src/serviceList.ts | 1 + libs/remixd/src/services/truffleClient.ts | 31 +++++++++++++++++++++++ libs/remixd/src/websocket.ts | 3 ++- 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 libs/remixd/src/services/truffleClient.ts diff --git a/libs/remixd/src/bin/remixd.ts b/libs/remixd/src/bin/remixd.ts index f30b68f247..75319b8448 100644 --- a/libs/remixd/src/bin/remixd.ts +++ b/libs/remixd/src/bin/remixd.ts @@ -25,6 +25,7 @@ async function warnLatestVersion () { const services = { git: (readOnly: boolean) => new servicesList.GitClient(readOnly), hardhat: (readOnly: boolean) => new servicesList.HardhatClient(readOnly), + truffle: (readOnly: boolean) => new servicesList.TruffleClient(readOnly), slither: (readOnly: boolean) => new servicesList.SlitherClient(readOnly), folder: (readOnly: boolean) => new servicesList.Sharedfolder(readOnly) } @@ -34,11 +35,12 @@ const ports = { git: 65521, hardhat: 65522, slither: 65523, + truffle: 65524, folder: 65520 } const killCallBack: Array = [] // any is function -function startService (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder, error?:Error) => void) { +function startService (service: S, callback: (ws: WS, sharedFolderClient: servicesList.Sharedfolder, error?:Error) => void) { const socket = new WebSocket(ports[service], { remixIdeUrl: program.remixIde }, () => services[service](program.readOnly || false)) socket.start(callback) killCallBack.push(socket.close.bind(socket)) @@ -100,6 +102,11 @@ function errorHandler (error: any, service: string) { sharedFolderClient.setWebSocket(ws) sharedFolderClient.sharedFolder(program.sharedFolder) }) + + startService('truffle', (ws: WS, sharedFolderClient: servicesList.Sharedfolder) => { + sharedFolderClient.setWebSocket(ws) + sharedFolderClient.sharedFolder(program.sharedFolder) + }) // Run hardhat service if a hardhat project is shared as folder const hardhatConfigFilePath = absolutePath('./', program.sharedFolder) const isHardhatProject = existsSync(hardhatConfigFilePath + '/hardhat.config.js') || existsSync(hardhatConfigFilePath + '/hardhat.config.ts') diff --git a/libs/remixd/src/index.ts b/libs/remixd/src/index.ts index 8b35cea6dd..05195edb3c 100644 --- a/libs/remixd/src/index.ts +++ b/libs/remixd/src/index.ts @@ -2,6 +2,7 @@ import { RemixdClient as sharedFolder } from './services/remixdClient' import { GitClient } from './services/gitClient' import { HardhatClient } from './services/hardhatClient' +import { TruffleClient } from './services/truffleClient' import { SlitherClient } from './services/slitherClient' import Websocket from './websocket' import * as utils from './utils' @@ -13,6 +14,7 @@ module.exports = { sharedFolder, GitClient, HardhatClient, + TruffleClient, SlitherClient } } diff --git a/libs/remixd/src/serviceList.ts b/libs/remixd/src/serviceList.ts index ec8c1f374d..0b71166649 100644 --- a/libs/remixd/src/serviceList.ts +++ b/libs/remixd/src/serviceList.ts @@ -1,4 +1,5 @@ export { RemixdClient as Sharedfolder } from './services/remixdClient' export { GitClient } from './services/gitClient' export { HardhatClient } from './services/hardhatClient' +export { TruffleClient } from './services/truffleClient' export { SlitherClient } from './services/slitherClient' diff --git a/libs/remixd/src/services/truffleClient.ts b/libs/remixd/src/services/truffleClient.ts new file mode 100644 index 0000000000..22e052a257 --- /dev/null +++ b/libs/remixd/src/services/truffleClient.ts @@ -0,0 +1,31 @@ +import * as WS from 'ws' // eslint-disable-line +import { PluginClient } from '@remixproject/plugin' +const { spawn } = require('child_process') // eslint-disable-line + +export class TruffleClient extends PluginClient { + methods: Array + websocket: WS + currentSharedFolder: string + + constructor (private readOnly = false) { + super() + this.methods = ['compile'] + } + + setWebSocket (websocket: WS): void { + this.websocket = websocket + } + + sharedFolder (currentSharedFolder: string): void { + this.currentSharedFolder = currentSharedFolder + } + + compile (configPath: string) { + return new Promise((resolve, reject) => { + if (this.readOnly) { + const errMsg = '[Truffle Compilation]: Cannot compile in read-only mode' + return reject(new Error(errMsg)) + } + }) + } +} diff --git a/libs/remixd/src/websocket.ts b/libs/remixd/src/websocket.ts index 84a0edb7f8..8b57f7311c 100644 --- a/libs/remixd/src/websocket.ts +++ b/libs/remixd/src/websocket.ts @@ -20,7 +20,8 @@ export default class WebSocket { 65520: 'remixd', 65521: 'git', 65522: 'hardhat', - 65523: 'slither' + 65523: 'slither', + 65524: 'truffle' } this.server.on('error', (error: Error) => { From 5703f65322e384c4d7f9268afe4cfb46a1b27e27 Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Thu, 17 Mar 2022 14:33:19 +0530 Subject: [PATCH 2/9] intermediate commiy --- .../solidity-compiler/src/app/compiler-api.ts | 34 + apps/solidity-compiler/webpack.config.js | 1 + libs/remix-lib/src/types/ICompilerApi.ts | 1 + .../src/lib/compiler-container.tsx | 31 +- .../src/lib/logic/compileTabLogic.ts | 35 +- .../src/lib/solidity-compiler.tsx | 6 +- .../solidity-compiler/src/lib/types/index.ts | 1 + package-lock.json | 1231 ++++++++++++++++- package.json | 2 + 9 files changed, 1305 insertions(+), 37 deletions(-) diff --git a/apps/solidity-compiler/src/app/compiler-api.ts b/apps/solidity-compiler/src/app/compiler-api.ts index 64747b3c51..98a5abe00c 100644 --- a/apps/solidity-compiler/src/app/compiler-api.ts +++ b/apps/solidity-compiler/src/app/compiler-api.ts @@ -1,6 +1,11 @@ +import React from 'react'; import { compile, helper } from '@remix-project/remix-solidity' import { CompileTabLogic, parseContracts } from '@remix-ui/solidity-compiler' // eslint-disable-line import type { ConfigurationSettings } from '@remix-project/remix-lib-ts' +import Schema from "@truffle/contract-schema" +import Config from '@truffle/config' +import { Compile } from "@truffle/compile-solidity" +import { Shims } from "@truffle/compile-common" export const CompilerApiMixin = (Base) => class extends Base { currentFile: string @@ -100,6 +105,35 @@ export const CompilerApiMixin = (Base) => class extends Base { return this.call('hardhat', 'compile', configFile) } + async compileWithTruffle (fileName, CompConfig) { + console.log('fileName-in compileWithTruffle-->', fileName) + console.log('config-in compileWithTruffle-->', CompConfig) + + const sources = { + Example: await this.call('fileManager', 'getFile', fileName) + } + let config = Config.default().with(CompConfig) + console.log('config---->', config) + console.log('sources---->', sources) + // Compile first + const { compilations } = await Compile.sources({ + sources, + options: config + }); + console.log('compilations----->', compilations) + const { contracts } = compilations[0]; + // use forEach + const exampleContract = contracts.find( + contract => contract.contractName === "Owner" + ); + const compiled = Schema.normalize( + Shims.NewToLegacy.forContract(exampleContract) + ); + if(!compiled.updatedAt) compiled.updatedAt = new Date().toISOString() + console.log('compiled----->', compiled) + return "done" + } + logToTerminal (content) { return this.call('terminal', 'log', content) } diff --git a/apps/solidity-compiler/webpack.config.js b/apps/solidity-compiler/webpack.config.js index 02143af15b..201a50c365 100644 --- a/apps/solidity-compiler/webpack.config.js +++ b/apps/solidity-compiler/webpack.config.js @@ -7,6 +7,7 @@ module.exports = config => { ...nxWebpackConfig, node: { fs: 'empty', + path: 'empty', tls: 'empty', readline: 'empty', net: 'empty', diff --git a/libs/remix-lib/src/types/ICompilerApi.ts b/libs/remix-lib/src/types/ICompilerApi.ts index 4c796ee3c1..1c92aa38f0 100644 --- a/libs/remix-lib/src/types/ICompilerApi.ts +++ b/libs/remix-lib/src/types/ICompilerApi.ts @@ -41,6 +41,7 @@ export interface ICompilerApi { logToTerminal: (log: terminalLog) => void compileWithHardhat: (configPath: string) => Promise + compileWithTruffle: (file: string, config: Record) => Promise statusChanged: (data: { key: string, title?: string, type?: string }) => void, emit?: (key: string, ...payload: any) => void } diff --git a/libs/remix-ui/solidity-compiler/src/lib/compiler-container.tsx b/libs/remix-ui/solidity-compiler/src/lib/compiler-container.tsx index f2efb75360..0d896d3498 100644 --- a/libs/remix-ui/solidity-compiler/src/lib/compiler-container.tsx +++ b/libs/remix-ui/solidity-compiler/src/lib/compiler-container.tsx @@ -20,7 +20,7 @@ declare global { const _paq = window._paq = window._paq || [] //eslint-disable-line export const CompilerContainer = (props: CompilerContainerProps) => { - const { api, compileTabLogic, tooltip, modal, compiledFileName, updateCurrentVersion, configurationSettings, isHardhatProject } = props // eslint-disable-line + const { api, compileTabLogic, tooltip, modal, compiledFileName, updateCurrentVersion, configurationSettings, isHardhatProject, isTruffleProject } = props // eslint-disable-line const [state, setState] = useState({ hideWarnings: false, autoCompile: false, @@ -42,6 +42,7 @@ export const CompilerContainer = (props: CompilerContainerProps) => { const compileIcon = useRef(null) const promptMessageInput = useRef(null) const [hhCompilation, sethhCompilation] = useState(false) + const [truffleCompilation, setTruffleCompilation] = useState(false) const [compilerContainer, dispatch] = useReducer(compilerReducer, compilerInitialState) useEffect(() => { @@ -325,7 +326,10 @@ export const CompilerContainer = (props: CompilerContainerProps) => { if (!isSolFileSelected()) return _setCompilerVersionFromPragma(currentFile) - compileTabLogic.runCompiler(hhCompilation) + let externalCompType + if (hhCompilation) externalCompType = 'hardhat' + else if (truffleCompilation) externalCompType = 'truffle' + compileTabLogic.runCompiler(externalCompType) } const _updateVersionSelector = (version, customUrl = '') => { @@ -500,6 +504,13 @@ export const CompilerContainer = (props: CompilerContainerProps) => { api.setAppParameter('hardhat-compilation', checked) } + const updateTruffleCompilation = (event) => { + const checked = event.target.checked + + setTruffleCompilation(checked) + api.setAppParameter('truffle-compilation', checked) + } + /* The following functions map with the above event handlers. They are an external API for modifying the compiler configuration. @@ -594,6 +605,22 @@ export const CompilerContainer = (props: CompilerContainerProps) => { } + { + isTruffleProject && + + }