From f1df4bbfc0f37df47b1750e9f7f89dda1cb42c1c Mon Sep 17 00:00:00 2001 From: Joseph Izang Date: Sat, 10 Feb 2024 18:44:04 +0100 Subject: [PATCH] refactor vyper to work with context command. --- apps/vyper/src/app/app.tsx | 28 +++- .../src/app/components/CompilerButton.tsx | 111 +-------------- apps/vyper/src/app/utils/compiler.tsx | 127 ++++++++++++++++++ apps/vyper/src/app/utils/remix-client.tsx | 23 +++- .../src/lib/vyper-compile-details.tsx | 12 +- .../src/lib/vyperCompile.tsx | 1 - 6 files changed, 186 insertions(+), 116 deletions(-) diff --git a/apps/vyper/src/app/app.tsx b/apps/vyper/src/app/app.tsx index ebcfdd3aac..0a20e9d462 100644 --- a/apps/vyper/src/app/app.tsx +++ b/apps/vyper/src/app/app.tsx @@ -28,7 +28,7 @@ interface OutputMap { const App = () => { const [contract, setContract] = useState() - const [output, setOutput] = useState({}) + const [output, setOutput] = useState(remixClient.compilerOutput) const [state, setState] = useState({ status: 'idle', environment: 'remote', @@ -53,6 +53,30 @@ const App = () => { start() }, []) + useEffect(() => { + remixClient.eventEmitter.on('resetCompilerState', () => { + resetCompilerResultState() + }) + + return () => { + remixClient.eventEmitter.off('resetCompilerState', () => { + resetCompilerResultState() + }) + } + }, []) + + useEffect(() => { + remixClient.eventEmitter.on('setOutput', (payload) => { + setOutput(payload) + }) + + return () => { + remixClient.eventEmitter.off('setOutput', (payload) => { + setOutput(payload) + }) + } + }, []) + /** Update the environment state value */ function setEnvironment(environment: 'local' | 'remote') { setState({...state, environment}) @@ -67,7 +91,7 @@ const App = () => { } function resetCompilerResultState() { - setOutput({}) + setOutput(remixClient.compilerOutput) } return ( diff --git a/apps/vyper/src/app/components/CompilerButton.tsx b/apps/vyper/src/app/components/CompilerButton.tsx index ad04d0ba04..f7d2331348 100644 --- a/apps/vyper/src/app/components/CompilerButton.tsx +++ b/apps/vyper/src/app/components/CompilerButton.tsx @@ -1,7 +1,6 @@ import React, { Fragment, useState } from 'react' -import {isVyper, compile, toStandardOutput, isCompilationError, remixClient, normalizeContractPath} from '../utils' +import {isVyper, compile, toStandardOutput, isCompilationError, remixClient, normalizeContractPath, compileContract} from '../utils' import Button from 'react-bootstrap/Button' -import _ from 'lodash' interface Props { compilerUrl: string @@ -21,112 +20,14 @@ function CompilerButton({contract, setOutput, compilerUrl, resetCompilerState}: } /** Compile a Contract */ - async function compileContract() { - resetCompilerState() - setLoadingSpinnerState(true) - try { - // await remixClient.discardHighlight() - let _contract: any - try { - _contract = await remixClient.getContract() - } catch (e: any) { - setOutput('', {status: 'failed', message: e.message}) - return - } - remixClient.changeStatus({ - key: 'loading', - type: 'info', - title: 'Compiling' - }) - let output - try { - output = await compile(compilerUrl, _contract) - } catch (e: any) { - remixClient.changeStatus({ - key: 'failed', - type: 'error', - title: e.message - }) - return - } - const compileReturnType = () => { - const t: any = toStandardOutput(contract, output) - const temp = _.merge(t['contracts'][contract]) - const normal = normalizeContractPath(contract)[2] - const abi = temp[normal]['abi'] - const evm = _.merge(temp[normal]['evm']) - const dpb = evm.deployedBytecode - const runtimeBytecode = evm.bytecode - const methodIdentifiers = evm.methodIdentifiers - - const result = { - contractName: normal, - abi: abi, - bytecode: dpb, - runtimeBytecode: runtimeBytecode, - ir: '', - methodIdentifiers: methodIdentifiers - } - return result - } - - // ERROR - if (isCompilationError(output)) { - const line = output.line - if (line) { - const lineColumnPos = { - start: {line: line - 1, column: 10}, - end: {line: line - 1, column: 10} - } - // remixClient.highlight(lineColumnPos as any, _contract.name, '#e0b4b4') - } else { - const regex = output?.message?.match(/line ((\d+):(\d+))+/g) - const errors = output?.message?.split(/line ((\d+):(\d+))+/g) // extract error message - if (regex) { - let errorIndex = 0 - regex.map((errorLocation) => { - const location = errorLocation?.replace('line ', '').split(':') - let message = errors[errorIndex] - errorIndex = errorIndex + 4 - if (message && message?.split('\n\n').length > 0) { - try { - message = message?.split('\n\n')[message.split('\n\n').length - 1] - } catch (e) {} - } - if (location?.length > 0) { - const lineColumnPos = { - start: {line: parseInt(location[0]) - 1, column: 10}, - end: {line: parseInt(location[0]) - 1, column: 10} - } - // remixClient.highlight(lineColumnPos as any, _contract.name, message) - } - }) - } - } - throw new Error(output.message) - } - // SUCCESS - // remixClient.discardHighlight() - remixClient.changeStatus({ - key: 'succeed', - type: 'success', - title: 'success' - }) - const data = toStandardOutput(_contract.name, output) - remixClient.compilationFinish(_contract.name, _contract.content, data) - setOutput(_contract.name, compileReturnType()) - } catch (err: any) { - remixClient.changeStatus({ - key: 'failed', - type: 'error', - title: err.message - }) - } - } return ( -