Commit e14d029c authored by Iuri Matias's avatar Iuri Matias

refactor deployer into a waterfall

parent 30d67d66
...@@ -26,7 +26,7 @@ var runTest = function(filename, web3) { ...@@ -26,7 +26,7 @@ var runTest = function(filename, web3) {
}, },
function runTests(contracts, next) { function runTests(contracts, next) {
let test = contracts.MyTest; let test = contracts.MyTest;
TestRunner.runTest("SimpleStorage", test, accounts, next); TestRunner.runTest("SimpleStorage", test, next);
} }
], function() { ], function() {
}); });
......
...@@ -5,63 +5,70 @@ var async = require('async'); ...@@ -5,63 +5,70 @@ var async = require('async');
function deployAll(compileResult, web3, accounts, callback) { function deployAll(compileResult, web3, accounts, callback) {
let compiledObject = {}, contracts = {}; let compiledObject = {}, contracts = {};
for (let contractFile in compileResult) { async.waterfall([
for (let contractName in compileResult[contractFile]) { function getContractData(next) {
let contract = compileResult[contractFile][contractName]; for (let contractFile in compileResult) {
for (let contractName in compileResult[contractFile]) {
let contract = compileResult[contractFile][contractName];
const className = contractName; const className = contractName;
const filename = contractFile; const filename = contractFile;
let abi = contract.abi; let abi = contract.abi;
let code = contract.evm.bytecode.object; let code = contract.evm.bytecode.object;
compiledObject[className] = {}; compiledObject[className] = {};
compiledObject[className].abi = abi; compiledObject[className].abi = abi;
compiledObject[className].code = code; compiledObject[className].code = code;
compiledObject[className].filename = filename; compiledObject[className].filename = filename;
compiledObject[className].className = className; compiledObject[className].className = className;
} }
}
async.eachOfLimit(compiledObject, 1, function(contract, contractName, next) {
let contractObject = new web3.eth.Contract(contract.abi);
let contractCode = "0x" + contract.code;
// TODO: temporary code, and terrible if the contracts are not in order...
for (let name in compiledObject) {
let contractObj = compiledObject[name];
let linkReference = '__' + contractObj.filename + ":" + contractObj.className;
let toReplace = linkReference + "_".repeat(40 - linkReference.length);
if (contractCode.indexOf(linkReference) < 0) {
continue
}
if (!contractObj.deployedAddress) {
throw new Error("linking not found for " + name + " when deploying " + contractName);
} }
next();
contractCode = contractCode.replace(new RegExp(toReplace, "g"), contractObj.deployedAddress) },
function deployContracts(next) {
async.eachOfLimit(compiledObject, 1, function(contract, contractName, nextEach) {
let contractObject = new web3.eth.Contract(contract.abi);
let contractCode = "0x" + contract.code;
// TODO: temporary code, and terrible if the contracts are not in order...
for (let name in compiledObject) {
let contractObj = compiledObject[name];
let linkReference = '__' + contractObj.filename + ":" + contractObj.className;
let toReplace = linkReference + "_".repeat(40 - linkReference.length);
if (contractCode.indexOf(linkReference) < 0) {
continue
}
if (!contractObj.deployedAddress) {
throw new Error("linking not found for " + name + " when deploying " + contractName);
}
contractCode = contractCode.replace(new RegExp(toReplace, "g"), contractObj.deployedAddress)
}
contractObject.deploy({arguments: [], data: contractCode}).send({
from: accounts[0],
gas: 4000 * 1000
}).on('receipt', function(receipt) {
contractObject.options.address = receipt.contractAddress;
contractObject.options.from = accounts[0];
contractObject.options.gas = 4000*1000;
compiledObject[contractName].deployedAddress = receipt.contractAddress;
contracts[contractName] = contractObject;
nextEach();
});
}, function() {
next(null, contracts);
});
} }
], callback);
contractObject.deploy({arguments: [], data: contractCode}).send({
from: accounts[0],
gas: 4000 * 1000
}).on('receipt', function(receipt) {
contractObject.options.address = receipt.contractAddress;
contractObject.options.from = accounts[0];
contractObject.options.gas = 4000*1000;
compiledObject[contractName].deployedAddress = receipt.contractAddress;
contracts[contractName] = contractObject;
next();
});
}, function() {
callback(null, contracts);
});
} }
module.exports = { module.exports = {
......
...@@ -2,7 +2,7 @@ var async = require('async'); ...@@ -2,7 +2,7 @@ var async = require('async');
var changeCase = require('change-case'); var changeCase = require('change-case');
require('colors'); require('colors');
function runTest(testName, testObject, accounts, callback) { function runTest(testName, testObject, callback) {
let runList = []; let runList = [];
let specialFunctions = ['beforeAll']; let specialFunctions = ['beforeAll'];
let availableFunctions = testObject._jsonInterface.filter((x) => x.type === 'function').map((x) => x.name); let availableFunctions = testObject._jsonInterface.filter((x) => x.type === 'function').map((x) => x.name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment