Watch contract events (and add new proposals)

This commit is contained in:
2017-02-05 19:27:17 +08:00
parent a7bfa3e45c
commit d0e10babfe
2 changed files with 45 additions and 4 deletions
+42 -2
View File
@@ -1,4 +1,5 @@
import Ember from 'ember'; import Ember from 'ember';
import Proposal from 'kredits-web/models/proposal';
const { const {
computed, computed,
@@ -22,7 +23,7 @@ export default Ember.Controller.extend({
let proposals = this.get('model.proposals') let proposals = this.get('model.proposals')
.filterBy('executed', false) .filterBy('executed', false)
.map(p => { .map(p => {
p.recipientName = this.findContributorByAddress(p.recipientAddress).github_username; p.set('recipientName', this.findContributorByAddress(p.get('recipientAddress')).github_username);
return p; return p;
}); });
return proposals; return proposals;
@@ -32,7 +33,7 @@ export default Ember.Controller.extend({
let proposals = this.get('model.proposals') let proposals = this.get('model.proposals')
.filterBy('executed', true) .filterBy('executed', true)
.map(p => { .map(p => {
p.recipientName = this.findContributorByAddress(p.recipientAddress).github_username; p.set('recipientName', this.findContributorByAddress(p.get('recipientAddress')).github_username);
return p; return p;
}); });
return proposals; return proposals;
@@ -45,6 +46,45 @@ export default Ember.Controller.extend({
contributorsSorting: ['kredits:desc'], contributorsSorting: ['kredits:desc'],
contributorsSorted: Ember.computed.sort('model.contributors', 'contributorsSorting'), contributorsSorted: Ember.computed.sort('model.contributors', 'contributorsSorting'),
watchContractEvents: function() {
let events = this.get('kredits.kreditsContract')
.allEvents(/* [additionalFilterObject], */);
events.watch((error, data) => {
Ember.Logger.debug('[index] Received contract event', data);
switch (data.event) {
case 'ProposalCreated':
this._handleProposalCreated(data);
break;
}
});
}.on('init'),
_handleProposalCreated(data) {
if (Ember.isPresent(this.get('model.proposals')
.findBy('id', data.args.id.toNumber()))) {
console.log('[index] proposal exists, not adding from event');
return false;
}
let proposal = Proposal.create({
id: data.args.id.toNumber(),
creatorAddress: data.args.creator,
recipientAddress: data.args.recipient,
recipientName: null,
votesCount: 0,
votesNeeded: 2,
amount: data.args.amount.toNumber(),
executed: false,
url: data.args.url,
ipfsHash: data.args.ipfsHash
});
console.log('new proposal created', proposal);
this.get('model.proposals').pushObject(proposal);
},
actions: { actions: {
confirmProposal(proposalId) { confirmProposal(proposalId) {
+3 -2
View File
@@ -3,8 +3,9 @@ import Ember from 'ember';
export default Ember.Object.extend({ export default Ember.Object.extend({
id: null, id: null,
creator: null, creatorAddress: null,
recipient: null, recipientAddress: null,
recipientName: null,
votesCount: null, votesCount: null,
votesNeeded: null, votesNeeded: null,
amount: null, amount: null,