From 05571459fac0e80c51b4e50dee5c6dd57dd7618d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 24 May 2015 08:48:43 -0400 Subject: [PATCH] add deploy task --- tasks/deploy.coffee | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 tasks/deploy.coffee diff --git a/tasks/deploy.coffee b/tasks/deploy.coffee new file mode 100644 index 000000000..72b6a2aea --- /dev/null +++ b/tasks/deploy.coffee @@ -0,0 +1,41 @@ +module.exports = (grunt) -> + web3 = require('web3') + readYaml = require('read-yaml'); + + grunt.registerTask "deploy_contracts", "deploy code", (env) => + blockchainConfig = readYaml.sync("config/blockchain.yml") + rpcHost = blockchainConfig[env || "development"].rpc_host + rpcPort = blockchainConfig[env || "development"].rpc_port + + try + web3.setProvider(new web3.providers.HttpProvider("http://#{rpcHost}:#{rpcPort}")) + primaryAddress = web3.eth.coinbase + web3.eth.defaultAccount = primaryAddress + catch e + grunt.log.writeln("==== can't connect to #{rpcHost}:#{rpcPort} check if an ethereum node is running") + exit + + grunt.log.writeln("address is : #{primaryAddress}") + + result = "web3.setProvider(new web3.providers.HttpProvider('http://#{rpcHost}:#{rpcPort}'));" + result += "web3.eth.defaultAccount = web3.eth.accounts[0];" + + contractFiles = grunt.file.expand(grunt.config.get("deploy.contracts")) + for contractFile in contractFiles + source = grunt.file.read(contractFile) + + grunt.log.writeln("deploying #{contractFile}") + contract = web3.eth.compile.solidity(source) + contractAddress = web3.eth.sendTransaction({from: primaryAddress, data: contract.code}) + grunt.log.writeln("deployed at #{contractAddress}") + + abi = JSON.stringify(contract.info.abiDefinition) + className = source.match(/contract (\w+)(?=\s[is|{])/g)[0].replace("contract ","") + + result += "var #{className}Abi = #{abi};" + result += "var #{className}Contract = web3.eth.contract(#{className}Abi);" + result += "var #{className} = new #{className}Contract('#{contractAddress}');"; + + destFile = grunt.config.get("deploy.dest") + grunt.file.write(destFile, result) +