Move contract event handling into service #51

Merged
fsmanuel merged 4 commits from feature/contract-events into master 2018-04-15 19:58:24 +00:00
Showing only changes of commit 3839eb2be4 - Show all commits
+17 -24
View File
@@ -86,31 +86,11 @@ export default Service.extend({
contributors: this.getContributors(),
proposals: this.getProposals(),
}).then(({ contributors, proposals }) => {
this.set('contributors', contributors);
this.set('proposals', proposals);
this.get('contributors').pushObjects(contributors);
this.get('proposals').pushObjects(proposals);
});
},
addContractEventHandlers() {
// Operator events
this.get('kredits').Operator
.on('ProposalCreated', (proposalId) => {
this.handleProposalCreated(proposalId);
})
.on('ProposalVoted', (proposalId, voter, totalVotes) => {
this.handleProposalVoted(proposalId, totalVotes);
})
.on('ProposalExecuted', (proposalId, contributorId, amount) => {
this.handleProposalExecuted(proposalId, contributorId, amount);
});
// Token events
this.get('kredits').Token
.on('Transfer', (from, to, value) => {
this.handleTransfer(from, to, value);
});
},
// TODO: Only assign valid attributes
buildModel(name, attributes) {
debug('[kredits] build', name, attributes);
@@ -191,6 +171,18 @@ export default Service.extend({
},
// Contract events
addContractEventHandlers() {
// Operator events
this.get('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
.on('Transfer', this.handleTransfer.bind(this));
},
handleProposalCreated(proposalId) {
let proposal = this.findProposalById(proposalId);
@@ -201,12 +193,13 @@ export default Service.extend({
this.get('kredits').Operator.getById(proposalId)
.then((proposal) => {
proposal = this.buildModel('proposal', proposal);
this.get('proposals').pushObject(proposal);
});
},
// TODO: We may want to reload that proposal to show the voter as voted
handleProposalVoted(proposalId, totalVotes) {
handleProposalVoted(proposalId, voterId, totalVotes) {
let proposal = this.findProposalById(proposalId);
if (proposal) {
@@ -226,7 +219,7 @@ export default Service.extend({
this.get('contributors')
.findBy('id', contributorId.toString())
.incrementProperty('balance', amount);
.incrementProperty('balance', amount.toNumber());
},
handleTransfer(from, to, value) {