-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (164 loc) · 6.29 KB
/
index.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var through = require('through2');
var gutil = require('gulp-util');
var graphlib = require('graphlib');
var Graph = require("graphlib").Graph;
var Web3 = require('web3');
var solc = require('solc');
var Pudding = require('ether-pudding');
var PluginError = gutil.PluginError;
// consts
const PLUGIN_NAME = 'gulp-contract-transform';
var contractTransform = {};
var createContractFile = function(contract,name) {
name = name || contract.contract_name;
var newFile = new gutil.File({
cwd: "",
base: "",
path: name+'.sol.js',
contents: new Buffer(contract)
});
return newFile;
};
var deployContract = function(web3,g,contract,account,gas) {
gutil.log('Deploying:',contract.contract_name);
var regex = /__([^_]*)_*/g;
var match;
while(match = regex.exec(contract.binary)) {
var address = g.node(match[1]).address.slice(2);
contract.binary = contract.binary
.replace(RegExp(match[0],'g'),address);
}
contract.setProvider(web3.currentProvider);
contract.defaults({ from: account, gas: gas });
var deployedContract = contract.new(contract,{});
deployedContract = deployedContract.then(function(contractInstance) {
gutil.log('Contract deployed with address:',contractInstance.address);
contract.address = contractInstance.address;
return contract;
}).catch(function(err) {
console.log("Error deploying contract!");
console.log(err.stack);
});
return deployedContract;
};
contractTransform.compileContracts = function() {
var g = new Graph();
var stream = through.obj(function(file, enc, cb) {
if (file.isStream()) {
this.emit('error',
new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return cb();
}
if (file.isBuffer()) {
var contractSrc = file.contents.toString();
var filename = file.path.slice(file.base.length);
g.setNode(filename,contractSrc);
var regex = /import\s+["']([^"']+)["']/g;
var match;
while(match = regex.exec(contractSrc)) {
g.setEdge(filename,match[1]);
}
}
//this.push(file)
cb();
},function(cb) {
gutil.log('Compiling contracts...');
if(!graphlib.alg.isAcyclic(g)) {
this.emit('error',
new PluginError(PLUGIN_NAME, 'Contract depencies cycle!'));
return cb();
} else {
var orderedNodes = graphlib.alg.topsort(g).reverse();
var source = orderedNodes.reduce(function(obj,filename) {
obj[filename] = g.node(filename)
.replace(/\/\/[^\n]*\n/g,' ')
.replace(/\n/g,' ')
.replace(/\/\*[^\*]*\*\//g,' ')
.replace(/\s+/g,' ');
return obj;
},{});
var compiled = solc.compile({sources:source}, 1);
var contractNameArray = Object.keys(compiled.contracts);
contractNameArray.forEach(function(name) {
var interface = compiled.contracts[name].interface;
var bytecode = compiled.contracts[name].bytecode;
var contract_data = {
abi: JSON.parse(interface),
binary: bytecode,
unlinked_binary: bytecode
};
var contract = Pudding.generate(name,contract_data);
this.push(createContractFile(contract,name));
gutil.log('Contract JS created for',name);
}.bind(this));
}
cb();
});
return stream;
};
contractTransform.deployContracts = function(providerURL,account,gas) {
var web3 = new Web3();
providerURL = providerURL || 'http://localhost:8545';
var provider = new web3.providers.HttpProvider(providerURL);
web3.setProvider(provider);
account = account || web3.eth.accounts[0];
gas = gas || 300000;
var g = new Graph();
var stream = through.obj(function(file, enc, cb) {
if (file.isStream()) {
this.emit('error',
new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
return cb();
}
if (file.isBuffer()) {
var filename = file.path.slice(file.base.length);
var contractJS = require(file.path);
g.setNode(contractJS.contract_name,contractJS);
//gutil.log(contractSrc);
var regex = /__([^_]*)_*/g;
var match;
while(match = regex.exec(contractJS.binary)) {
g.setEdge(contractJS.contract_name,match[1]);
}
}
//this.push(file)
cb();
},function(cb) {
gutil.log('Deploying contracts...');
if(!graphlib.alg.isAcyclic(g)) {
this.emit('error',
new PluginError(PLUGIN_NAME, 'Contract depencies cycle!'));
return cb();
} else {
var orderedNodes = graphlib.alg.topsort(g).reverse();
var lastProm = orderedNodes.reduce(function(promise,contractName) {
var contract = g.node(contractName);
var deployFunc = deployContract.bind(this,web3,g,contract,account,gas);
if(promise) promise = promise.then(deployFunc);
else promise = deployFunc()
return promise.then(function(x){
// this is function and needs to be made into a string
// to be saved properly
var name = x.contract_name
var contract_data = {
abi: x.abi,
binary: x.binary,
unlinked_binary: x.unlinked_binary,
address: x.address
};
var contract = Pudding.generate(name,contract_data);
this.push(createContractFile(contract,name));
}.bind(this));
}.bind(this),null);
//run cb after it ended creating all the contracts
lastProm.then(function(){
gutil.log('All contracts deployed!');
cb();
});
}
//cb();
});
return stream;
};
// exporting the plugin main function
module.exports = contractTransform;