refactor deploy script + tests

This commit is contained in:
Iuri Matias 2015-07-03 04:59:33 -04:00
parent de9dff3397
commit ad071bc1b2
3 changed files with 100 additions and 27 deletions

View File

@ -10,6 +10,7 @@ ContractsConfig = function(files, blockchainConfig, web3) {
this.contractFiles = files;
this.web3 = web3;
this.contractDependencies = {};
this.blockchainConfig = blockchainConfig;
try {
this.web3.setProvider(new this.web3.providers.HttpProvider("http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort));

View File

@ -4,52 +4,57 @@ var grunt = require('grunt');
var readYaml = require('read-yaml');
var Config = require('./config/config.js');
deployContracts = function(env, contractFiles, destFile) {
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Deploy = function(env, contractFiles, blockchainConfig, contractsConfig) {
//this.blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
this.blockchainConfig = blockchainConfig;
blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
contractsManager = (new Config.Contracts(contractFiles, blockchainConfig)).loadConfigFile('config/contracts.yml');
contractsConfig = contractsManager.config(env);
//this.contractsManager = (new Config.Contracts(contractFiles, blockchainConfig)).loadConfigFile('config/contracts.yml');
this.contractsManager = contractsConfig;
this.contractsConfig = this.contractsManager.config(env);
this.deployedContracts = {};
try {
web3.setProvider(new web3.providers.HttpProvider("http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort));
web3.setProvider(new web3.providers.HttpProvider("http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort));
primaryAddress = web3.eth.coinbase;
web3.eth.defaultAccount = primaryAddress;
} catch (_error) {
e = _error;
console.log("==== can't connect to " + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort + " check if an ethereum node is running");
console.log("==== can't connect to " + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + " check if an ethereum node is running");
exit;
}
console.log("address is : " + primaryAddress);
}
result = "web3.setProvider(new web3.providers.HttpProvider('http://" + blockchainConfig.rpcHost + ":" + blockchainConfig.rpcPort + "'));";
result += "web3.eth.defaultAccount = web3.eth.accounts[0];";
Deploy.prototype.deploy_contracts = function(env) {
this.contractsManager.compileContracts(env);
all_contracts = this.contractsManager.all_contracts;
this.contractDB = this.contractsManager.contractDB;
contractDependencies = this.contractsManager.contractDependencies;
contractsManager.compileContracts(env);
all_contracts = contractsManager.all_contracts;
contractDB = contractsManager.contractDB;
contractDependencies = contractsManager.contractDependencies;
this.deployedContracts = {};
deployedContracts = {};
for (k = 0, len2 = all_contracts.length; k < len2; k++) {
for (k = 0; k < all_contracts.length; k++) {
className = all_contracts[k];
contract = contractDB[className];
contractGasLimit = (contractsConfig != null ? (ref1 = contractsConfig[className]) != null ? ref1.gasLimit : void 0 : void 0) || blockchainConfig.gasLimit;
contractGasPrice = (contractsConfig != null ? (ref2 = contractsConfig[className]) != null ? ref2.gasPrice : void 0 : void 0) || blockchainConfig.gasPrice;
args = (contractsConfig != null ? (ref3 = contractsConfig[className]) != null ? ref3.args : void 0 : void 0) || [];
contract = this.contractDB[className];
contractGasLimit = (this.contractsConfig != null ? (ref1 = this.contractsConfig[className]) != null ? ref1.gasLimit : void 0 : void 0) || this.blockchainConfig.gasLimit;
contractGasPrice = (this.contractsConfig != null ? (ref2 = this.contractsConfig[className]) != null ? ref2.gasPrice : void 0 : void 0) || this.blockchainConfig.gasPrice;
args = (this.contractsConfig != null ? (ref3 = this.contractsConfig[className]) != null ? ref3.args : void 0 : void 0) || [];
contractObject = web3.eth.contract(contract.info.abiDefinition);
realArgs = [];
for (l = 0, len3 = args.length; l < len3; l++) {
arg = args[l];
if (arg[0] === "$") {
realArgs.push(deployedContracts[arg.substr(1)]);
realArgs.push(this.deployedContracts[arg.substr(1)]);
} else {
realArgs.push(arg);
}
}
contractParams = realArgs;
contractParams.push({
from: primaryAddress,
@ -57,21 +62,39 @@ deployContracts = function(env, contractFiles, destFile) {
gas: contractGasLimit,
gasPrice: contractGasPrice
});
contractAddress = contractObject["new"].apply(contractObject, contractParams).address;
deployedContracts[className] = contractAddress;
this.deployedContracts[className] = contractAddress;
console.log("address is " + contractAddress);
console.log("deployed " + className + " at " + contractAddress);
abi = JSON.stringify(contract.info.abiDefinition);
}
}
Deploy.prototype.generate_abi_file = function() {
var result;
result = "web3.setProvider(new web3.providers.HttpProvider('http://" + this.blockchainConfig.rpcHost + ":" + this.blockchainConfig.rpcPort + "'));";
result += "web3.eth.defaultAccount = web3.eth.accounts[0];";
for(className in this.deployedContracts) {
var deployedContract = this.deployedContracts[className];
var contract = this.contractDB[className];
var abi = JSON.stringify(contract.info.abiDefinition);
var contractAddress = deployedContract;
result += "var " + className + "Abi = " + abi + ";";
result += "var " + className + "Contract = web3.eth.contract(" + className + "Abi);";
result += "var " + className + " = " + className + "Contract.at('" + contractAddress + "');";
}
grunt.file.write(destFile, result);
return result;
}
Deploy = {
deployContracts: deployContracts
Deploy.prototype.generate_and_write_abi_file = function(destFile) {
var result = this.generate_abi_file();
grunt.file.write(destFile, result);
}
module.exports = Deploy

49
test/deploy.js Normal file
View File

@ -0,0 +1,49 @@
var Config = require('../lib/config/config.js');
var Deploy = require('../lib/deploy.js');
var assert = require('assert');
var web3 = require('web3');
describe('embark.deploy', function() {
var files = [
'test/support/contracts/wallet.sol',
'test/support/contracts/simple_storage.sol',
'test/support/contracts/another_storage.sol',
'test/support/contracts/wallets.sol'
];
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development");
var contractsConfig = new Config.Contracts(files, blockchainConfig, web3);
contractsConfig.loadConfigFile('test/support/arguments.yml');
var deploy = new Deploy('development', files, blockchainConfig, contractsConfig);
describe('#deploy_contracts', function() {
deploy.deploy_contracts("development");
it("should deploy contracts", function() {
var all_contracts = ['Wallet', 'SimpleStorage', 'AnotherStorage', 'Wallets'];
for(var i=0; i < all_contracts; i++) {
var className = all_contracts[i];
assert.equal(deploy.deployedContracts.hasOwnProperty(className), true);
}
});
});
describe('#generate_abi_file', function() {
deploy.deployedContracts = {
"SimpleStorage": "0x123",
"AnotherStorage": "0x234"
}
deploy.contractDB = {
"SimpleStorage": {info: {abiDefinition: 123}},
"AnotherStorage": {info: {abiDefinition: 234}}
}
it("should deploy contracts", function() {
var result = deploy.generate_abi_file();
assert.strictEqual(result, "web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));web3.eth.defaultAccount = web3.eth.accounts[0];var SimpleStorageAbi = 123;var SimpleStorageContract = web3.eth.contract(SimpleStorageAbi);var SimpleStorage = SimpleStorageContract.at('0x123');var AnotherStorageAbi = 234;var AnotherStorageContract = web3.eth.contract(AnotherStorageAbi);var AnotherStorage = AnotherStorageContract.at('0x234');");
});
});
});