2018-03-22 14:43:29 +00:00
|
|
|
const Viz = require('viz.js');
|
2018-03-22 20:18:13 +00:00
|
|
|
const fs = require('fs');
|
2018-03-22 14:43:29 +00:00
|
|
|
|
|
|
|
class GraphGenerator {
|
2018-03-22 20:18:13 +00:00
|
|
|
constructor(engine) {
|
|
|
|
this.engine = engine;
|
2018-03-22 14:43:29 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:17:12 +00:00
|
|
|
generate(options) {
|
2018-03-22 20:18:13 +00:00
|
|
|
let id = 0;
|
|
|
|
let contractString = "";
|
2018-03-22 21:11:11 +00:00
|
|
|
let relationshipString = "";
|
|
|
|
let idMapping = {};
|
|
|
|
let contractInheritance = {};
|
2018-03-22 20:18:13 +00:00
|
|
|
|
|
|
|
for (let contract in this.engine.contractsManager.contracts) {
|
2018-05-04 20:17:12 +00:00
|
|
|
if(options.skipUndeployed && !this.engine.contractsManager.contracts[contract].deploy) continue;
|
|
|
|
|
2018-03-22 20:18:13 +00:00
|
|
|
id++;
|
2018-03-22 21:11:11 +00:00
|
|
|
|
|
|
|
idMapping[contract] = id;
|
|
|
|
|
2018-03-22 20:18:13 +00:00
|
|
|
let contractLabel = "";
|
|
|
|
|
|
|
|
contractLabel += `${contract}`;
|
2018-03-23 15:37:56 +00:00
|
|
|
let tooltip = contract;
|
2018-03-22 20:18:13 +00:00
|
|
|
|
2018-03-22 21:11:11 +00:00
|
|
|
if(this.engine.contractsManager.contracts[contract].instanceOf !== undefined &&
|
|
|
|
this.engine.contractsManager.contracts[this.engine.contractsManager.contracts[contract].instanceOf] !== undefined){
|
|
|
|
contractInheritance[contract] = this.engine.contractsManager.contracts[contract].instanceOf;
|
2018-03-23 15:37:56 +00:00
|
|
|
contractLabel += ": " + this.engine.contractsManager.contracts[contract].instanceOf;
|
|
|
|
tooltip += " instance of " + this.engine.contractsManager.contracts[contract].instanceOf;
|
|
|
|
} else {
|
2018-05-04 20:17:12 +00:00
|
|
|
if(!(options.skipFunctions === true && options.skipEvents === true)) contractLabel += "|";
|
2018-03-23 15:37:56 +00:00
|
|
|
|
|
|
|
for(let i = 0; i < this.engine.contractsManager.contracts[contract].abiDefinition.length; i++){
|
2018-05-04 20:17:12 +00:00
|
|
|
let abiDef = this.engine.contractsManager.contracts[contract].abiDefinition[i];
|
|
|
|
if(abiDef.type == 'event' && options.skipEvents) continue;
|
|
|
|
if(['constructor', 'fallback'].indexOf(abiDef.type) > -1 && options.skipFunctions) continue;
|
|
|
|
|
|
|
|
switch(abiDef.type){
|
2018-03-23 15:37:56 +00:00
|
|
|
case 'fallback':
|
|
|
|
contractLabel += "«fallback»()\\l";
|
|
|
|
break;
|
2018-05-04 20:17:12 +00:00
|
|
|
case 'constructor':
|
2018-03-23 15:37:56 +00:00
|
|
|
contractLabel += "«constructor»(";
|
2018-05-04 20:17:12 +00:00
|
|
|
abiDef.inputs.forEach(function(elem, index){
|
2018-04-05 15:41:28 +00:00
|
|
|
contractLabel += (index == 0 ? "" : ", ") + elem.type;
|
|
|
|
});
|
2018-03-23 15:37:56 +00:00
|
|
|
contractLabel += ")\\l";
|
|
|
|
break;
|
2018-05-04 20:17:12 +00:00
|
|
|
case 'event':
|
|
|
|
contractLabel += "«event»" + abiDef.name + "(";
|
|
|
|
abiDef.inputs.forEach(function(elem, index){
|
2018-04-05 15:41:28 +00:00
|
|
|
contractLabel += (index == 0 ? "" : ", ") + elem.type;
|
|
|
|
});
|
2018-03-23 15:37:56 +00:00
|
|
|
contractLabel += ")\\l";
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-22 21:11:11 +00:00
|
|
|
|
2018-03-23 15:37:56 +00:00
|
|
|
let fHashes = this.engine.contractsManager.contracts[contract].functionHashes;
|
2018-05-04 20:17:12 +00:00
|
|
|
if(fHashes != {} && fHashes != undefined && !options.skipFunctions){
|
2018-03-23 15:37:56 +00:00
|
|
|
for(let method in this.engine.contractsManager.contracts[contract].functionHashes){
|
|
|
|
contractLabel += method + '\\l';
|
|
|
|
}
|
2018-03-22 20:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 15:41:28 +00:00
|
|
|
let others = '';
|
2018-03-23 15:37:56 +00:00
|
|
|
if(!this.engine.contractsManager.contracts[contract].deploy){
|
|
|
|
others = 'fontcolor="#c3c3c3", color="#a0a0a0"';
|
|
|
|
tooltip += " (not deployed)";
|
|
|
|
}
|
2018-03-23 15:39:42 +00:00
|
|
|
|
2018-03-23 15:37:56 +00:00
|
|
|
contractString += `${id}[label = "{${contractLabel}}", tooltip="${tooltip}", fillcolor=gray95, ${others}]\n`;
|
2018-03-23 12:28:42 +00:00
|
|
|
|
2018-03-22 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:17:12 +00:00
|
|
|
for (let c in this.engine.contractsManager.contractDependencies){
|
2018-03-22 21:11:11 +00:00
|
|
|
let contractDependencies = Array.from(new Set(this.engine.contractsManager.contractDependencies[c]));
|
2018-05-04 20:17:12 +00:00
|
|
|
contractDependencies.forEach((d) => {
|
2018-03-22 21:11:11 +00:00
|
|
|
if(idMapping[c] !== undefined && idMapping[d] !== undefined){
|
2018-05-04 20:17:12 +00:00
|
|
|
if(options.skipUndeployed && this.engine.contractsManager.contracts[c].deploy && this.engine.contractsManager.contracts[d].deploy){
|
|
|
|
relationshipString += `${idMapping[d]}->${idMapping[c]}[constraint=true, arrowtail=diamond, tooltip="${c} uses ${d}"]\n`;
|
|
|
|
}
|
2018-03-22 21:11:11 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let c in contractInheritance){
|
2018-05-04 20:17:12 +00:00
|
|
|
if(options.skipUndeployed && !this.engine.contractsManager.contracts[contractInheritance[c]].deploy) continue;
|
|
|
|
|
2018-03-23 15:37:56 +00:00
|
|
|
relationshipString += `${idMapping[contractInheritance[c]]}->${idMapping[c]}[tooltip="${c} instance of ${contractInheritance[c]}"]\n`;
|
2018-03-22 21:11:11 +00:00
|
|
|
}
|
|
|
|
|
2018-03-22 20:18:13 +00:00
|
|
|
let dot = `
|
2018-03-23 15:37:56 +00:00
|
|
|
digraph Contracts {
|
|
|
|
node[shape=record,style=filled]
|
|
|
|
edge[dir=back, arrowtail=empty]
|
2018-03-22 20:18:13 +00:00
|
|
|
${contractString}
|
2018-03-22 21:11:11 +00:00
|
|
|
${relationshipString}
|
2018-03-22 20:18:13 +00:00
|
|
|
}`;
|
|
|
|
|
|
|
|
let svg = Viz(dot);
|
|
|
|
|
|
|
|
let filename = "diagram.svg";
|
|
|
|
|
|
|
|
fs.writeFileSync(filename, svg, (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-03-22 14:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GraphGenerator;
|