diff --git a/js/build/embark.bundle.js b/js/build/embark.bundle.js index eed9d3d46..6556a58d4 100644 --- a/js/build/embark.bundle.js +++ b/js/build/embark.bundle.js @@ -282,7 +282,7 @@ var EmbarkJS = var ipfs; if (provider === 'whisper') { this.currentMessages = EmbarkJS.Messages.Whisper; - if (typeof variable === 'undefined') { + if (typeof variable === 'undefined' && typeof(web3) === 'undefined') { if (options === undefined) { web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); } else { diff --git a/js/embark.js b/js/embark.js index 93b7e39fe..d9aba4ac1 100644 --- a/js/embark.js +++ b/js/embark.js @@ -235,7 +235,7 @@ EmbarkJS.Messages.setProvider = function(provider, options) { var ipfs; if (provider === 'whisper') { this.currentMessages = EmbarkJS.Messages.Whisper; - if (typeof variable === 'undefined') { + if (typeof variable === 'undefined' && typeof(web3) === 'undefined') { if (options === undefined) { web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); } else { diff --git a/lib/contracts/abi.js b/lib/contracts/abi.js index 479b9f771..95c444855 100644 --- a/lib/contracts/abi.js +++ b/lib/contracts/abi.js @@ -18,6 +18,14 @@ ABIGenerator.prototype.generateProvider = function() { return ""; } + result += "\nvar whenEnvIsLoaded = function(cb) {"; + result += "\n if (typeof window !== 'undefined' && window !== null) {"; + result += "\n window.addEventListener('load', cb);"; + result += "\n } else {"; + result += "\n cb();"; + result += "\n }"; + result += "\n}"; + if (this.plugins) { providerPlugins = this.plugins.getPluginsFor('clientWeb3Provider'); } @@ -27,12 +35,14 @@ ABIGenerator.prototype.generateProvider = function() { result += plugin.generateProvider(self) + "\n"; }); } else { + result += "\nwhenEnvIsLoaded(function() {"; result += "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {"; result += '\n\tweb3 = new Web3(web3.currentProvider);'; result += "\n} else if (typeof Web3 !== 'undefined') {"; result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("http://' + this.rpcHost + ':' + this.rpcPort + '"));'; result += '\n}'; result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];"; + result += '\n})'; } return result; @@ -62,6 +72,16 @@ ABIGenerator.prototype.generateContracts = function(useEmbarkJS) { var abi = JSON.stringify(contract.abiDefinition); var gasEstimates = JSON.stringify(contract.gasEstimates); + // TODO: refactor this + result += "\nvar whenEnvIsLoaded = function(cb) {"; + result += "\n if (typeof window !== 'undefined' && window !== null) {"; + result += "\n window.addEventListener('load', cb);"; + result += "\n } else {"; + result += "\n cb();"; + result += "\n }"; + result += "\n}"; + + result += "\nwhenEnvIsLoaded(function() {"; if (useEmbarkJS) { result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: '" + contract.deployedAddress + "', code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});"; } else { @@ -69,6 +89,7 @@ ABIGenerator.prototype.generateContracts = function(useEmbarkJS) { result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);"; result += "\n" + className + " = " + className + "Contract.at('" + contract.deployedAddress + "');"; } + result += '\n});'; } } diff --git a/test/abi.js b/test/abi.js index 4d2533ba3..05a443d31 100644 --- a/test/abi.js +++ b/test/abi.js @@ -10,7 +10,7 @@ describe('embark.ABIGenerator', function() { var generator = new ABIGenerator({blockchainConfig: {rpcHost: 'somehost', rpcPort: '1234'}, contractsManager: {}}); it('should generate code to connect to a provider', function() { - var providerCode = "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(web3.currentProvider);\n} else if (typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(\"http://somehost:1234\"));\n}\nweb3.eth.defaultAccount = web3.eth.accounts[0];"; + var providerCode = "\nvar whenEnvIsLoaded = function(cb) {\n if (typeof window !== 'undefined' && window !== null) {\n window.addEventListener('load', cb);\n } else {\n cb();\n }\n}\nwhenEnvIsLoaded(function() {\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(web3.currentProvider);\n} else if (typeof Web3 !== 'undefined') {\n\tweb3 = new Web3(new Web3.providers.HttpProvider(\"http://somehost:1234\"));\n}\nweb3.eth.defaultAccount = web3.eth.accounts[0];\n})" assert.equal(generator.generateProvider(), providerCode); }); @@ -38,7 +38,7 @@ describe('embark.ABIGenerator', function() { var withEmbarkJS = true; it('should generate contract code', function() { - var contractCode = "\n\nSimpleStorage = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x123', code: '12345', gasEstimates: 12000});\nFoo = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x124', code: '123456', gasEstimates: 12000});"; + var contractCode = "\n\nwhenEnvIsLoaded(function() {\nSimpleStorage = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x123', code: '12345', gasEstimates: 12000});\n});\nwhenEnvIsLoaded(function() {\nFoo = new EmbarkJS.Contract({abi: [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}], address: '0x124', code: '123456', gasEstimates: 12000});\n});"; assert.equal(generator.generateContracts(withEmbarkJS), contractCode); }); }); @@ -47,7 +47,7 @@ describe('embark.ABIGenerator', function() { var withEmbarkJS = false; it('should generate contract code', function() { - var contractCode = "\n\nSimpleStorageAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nSimpleStorageContract = web3.eth.contract(SimpleStorageAbi);\nSimpleStorage = SimpleStorageContract.at('0x123');\nFooAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nFooContract = web3.eth.contract(FooAbi);\nFoo = FooContract.at('0x124');"; + var contractCode = "\n\nwhenEnvIsLoaded(function() {\nSimpleStorageAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nSimpleStorageContract = web3.eth.contract(SimpleStorageAbi);\nSimpleStorage = SimpleStorageContract.at('0x123');\n});\nwhenEnvIsLoaded(function() {\nFooAbi = [{\"constant\":true,\"inputs\":[],\"name\":\"storedData\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialValue\",\"type\":\"uint256\"}],\"type\":\"constructor\"}];\nFooContract = web3.eth.contract(FooAbi);\nFoo = FooContract.at('0x124');\n});"; assert.equal(generator.generateContracts(withEmbarkJS), contractCode); }); });