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() {
|
isValidAddress: function() {
|
||||||
// TODO: add proper address validation
|
// TODO: add proper address validation
|
||||||
return this.get('newContributor.address') !== '';
|
return this.get('newContributor.address') !== '';
|
||||||
}.property('kredits.web3', 'newContributor.address'),
|
}.property('kredits.ethProvider', 'newContributor.address'),
|
||||||
|
|
||||||
isValidName: function() {
|
isValidName: function() {
|
||||||
return isPresent(this.get('newContributor.name'));
|
return isPresent(this.get('newContributor.name'));
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ export default Ember.Controller.extend({
|
|||||||
|
|
||||||
kredits: service(),
|
kredits: service(),
|
||||||
|
|
||||||
contractInteractionEnabled: computed.alias('kredits.web3Provided'),
|
contractInteractionEnabled: computed.alias('kredits.hasAccounts'),
|
||||||
|
|
||||||
proposalsOpen: function() {
|
proposalsOpen: function() {
|
||||||
let proposals = this.get('model.proposals')
|
let proposals = this.get('model.proposals')
|
||||||
@@ -96,7 +96,7 @@ export default Ember.Controller.extend({
|
|||||||
|
|
||||||
this.get('model.contributors')
|
this.get('model.contributors')
|
||||||
.findBy('id', recipientId)
|
.findBy('id', recipientId)
|
||||||
.incrementProperty('kredits', amount.toNumber());
|
.incrementProperty('balance', amount.toNumber());
|
||||||
},
|
},
|
||||||
|
|
||||||
_handleProposalVoted(proposalId, voter, totalVotes) {
|
_handleProposalVoted(proposalId, voter, totalVotes) {
|
||||||
|
|||||||
+6
-9
@@ -6,16 +6,13 @@ export default Ember.Route.extend({
|
|||||||
|
|
||||||
beforeModel(transition) {
|
beforeModel(transition) {
|
||||||
const kredits = this.get('kredits');
|
const kredits = this.get('kredits');
|
||||||
|
return kredits.initEthProvider().then((ethProvider) => {
|
||||||
if (kredits.get('web3') && kredits.get('web3Provided')) {
|
if (kredits.get('accountNeedsUnlock')) {
|
||||||
kredits.get('listAccounts').then((accounts) => {
|
if (confirm('It looks like you have an Ethereum wallet available. Please unlock your account.')) {
|
||||||
if (accounts.length === 0) {
|
transition.retry();
|
||||||
if (confirm('It looks like you have an Ethereum wallet available. Please unlock your account.')) {
|
|
||||||
transition.retry();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
model() {
|
model() {
|
||||||
|
|||||||
+40
-40
@@ -28,57 +28,57 @@ export default Service.extend({
|
|||||||
|
|
||||||
ipfs: injectService(),
|
ipfs: injectService(),
|
||||||
|
|
||||||
web3Instance: null,
|
ethProvider: null,
|
||||||
web3Provided: false, // Web3 provided (using Mist Browser, Metamask et al.)
|
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded.
|
||||||
|
|
||||||
web3: function() {
|
initEthProvider: function() {
|
||||||
if (this.get('web3Provider')) {
|
return new Ember.RSVP.Promise((resolve, reject) => {
|
||||||
return this.get('web3Provider');
|
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;
|
accountNeedsUnlock: computed('currentUserAccounts', function() {
|
||||||
if (typeof window.web3 !== 'undefined') {
|
return this.get('currentUserAccounts') && Ember.isEmpty(this.get('currentUserAccounts'));
|
||||||
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});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.set('web3Provider', web3Provider);
|
hasAccounts: computed('currentUserAccounts', function() {
|
||||||
return web3Provider;
|
return !Ember.isEmpty(this.get('currentUserAccounts'));
|
||||||
}.property('web3Provider'),
|
}),
|
||||||
|
|
||||||
listAccounts: function() {
|
registryContract: computed('ethProvider', function() {
|
||||||
return this.get('web3').listAccounts();
|
let networkId = this.get('ethProvider').chainId;
|
||||||
}.property('web3'),
|
let registry = new ethers.Contract(addresses['Registry'][networkId], abis['Registry'], this.get('ethProvider'));
|
||||||
|
|
||||||
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'));
|
|
||||||
return registry;
|
return registry;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
contributorsContract: computed('web3', function() {
|
contributorsContract: computed('ethProvider', function() {
|
||||||
return this.contractFor('Contributors');
|
return this.contractFor('Contributors');
|
||||||
}),
|
}),
|
||||||
|
|
||||||
kreditsContract: computed('web3', function() {
|
kreditsContract: computed('ethProvider', function() {
|
||||||
return this.contractFor('Operator');
|
return this.contractFor('Operator');
|
||||||
}),
|
}),
|
||||||
|
|
||||||
tokenContract: computed('web3', function() {
|
tokenContract: computed('ethProvider', function() {
|
||||||
return this.contractFor('Token');
|
return this.contractFor('Token');
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ export default Service.extend({
|
|||||||
return this.get('registryContract').functions.getProxyFor(name)
|
return this.get('registryContract').functions.getProxyFor(name)
|
||||||
.then((address) => {
|
.then((address) => {
|
||||||
debug('[kredits] get contract', name, 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 => {
|
.then(profileHash => {
|
||||||
contributor.setProperties({
|
contributor.setProperties({
|
||||||
profileHash: profileHash,
|
profileHash: profileHash,
|
||||||
kredits: 0,
|
balance: 0,
|
||||||
isCurrentUser: this.get('currentUserAccounts').includes(contributor.address)
|
isCurrentUser: this.get('currentUserAccounts').includes(contributor.address)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user