|
| 1 | +const Viz = require('viz.js'); |
| 2 | +const fs = require('fs'); |
| 3 | + |
| 4 | +class GraphGenerator { |
| 5 | + constructor(engine) { |
| 6 | + this.engine = engine; |
| 7 | + } |
| 8 | + |
| 9 | + generate() { |
| 10 | + let id = 0; |
| 11 | + let contractString = ""; |
| 12 | + let relationshipString = ""; |
| 13 | + let idMapping = {}; |
| 14 | + let contractInheritance = {}; |
| 15 | + |
| 16 | + |
| 17 | + for (let contract in this.engine.contractsManager.contracts) { |
| 18 | + id++; |
| 19 | + |
| 20 | + idMapping[contract] = id; |
| 21 | + |
| 22 | + let contractLabel = ""; |
| 23 | + |
| 24 | + contractLabel += `${contract}`; |
| 25 | + let tooltip = contract; |
| 26 | + |
| 27 | + if(this.engine.contractsManager.contracts[contract].instanceOf !== undefined && |
| 28 | + this.engine.contractsManager.contracts[this.engine.contractsManager.contracts[contract].instanceOf] !== undefined){ |
| 29 | + contractInheritance[contract] = this.engine.contractsManager.contracts[contract].instanceOf; |
| 30 | + contractLabel += ": " + this.engine.contractsManager.contracts[contract].instanceOf; |
| 31 | + tooltip += " instance of " + this.engine.contractsManager.contracts[contract].instanceOf; |
| 32 | + } else { |
| 33 | + contractLabel += "|"; |
| 34 | + |
| 35 | + for(let i = 0; i < this.engine.contractsManager.contracts[contract].abiDefinition.length; i++){ |
| 36 | + switch(this.engine.contractsManager.contracts[contract].abiDefinition[i].type){ |
| 37 | + case 'fallback': |
| 38 | + contractLabel += "«fallback»()\\l"; |
| 39 | + break; |
| 40 | + case 'constructor': |
| 41 | + contractLabel += "«constructor»("; |
| 42 | + this.engine.contractsManager.contracts[contract].abiDefinition[i].inputs.forEach(function(elem, index){ |
| 43 | + contractLabel += (index == 0 ? "" : ", ") + elem.type; |
| 44 | + }); |
| 45 | + contractLabel += ")\\l"; |
| 46 | + break; |
| 47 | + case 'event': |
| 48 | + contractLabel += "«event»" + this.engine.contractsManager.contracts[contract].abiDefinition[i].name + "("; |
| 49 | + this.engine.contractsManager.contracts[contract].abiDefinition[i].inputs.forEach(function(elem, index){ |
| 50 | + contractLabel += (index == 0 ? "" : ", ") + elem.type; |
| 51 | + }); |
| 52 | + contractLabel += ")\\l"; |
| 53 | + break; |
| 54 | + default: break; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + let fHashes = this.engine.contractsManager.contracts[contract].functionHashes; |
| 59 | + if(fHashes != {} && fHashes != undefined){ |
| 60 | + for(let method in this.engine.contractsManager.contracts[contract].functionHashes){ |
| 61 | + contractLabel += method + '\\l'; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + let others = ''; |
| 67 | + if(!this.engine.contractsManager.contracts[contract].deploy){ |
| 68 | + others = 'fontcolor="#c3c3c3", color="#a0a0a0"'; |
| 69 | + tooltip += " (not deployed)"; |
| 70 | + } |
| 71 | + |
| 72 | + contractString += `${id}[label = "{${contractLabel}}", tooltip="${tooltip}", fillcolor=gray95, ${others}]\n`; |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + for (let c in this.engine.contractsManager.contractDependencies){ |
| 77 | + let contractDependencies = Array.from(new Set(this.engine.contractsManager.contractDependencies[c])); |
| 78 | + contractDependencies.forEach(function(d){ |
| 79 | + if(idMapping[c] !== undefined && idMapping[d] !== undefined){ |
| 80 | + relationshipString += `${idMapping[d]}->${idMapping[c]}[constraint=true, arrowtail=diamond, tooltip="${c} uses ${d}"]\n`; |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + for (let c in contractInheritance){ |
| 86 | + relationshipString += `${idMapping[contractInheritance[c]]}->${idMapping[c]}[tooltip="${c} instance of ${contractInheritance[c]}"]\n`; |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + let dot = ` |
| 91 | + digraph Contracts { |
| 92 | + node[shape=record,style=filled] |
| 93 | + edge[dir=back, arrowtail=empty] |
| 94 | + ${contractString} |
| 95 | + ${relationshipString} |
| 96 | + }`; |
| 97 | + |
| 98 | + let svg = Viz(dot); |
| 99 | + |
| 100 | + let filename = "diagram.svg"; |
| 101 | + |
| 102 | + fs.writeFileSync(filename, svg, (err) => { |
| 103 | + if (err) throw err; |
| 104 | + }); |
| 105 | + |
| 106 | + |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +module.exports = GraphGenerator; |
0 commit comments