From 60d860f27a177c30ede461ddbe289b95bae4f5ab Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 9 Aug 2016 21:59:49 -0400 Subject: [PATCH] support deploying a single contract with custom arguments in the tests --- lib/index.js | 12 +++++ lib/single_deploy.js | 111 +++++++++++++++++++++++++++++++++++++++++++ lib/test.js | 8 ++++ 3 files changed, 131 insertions(+) create mode 100644 lib/single_deploy.js diff --git a/lib/index.js b/lib/index.js index be233afd..5e699a29 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,7 @@ var grunt = require('grunt'); //var Tests = require('./test.js'); var Blockchain = require('./blockchain.js'); var Deploy = require('./deploy.js'); +var SingleDeploy = require('./single_deploy.js'); var Release = require('./ipfs.js'); var Config = require('./config/config.js'); var Compiler = require('./compiler.js'); @@ -65,6 +66,17 @@ Embark = { chain.execGeth(args); }, + deployContract: function(contractFiles, className, args, cb) { + var compiledContracts = this.compiler.compile_solidity(contractFiles); + + var deploy = new SingleDeploy(compiledContracts, this.web3); + deploy.deploy_a_contract(className, args, function() { + var result = ""; + result += deploy.generate_abi_file(); + cb(result); + }); + }, + release: Release, initTests: function() { diff --git a/lib/single_deploy.js b/lib/single_deploy.js new file mode 100644 index 00000000..dc84263c --- /dev/null +++ b/lib/single_deploy.js @@ -0,0 +1,111 @@ +var web3 = require('web3'); +var fs = require('fs'); +var grunt = require('grunt'); +var readYaml = require('read-yaml'); +var Compiler = require('./compiler.js'); +var Config = require('./config/config.js'); +var BigNumber = require('bignumber.js'); + +// this is a temporary module to deploy a single contract, will be refactored + +SingleDeploy = function(compiledContracts, _web3) { + if (_web3 !== undefined) { + web3 = _web3; + } + this.compiledContracts = compiledContracts; +}; + +SingleDeploy.waitForContract = function(transactionHash, cb) { + web3.eth.getTransactionReceipt(transactionHash, function(e, receipt) { + if (!e && receipt && receipt.contractAddress !== undefined) { + cb(receipt.contractAddress); + } + else { + Deploy.waitForContract(transactionHash, cb); + } + }); +}; + +SingleDeploy.prototype.deploy_contract = function(contractObject, contractParams, cb) { + var callback = function(e, contract) { + if(!e && contract.address !== undefined) { + cb(contract.address); + } + else { + Deploy.waitForContract(contract.transactionHash, cb); + } + }; + + contractParams.push(callback); + contractObject["new"].apply(contractObject, contractParams); +}; + +Deploy.prototype.deploy_a_contract = function(className, args, cb) { + var contract = this.compiledContracts[className]; + var contractObject = web3.eth.contract(contract.compiled.info.abiDefinition); + + contractParams = args.slice(); + contractParams.push({ + from: primaryAddress, + data: contract.compiled.code, + gas: contract.gasLimit, + gasPrice: contract.gasPrice + }); + + console.log('trying to obtain ' + className + ' address...'); + + this.deploy_contract(contractObject, contractParams, function(contractAddress) { + if (web3.eth.getCode(contractAddress) === "0x") { + console.log("========="); + console.log("contract was deployed at " + contractAddress + " but doesn't seem to be working"); + console.log("try adjusting your gas values"); + console.log("========="); + } + + cb(); + }); + +}; + +SingleDeploy.prototype.generate_provider_file = function() { + var result = ""; + + result += "if (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {"; + result += 'web3 = new Web3(web3.currentProvider);'; + result += "} else if (typeof Web3 !== 'undefined') {"; + result += 'web3 = new Web3(new Web3.providers.HttpProvider("http://' + this.blockchainConfig.rpcHost + ':' + this.blockchainConfig.rpcPort + '"));'; + result += '}'; + result += "web3.eth.defaultAccount = web3.eth.accounts[0];"; + + return result; +}; + +SingleDeploy.prototype.generate_abi_file = function() { + var result = ""; + + result += 'blockchain = '+JSON.stringify(this.blockchainConfig)+';'; + + for(className in this.deployedContracts) { + var deployedContract = this.deployedContracts[className]; + var contract = this.contractDB[className]; + + var abi = JSON.stringify(contract.compiled.info.abiDefinition); + var contractAddress = deployedContract; + + console.log('address is ' + contractAddress); + + result += className + "Abi = " + abi + ";"; + result += className + "Contract = web3.eth.contract(" + className + "Abi);"; + result += className + " = " + className + "Contract.at('" + contractAddress + "');"; + } + result += 'contractDB = '+JSON.stringify(this.contractDB)+';' + + return result; +}; + +SingleDeploy.prototype.generate_and_write_abi_file = function(destFile) { + var result = this.generate_abi_file(); + grunt.file.write(destFile, result); +}; + +module.exports = SingleDeploy; diff --git a/lib/test.js b/lib/test.js index a3aece95..397853c7 100644 --- a/lib/test.js +++ b/lib/test.js @@ -34,5 +34,13 @@ Test.prototype.deployAll = function(cb) { }); } +Test.prototype.deployContract = function(className, args, cb) { + var web3 = this.web3; + Embark.deployContract(this.contractFiles, className, args, function(abi) { + eval(abi); + cb(); + }); +}; + module.exports = Test;