Refactor web3 initialization

This changes how we wait for web3 and accounts to be available and
renames web3Provider to ethProvider as we could be able to swap the
different providers (ethers.js vs. web3)
This commit is contained in:
2018-04-04 13:56:41 +02:00
parent a418ddbe52
commit 6dafc26deb
4 changed files with 49 additions and 52 deletions
+40 -40
View File
@@ -28,57 +28,57 @@ export default Service.extend({
ipfs: injectService(),
web3Instance: null,
web3Provided: false, // Web3 provided (using Mist Browser, Metamask et al.)
ethProvider: null,
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded.
web3: function() {
if (this.get('web3Provider')) {
return this.get('web3Provider');
}
initEthProvider: function() {
return new Ember.RSVP.Promise((resolve, reject) => {
let ethProvider;
let networkId;
if (typeof window.web3 !== 'undefined') {
debug('[kredits] Using user-provided instance, e.g. from Mist browser or Metamask');
networkId = parseInt(window.web3.version.network);
ethProvider = new ethers.providers.Web3Provider(window.web3.currentProvider, {chainId: networkId});
ethProvider.listAccounts().then((accounts) => {
this.set('currentUserAccounts', accounts);
this.set('ethProvider', ethProvider)
resolve(ethProvider);
});
} else {
debug('[kredits] Creating new instance from npm module class');
let providerUrl = localStorage.getItem('config:web3ProviderUrl') || config.web3ProviderUrl;
networkId = parseInt(config.contractMetadata.networkId);
ethProvider = new ethers.providers.JsonRpcProvider(providerUrl, {chainId: networkId});
this.set('ethProvider', ethProvider);
resolve(ethProvider);
}
window.ethProvider = ethProvider;
});
},
let web3Provider;
if (typeof window.web3 !== 'undefined') {
debug('[kredits] Using user-provided instance, e.g. from Mist browser or Metamask');
let networkId = parseInt(window.web3.version.network);
web3Provider = new ethers.providers.Web3Provider(window.web3.currentProvider, {chainId: networkId});
this.set('web3Provided', true);
} else {
debug('[kredits] Creating new instance from npm module class');
let providerUrl = localStorage.getItem('config:web3ProviderUrl') || config.web3ProviderUrl;
let networkId = parseInt(config.contractMetadata.networkId);
web3Provider = new ethers.providers.JsonRpcProvider(providerUrl, {chainId: networkId});
}
accountNeedsUnlock: computed('currentUserAccounts', function() {
return this.get('currentUserAccounts') && Ember.isEmpty(this.get('currentUserAccounts'));
}),
this.set('web3Provider', web3Provider);
return web3Provider;
}.property('web3Provider'),
hasAccounts: computed('currentUserAccounts', function() {
return !Ember.isEmpty(this.get('currentUserAccounts'));
}),
listAccounts: function() {
return this.get('web3').listAccounts();
}.property('web3'),
currentUserAccounts: function() {
// TODO: listAccounts returns now a promise
return [];
//return ethers.listAccounts();
// return (this.get('web3Provided') && this.get('web3').eth.accounts) || [];
}.property('web3Provided', 'web3'),
registryContract: computed('web3', function() {
let networkId = this.get('web3').chainId;
let registry = new ethers.Contract(addresses['Registry'][networkId], abis['Registry'], this.get('web3'));
registryContract: computed('ethProvider', function() {
let networkId = this.get('ethProvider').chainId;
let registry = new ethers.Contract(addresses['Registry'][networkId], abis['Registry'], this.get('ethProvider'));
return registry;
}),
contributorsContract: computed('web3', function() {
contributorsContract: computed('ethProvider', function() {
return this.contractFor('Contributors');
}),
kreditsContract: computed('web3', function() {
kreditsContract: computed('ethProvider', function() {
return this.contractFor('Operator');
}),
tokenContract: computed('web3', function() {
tokenContract: computed('ethProvider', function() {
return this.contractFor('Token');
}),
@@ -86,7 +86,7 @@ export default Service.extend({
return this.get('registryContract').functions.getProxyFor(name)
.then((address) => {
debug('[kredits] get contract', name, address);
return new ethers.Contract(address, abis[name], this.get('web3').getSigner());
return new ethers.Contract(address, abis[name], this.get('ethProvider').getSigner());
});
},
@@ -229,7 +229,7 @@ export default Service.extend({
.then(profileHash => {
contributor.setProperties({
profileHash: profileHash,
kredits: 0,
balance: 0,
isCurrentUser: this.get('currentUserAccounts').includes(contributor.address)
});