From 5a375d9a5e1f17ee39b646a6c5329fb7256dca1f Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Fri, 16 Nov 2018 09:05:42 +0000 Subject: [PATCH] feat: enable ethereum manually --- src/lib/core/config.js | 1 + .../code_generator/code_templates/web3-connector.js.ejs | 1 + src/lib/modules/code_generator/index.js | 7 ++++++- src/test/config.js | 2 ++ templates/boilerplate/config/contracts.js | 5 +++++ templates/demo/app/components/blockchain.js | 6 +++--- templates/demo/config/contracts.js | 5 +++++ templates/simple/contracts.js | 5 +++++ 8 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/lib/core/config.js b/src/lib/core/config.js index 636e138cd..c617d6ba2 100644 --- a/src/lib/core/config.js +++ b/src/lib/core/config.js @@ -284,6 +284,7 @@ Config.prototype.loadContractsConfigFile = function() { "$WEB3", "localhost:8545" ], + "dappAutoEnable": true, "strategy": constants.deploymentStrategy.implicit, "gas": "auto", "contracts": { diff --git a/src/lib/modules/code_generator/code_templates/web3-connector.js.ejs b/src/lib/modules/code_generator/code_templates/web3-connector.js.ejs index 1666f3879..69a2625ec 100644 --- a/src/lib/modules/code_generator/code_templates/web3-connector.js.ejs +++ b/src/lib/modules/code_generator/code_templates/web3-connector.js.ejs @@ -1,3 +1,4 @@ +EmbarkJS.Blockchain.autoEnable = <%= autoEnable %>; EmbarkJS.Blockchain.connect(<%- connectionList %>, {warnAboutMetamask: <%= warnAboutMetamask %>}, function(err) { <%- done %> }); diff --git a/src/lib/modules/code_generator/index.js b/src/lib/modules/code_generator/index.js index c91b2ba96..5666b121c 100644 --- a/src/lib/modules/code_generator/index.js +++ b/src/lib/modules/code_generator/index.js @@ -120,7 +120,12 @@ class CodeGenerator { } else { let connectionList = "[" + this.contractsConfig.dappConnection.map((x) => '"' + x + '"').join(',') + "]"; let isDev = (self.env === 'development'); - web3Load = Templates.web3_connector({connectionList: connectionList, done: 'done(err);', warnAboutMetamask: isDev}); + web3Load = Templates.web3_connector({ + autoEnable: this.contractsConfig.dappAutoEnable, + connectionList: connectionList, + done: 'done(err);', + warnAboutMetamask: isDev + }); } result += Templates.do_when_loaded({block: web3Load, environment: this.env}); diff --git a/src/test/config.js b/src/test/config.js index f26ae1eeb..c32e3eb35 100644 --- a/src/test/config.js +++ b/src/test/config.js @@ -114,6 +114,7 @@ describe('embark.Config', function () { versions: {'web3': '1.0.0-beta', solc: '0.5.0'}, deployment: {host: 'localhost', port: 8545, type: 'rpc', "accounts": [{"mnemonic": "12 word mnemonic", "balance": "5000000000"}]}, dappConnection: ['$WEB3', 'localhost:8545'], + dappAutoEnable: true, "gas": "400000", "strategy": "implicit", "contracts": { @@ -136,6 +137,7 @@ describe('embark.Config', function () { versions: {'web3': '1.0.0-beta', solc: '0.5.0'}, deployment: {host: 'localhost', port: 8545, type: 'rpc'}, dappConnection: ['$WEB3', 'localhost:8545'], + dappAutoEnable: true, "gas": "auto", "strategy": "implicit", "contracts": { diff --git a/templates/boilerplate/config/contracts.js b/templates/boilerplate/config/contracts.js index 380cad251..d1bb569b9 100644 --- a/templates/boilerplate/config/contracts.js +++ b/templates/boilerplate/config/contracts.js @@ -32,6 +32,11 @@ module.exports = { "http://localhost:8545" ], + // Automatically call `ethereum.enable` if true. + // If false, the following code must run before sending any transaction: `await EmbarkJS.enableEthereum();` + // Default value is true. + // dappAutoEnable: true, + gas: "auto", // Strategy for the deployment of the contracts: diff --git a/templates/demo/app/components/blockchain.js b/templates/demo/app/components/blockchain.js index 671474b60..d2243554c 100644 --- a/templates/demo/app/components/blockchain.js +++ b/templates/demo/app/components/blockchain.js @@ -12,7 +12,7 @@ class Blockchain extends React.Component { valueSet: 10, valueGet: "", logs: [] - } + }; } handleChange(e) { @@ -27,9 +27,9 @@ class Blockchain extends React.Component { func.apply(this, [e]); } - setValue(e) { + async setValue(e) { e.preventDefault(); - + await EmbarkJS.enableEthereum(); var value = parseInt(this.state.valueSet, 10); SimpleStorage.methods.set(value).send(); diff --git a/templates/demo/config/contracts.js b/templates/demo/config/contracts.js index 9e4cb3c2d..b60e68777 100644 --- a/templates/demo/config/contracts.js +++ b/templates/demo/config/contracts.js @@ -32,6 +32,11 @@ module.exports = { "http://localhost:8545" ], + // Automatically call `ethereum.enable` if true. + // If false, the following code must run before sending any transaction: `await EmbarkJS.enableEthereum();` + // Default value is true. + // dappAutoEnable: true, + gas: "auto", // Strategy for the deployment of the contracts: diff --git a/templates/simple/contracts.js b/templates/simple/contracts.js index 380cad251..d1bb569b9 100644 --- a/templates/simple/contracts.js +++ b/templates/simple/contracts.js @@ -32,6 +32,11 @@ module.exports = { "http://localhost:8545" ], + // Automatically call `ethereum.enable` if true. + // If false, the following code must run before sending any transaction: `await EmbarkJS.enableEthereum();` + // Default value is true. + // dappAutoEnable: true, + gas: "auto", // Strategy for the deployment of the contracts: