Move contract event handling into service
This commit is contained in:
@@ -6,15 +6,6 @@ import injectService from 'ember-service/inject';
|
||||
export default Controller.extend({
|
||||
kredits: injectService(),
|
||||
|
||||
init() {
|
||||
this._super(...arguments);
|
||||
let contract = this.get('kredits.kredits').Operator.contract;
|
||||
contract.onproposalvoted = this._handleProposalVoted.bind(this);
|
||||
contract.onproposalcreated = this._handleProposalCreated.bind(this);
|
||||
contract.onproposalexecuted = this._handleProposalExecuted.bind(this);
|
||||
// TODO: transfer on the token contract
|
||||
},
|
||||
|
||||
contributors: alias('kredits.contributors'),
|
||||
contributorsWithKredits: filter('contributors', function(contributor) {
|
||||
return contributor.get('balance') !== 0;
|
||||
@@ -29,54 +20,6 @@ export default Controller.extend({
|
||||
proposalsClosedSorted: sort('proposalsClosed', 'proposalsSorting'),
|
||||
proposalsOpenSorted: sort('proposalsOpen', 'proposalsSorting'),
|
||||
|
||||
_handleProposalCreated(proposalId) {
|
||||
// TODO: check if proposalId is already a string
|
||||
let proposal = this.get('proposals')
|
||||
.findBy('id', proposalId.toString());
|
||||
if (proposal) {
|
||||
Ember.Logger.debug('[index] proposal exists, not adding from event');
|
||||
return;
|
||||
}
|
||||
|
||||
proposal = this.get('kredits.kredits').Operator.getById(proposalId);
|
||||
this.get('proposals').pushObject(proposal);
|
||||
},
|
||||
|
||||
_handleProposalExecuted(proposalId, contributorId, amount) {
|
||||
let proposal = this.get('proposals')
|
||||
.findBy('id', proposalId);
|
||||
|
||||
if (proposal.get('isExecuted')) {
|
||||
Ember.Logger.debug('[index] proposal already executed, not adding from event');
|
||||
return;
|
||||
}
|
||||
|
||||
proposal.setProperties({
|
||||
'executed': true,
|
||||
});
|
||||
|
||||
this.get('contributors')
|
||||
.findBy('id', contributorId)
|
||||
.incrementProperty('balance', amount);
|
||||
},
|
||||
|
||||
_handleProposalVoted(proposalId, voter, totalVotes) {
|
||||
this.get('proposals')
|
||||
.findBy('id', proposalId)
|
||||
.setProperties({ 'votesCount': totalVotes });
|
||||
},
|
||||
|
||||
_handleTransfer(from, to, value) {
|
||||
value = value.toNumber();
|
||||
this.get('contributors')
|
||||
.findBy('address', from)
|
||||
.decrementProperty('balance', value);
|
||||
this.get('contributors')
|
||||
.findBy('address', to)
|
||||
.incrementProperty('balance', value);
|
||||
},
|
||||
|
||||
|
||||
actions: {
|
||||
confirmProposal(proposalId) {
|
||||
this.get('kredits').vote(proposalId).then(transaction => {
|
||||
|
||||
@@ -7,4 +7,11 @@ export default class Base {
|
||||
return this.contract.functions;
|
||||
}
|
||||
|
||||
on(type, callback) {
|
||||
let eventMethod = `on${type.toLowerCase()}`;
|
||||
// Don't use this.contract.events here. Seems to be a bug in ethers.js
|
||||
this.contract[eventMethod] = callback;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
+72
-1
@@ -88,6 +88,22 @@ export default Service.extend({
|
||||
}).then(({ contributors, proposals }) => {
|
||||
this.set('contributors', contributors);
|
||||
this.set('proposals', proposals);
|
||||
}).then(() => {
|
||||
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);
|
||||
});
|
||||
|
||||
this.get('kredits').Token
|
||||
.on('Transfer', (from, to, value) => {
|
||||
this.handleTransfer(from, to, value);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
@@ -169,5 +185,60 @@ export default Service.extend({
|
||||
return this.get('kredits').Contributor.getById(id);
|
||||
}
|
||||
});
|
||||
})
|
||||
}),
|
||||
|
||||
findProposalById(proposalId) {
|
||||
return this.get('proposals').findBy('id', proposalId.toString());
|
||||
},
|
||||
|
||||
// Contract events
|
||||
handleProposalCreated(proposalId) {
|
||||
let proposal = this.findProposalById(proposalId);
|
||||
|
||||
if (proposal) {
|
||||
debug('[events] proposal exists, not adding from event');
|
||||
return;
|
||||
}
|
||||
|
||||
this.get('kredits').Operator.getById(proposalId)
|
||||
.then((proposal) => {
|
||||
this.get('proposals').pushObject(proposal);
|
||||
});
|
||||
},
|
||||
|
||||
// TODO: We may want to reload that proposal to show the voter as voted
|
||||
handleProposalVoted(proposalId, totalVotes) {
|
||||
let proposal = this.findProposalById(proposalId);
|
||||
|
||||
if (proposal) {
|
||||
proposal.set('votesCount', totalVotes);
|
||||
}
|
||||
},
|
||||
|
||||
handleProposalExecuted(proposalId, contributorId, amount) {
|
||||
let proposal = this.findProposalById(proposalId);
|
||||
|
||||
if (proposal.get('isExecuted')) {
|
||||
debug('[events] proposal already executed, not adding from event');
|
||||
return;
|
||||
}
|
||||
|
||||
proposal.set('executed', true);
|
||||
|
||||
this.get('contributors')
|
||||
.findBy('id', contributorId.toString())
|
||||
.incrementProperty('balance', amount);
|
||||
},
|
||||
|
||||
handleTransfer(from, to, value) {
|
||||
value = value.toNumber();
|
||||
|
||||
this.get('contributors')
|
||||
.findBy('address', from)
|
||||
.decrementProperty('balance', value);
|
||||
|
||||
this.get('contributors')
|
||||
.findBy('address', to)
|
||||
.incrementProperty('balance', value);
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user