Merge branch 'master' into fix-with-missing-metamask
This commit is contained in:
+58
-63
@@ -1,33 +1,29 @@
|
||||
import ethers from 'npm:ethers';
|
||||
import Kredits from 'kredits-web/lib/kredits';
|
||||
import Service from '@ember/service';
|
||||
import { computed } from '@ember/object';
|
||||
import { alias } from '@ember/object/computed';
|
||||
import { isEmpty, isPresent } from '@ember/utils';
|
||||
import RSVP from 'rsvp';
|
||||
import Ember from 'ember';
|
||||
import Service from 'ember-service';
|
||||
import computed, { alias } from 'ember-computed';
|
||||
import { isEmpty, isPresent } from 'ember-utils';
|
||||
import Kredits from 'kredits-web/lib/kredits';
|
||||
import Contributor from 'kredits-web/models/contributor'
|
||||
import Proposal from 'kredits-web/models/proposal'
|
||||
import ethers from 'npm:ethers';
|
||||
|
||||
import config from 'kredits-web/config/environment';
|
||||
|
||||
const {
|
||||
getOwner,
|
||||
Logger: {
|
||||
debug
|
||||
}
|
||||
} = Ember;
|
||||
|
||||
export default Service.extend({
|
||||
|
||||
ethProvider: null,
|
||||
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded.
|
||||
currentUser: null,
|
||||
currentUserIsContributor: computed('currentUser', function() {
|
||||
return isPresent(this.get('currentUser'));
|
||||
return isPresent(this.currentUser);
|
||||
}),
|
||||
currentUserIsCore: alias('currentUser.isCore'),
|
||||
hasAccounts: computed('currentUserAccounts', function() {
|
||||
return !isEmpty(this.get('currentUserAccounts'));
|
||||
return !isEmpty(this.currentUserAccounts);
|
||||
}),
|
||||
accountNeedsUnlock: computed('currentUserAccounts', function() {
|
||||
return this.get('currentUserAccounts') && isEmpty(this.get('currentUserAccounts'));
|
||||
return this.currentUserAccounts && isEmpty(this.currentUserAccounts);
|
||||
}),
|
||||
|
||||
// this is called called in the routes beforeModel(). So it is initialized before everything else
|
||||
@@ -38,7 +34,7 @@ export default Service.extend({
|
||||
let ethSigner;
|
||||
let networkId;
|
||||
if (typeof window.web3 !== 'undefined') {
|
||||
debug('[kredits] Using user-provided instance, e.g. from Mist browser or Metamask');
|
||||
console.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});
|
||||
ethSigner = ethProvider.getSigner();
|
||||
@@ -49,7 +45,7 @@ export default Service.extend({
|
||||
resolve(ethProvider, ethSigner);
|
||||
});
|
||||
} else {
|
||||
debug('[kredits] Creating new instance from npm module class');
|
||||
console.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});
|
||||
@@ -67,8 +63,8 @@ export default Service.extend({
|
||||
return Kredits.setup(ethProvider, ethSigner, config.ipfs).then((kredits) => {
|
||||
this.set('kredits', kredits);
|
||||
// TODO: Cleanup (!!!)
|
||||
if (this.get('currentUserAccounts') && this.get('currentUserAccounts').length > 0) {
|
||||
this.get('getCurrentUser').then((contributorData) => {
|
||||
if (this.currentUserAccounts && this.currentUserAccounts.length > 0) {
|
||||
this.getCurrentUser.then(contributorData => {
|
||||
this.set('currentUser', contributorData);
|
||||
});
|
||||
}
|
||||
@@ -78,85 +74,84 @@ export default Service.extend({
|
||||
},
|
||||
|
||||
totalSupply: computed(function() {
|
||||
return this.get('kredits').Token.functions.totalSupply();
|
||||
return this.kredits.Token.functions.totalSupply();
|
||||
}),
|
||||
|
||||
contributors: [],
|
||||
proposals: [],
|
||||
|
||||
loadContributorsAndProposals() {
|
||||
return RSVP.hash({
|
||||
contributors: this.getContributors(),
|
||||
proposals: this.getProposals(),
|
||||
}).then(({ contributors, proposals }) => {
|
||||
this.get('contributors').pushObjects(contributors);
|
||||
this.get('proposals').pushObjects(proposals);
|
||||
});
|
||||
return this.getContributors()
|
||||
.then(contributors => this.contributors.pushObjects(contributors))
|
||||
.then(() => this.getProposals())
|
||||
.then(proposals => this.proposals.pushObjects(proposals))
|
||||
},
|
||||
|
||||
// TODO: Only assign valid attributes
|
||||
buildModel(name, attributes) {
|
||||
debug('[kredits] build', name, attributes);
|
||||
let model = getOwner(this).lookup(`model:${name}`);
|
||||
|
||||
model.setProperties(attributes);
|
||||
return model;
|
||||
},
|
||||
// buildModel(name, attributes) {
|
||||
// console.debug('[kredits] build', name, attributes);
|
||||
// let model = getOwner(this).lookup(`model:${name}`);
|
||||
//
|
||||
// model.setProperties(attributes);
|
||||
// return model;
|
||||
// },
|
||||
|
||||
addContributor(attributes) {
|
||||
debug('[kredits] add contributor', attributes);
|
||||
console.debug('[kredits] add contributor', attributes);
|
||||
|
||||
return this.get('kredits').Contributor.add(attributes)
|
||||
return this.kredits.Contributor.add(attributes)
|
||||
.then((data) => {
|
||||
debug('[kredits] add contributor response', data);
|
||||
return this.buildModel('contributor', attributes);
|
||||
console.debug('[kredits] add contributor response', data);
|
||||
return Contributor.create(attributes);
|
||||
});
|
||||
},
|
||||
|
||||
getContributors() {
|
||||
return this.get('kredits').Contributor.all()
|
||||
return this.kredits.Contributor.all()
|
||||
.then((contributors) => {
|
||||
return contributors.map((contributor) => {
|
||||
return this.buildModel('contributor', contributor);
|
||||
return Contributor.create(contributor);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
addProposal(attributes) {
|
||||
debug('[kredits] add proposal', attributes);
|
||||
console.debug('[kredits] add proposal', attributes);
|
||||
|
||||
return this.get('kredits').Operator.addProposal(attributes)
|
||||
return this.kredits.Operator.addProposal(attributes)
|
||||
.then((data) => {
|
||||
debug('[kredits] add proposal response', data);
|
||||
return this.buildModel('proposal', attributes);
|
||||
console.debug('[kredits] add proposal response', data);
|
||||
attributes.contributor = this.contributors.findBy('id', attributes.contributorId);
|
||||
return Proposal.create(attributes);
|
||||
});
|
||||
},
|
||||
|
||||
getProposals() {
|
||||
return this.get('kredits').Operator.all()
|
||||
return this.kredits.Operator.all()
|
||||
.then((proposals) => {
|
||||
return proposals.map((proposal) => {
|
||||
return this.buildModel('proposal', proposal);
|
||||
proposal.contributor = this.contributors.findBy('id', proposal.contributorId.toString());
|
||||
return Proposal.create(proposal);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
vote(proposalId) {
|
||||
debug('[kredits] vote for', proposalId);
|
||||
console.debug('[kredits] vote for', proposalId);
|
||||
|
||||
return this.get('kredits').Operator.functions.vote(proposalId)
|
||||
return this.kredits.Operator.functions.vote(proposalId)
|
||||
.then((data) => {
|
||||
debug('[kredits] vote response', data);
|
||||
console.debug('[kredits] vote response', data);
|
||||
return data;
|
||||
});
|
||||
},
|
||||
|
||||
// TODO: Cleanup
|
||||
getCurrentUser: computed('ethProvider', function() {
|
||||
if (isEmpty(this.get('currentUserAccounts'))) {
|
||||
if (isEmpty(this.currentUserAccounts)) {
|
||||
return RSVP.resolve();
|
||||
}
|
||||
return this.get('kredits').Contributor
|
||||
return this.kredits.Contributor
|
||||
.functions.getContributorIdByAddress(this.get('currentUserAccounts.firstObject'))
|
||||
.then((id) => {
|
||||
id = id.toNumber();
|
||||
@@ -164,25 +159,25 @@ export default Service.extend({
|
||||
if (id === 0) {
|
||||
return RSVP.resolve();
|
||||
} else {
|
||||
return this.get('kredits').Contributor.getById(id);
|
||||
return this.kredits.Contributor.getById(id);
|
||||
}
|
||||
});
|
||||
}),
|
||||
|
||||
findProposalById(proposalId) {
|
||||
return this.get('proposals').findBy('id', proposalId.toString());
|
||||
return this.proposals.findBy('id', proposalId.toString());
|
||||
},
|
||||
|
||||
// Contract events
|
||||
addContractEventHandlers() {
|
||||
// Operator events
|
||||
this.get('kredits').Operator
|
||||
this.kredits.Operator
|
||||
.on('ProposalCreated', this.handleProposalCreated.bind(this))
|
||||
.on('ProposalVoted', this.handleProposalVoted.bind(this))
|
||||
.on('ProposalExecuted', this.handleProposalExecuted.bind(this));
|
||||
|
||||
// Token events
|
||||
this.get('kredits').Token
|
||||
this.kredits.Token
|
||||
.on('Transfer', this.handleTransfer.bind(this));
|
||||
},
|
||||
|
||||
@@ -190,14 +185,14 @@ export default Service.extend({
|
||||
let proposal = this.findProposalById(proposalId);
|
||||
|
||||
if (proposal) {
|
||||
debug('[events] proposal exists, not adding from event');
|
||||
console.debug('[events] proposal exists, not adding from event');
|
||||
return;
|
||||
}
|
||||
|
||||
this.get('kredits').Operator.getById(proposalId)
|
||||
this.kredits.Operator.getById(proposalId)
|
||||
.then((proposal) => {
|
||||
proposal = this.buildModel('proposal', proposal);
|
||||
this.get('proposals').pushObject(proposal);
|
||||
this.proposals.pushObject(proposal);
|
||||
});
|
||||
},
|
||||
|
||||
@@ -214,13 +209,13 @@ export default Service.extend({
|
||||
let proposal = this.findProposalById(proposalId);
|
||||
|
||||
if (proposal.get('isExecuted')) {
|
||||
debug('[events] proposal already executed, not adding from event');
|
||||
console.debug('[events] proposal already executed, not adding from event');
|
||||
return;
|
||||
}
|
||||
|
||||
proposal.set('executed', true);
|
||||
|
||||
this.get('contributors')
|
||||
this.contributors
|
||||
.findBy('id', contributorId.toString())
|
||||
.incrementProperty('balance', amount.toNumber());
|
||||
},
|
||||
@@ -228,11 +223,11 @@ export default Service.extend({
|
||||
handleTransfer(from, to, value) {
|
||||
value = value.toNumber();
|
||||
|
||||
this.get('contributors')
|
||||
this.contributors
|
||||
.findBy('address', from)
|
||||
.decrementProperty('balance', value);
|
||||
|
||||
this.get('contributors')
|
||||
this.contributors
|
||||
.findBy('address', to)
|
||||
.incrementProperty('balance', value);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user