Skip to content

Commit 5a8304d

Browse files
committed
fix for metamask integration;
1 parent 3490083 commit 5a8304d

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

js/build/embark.bundle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ var EmbarkJS =
282282
var ipfs;
283283
if (provider === 'whisper') {
284284
this.currentMessages = EmbarkJS.Messages.Whisper;
285-
if (typeof variable === 'undefined') {
285+
if (typeof variable === 'undefined' && typeof(web3) === 'undefined') {
286286
if (options === undefined) {
287287
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
288288
} else {

js/embark.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ EmbarkJS.Messages.setProvider = function(provider, options) {
235235
var ipfs;
236236
if (provider === 'whisper') {
237237
this.currentMessages = EmbarkJS.Messages.Whisper;
238-
if (typeof variable === 'undefined') {
238+
if (typeof variable === 'undefined' && typeof(web3) === 'undefined') {
239239
if (options === undefined) {
240240
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
241241
} else {

lib/contracts/abi.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ ABIGenerator.prototype.generateProvider = function() {
1818
return "";
1919
}
2020

21+
result += "\nvar whenEnvIsLoaded = function(cb) {";
22+
result += "\n if (typeof window !== 'undefined' && window !== null) {";
23+
result += "\n window.addEventListener('load', cb);";
24+
result += "\n } else {";
25+
result += "\n cb();";
26+
result += "\n }";
27+
result += "\n}";
28+
2129
if (this.plugins) {
2230
providerPlugins = this.plugins.getPluginsFor('clientWeb3Provider');
2331
}
@@ -27,12 +35,14 @@ ABIGenerator.prototype.generateProvider = function() {
2735
result += plugin.generateProvider(self) + "\n";
2836
});
2937
} else {
38+
result += "\nwhenEnvIsLoaded(function() {";
3039
result += "\nif (typeof web3 !== 'undefined' && typeof Web3 !== 'undefined') {";
3140
result += '\n\tweb3 = new Web3(web3.currentProvider);';
3241
result += "\n} else if (typeof Web3 !== 'undefined') {";
3342
result += '\n\tweb3 = new Web3(new Web3.providers.HttpProvider("http://' + this.rpcHost + ':' + this.rpcPort + '"));';
3443
result += '\n}';
3544
result += "\nweb3.eth.defaultAccount = web3.eth.accounts[0];";
45+
result += '\n})';
3646
}
3747

3848
return result;
@@ -62,13 +72,24 @@ ABIGenerator.prototype.generateContracts = function(useEmbarkJS) {
6272
var abi = JSON.stringify(contract.abiDefinition);
6373
var gasEstimates = JSON.stringify(contract.gasEstimates);
6474

75+
// TODO: refactor this
76+
result += "\nvar whenEnvIsLoaded = function(cb) {";
77+
result += "\n if (typeof window !== 'undefined' && window !== null) {";
78+
result += "\n window.addEventListener('load', cb);";
79+
result += "\n } else {";
80+
result += "\n cb();";
81+
result += "\n }";
82+
result += "\n}";
83+
84+
result += "\nwhenEnvIsLoaded(function() {";
6585
if (useEmbarkJS) {
6686
result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: '" + contract.deployedAddress + "', code: '" + contract.code + "', gasEstimates: " + gasEstimates + "});";
6787
} else {
6888
result += "\n" + className + "Abi = " + abi + ";";
6989
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";
7090
result += "\n" + className + " = " + className + "Contract.at('" + contract.deployedAddress + "');";
7191
}
92+
result += '\n});';
7293
}
7394
}
7495

test/abi.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('embark.ABIGenerator', function() {
1010
var generator = new ABIGenerator({blockchainConfig: {rpcHost: 'somehost', rpcPort: '1234'}, contractsManager: {}});
1111

1212
it('should generate code to connect to a provider', function() {
13-
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];";
13+
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})"
1414

1515
assert.equal(generator.generateProvider(), providerCode);
1616
});
@@ -38,7 +38,7 @@ describe('embark.ABIGenerator', function() {
3838
var withEmbarkJS = true;
3939

4040
it('should generate contract code', function() {
41-
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});";
41+
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});";
4242
assert.equal(generator.generateContracts(withEmbarkJS), contractCode);
4343
});
4444
});
@@ -47,7 +47,7 @@ describe('embark.ABIGenerator', function() {
4747
var withEmbarkJS = false;
4848

4949
it('should generate contract code', function() {
50-
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');";
50+
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});";
5151
assert.equal(generator.generateContracts(withEmbarkJS), contractCode);
5252
});
5353
});

0 commit comments

Comments
 (0)