@@ -10,9 +10,11 @@ sleep = function sleep(ms) {
1010 while ( new Date ( ) . getTime ( ) < start + ms ) ;
1111}
1212
13- Deploy = function ( env , contractFiles , blockchainConfig , contractsConfig ) {
13+ Deploy = function ( env , contractFiles , blockchainConfig , contractsConfig , chainManager ) {
1414 //this.blockchainConfig = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env);
1515 this . blockchainConfig = blockchainConfig ;
16+ this . chainManager = chainManager ;
17+ this . chainManager . init ( env , this . blockchainConfig ) ;
1618
1719 //this.contractsManager = (new Config.Contracts(contractFiles, blockchainConfig)).loadConfigFile('config/contracts.yml');
1820 this . contractsManager = contractsConfig ;
@@ -56,59 +58,90 @@ Deploy.prototype.deploy_contracts = function(env) {
5658 className = all_contracts [ k ] ;
5759 contract = this . contractDB [ className ] ;
5860
61+
5962 if ( contract . address !== undefined ) {
6063 this . deployedContracts [ className ] = contract . address ;
6164
6265 //console.log("contract " + className + " at " + contractAddress);
6366 console . log ( "contract " + className + " at " + contract . address ) ;
6467 }
6568 else {
66- contractObject = web3 . eth . contract ( contract . compiled . info . abiDefinition ) ;
67-
68- realArgs = [ ] ;
69- for ( var l = 0 ; l < contract . args . length ; l ++ ) {
70- arg = contract . args [ l ] ;
71- if ( arg [ 0 ] === "$" ) {
72- realArgs . push ( this . deployedContracts [ arg . substr ( 1 ) ] ) ;
73- } else {
74- realArgs . push ( arg ) ;
75- }
69+ var chainContract = this . chainManager . getContract ( contract . compiled . code ) ;
70+
71+ if ( chainContract != undefined ) {
72+ console . log ( "contract " + className + " is unchanged and already deployed at " + chainContract . address ) ;
7673 }
74+ else {
7775
78- contractParams = realArgs ;
79- contractParams . push ( {
80- from : primaryAddress ,
81- data : contract . compiled . code ,
82- gas : contract . gasLimit ,
83- gasPrice : contract . gasPrice
84- } ) ;
76+ contractObject = web3 . eth . contract ( contract . compiled . info . abiDefinition ) ;
8577
86- console . log ( 'trying to obtain ' + className + ' address...' ) ;
78+ realArgs = [ ] ;
79+ for ( var l = 0 ; l < contract . args . length ; l ++ ) {
80+ arg = contract . args [ l ] ;
81+ if ( arg [ 0 ] === "$" ) {
82+ realArgs . push ( this . deployedContracts [ arg . substr ( 1 ) ] ) ;
83+ } else {
84+ realArgs . push ( arg ) ;
85+ }
86+ }
8787
88- while ( ( receipt = this . deploy_contract ( contractObject , contractParams ) ) === false ) {
89- console . log ( "timeout... failed to deploy contract.. retrying..." ) ;
90- }
88+ contractParams = realArgs ;
89+ contractParams . push ( {
90+ from : primaryAddress ,
91+ data : contract . compiled . code ,
92+ gas : contract . gasLimit ,
93+ gasPrice : contract . gasPrice
94+ } ) ;
9195
92- var contractAddress = receipt . contractAddress ;
96+ console . log ( 'trying to obtain ' + className + ' address...' ) ;
9397
94- if ( web3 . eth . getCode ( contractAddress ) === "0x" ) {
95- console . log ( "=========" ) ;
96- console . log ( "contract was deployed at " + contractAddress + " but doesn't seem to be working" ) ;
97- console . log ( "try adjusting your gas values" ) ;
98- console . log ( "=========" ) ;
99- }
100- else {
101- console . log ( "deployed " + className + " at " + contractAddress ) ;
102- }
98+ while ( ( receipt = this . deploy_contract ( contractObject , contractParams ) ) === false ) {
99+ console . log ( "timeout... failed to deploy contract.. retrying..." ) ;
100+ }
103101
104- this . deployedContracts [ className ] = contractAddress ;
102+ var contractAddress = receipt . contractAddress ;
105103
106- console . log ( "deployed " + className + " at " + contractAddress ) ;
104+ if ( web3 . eth . getCode ( contractAddress ) === "0x" ) {
105+ console . log ( "=========" ) ;
106+ console . log ( "contract was deployed at " + contractAddress + " but doesn't seem to be working" ) ;
107+ console . log ( "try adjusting your gas values" ) ;
108+ console . log ( "=========" ) ;
109+ }
110+ else {
111+ console . log ( "deployed " + className + " at " + contractAddress ) ;
112+ }
113+
114+ this . deployedContracts [ className ] = contractAddress ;
115+ this . chainManager . addContract ( className , contract . compiled . code , contractAddress ) ;
116+ this . chainManager . save ( ) ;
117+
118+ console . log ( "deployed " + className + " at " + contractAddress ) ;
119+ this . execute_cmds ( contract . onDeploy ) ;
120+ }
107121 }
108122 }
109123
110124} ;
111125
126+ Deploy . prototype . execute_cmds = function ( cmds ) {
127+ if ( cmds . length === 0 ) return ;
128+
129+ eval ( this . generate_abi_file ( ) ) ;
130+ for ( var i = 0 ; i < cmds . length ; i ++ ) {
131+ var cmd = cmds [ i ] ;
132+
133+ for ( className in this . deployedContracts ) {
134+ var contractAddress = this . deployedContracts [ className ] ;
135+
136+ var re = new RegExp ( "\\$" + className , 'g' ) ;
137+ cmd = cmd . replace ( re , '"' + contractAddress + '"' ) ;
138+ }
139+
140+ console . log ( "executing: " + cmd ) ;
141+ eval ( cmd ) ;
142+ }
143+ }
144+
112145Deploy . prototype . generate_abi_file = function ( ) {
113146 var result ;
114147
0 commit comments