1
1
'use strict'
2
- import { Plugin } from '@remixproject/engine'
3
- import { util } from '@remix-project/remix-lib'
4
- import { CompilerAbstract } from '@remix-project/remix-solidity'
2
+ import { Plugin } from '@remixproject/engine'
3
+ import { util } from '@remix-project/remix-lib'
4
+ import { CompilerAbstract } from '@remix-project/remix-solidity'
5
5
6
6
const profile = {
7
7
name : 'compilerArtefacts' ,
8
- methods : [ 'get' , 'addResolvedContract' , 'getCompilerAbstract' , 'getAllContractDatas' , 'getLastCompilationResult' , 'getArtefactsByContractName' , 'getContractDataFromAddress' , 'getContractDataFromByteCode' , 'saveCompilerAbstract' ] ,
8
+ methods : [ 'get' , 'addResolvedContract' , 'getCompilerAbstract' , 'getAllContractDatas' , 'getLastCompilationResult' , 'getArtefactsByContractName' , 'getContractDataFromAddress' , 'getContractDataFromByteCode' , 'saveCompilerAbstract' , 'getAllCompilerAbstracts' ] ,
9
9
events : [ ] ,
10
- version : '0.0.1'
10
+ version : '0.0.1' ,
11
11
}
12
12
13
13
export class CompilerArtefacts extends Plugin {
14
14
compilersArtefactsPerFile : any
15
15
compilersArtefacts : any
16
- constructor ( ) {
16
+ constructor ( ) {
17
17
super ( profile )
18
18
this . compilersArtefacts = { }
19
19
this . compilersArtefactsPerFile = { }
20
20
}
21
21
22
- clear ( ) {
22
+ clear ( ) {
23
23
this . compilersArtefacts = { }
24
24
this . compilersArtefactsPerFile = { }
25
25
}
26
26
27
- saveCompilerAbstract ( file : string , compilerAbstract : CompilerAbstract ) {
27
+ saveCompilerAbstract ( file : string , compilerAbstract : CompilerAbstract ) {
28
28
this . compilersArtefactsPerFile [ file ] = compilerAbstract
29
29
}
30
30
31
- onActivation ( ) {
32
- const saveCompilationPerFileResult = ( file , source , languageVersion , data , input ?) => {
31
+ getAllCompilerAbstracts ( ) {
32
+ return this . compilersArtefactsPerFile
33
+ }
34
+
35
+ onActivation ( ) {
36
+ const saveCompilationResult = ( file , source , languageVersion , data , input ?) => {
37
+ this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source , input )
33
38
this . compilersArtefactsPerFile [ file ] = new CompilerAbstract ( languageVersion , data , source , input )
34
39
}
35
40
36
- this . on ( 'solidity' , 'compilationFinished' , ( file , source , languageVersion , data , input , version ) => {
37
- this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source , input )
38
- saveCompilationPerFileResult ( file , source , languageVersion , data )
39
- } )
41
+ this . on ( 'solidity' , 'compilationFinished' , saveCompilationResult )
40
42
41
- this . on ( 'vyper' , 'compilationFinished' , ( file , source , languageVersion , data ) => {
42
- this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source )
43
- saveCompilationPerFileResult ( file , source , languageVersion , data )
44
- } )
43
+ this . on ( 'vyper' , 'compilationFinished' , saveCompilationResult )
45
44
46
- this . on ( 'lexon' , 'compilationFinished' , ( file , source , languageVersion , data ) => {
47
- this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source )
48
- saveCompilationPerFileResult ( file , source , languageVersion , data )
49
- } )
45
+ this . on ( 'lexon' , 'compilationFinished' , saveCompilationResult )
50
46
51
- this . on ( 'yulp' , 'compilationFinished' , ( file , source , languageVersion , data ) => {
52
- this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source )
53
- saveCompilationPerFileResult ( file , source , languageVersion , data )
54
- } )
47
+ this . on ( 'yulp' , 'compilationFinished' , saveCompilationResult )
55
48
56
- this . on ( 'solidityUnitTesting' , 'compilationFinished' , ( file , source , languageVersion , data , input , version ) => {
57
- this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source , input )
58
- saveCompilationPerFileResult ( file , source , languageVersion , data , input )
59
- } )
49
+ this . on ( 'solidityUnitTesting' , 'compilationFinished' , saveCompilationResult )
60
50
61
- this . on ( 'nahmii-compiler' , 'compilationFinished' , ( file , source , languageVersion , data ) => {
62
- this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source )
63
- saveCompilationPerFileResult ( file , source , languageVersion , data )
64
- } )
51
+ this . on ( 'nahmii-compiler' , 'compilationFinished' , saveCompilationResult )
65
52
66
- this . on ( 'hardhat' , 'compilationFinished' , ( file , source , languageVersion , data ) => {
67
- this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source )
68
- saveCompilationPerFileResult ( file , source , languageVersion , data )
69
- } )
53
+ this . on ( 'hardhat' , 'compilationFinished' , saveCompilationResult )
70
54
71
- this . on ( 'truffle' , 'compilationFinished' , ( file , source , languageVersion , data ) => {
72
- this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source )
73
- saveCompilationPerFileResult ( file , source , languageVersion , data )
74
- } )
55
+ this . on ( 'truffle' , 'compilationFinished' , saveCompilationResult )
75
56
76
- this . on ( 'foundry' , 'compilationFinished' , ( file , source , languageVersion , data ) => {
77
- this . compilersArtefacts . __last = new CompilerAbstract ( languageVersion , data , source )
78
- saveCompilationPerFileResult ( file , source , languageVersion , data )
79
- } )
57
+ this . on ( 'foundry' , 'compilationFinished' , saveCompilationResult )
80
58
}
81
59
82
60
/**
83
61
* Get artefacts for last compiled contract
84
62
* * @returns last compiled contract compiler abstract
85
63
*/
86
- getLastCompilationResult ( ) {
64
+ getLastCompilationResult ( ) {
87
65
return this . compilersArtefacts . __last
88
66
}
89
67
90
68
/**
91
69
* Get compilation output for contracts compiled during a session of Remix IDE
92
70
* @returns compilatin output
93
71
*/
94
- getAllContractDatas ( ) {
72
+ getAllContractDatas ( ) {
95
73
return this . filterAllContractDatas ( ( ) => true )
96
74
}
97
75
98
76
/**
99
77
* filter compilation output for contracts compiled during a session of Remix IDE
100
78
* @returns compilatin output
101
79
*/
102
- filterAllContractDatas ( filter ) {
80
+ filterAllContractDatas ( filter ) {
103
81
const contractsData = { }
104
82
Object . keys ( this . compilersArtefactsPerFile ) . map ( ( targetFile ) => {
105
83
const artefact = this . compilersArtefactsPerFile [ targetFile ]
@@ -124,7 +102,7 @@ export class CompilerArtefacts extends Plugin {
124
102
* @param contractName contract name
125
103
* @returns arefacts object, with fully qualified name (e.g; contracts/1_Storage.sol:Storage) as key
126
104
*/
127
- _getAllContractArtefactsfromOutput ( compilerOutput , contractName ) {
105
+ _getAllContractArtefactsfromOutput ( compilerOutput , contractName ) {
128
106
const contractArtefacts = { }
129
107
for ( const filename in compilerOutput ) {
130
108
if ( Object . keys ( compilerOutput [ filename ] ) . includes ( contractName ) ) contractArtefacts [ filename + ':' + contractName ] = compilerOutput [ filename ] [ contractName ]
@@ -139,12 +117,12 @@ export class CompilerArtefacts extends Plugin {
139
117
* @param contractArtefacts populated resultant artefacts object, with fully qualified name (e.g: contracts/1_Storage.sol:Storage) as key
140
118
* Once method execution completes, contractArtefacts object will hold all possible artefacts for contract
141
119
*/
142
- async _populateAllContractArtefactsFromFE ( path , contractName , contractArtefacts ) {
120
+ async _populateAllContractArtefactsFromFE ( path , contractName , contractArtefacts ) {
143
121
const dirList = await this . call ( 'fileManager' , 'dirList' , path )
144
122
if ( dirList && dirList . length ) {
145
123
for ( const dirPath of dirList ) {
146
124
// check if directory contains an 'artifacts' folder and a 'build-info' folder inside 'artifacts'
147
- if ( dirPath === path + '/artifacts' && await this . call ( 'fileManager' , 'exists' , dirPath + '/build-info' ) ) {
125
+ if ( dirPath === path + '/artifacts' && ( await this . call ( 'fileManager' , 'exists' , dirPath + '/build-info' ) ) ) {
148
126
const buildFileList = await this . call ( 'fileManager' , 'fileList' , dirPath + '/build-info' )
149
127
// process each build-info file to populate the artefacts for contractName
150
128
for ( const buildFile of buildFileList ) {
@@ -155,7 +133,7 @@ export class CompilerArtefacts extends Plugin {
155
133
// populate the resultant object with artefacts
156
134
Object . assign ( contractArtefacts , artefacts )
157
135
}
158
- } else await this . _populateAllContractArtefactsFromFE ( dirPath , contractName , contractArtefacts )
136
+ } else await this . _populateAllContractArtefactsFromFE ( dirPath , contractName , contractArtefacts )
159
137
}
160
138
} else return
161
139
}
@@ -165,31 +143,30 @@ export class CompilerArtefacts extends Plugin {
165
143
* @param name contract name or fully qualified name i.e. <filename>:<contractname> e.g: contracts/1_Storage.sol:Storage
166
144
* @returns artefacts for the contract
167
145
*/
168
- async getArtefactsByContractName ( name ) {
146
+ async getArtefactsByContractName ( name ) {
169
147
const contractsDataByFilename = this . getAllContractDatas ( )
170
148
// check if name is a fully qualified name
171
149
if ( name . includes ( ':' ) ) {
172
150
const fullyQualifiedName = name
173
151
const nameArr = fullyQualifiedName . split ( ':' )
174
152
const filename = nameArr [ 0 ]
175
153
const contract = nameArr [ 1 ]
176
- if ( Object . keys ( contractsDataByFilename ) . includes ( filename ) && contractsDataByFilename [ filename ] [ contract ] )
177
- return contractsDataByFilename [ filename ] [ contract ]
154
+ if ( Object . keys ( contractsDataByFilename ) . includes ( filename ) && contractsDataByFilename [ filename ] [ contract ] ) return contractsDataByFilename [ filename ] [ contract ]
178
155
else {
179
156
const allContractsData = { }
180
- await this . _populateAllContractArtefactsFromFE ( 'contracts' , contract , allContractsData )
181
- if ( allContractsData [ fullyQualifiedName ] ) return { fullyQualifiedName, artefact : allContractsData [ fullyQualifiedName ] }
157
+ await this . _populateAllContractArtefactsFromFE ( 'contracts' , contract , allContractsData )
158
+ if ( allContractsData [ fullyQualifiedName ] ) return { fullyQualifiedName, artefact : allContractsData [ fullyQualifiedName ] }
182
159
else throw new Error ( `Could not find artifacts for ${ fullyQualifiedName } . Compile contract to generate artifacts.` )
183
160
}
184
161
} else {
185
162
const contractName = name
186
163
const contractArtefacts = this . _getAllContractArtefactsfromOutput ( contractsDataByFilename , contractName )
187
164
let keys = Object . keys ( contractArtefacts )
188
165
if ( ! keys . length ) {
189
- await this . _populateAllContractArtefactsFromFE ( 'contracts' , contractName , contractArtefacts )
166
+ await this . _populateAllContractArtefactsFromFE ( 'contracts' , contractName , contractArtefacts )
190
167
keys = Object . keys ( contractArtefacts )
191
168
}
192
- if ( keys . length === 1 ) return { fullyQualifiedName : keys [ 0 ] , artefact : contractArtefacts [ keys [ 0 ] ] }
169
+ if ( keys . length === 1 ) return { fullyQualifiedName : keys [ 0 ] , artefact : contractArtefacts [ keys [ 0 ] ] }
193
170
else if ( keys . length > 1 ) {
194
171
throw new Error ( `There are multiple artifacts for contract "${ contractName } ", please use a fully qualified name.\n
195
172
Please replace ${ contractName } for one of these options wherever you are trying to read its artifact: \n
@@ -199,7 +176,7 @@ export class CompilerArtefacts extends Plugin {
199
176
}
200
177
}
201
178
202
- async getCompilerAbstract ( file ) {
179
+ async getCompilerAbstract ( file ) {
203
180
if ( ! file ) return null
204
181
if ( this . compilersArtefactsPerFile [ file ] ) return this . compilersArtefactsPerFile [ file ]
205
182
const path = await this . call ( 'fileManager' , 'getPathFromUrl' , file )
@@ -215,30 +192,30 @@ export class CompilerArtefacts extends Plugin {
215
192
return artefact
216
193
}
217
194
218
- addResolvedContract ( address : string , compilerData : CompilerAbstract ) {
195
+ addResolvedContract ( address : string , compilerData : CompilerAbstract ) {
219
196
this . compilersArtefacts [ address ] = compilerData
220
197
}
221
198
222
- isResolved ( address ) {
199
+ isResolved ( address ) {
223
200
return this . compilersArtefacts [ address ] !== undefined
224
201
}
225
202
226
- get ( key ) {
203
+ get ( key ) {
227
204
return this . compilersArtefacts [ key ]
228
205
}
229
206
230
- async getContractDataFromAddress ( address ) {
207
+ async getContractDataFromAddress ( address ) {
231
208
const code = await this . call ( 'blockchain' , 'getCode' , address )
232
209
return this . getContractDataFromByteCode ( code )
233
210
}
234
211
235
- async getContractDataFromByteCode ( code ) {
212
+ async getContractDataFromByteCode ( code ) {
236
213
let found
237
214
this . filterAllContractDatas ( ( file , contractsData ) => {
238
215
for ( const name of Object . keys ( contractsData ) ) {
239
216
const contract = contractsData [ name ]
240
217
if ( util . compareByteCode ( code , '0x' + contract . evm . deployedBytecode . object ) ) {
241
- found = { name, contract, file }
218
+ found = { name, contract, file}
242
219
return true
243
220
}
244
221
}
0 commit comments