Align *Proposal to *Contributor methods
This commit is contained in:
+77
-49
@@ -8,11 +8,11 @@ import computed, { alias } from 'ember-computed';
|
|||||||
import { isEmpty, isPresent } from 'ember-utils';
|
import { isEmpty, isPresent } from 'ember-utils';
|
||||||
|
|
||||||
import config from 'kredits-web/config/environment';
|
import config from 'kredits-web/config/environment';
|
||||||
import Proposal from 'kredits-web/models/proposal';
|
|
||||||
|
|
||||||
import abis from 'contracts/abis';
|
import abis from 'contracts/abis';
|
||||||
import addresses from 'contracts/addresses';
|
import addresses from 'contracts/addresses';
|
||||||
import {
|
import {
|
||||||
|
ContributionSerializer,
|
||||||
ContributorSerializer,
|
ContributorSerializer,
|
||||||
fromBytes32,
|
fromBytes32,
|
||||||
toBytes32
|
toBytes32
|
||||||
@@ -22,7 +22,6 @@ const {
|
|||||||
getOwner,
|
getOwner,
|
||||||
Logger: {
|
Logger: {
|
||||||
debug,
|
debug,
|
||||||
warn,
|
|
||||||
error
|
error
|
||||||
}
|
}
|
||||||
} = Ember;
|
} = Ember;
|
||||||
@@ -82,7 +81,11 @@ export default Service.extend({
|
|||||||
|
|
||||||
registryContract: computed('ethProvider', function() {
|
registryContract: computed('ethProvider', function() {
|
||||||
let networkId = this.get('ethProvider').chainId;
|
let networkId = this.get('ethProvider').chainId;
|
||||||
let registry = new ethers.Contract(addresses['Registry'][networkId], abis['Registry'], this.get('ethProvider'));
|
let registry = new ethers.Contract(
|
||||||
|
addresses['Registry'][networkId],
|
||||||
|
abis['Registry'],
|
||||||
|
this.get('ethProvider')
|
||||||
|
);
|
||||||
return registry;
|
return registry;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -115,6 +118,14 @@ export default Service.extend({
|
|||||||
return contributor;
|
return contributor;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
buildProposal(attributes) {
|
||||||
|
debug('[kredits] buildProposal', attributes);
|
||||||
|
|
||||||
|
let proposal = getOwner(this).lookup('model:proposal');
|
||||||
|
proposal.setProperties(attributes);
|
||||||
|
return proposal;
|
||||||
|
},
|
||||||
|
|
||||||
getContributorById(id) {
|
getContributorById(id) {
|
||||||
return this.get('contributorsContract')
|
return this.get('contributorsContract')
|
||||||
.then((contract) => contract.getContributorById(id))
|
.then((contract) => contract.getContributorById(id))
|
||||||
@@ -124,7 +135,7 @@ export default Service.extend({
|
|||||||
let isCurrentUser = this.get('currentUserAccounts').includes(address);
|
let isCurrentUser = this.get('currentUserAccounts').includes(address);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id,
|
id: id.toString(),
|
||||||
address,
|
address,
|
||||||
balance: balance.toNumber(),
|
balance: balance.toNumber(),
|
||||||
ipfsHash,
|
ipfsHash,
|
||||||
@@ -191,44 +202,50 @@ export default Service.extend({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getProposalData(i) {
|
getProposalById(id) {
|
||||||
return this.get('kreditsContract')
|
return this.get('kreditsContract')
|
||||||
.then((contract) => contract.proposals(i))
|
.then((contract) => contract.proposals(id))
|
||||||
.then(p => {
|
.then(this.reassembleIpfsHash)
|
||||||
let { ipfsHash: digest, hashFunction, hashSize } = p;
|
// Set basic data
|
||||||
let ipfsHash = fromBytes32({ digest, hashFunction, hashSize });
|
.then(({
|
||||||
|
creator: creatorAddress,
|
||||||
let proposal = Proposal.create({
|
recipientId,
|
||||||
id : i,
|
votesCount,
|
||||||
creatorAddress : p.creator,
|
votesNeeded,
|
||||||
recipientId : p.recipientId.toNumber(),
|
amount,
|
||||||
votesCount : p.votesCount.toNumber(),
|
executed,
|
||||||
votesNeeded : p.votesNeeded.toNumber(),
|
ipfsHash,
|
||||||
amount : p.amount.toNumber(),
|
}) => {
|
||||||
executed : p.executed,
|
return {
|
||||||
|
id: id.toString(),
|
||||||
|
creatorAddress,
|
||||||
|
recipientId: recipientId.toNumber(),
|
||||||
|
votesCount: votesCount.toNumber(),
|
||||||
|
votesNeeded: votesNeeded.toNumber(),
|
||||||
|
amount: amount.toNumber(),
|
||||||
|
executed,
|
||||||
ipfsHash
|
ipfsHash
|
||||||
});
|
};
|
||||||
|
})
|
||||||
if (proposal.get('ipfsHash')) {
|
// Fetch IPFS data if available
|
||||||
// TODO: move ipfs into model
|
.then((data) => {
|
||||||
return proposal
|
return this.fetchAndMergeIpfsData(data, ContributionSerializer);
|
||||||
.loadContribution(this.get('ipfs'))
|
})
|
||||||
.then(() => { return proposal; });
|
.then((attributes) => {
|
||||||
} else {
|
return this.buildProposal(attributes);
|
||||||
warn('[kredits] proposal from blockchain is missing IPFS hash', proposal);
|
|
||||||
return proposal;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
getProposals() {
|
getProposals() {
|
||||||
return this.get('kreditsContract')
|
return this.get('kreditsContract')
|
||||||
.then((contract) => contract.proposalsCount())
|
.then((contract) => contract.proposalsCount())
|
||||||
.then(proposalsCount => {
|
.then((count) => {
|
||||||
|
count = count.toNumber();
|
||||||
|
debug('[kredits] proposals count:', count);
|
||||||
let proposals = [];
|
let proposals = [];
|
||||||
|
|
||||||
for(var i = 0; i < proposalsCount.toNumber(); i++) {
|
for(var i = 0; i < count; i++) {
|
||||||
proposals.push(this.getProposalData(i));
|
proposals.push(this.getProposalById(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
return RSVP.all(proposals);
|
return RSVP.all(proposals);
|
||||||
@@ -283,29 +300,40 @@ export default Service.extend({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
addProposal(proposal) {
|
addProposal(attributes) {
|
||||||
const {
|
debug('[kredits] add proposal', attributes);
|
||||||
recipientAddress,
|
|
||||||
amount,
|
let json = ContributionSerializer.serialize(attributes);
|
||||||
url
|
// TODO: validate against schema
|
||||||
} = proposal.getProperties('recipientAddress', 'amount', 'url');
|
|
||||||
|
|
||||||
return this.get('ipfs')
|
return this.get('ipfs')
|
||||||
.storeFile(proposal.serializeContribution())
|
.storeFile(json)
|
||||||
.then(ipfsHash => {
|
// Set ipfsHash
|
||||||
|
.then((ipfsHash) => {
|
||||||
|
delete attributes.contributorIpfsHash;
|
||||||
|
attributes.ipfsHash = ipfsHash;
|
||||||
|
return attributes;
|
||||||
|
})
|
||||||
|
.then((attributes) => {
|
||||||
return this.get('kreditsContract')
|
return this.get('kreditsContract')
|
||||||
.then((contract) => {
|
.then((contract) => {
|
||||||
return contract.addProposal(
|
let { recipientId, amount, ipfsHash } = attributes;
|
||||||
recipientAddress,
|
let { digest, hashFunction, hashSize } = toBytes32(ipfsHash);
|
||||||
|
|
||||||
|
let proposal = [
|
||||||
|
recipientId,
|
||||||
amount,
|
amount,
|
||||||
url,
|
digest,
|
||||||
ipfsHash
|
hashFunction,
|
||||||
);
|
hashSize,
|
||||||
})
|
];
|
||||||
.then((data) => {
|
debug('[kredits] addProposal', ...proposal);
|
||||||
debug('[kredits] add proposal response', data);
|
return contract.addProposal(...proposal);
|
||||||
return data;
|
|
||||||
});
|
});
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
debug('[kredits] add proposal response', data);
|
||||||
|
return this.buildProposal(attributes);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user