2019-02-12 13:58:09 +00:00
|
|
|
/*global Web3*/
|
2019-02-22 15:55:21 +00:00
|
|
|
const embarkJSWeb3Connector = {};
|
2019-02-11 15:35:28 +00:00
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.init = function(config) {
|
2019-01-23 04:57:24 +00:00
|
|
|
global.web3 = config.web3 || global.web3;
|
2019-02-11 15:35:28 +00:00
|
|
|
// Check if the global web3 object uses the old web3 (0.x)
|
|
|
|
if (global.web3 && typeof global.web3.version !== 'string') {
|
|
|
|
// If so, use a new instance using 1.0, but use its provider
|
|
|
|
this.web3 = new Web3(global.web3.currentProvider);
|
|
|
|
} else {
|
|
|
|
this.web3 = global.web3 || new Web3();
|
|
|
|
}
|
|
|
|
global.web3 = this.web3;
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.getInstance = function () {
|
2019-02-11 15:35:28 +00:00
|
|
|
return this.web3;
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.getAccounts = function () {
|
2019-02-11 15:35:28 +00:00
|
|
|
return this.web3.eth.getAccounts(...arguments);
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.getNewProvider = function (providerName, ...args) {
|
2019-02-11 15:35:28 +00:00
|
|
|
return new Web3.providers[providerName](...args);
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.setProvider = function (provider) {
|
2019-02-11 15:35:28 +00:00
|
|
|
return this.web3.setProvider(provider);
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.getCurrentProvider = function () {
|
2019-02-11 15:35:28 +00:00
|
|
|
return this.web3.currentProvider;
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.getDefaultAccount = function () {
|
2019-02-11 15:35:28 +00:00
|
|
|
return this.web3.eth.defaultAccount;
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.setDefaultAccount = function (account) {
|
2019-02-11 15:35:28 +00:00
|
|
|
this.web3.eth.defaultAccount = account;
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.newContract = function (options) {
|
2019-02-11 15:35:28 +00:00
|
|
|
return new this.web3.eth.Contract(options.abi, options.address);
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.send = function () {
|
2019-02-11 15:35:28 +00:00
|
|
|
return this.web3.eth.sendTransaction(...arguments);
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.toWei = function () {
|
2019-02-11 15:35:28 +00:00
|
|
|
return this.web3.toWei(...arguments);
|
|
|
|
};
|
|
|
|
|
2019-02-22 15:55:21 +00:00
|
|
|
embarkJSWeb3Connector.getNetworkId = function () {
|
2019-02-11 15:35:28 +00:00
|
|
|
return this.web3.eth.net.getId();
|
|
|
|
};
|