11class ABIGenerator {
22 constructor ( options ) {
33 this . blockchainConfig = options . blockchainConfig || { } ;
4+ this . contractsConfig = options . contractsConfig || { } ;
45 this . storageConfig = options . storageConfig || { } ;
56 this . communicationConfig = options . communicationConfig || { } ;
67 this . contractsManager = options . contractsManager ;
78 this . rpcHost = options . blockchainConfig && options . blockchainConfig . rpcHost ;
89 this . rpcPort = options . blockchainConfig && options . blockchainConfig . rpcPort ;
910 this . plugins = options . plugins ;
11+ this . events = options . events ;
1012 }
1113
12- generateProvider ( ) {
14+ listenToCommands ( ) {
15+ let self = this ;
16+
17+ this . events . setCommandHandler ( 'abi-vanila' , function ( cb ) {
18+ let vanillaABI = self . generateABI ( { useEmbarkJS : false } ) ;
19+ let contractsJSON = self . generateContractsJSON ( ) ;
20+
21+ cb ( vanillaABI , contractsJSON ) ;
22+ } ) ;
23+
24+ this . events . setCommandHandler ( 'abi' , function ( cb ) {
25+ let embarkJSABI = self . generateABI ( { useEmbarkJS : true } ) ;
26+ let contractsJSON = self . generateContractsJSON ( ) ;
27+
28+ cb ( embarkJSABI , contractsJSON ) ;
29+ } ) ;
30+
31+ this . events . setCommandHandler ( 'abi-contracts-vanila' , function ( cb ) {
32+ let vanillaContractsABI = self . generateContracts ( false ) ;
33+ let contractsJSON = self . generateContractsJSON ( ) ;
34+
35+ cb ( vanillaContractsABI , contractsJSON ) ;
36+ } ) ;
37+
38+ this . events . setCommandHandler ( 'abi-vanila-deployment' , function ( cb ) {
39+ let vanillaABI = self . generateABI ( { useEmbarkJS : false , deployment : true } ) ;
40+ let contractsJSON = self . generateContractsJSON ( ) ;
41+
42+ cb ( vanillaABI , contractsJSON ) ;
43+ } ) ;
44+ }
45+
46+ generateProvider ( isDeployment ) {
1347 let self = this ;
1448 let result = "" ;
1549 let providerPlugins ;
1650
51+ // TODO: check contractsConfig for enabled
1752 if ( self . blockchainConfig === { } || self . blockchainConfig . enabled === false ) {
1853 return "" ;
1954 }
@@ -35,12 +70,32 @@ class ABIGenerator {
3570 result += plugin . generateProvider ( self ) + "\n" ;
3671 } ) ;
3772 } else {
38- result += "\nwhenEnvIsLoaded(function() {" ;
39- result += "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {" ;
40- result += '\n\tweb3 = new Web3(web3.currentProvider);' ;
41- result += "\n} else if (typeof Web3 !== 'undefined') {" ;
42- result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("http://' + this . rpcHost + ':' + this . rpcPort + '"));' ;
43- result += '\n}' ;
73+ result += "\nwhenEnvIsLoaded(function() {\n" ;
74+
75+ if ( isDeployment ) {
76+
77+ let connection = "http://" + this . contractsConfig . deployment . host + ":" + this . contractsConfig . deployment . port ;
78+ result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));' ;
79+ } else {
80+
81+ let connectionCode = this . contractsConfig . dappConnection . map ( function ( connection ) {
82+ let code = "" ;
83+ if ( connection === '$WEB3' ) {
84+ code += "if (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {" ;
85+ code += '\n\tweb3 = new Web3(web3.currentProvider);' ;
86+ code += '\n}' ;
87+ } else {
88+ code += "if (typeof Web3 !== 'undefined' && !web3.isConnected()) {" ;
89+ code += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("' + connection + '"));' ;
90+ code += '\n}' ;
91+ }
92+ return code ;
93+ } ) ;
94+
95+ result += connectionCode . join ( ' else ' ) ;
96+ }
97+
98+
4499 result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];" ;
45100 result += '\n})' ;
46101 }
@@ -133,7 +188,7 @@ class ABIGenerator {
133188 generateABI ( options ) {
134189 let result = "" ;
135190
136- result += this . generateProvider ( ) ;
191+ result += this . generateProvider ( options . deployment ) ;
137192 result += this . generateContracts ( options . useEmbarkJS ) ;
138193 result += this . generateStorageInitialization ( options . useEmbarkJS ) ;
139194 result += this . generateCommunicationInitialization ( options . useEmbarkJS ) ;
0 commit comments