11import React , { Fragment , useState } from 'react'
2- import { isVyper , compile , toStandardOutput , isCompilationError , remixClient , normalizeContractPath } from '../utils'
2+ import { isVyper , compile , toStandardOutput , isCompilationError , remixClient , normalizeContractPath , compileContract } from '../utils'
33import Button from 'react-bootstrap/Button'
4- import _ from 'lodash'
54
65interface Props {
76 compilerUrl : string
@@ -21,112 +20,14 @@ function CompilerButton({contract, setOutput, compilerUrl, resetCompilerState}:
2120 }
2221
2322 /** Compile a Contract */
24- async function compileContract ( ) {
25- resetCompilerState ( )
26- setLoadingSpinnerState ( true )
27- try {
28- // await remixClient.discardHighlight()
29- let _contract : any
30- try {
31- _contract = await remixClient . getContract ( )
32- } catch ( e : any ) {
33- setOutput ( '' , { status : 'failed' , message : e . message } )
34- return
35- }
36- remixClient . changeStatus ( {
37- key : 'loading' ,
38- type : 'info' ,
39- title : 'Compiling'
40- } )
41- let output
42- try {
43- output = await compile ( compilerUrl , _contract )
44- } catch ( e : any ) {
45- remixClient . changeStatus ( {
46- key : 'failed' ,
47- type : 'error' ,
48- title : e . message
49- } )
50- return
51- }
52- const compileReturnType = ( ) => {
53- const t : any = toStandardOutput ( contract , output )
54- const temp = _ . merge ( t [ 'contracts' ] [ contract ] )
55- const normal = normalizeContractPath ( contract ) [ 2 ]
56- const abi = temp [ normal ] [ 'abi' ]
57- const evm = _ . merge ( temp [ normal ] [ 'evm' ] )
58- const dpb = evm . deployedBytecode
59- const runtimeBytecode = evm . bytecode
60- const methodIdentifiers = evm . methodIdentifiers
61-
62- const result = {
63- contractName : normal ,
64- abi : abi ,
65- bytecode : dpb ,
66- runtimeBytecode : runtimeBytecode ,
67- ir : '' ,
68- methodIdentifiers : methodIdentifiers
69- }
70- return result
71- }
72-
73- // ERROR
74- if ( isCompilationError ( output ) ) {
75- const line = output . line
76- if ( line ) {
77- const lineColumnPos = {
78- start : { line : line - 1 , column : 10 } ,
79- end : { line : line - 1 , column : 10 }
80- }
81- // remixClient.highlight(lineColumnPos as any, _contract.name, '#e0b4b4')
82- } else {
83- const regex = output ?. message ?. match ( / l i n e ( ( \d + ) : ( \d + ) ) + / g)
84- const errors = output ?. message ?. split ( / l i n e ( ( \d + ) : ( \d + ) ) + / g) // extract error message
85- if ( regex ) {
86- let errorIndex = 0
87- regex . map ( ( errorLocation ) => {
88- const location = errorLocation ?. replace ( 'line ' , '' ) . split ( ':' )
89- let message = errors [ errorIndex ]
90- errorIndex = errorIndex + 4
91- if ( message && message ?. split ( '\n\n' ) . length > 0 ) {
92- try {
93- message = message ?. split ( '\n\n' ) [ message . split ( '\n\n' ) . length - 1 ]
94- } catch ( e ) { }
95- }
96- if ( location ?. length > 0 ) {
97- const lineColumnPos = {
98- start : { line : parseInt ( location [ 0 ] ) - 1 , column : 10 } ,
99- end : { line : parseInt ( location [ 0 ] ) - 1 , column : 10 }
100- }
101- // remixClient.highlight(lineColumnPos as any, _contract.name, message)
102- }
103- } )
104- }
105- }
106- throw new Error ( output . message )
107- }
108- // SUCCESS
109- // remixClient.discardHighlight()
110- remixClient . changeStatus ( {
111- key : 'succeed' ,
112- type : 'success' ,
113- title : 'success'
114- } )
115- const data = toStandardOutput ( _contract . name , output )
116- remixClient . compilationFinish ( _contract . name , _contract . content , data )
117- setOutput ( _contract . name , compileReturnType ( ) )
118- } catch ( err : any ) {
119- remixClient . changeStatus ( {
120- key : 'failed' ,
121- type : 'error' ,
122- title : err . message
123- } )
124- }
125- }
12623
12724 return (
12825 < Fragment >
129- < button data-id = "compile" onClick = { compileContract } title = { contract } className = "btn btn-primary w-100 d-block btn-block text-break remixui_disabled mb-1 mt-3" >
26+ < button data-id = "compile"
27+ onClick = { ( ) => compileContract ( contract , compilerUrl , setOutput ) }
28+ title = { contract }
29+ className = "btn btn-primary w-100 d-block btn-block text-break remixui_disabled mb-1 mt-3"
30+ >
13031 < div className = "d-flex align-items-center justify-content-center fa-1x" >
13132 < span className = "fas fa-sync fa-pulse mr-1" />
13233 < div className = "text-truncate overflow-hidden text-nowrap" >
0 commit comments