Add import files in Contract Dev

This commit is contained in:
Nicolas Gotchac 2016-12-20 02:37:16 +01:00
parent 5886034265
commit 1627c3fa71
2 changed files with 32 additions and 27 deletions

View File

@ -19,7 +19,7 @@ import solc from 'solc/browser-wrapper';
export default class SolidityUtils { export default class SolidityUtils {
static compile (data, compiler) { static compile (data, compiler) {
const { sourcecode, build, optimize } = data; const { sourcecode, build, optimize, files } = data;
const start = Date.now(); const start = Date.now();
console.log('[solidity] compiling...'); console.log('[solidity] compiling...');
@ -28,7 +28,17 @@ export default class SolidityUtils {
'': sourcecode '': sourcecode
}; };
const compiled = compiler.compile({ sources: input }, optimize ? 1 : 0); const findFiles = (path) => {
const file = files.find((f) => f.name === path);
if (file) {
return { contents: file.sourcecode };
} else {
return { error: 'File not found' };
}
};
const compiled = compiler.compile({ sources: input }, optimize ? 1 : 0, findFiles);
const time = Math.round((Date.now() - start) / 100) / 10; const time = Math.round((Date.now() - start) / 100) / 10;
console.log(`[solidity] done compiling in ${time}s`); console.log(`[solidity] done compiling in ${time}s`);

View File

@ -81,7 +81,7 @@ export default class WriteContractStore {
loadingSolidity = false; loadingSolidity = false;
lastCompilation = {}; lastCompilation = {};
snippets = SNIPPETS; snippets = SNIPPETS;
worker = null; worker = undefined;
useWorker = true; useWorker = true;
solc = {}; solc = {};
@ -107,6 +107,10 @@ export default class WriteContractStore {
} }
@action setWorker (worker) { @action setWorker (worker) {
if (this.worker !== undefined) {
return;
}
this.worker = worker; this.worker = worker;
this this
@ -150,7 +154,9 @@ export default class WriteContractStore {
@action handleSelectBuild = (_, index, value) => { @action handleSelectBuild = (_, index, value) => {
this.selectedBuild = value; this.selectedBuild = value;
return this.loadSolidityVersion(this.builds[value]); return this
.loadSolidityVersion(this.builds[value])
.then(() => this.handleCompile());
} }
getCompiler (build) { getCompiler (build) {
@ -182,6 +188,8 @@ export default class WriteContractStore {
return this.loadingSolidity; return this.loadingSolidity;
} }
this.loading = true;
if (this.useWorker) { if (this.useWorker) {
this.loadingSolidity = this.worker this.loadingSolidity = this.worker
.postMessage({ .postMessage({
@ -272,7 +280,7 @@ export default class WriteContractStore {
}); });
} }
@action handleCompile = (loadFiles = false) => { @action handleCompile = () => {
transaction(() => { transaction(() => {
this.compiled = false; this.compiled = false;
this.compiling = true; this.compiling = true;
@ -292,17 +300,12 @@ export default class WriteContractStore {
}, 500); }, 500);
}); });
} else { } else {
promise = loadFiles && this.useWorker promise = this
? this.sendFilesToWorker() .compile({
: Promise.resolve();
promise = promise
.then(() => {
return this.compile({
sourcecode: sourcecode, sourcecode: sourcecode,
build: build, build: build,
optimize: this.optimize optimize: this.optimize,
}); files: this.files
}) })
.then((data) => { .then((data) => {
const result = this.parseCompiled(data); const result = this.parseCompiled(data);
@ -468,8 +471,7 @@ export default class WriteContractStore {
this.resizeEditor(); this.resizeEditor();
// Send the new files to the Worker and compile return this.handleCompile();
return this.handleCompile(true);
} }
@action handleLoadContract = (contract) => { @action handleLoadContract = (contract) => {
@ -504,20 +506,13 @@ export default class WriteContractStore {
} catch (e) {} } catch (e) {}
} }
sendFilesToWorker = () => { get files() {
if (!this.useWorker) {
return Promise.resolve();
}
const files = [].concat( const files = [].concat(
Object.values(this.snippets), Object.values(this.snippets),
Object.values(this.savedContracts) Object.values(this.savedContracts)
); );
return this.worker.postMessage({ return files;
action: 'setFiles',
data: files
});
} }
} }