forked from perikost/ExploringEthereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.js
149 lines (125 loc) · 5.32 KB
/
formatter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const fs = require('fs');
const assert = require('assert');
const path = require('path')
const defaultContractsPath = './compiled_contracts/';
const formattedContractsPath = './formatted_contracts';
const { program } = require('commander');
var contractsPath;
program
.description('A util to format compiled contracts')
.option('-p, --path <string>', 'path to compiled contracts')
.option('-c, --contracts <string>', 'contracts to load separated with , (e.g., \'Contract1, Contract2...\' )');
program.parse();
const options = program.opts();
function formatContract(con){
let formattedCon = {};
formattedCon.name = con.contractName;
formattedCon.jsonPath = path.resolve(path.join(contractsPath, `${con.contractName}.json`));
formattedCon.contractAddress = "fill in the contract's address";
formattedCon.fallback = false;
formattedCon.functions = [];
formattedCon.getters = [];
formattedCon.events = [];
formattedCon.indexedEvents = [];
formattedCon.anonymousEvents = [];
formattedCon.abi = con.abi;
const abi = con.abi;
abi.forEach((item, i) => {
if(item.type == 'event'){
let indexed = false;
let event = {};
event.name = item.name;
event.retrieve = true;
item.inputs.forEach((input, i) => {
if(input.indexed) indexed = true;
});
event.toFind = null;
event.paramType = 'type of parameter to search';
event.unused = false;
event.fallback = false;
// When filtering an event you must specify the name of the indexed parameter,
// along with the value_to_match(value that the target event is expected to have).
// Index property is used make this info accesible when needed(ropstenModule).
event.index = {
name : 'name of parameter to search',
value : null
};
if(item.anonymous){
event.indexed = indexed;
formattedCon.anonymousEvents.push(event);
}
else if(indexed){
formattedCon.indexedEvents.push(event);
}else{
formattedCon.events.push(event);
}
}else if(item.type == 'fallback'){
formattedCon.fallback = true;
}else{
let func = {};
func.name = item.name;
func.execute = true;
func.inputs = [];
func.outputs = []
if(item.constant || item.stateMutability === 'view'){
// TODO: check when a func is constant (probably all view functions). Change the code if needed
if(item.outputs.length != 0){
item.outputs.forEach((output, i) => {
func.outputs.push({type: output.type});
});
}
if(item.inputs.length != 0){
item.inputs.forEach((input, i) => {
let arg = {
type : input.type,
value : null
};
if(!input.type.includes("uint") || !input.type.match(/(\d+)/)) arg.length = null;
func.inputs.push(arg);
});
}
formattedCon.getters.push(func);
}else{
if(item.inputs.length != 0){
item.inputs.forEach((input, i) => {
let arg = {
type : input.type,
value : null
};
if(!input.type.includes("uint") || !input.type.match(/(\d+)/)) arg.length = null;
func.inputs.push(arg);
});
}
formattedCon.functions.push(func);
}
}
});
return formattedCon;
}
function getJsonFiles(chosenJsons){
chosenJsons = chosenJsons.toLowerCase().replace(/ /g, '').split(',');
let files = fs.readdirSync(contractsPath).filter(file => {
if (chosenJsons && chosenJsons.length > 0) return chosenJsons.includes(String(path.parse(file).name).toLocaleLowerCase());
else return file.includes('.json');
})
assert(files.length > 0, `No JSON files in ${contractsPath} to format`);
return files;
}
(function(){
if(options.path){
assert(fs.existsSync(options.path), `No such directory: '${options.path}'`);
contractsPath = options.path;
}else{
assert(fs.existsSync(defaultContractsPath), `No such directory: '${defaultContractsPath}'`);
contractsPath = defaultContractsPath;
}
if (!fs.existsSync(formattedContractsPath)){
fs.mkdirSync(formattedContractsPath);
}
let files = getJsonFiles(options.contracts);
files.forEach(file => {
let con = fs.readFileSync(path.join(contractsPath, file), 'utf8');
let formattedCon = formatContract(JSON.parse(con));
fs.writeFileSync(path.join(formattedContractsPath, `${formattedCon.name}.json`), JSON.stringify(formattedCon, null, 2));
});
})();