2015-05-24 12:48:43 +00:00
|
|
|
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
|
2015-06-04 08:37:09 +00:00
|
|
|
gasLimit = blockchainConfig[env || "development"].gas_limit || 100000
|
2015-06-10 01:05:41 +00:00
|
|
|
gasPrice = blockchainConfig[env || "development"].gas_price || 10000000000000
|
2015-05-24 12:48:43 +00:00
|
|
|
|
|
|
|
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}")
|
2015-05-27 02:11:35 +00:00
|
|
|
compiled_contracts = web3.eth.compile.solidity(source)
|
|
|
|
|
|
|
|
#TODO: refactor this into a common method
|
|
|
|
if compiled_contracts.info is undefined
|
|
|
|
for className, contract of compiled_contracts
|
2015-06-10 01:05:41 +00:00
|
|
|
contractAddress = web3.eth.sendTransaction({from: primaryAddress, data: contract.code, gas: gasLimit, gasPrice: gasPrice})
|
2015-05-27 02:11:35 +00:00
|
|
|
grunt.log.writeln("deployed #{className} at #{contractAddress}")
|
|
|
|
|
|
|
|
abi = JSON.stringify(contract.info.abiDefinition)
|
|
|
|
|
|
|
|
result += "var #{className}Abi = #{abi};"
|
|
|
|
result += "var #{className}Contract = web3.eth.contract(#{className}Abi);"
|
2015-06-04 10:33:14 +00:00
|
|
|
result += "var #{className} = #{className}Contract.at('#{contractAddress}');";
|
2015-05-27 02:11:35 +00:00
|
|
|
else
|
|
|
|
#for geth < 0.9.23
|
|
|
|
contract = compiled_contracts
|
2015-06-04 08:17:14 +00:00
|
|
|
contractAddress = web3.eth.sendTransaction({from: primaryAddress, data: contract.code, gas: gasLimit})
|
2015-05-27 02:11:35 +00:00
|
|
|
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);"
|
2015-06-04 10:33:14 +00:00
|
|
|
result += "var #{className} = #{className}Contract.at('#{contractAddress}');";
|
2015-05-24 12:48:43 +00:00
|
|
|
|
|
|
|
destFile = grunt.config.get("deploy.dest")
|
|
|
|
grunt.file.write(destFile, result)
|
|
|
|
|