refactor(blockchain): add a web3 provider for embarkJS

This commit is contained in:
Jonathan Rainville 2018-11-29 17:12:15 -05:00 committed by Iuri Matias
parent 75111569a2
commit 20fccfb1c6
8 changed files with 114 additions and 74 deletions

View File

@ -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);
};

View File

@ -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...');

View File

@ -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 || {},

View File

@ -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) {

View File

@ -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) => {

View File

@ -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;
export default Blockchain;

View File

@ -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});
});
});
}

View File

@ -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)");
});
});