|
| 1 | +var web3 = require('web3'); |
| 2 | +var fs = require('fs'); |
| 3 | +var grunt = require('grunt'); |
| 4 | +var readYaml = require('read-yaml'); |
| 5 | +var Compiler = require('./compiler.js'); |
| 6 | +var Config = require('./config/config.js'); |
| 7 | +var BigNumber = require('bignumber.js'); |
| 8 | + |
| 9 | +// this is a temporary module to deploy a single contract, will be refactored |
| 10 | + |
| 11 | +SingleDeploy = function(compiledContracts, _web3) { |
| 12 | + if (_web3 !== undefined) { |
| 13 | + web3 = _web3; |
| 14 | + } |
| 15 | + this.compiledContracts = compiledContracts; |
| 16 | +}; |
| 17 | + |
| 18 | +SingleDeploy.waitForContract = function(transactionHash, cb) { |
| 19 | + web3.eth.getTransactionReceipt(transactionHash, function(e, receipt) { |
| 20 | + if (!e && receipt && receipt.contractAddress !== undefined) { |
| 21 | + cb(receipt.contractAddress); |
| 22 | + } |
| 23 | + else { |
| 24 | + Deploy.waitForContract(transactionHash, cb); |
| 25 | + } |
| 26 | + }); |
| 27 | +}; |
| 28 | + |
| 29 | +SingleDeploy.prototype.deploy_contract = function(contractObject, contractParams, cb) { |
| 30 | + var callback = function(e, contract) { |
| 31 | + if(!e && contract.address !== undefined) { |
| 32 | + cb(contract.address); |
| 33 | + } |
| 34 | + else { |
| 35 | + Deploy.waitForContract(contract.transactionHash, cb); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + contractParams.push(callback); |
| 40 | + contractObject["new"].apply(contractObject, contractParams); |
| 41 | +}; |
| 42 | + |
| 43 | +Deploy.prototype.deploy_a_contract = function(className, args, cb) { |
| 44 | + var contract = this.compiledContracts[className]; |
| 45 | + var contractObject = web3.eth.contract(contract.compiled.info.abiDefinition); |
| 46 | + |
| 47 | + contractParams = args.slice(); |
| 48 | + contractParams.push({ |
| 49 | + from: primaryAddress, |
| 50 | + data: contract.compiled.code, |
| 51 | + gas: contract.gasLimit, |
| 52 | + gasPrice: contract.gasPrice |
| 53 | + }); |
| 54 | + |
| 55 | + console.log('trying to obtain ' + className + ' address...'); |
| 56 | + |
| 57 | + this.deploy_contract(contractObject, contractParams, function(contractAddress) { |
| 58 | + if (web3.eth.getCode(contractAddress) === "0x") { |
| 59 | + console.log("========="); |
| 60 | + console.log("contract was deployed at " + contractAddress + " but doesn't seem to be working"); |
| 61 | + console.log("try adjusting your gas values"); |
| 62 | + console.log("========="); |
| 63 | + } |
| 64 | + |
| 65 | + cb(); |
| 66 | + }); |
| 67 | + |
| 68 | +}; |
| 69 | + |
| 70 | +SingleDeploy.prototype.generate_provider_file = function() { |
| 71 | + var result = ""; |
| 72 | + |
| 73 | + result += "if (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {"; |
| 74 | + result += 'web3 = new Web3(web3.currentProvider);'; |
| 75 | + result += "} else if (typeof Web3 !== 'undefined') {"; |
| 76 | + result += 'web3 = new Web3(new Web3.providers.HttpProvider("http://' + this.blockchainConfig.rpcHost + ':' + this.blockchainConfig.rpcPort + '"));'; |
| 77 | + result += '}'; |
| 78 | + result += "web3.eth.defaultAccount = web3.eth.accounts[0];"; |
| 79 | + |
| 80 | + return result; |
| 81 | +}; |
| 82 | + |
| 83 | +SingleDeploy.prototype.generate_abi_file = function() { |
| 84 | + var result = ""; |
| 85 | + |
| 86 | + result += 'blockchain = '+JSON.stringify(this.blockchainConfig)+';'; |
| 87 | + |
| 88 | + for(className in this.deployedContracts) { |
| 89 | + var deployedContract = this.deployedContracts[className]; |
| 90 | + var contract = this.contractDB[className]; |
| 91 | + |
| 92 | + var abi = JSON.stringify(contract.compiled.info.abiDefinition); |
| 93 | + var contractAddress = deployedContract; |
| 94 | + |
| 95 | + console.log('address is ' + contractAddress); |
| 96 | + |
| 97 | + result += className + "Abi = " + abi + ";"; |
| 98 | + result += className + "Contract = web3.eth.contract(" + className + "Abi);"; |
| 99 | + result += className + " = " + className + "Contract.at('" + contractAddress + "');"; |
| 100 | + } |
| 101 | + result += 'contractDB = '+JSON.stringify(this.contractDB)+';' |
| 102 | + |
| 103 | + return result; |
| 104 | +}; |
| 105 | + |
| 106 | +SingleDeploy.prototype.generate_and_write_abi_file = function(destFile) { |
| 107 | + var result = this.generate_abi_file(); |
| 108 | + grunt.file.write(destFile, result); |
| 109 | +}; |
| 110 | + |
| 111 | +module.exports = SingleDeploy; |
0 commit comments