From 20fccfb1c6a6d9196af4e28c120f63a6538e494b Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 29 Nov 2018 17:12:15 -0500 Subject: [PATCH] refactor(blockchain): add a web3 provider for embarkJS --- .../modules/blockchain_connector/embarkjs.js | 45 +++++++++++++++++++ src/lib/modules/blockchain_connector/index.js | 21 +++++++++ src/lib/modules/console/index.ts | 14 +++++- src/lib/modules/ens/index.js | 22 ++++----- src/lib/modules/tests/test.js | 5 +++ .../coverage_app/app/components/blockchain.js | 30 ++++--------- test_apps/coverage_app/app/dapp.js | 24 ++-------- test_apps/test_app/app/js/index.js | 27 +++-------- 8 files changed, 114 insertions(+), 74 deletions(-) create mode 100644 src/lib/modules/blockchain_connector/embarkjs.js diff --git a/src/lib/modules/blockchain_connector/embarkjs.js b/src/lib/modules/blockchain_connector/embarkjs.js new file mode 100644 index 000000000..8d79df1ff --- /dev/null +++ b/src/lib/modules/blockchain_connector/embarkjs.js @@ -0,0 +1,45 @@ +/*global Web3*/ + +const __embarkWeb3 = {}; + +__embarkWeb3.init = function (_config) { + this.web3 = new Web3(); +}; + +__embarkWeb3.getAccounts = function () { + return this.web3.eth.getAccounts(...arguments); +}; + +__embarkWeb3.getNewProvider = function (providerName, ...args) { + return new Web3.providers[providerName](...args); +}; + +__embarkWeb3.setProvider = function (provider) { + return this.web3.setProvider(provider); +}; + +__embarkWeb3.getCurrentProvider = function () { + return this.web3.currentProvider; +}; + +__embarkWeb3.getDefaultAccount = function () { + return this.web3.eth.defaultAccount; +}; + +__embarkWeb3.setDefaultAccount = function (account) { + this.web3.eth.defaultAccount = account; +}; + +__embarkWeb3.newContract = function (options) { + return new this.web3.eth.Contract(options.abi, options.address); +}; + +__embarkWeb3.send = function () { + return this.web3.eth.sendTransaction(...arguments); +}; + +__embarkWeb3.toWei = function () { + return this.web3.toWei(...arguments); +}; + + diff --git a/src/lib/modules/blockchain_connector/index.js b/src/lib/modules/blockchain_connector/index.js index 910c51a96..6b60f536e 100644 --- a/src/lib/modules/blockchain_connector/index.js +++ b/src/lib/modules/blockchain_connector/index.js @@ -14,6 +14,7 @@ const BLOCK_LIMIT = 100; class BlockchainConnector { constructor(embark, options) { const self = this; + this.embark = embark; this.plugins = options.plugins; this.logger = embark.logger; this.events = embark.events; @@ -68,6 +69,7 @@ class BlockchainConnector { this.registerWeb3Object(); this.registerEvents(); this.subscribeToPendingTransactions(); + this.addWeb3ToEmbarkJS(); } initWeb3(cb) { @@ -207,6 +209,25 @@ class BlockchainConnector { } } + addWeb3ToEmbarkJS() { + let code = ''; + code += fs.readFileSync(utils.joinPath(__dirname, 'embarkjs.js')).toString(); + code += "\nEmbarkJS.Blockchain.registerProvider('web3', __embarkWeb3);"; + + // TODO when we refactor code generator, refactor this to actually do something like connect + code += "\nEmbarkJS.Blockchain.setProvider('web3', {});"; + + this.embark.addCodeToEmbarkJS(code); + + code = "EmbarkJS.Blockchain.setProvider('web3', {});"; + + const shouldInit = (_config) => { + return true; + }; + + this.embark.addConsoleProviderInit('blockchain', code, shouldInit); + } + registerEvents() { this.events.on('check:wentOffline:Ethereum', () => { this.logger.warn('Ethereum went offline: stopping web3 provider...'); diff --git a/src/lib/modules/console/index.ts b/src/lib/modules/console/index.ts index 2d5610b1e..bb982b765 100644 --- a/src/lib/modules/console/index.ts +++ b/src/lib/modules/console/index.ts @@ -20,6 +20,7 @@ class Console { private history: string[]; private cmdHistoryFile: string; private suggestions: Suggestions; + private providerReady: boolean; constructor(embark: Embark, options: any) { this.embark = embark; @@ -31,6 +32,7 @@ class Console { this.config = options.config; this.history = []; this.cmdHistoryFile = options.cmdHistoryFile || fs.dappPath(".embark", "cmd_history"); + this.providerReady = false; this.loadHistory(); if (this.ipc.isServer()) { @@ -38,6 +40,12 @@ class Console { } this.events.setCommandHandler("console:executeCmd", this.executeCmd.bind(this)); this.events.setCommandHandler("console:history", (cb: any) => this.getHistory(this.cmdHistorySize(), cb)); + this.events.setCommandHandler("console:provider:ready", (cb: any) => { + if (this.providerReady) { + return cb(); + } + this.events.once("console:provider:done", cb); + }); this.registerEmbarkJs(); this.registerConsoleCommands(); this.registerApi(); @@ -148,13 +156,17 @@ class Console { this.events.request("code-generator:embarkjs:provider-code", (code: string) => { const func = () => {}; this.events.request("runcode:eval", code, func, true); - this.events.request("runcode:eval", this.getInitProviderCode(), func, true); + this.events.request("runcode:eval", this.getInitProviderCode(), () => { + this.events.emit("console:provider:done"); + this.providerReady = true; + }, true); }); }); } private getInitProviderCode() { const codeTypes: any = { + blockchain: this.config.blockchainConfig || {}, communication: this.config.communicationConfig || {}, names: this.config.namesystemConfig || {}, storage: this.config.storageConfig || {}, diff --git a/src/lib/modules/ens/index.js b/src/lib/modules/ens/index.js index 97efa8f63..4d0254f3a 100644 --- a/src/lib/modules/ens/index.js +++ b/src/lib/modules/ens/index.js @@ -348,6 +348,17 @@ class ENS { this.embark.addCodeToEmbarkJS(code); } + addSetProvider(config) { + let code = "\nEmbarkJS.Names.setProvider('ens'," + JSON.stringify(config) + ");"; + + let shouldInit = (namesConfig) => { + return (namesConfig.provider === 'ens' && namesConfig.enabled === true); + }; + + this.embark.addProviderInit('names', code, shouldInit); + this.embark.addConsoleProviderInit('names', code, shouldInit); + } + configureContractsAndRegister(cb) { const NO_REGISTRATION = 'NO_REGISTRATION'; const self = this; @@ -476,17 +487,6 @@ class ENS { }); } - addSetProvider(config) { - let code = "\nEmbarkJS.Names.setProvider('ens'," + JSON.stringify(config) + ");"; - - let shouldInit = (namesConfig) => { - return (namesConfig.provider === 'ens' && namesConfig.enabled === true); - }; - - this.embark.addProviderInit('names', code, shouldInit); - this.embark.addConsoleProviderInit('names', code, shouldInit); - } - ensResolve(name, cb) { const self = this; if (!self.enabled) { diff --git a/src/lib/modules/tests/test.js b/src/lib/modules/tests/test.js index 0f71724f1..5751db1a3 100644 --- a/src/lib/modules/tests/test.js +++ b/src/lib/modules/tests/test.js @@ -261,6 +261,11 @@ class Test { next(null, accounts, web3); }); }, + function waitForProvidersReady(accounts, web3, next) { + self.events.request('console:provider:ready', () => { + next(null, accounts, web3); + }); + }, function createContractObject(accounts, web3, next) { self.events.request('contracts:all', (err, contracts) => { diff --git a/test_apps/coverage_app/app/components/blockchain.js b/test_apps/coverage_app/app/components/blockchain.js index b9e5e313c..242af464f 100644 --- a/test_apps/coverage_app/app/components/blockchain.js +++ b/test_apps/coverage_app/app/components/blockchain.js @@ -21,31 +21,19 @@ class Blockchain extends React.Component { setValue(e){ e.preventDefault(); - + var value = parseInt(this.state.valueSet, 10); - - // If web3.js 1.0 is being used - if (EmbarkJS.isNewWeb3()) { - SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount}); - this._addToLog("SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount})"); - } else { - SimpleStorage.set(value); - this._addToLog("#blockchain", "SimpleStorage.set(" + value + ")"); - } + + SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount}); + this._addToLog("SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount})"); } getValue(e){ e.preventDefault(); - - if (EmbarkJS.isNewWeb3()) { - SimpleStorage.methods.get().call() - .then(_value => this.setState({valueGet: _value})) - this._addToLog("SimpleStorage.methods.get(console.log)"); - } else { - SimpleStorage.get() - .then(_value => this.setState({valueGet: _value})); - this._addToLog("SimpleStorage.get()"); - } + + SimpleStorage.methods.get().call() + .then(_value => this.setState({valueGet: _value})); + this._addToLog("SimpleStorage.methods.get(console.log)"); } _addToLog(txt){ @@ -88,4 +76,4 @@ class Blockchain extends React.Component { } } - export default Blockchain; \ No newline at end of file + export default Blockchain; diff --git a/test_apps/coverage_app/app/dapp.js b/test_apps/coverage_app/app/dapp.js index c48ab0bdb..4184e0a9b 100644 --- a/test_apps/coverage_app/app/dapp.js +++ b/test_apps/coverage_app/app/dapp.js @@ -27,27 +27,11 @@ class App extends React.Component { componentDidMount() { EmbarkJS.onReady(() => { - if (EmbarkJS.isNewWeb3()) { - EmbarkJS.Messages.Providers.whisper.getWhisperVersion((err, _version) => { - if (err) { - return console.log(err); - } - this.setState({whisperEnabled: true}); - }); - } else { - if (EmbarkJS.Messages.providerName === 'whisper') { - EmbarkJS.Messages.getWhisperVersion((err, _version) => { - if (err) { - return console.log(err); - } - this.setState({whisperEnabled: true}); - }); + EmbarkJS.Messages.Providers.whisper.getWhisperVersion((err, _version) => { + if (err) { + return console.log(err); } - } - this.setState({ - storageEnabled: EmbarkJS.Storage.isAvailable(), - ensEnabled: EmbarkJS.Names.isAvailable(), - ensNameSystems: EmbarkJS.Names.currentNameSystems + this.setState({whisperEnabled: true}); }); }); } diff --git a/test_apps/test_app/app/js/index.js b/test_apps/test_app/app/js/index.js index 85f9d6a2d..0d7bbaafb 100644 --- a/test_apps/test_app/app/js/index.js +++ b/test_apps/test_app/app/js/index.js @@ -49,30 +49,15 @@ $(document).ready(function() { $("#blockchain button.set").click(function() { var value = parseInt($("#blockchain input.text").val(), 10); - // If web3.js 1.0 is being used - if (EmbarkJS.isNewWeb3()) { - SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount, gas: 5300000}); - addToLog("#blockchain", "SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount, gas: 5300000})"); - } else { - SimpleStorage.set(value); - addToLog("#blockchain", "SimpleStorage.set(" + value + ")"); - } - + SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount, gas: 5300000}); + addToLog("#blockchain", "SimpleStorage.methods.set(value).send({from: web3.eth.defaultAccount, gas: 5300000})"); }); $("#blockchain button.get").click(function() { - // If web3.js 1.0 is being used - if (EmbarkJS.isNewWeb3()) { - SimpleStorage.methods.get().call(function(err, value) { - $("#blockchain .value").html(value); - }); - addToLog("#blockchain", "SimpleStorage.methods.get(console.log)"); - } else { - SimpleStorage.get().then(function(value) { - $("#blockchain .value").html(value.toNumber()); - }); - addToLog("#blockchain", "SimpleStorage.get()"); - } + SimpleStorage.methods.get().call(function(err, value) { + $("#blockchain .value").html(value); + }); + addToLog("#blockchain", "SimpleStorage.methods.get(console.log)"); }); });