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:
@@ -20,7 +20,7 @@ export default Component.extend({
|
||||
isValidAddress: function() {
|
||||
// TODO: add proper address validation
|
||||
return this.get('newContributor.address') !== '';
|
||||
}.property('kredits.web3', 'newContributor.address'),
|
||||
}.property('kredits.ethProvider', 'newContributor.address'),
|
||||
|
||||
isValidName: function() {
|
||||
return isPresent(this.get('newContributor.name'));
|
||||
|
||||
@@ -12,7 +12,7 @@ export default Ember.Controller.extend({
|
||||
|
||||
kredits: service(),
|
||||
|
||||
contractInteractionEnabled: computed.alias('kredits.web3Provided'),
|
||||
contractInteractionEnabled: computed.alias('kredits.hasAccounts'),
|
||||
|
||||
proposalsOpen: function() {
|
||||
let proposals = this.get('model.proposals')
|
||||
@@ -96,7 +96,7 @@ export default Ember.Controller.extend({
|
||||
|
||||
this.get('model.contributors')
|
||||
.findBy('id', recipientId)
|
||||
.incrementProperty('kredits', amount.toNumber());
|
||||
.incrementProperty('balance', amount.toNumber());
|
||||
},
|
||||
|
||||
_handleProposalVoted(proposalId, voter, totalVotes) {
|
||||
|
||||
+6
-9
@@ -6,16 +6,13 @@ export default Ember.Route.extend({
|
||||
|
||||
beforeModel(transition) {
|
||||
const kredits = this.get('kredits');
|
||||
|
||||
if (kredits.get('web3') && kredits.get('web3Provided')) {
|
||||
kredits.get('listAccounts').then((accounts) => {
|
||||
if (accounts.length === 0) {
|
||||
if (confirm('It looks like you have an Ethereum wallet available. Please unlock your account.')) {
|
||||
transition.retry();
|
||||
}
|
||||
return kredits.initEthProvider().then((ethProvider) => {
|
||||
if (kredits.get('accountNeedsUnlock')) {
|
||||
if (confirm('It looks like you have an Ethereum wallet available. Please unlock your account.')) {
|
||||
transition.retry();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
model() {
|
||||
|
||||
+40
-40
@@ -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)
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user