mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-05 02:14:35 +00:00
support receiving contract as argument; automatically determine correct deployment order
This commit is contained in:
parent
9abd93ff7f
commit
a1a3aab6e5
@ -23,6 +23,19 @@ module.exports = (grunt) ->
|
|||||||
result = "web3.setProvider(new web3.providers.HttpProvider('http://#{rpcHost}:#{rpcPort}'));"
|
result = "web3.setProvider(new web3.providers.HttpProvider('http://#{rpcHost}:#{rpcPort}'));"
|
||||||
result += "web3.eth.defaultAccount = web3.eth.accounts[0];"
|
result += "web3.eth.defaultAccount = web3.eth.accounts[0];"
|
||||||
|
|
||||||
|
contractDependencies = {}
|
||||||
|
if contractsConfig?
|
||||||
|
for className, options of contractsConfig
|
||||||
|
continue unless options.args?
|
||||||
|
for arg in options.args
|
||||||
|
if arg[0] is "$"
|
||||||
|
if contractDependencies[className] is undefined
|
||||||
|
contractDependencies[className] = []
|
||||||
|
contractDependencies[className].push(arg.substr(1))
|
||||||
|
|
||||||
|
all_contracts = []
|
||||||
|
contractDB = {}
|
||||||
|
|
||||||
contractFiles = grunt.file.expand(grunt.config.get("deploy.contracts"))
|
contractFiles = grunt.file.expand(grunt.config.get("deploy.contracts"))
|
||||||
for contractFile in contractFiles
|
for contractFile in contractFiles
|
||||||
source = grunt.file.read(contractFile)
|
source = grunt.file.read(contractFile)
|
||||||
@ -31,26 +44,56 @@ module.exports = (grunt) ->
|
|||||||
compiled_contracts = web3.eth.compile.solidity(source)
|
compiled_contracts = web3.eth.compile.solidity(source)
|
||||||
|
|
||||||
for className, contract of compiled_contracts
|
for className, contract of compiled_contracts
|
||||||
contractGasLimit = contractsConfig?[className]?.gasLimit || gasLimit
|
all_contracts.push(className)
|
||||||
contractGasPrice = contractsConfig?[className]?.gasPrice || gasPrice
|
contractDB[className] = contract
|
||||||
|
|
||||||
args = contractsConfig?[className]?.args
|
all_contracts.sort (a,b) =>
|
||||||
|
contract_1 = contractDependencies[a]
|
||||||
|
contract_2 = contractDependencies[b]
|
||||||
|
|
||||||
contractObject = web3.eth.contract(contract.info.abiDefinition)
|
if a in contract_1 and b in contract_2
|
||||||
|
grunt.log.writeln("looks like you have a circular dependency between #{a} and #{b}")
|
||||||
|
exit
|
||||||
|
else if b in contract_1
|
||||||
|
1
|
||||||
|
else if a in contract_2
|
||||||
|
-1
|
||||||
|
else
|
||||||
|
0
|
||||||
|
|
||||||
contractParams = args || []
|
deployedContracts = {}
|
||||||
contractParams.push({from: primaryAddress, data: contract.code, gas: contractGasLimit, gasPrice: contractGasPrice})
|
|
||||||
contractAddress = contractObject.new.apply(contractObject, contractParams).address
|
|
||||||
|
|
||||||
console.log "address is #{contractAddress}"
|
for className in all_contracts
|
||||||
|
contract = contractDB[className]
|
||||||
|
contractGasLimit = contractsConfig?[className]?.gasLimit || gasLimit
|
||||||
|
contractGasPrice = contractsConfig?[className]?.gasPrice || gasPrice
|
||||||
|
|
||||||
grunt.log.writeln("deployed #{className} at #{contractAddress}")
|
args = contractsConfig?[className]?.args || []
|
||||||
|
|
||||||
abi = JSON.stringify(contract.info.abiDefinition)
|
contractObject = web3.eth.contract(contract.info.abiDefinition)
|
||||||
|
|
||||||
result += "var #{className}Abi = #{abi};"
|
realArgs = []
|
||||||
result += "var #{className}Contract = web3.eth.contract(#{className}Abi);"
|
|
||||||
result += "var #{className} = #{className}Contract.at('#{contractAddress}');";
|
for arg in args
|
||||||
|
if arg[0] is "$"
|
||||||
|
realArgs.push(deployedContracts[arg.substr(1)])
|
||||||
|
else
|
||||||
|
realArgs.push(arg)
|
||||||
|
|
||||||
|
contractParams = realArgs
|
||||||
|
contractParams.push({from: primaryAddress, data: contract.code, gas: contractGasLimit, gasPrice: contractGasPrice})
|
||||||
|
contractAddress = contractObject.new.apply(contractObject, contractParams).address
|
||||||
|
deployedContracts[className] = contractAddress
|
||||||
|
|
||||||
|
console.log "address is #{contractAddress}"
|
||||||
|
|
||||||
|
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);"
|
||||||
|
result += "var #{className} = #{className}Contract.at('#{contractAddress}');";
|
||||||
|
|
||||||
destFile = grunt.config.get("deploy.dest")
|
destFile = grunt.config.get("deploy.dest")
|
||||||
grunt.file.write(destFile, result)
|
grunt.file.write(destFile, result)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user