Cleanup kredits service

This commit is contained in:
2018-04-21 11:05:02 +02:00
parent ade47dadde
commit 75650c19ab
5 changed files with 46 additions and 59 deletions
+1 -3
View File
@@ -24,10 +24,8 @@ export default Component.extend({
this.reset(); this.reset();
}, },
isValidAccount: computed('kredits.ethProvider', 'account', function() {
// TODO: add proper address validation // TODO: add proper address validation
return this.account !== ''; isValidAccount: notEmpty('account'),
}),
isValidName: notEmpty('name'), isValidName: notEmpty('name'),
isValidURL: notEmpty('url'), isValidURL: notEmpty('url'),
isValidGithubUID: notEmpty('github_uid'), isValidGithubUID: notEmpty('github_uid'),
+2 -2
View File
@@ -19,13 +19,13 @@ export default Component.extend({
contributors: [], contributors: [],
isValidRecipient: notEmpty('contributorId'), isValidContributor: notEmpty('contributorId'),
isValidAmount: computed('amount', function() { isValidAmount: computed('amount', function() {
return parseInt(this.amount, 10) > 0; return parseInt(this.amount, 10) > 0;
}), }),
isValidDescription: notEmpty('description'), isValidDescription: notEmpty('description'),
isValidUrl: notEmpty('url'), isValidUrl: notEmpty('url'),
isValid: and('isValidRecipient', isValid: and('isValidContributor',
'isValidAmount', 'isValidAmount',
'isValidDescription'), 'isValidDescription'),
+31 -38
View File
@@ -1,32 +1,29 @@
import ethers from 'npm:ethers';
import Kredits from 'npm:kredits-contracts';
import RSVP from 'rsvp';
import Service from '@ember/service'; import Service from '@ember/service';
import { computed } from '@ember/object'; import { computed } from '@ember/object';
import { alias } from '@ember/object/computed'; import { alias, notEmpty } from '@ember/object/computed';
import { isEmpty, isPresent } from '@ember/utils'; import { isEmpty } from '@ember/utils';
import RSVP from 'rsvp';
import Kredits from 'npm:kredits-contracts';
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'; import config from 'kredits-web/config/environment';
import Contributor from 'kredits-web/models/contributor'
import Proposal from 'kredits-web/models/proposal'
export default Service.extend({ export default Service.extend({
ethProvider: null,
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded. currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded.
currentUser: null, currentUser: null,
currentUserIsContributor: computed('currentUser', function() { currentUserIsContributor: notEmpty('currentUser'),
return isPresent(this.currentUser);
}),
currentUserIsCore: alias('currentUser.isCore'), currentUserIsCore: alias('currentUser.isCore'),
hasAccounts: computed('currentUserAccounts', function() { hasAccounts: notEmpty('currentUserAccounts'),
return !isEmpty(this.currentUserAccounts);
}),
accountNeedsUnlock: computed('currentUserAccounts', function() { accountNeedsUnlock: computed('currentUserAccounts', function() {
return this.currentUserAccounts && isEmpty(this.currentUserAccounts); return this.currentUserAccounts && isEmpty(this.currentUserAccounts);
}), }),
// this is called called in the routes beforeModel(). So it is initialized before everything else // this is called in the routes beforeModel(). So it is initialized before everything else
// and we can rely on the ethProvider and the potential currentUserAccounts to be available // and we can rely on the ethProvider and the potential currentUserAccounts to be available
initEthProvider: function() { initEthProvider: function() {
return new RSVP.Promise((resolve) => { return new RSVP.Promise((resolve) => {
@@ -35,32 +32,38 @@ export default Service.extend({
if (typeof window.web3 !== 'undefined') { if (typeof window.web3 !== 'undefined') {
console.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); networkId = parseInt(window.web3.version.network);
ethProvider = new ethers.providers.Web3Provider(window.web3.currentProvider, {chainId: networkId}); ethProvider = new ethers.providers.Web3Provider(
window.web3.currentProvider,
{ chainId: networkId }
);
ethProvider.listAccounts().then((accounts) => { ethProvider.listAccounts().then((accounts) => {
this.set('currentUserAccounts', accounts); this.set('currentUserAccounts', accounts);
this.set('ethProvider', ethProvider);
resolve(ethProvider); resolve(ethProvider);
}); });
} else { } else {
console.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); networkId = parseInt(config.contractMetadata.networkId);
ethProvider = new ethers.providers.JsonRpcProvider(providerUrl, {chainId: networkId}); ethProvider = new ethers.providers.JsonRpcProvider(
this.set('ethProvider', ethProvider); config.web3ProviderUrl,
{ chainId: networkId }
);
resolve(ethProvider); resolve(ethProvider);
} }
window.ethProvider = ethProvider;
}); });
}, },
setup() { setup() {
return this.initEthProvider().then((ethProvider) => { return this.initEthProvider().then((ethProvider) => {
let signer = ethProvider.getSigner(); let ethSigner;
return Kredits.setup(ethProvider, signer, config.ipfs).then((kredits) => {
if (ethProvider.getSigner) {
ethSigner = ethProvider.getSigner();
}
return Kredits.setup(ethProvider, ethSigner, config.ipfs).then((kredits) => {
this.set('kredits', kredits); this.set('kredits', kredits);
// TODO: Cleanup if (this.currentUserAccounts && this.currentUserAccounts.length > 0) {
if (this.currentUserAccounts.length > 0) {
this.getCurrentUser.then((contributorData) => { this.getCurrentUser.then((contributorData) => {
this.set('currentUser', contributorData); this.set('currentUser', contributorData);
}); });
@@ -84,15 +87,6 @@ export default Service.extend({
.then(proposals => this.proposals.pushObjects(proposals)) .then(proposals => this.proposals.pushObjects(proposals))
}, },
// TODO: Only assign valid attributes
// buildModel(name, attributes) {
// console.debug('[kredits] build', name, attributes);
// let model = getOwner(this).lookup(`model:${name}`);
//
// model.setProperties(attributes);
// return model;
// },
addContributor(attributes) { addContributor(attributes) {
console.debug('[kredits] add contributor', attributes); console.debug('[kredits] add contributor', attributes);
@@ -143,8 +137,7 @@ export default Service.extend({
}); });
}, },
// TODO: Cleanup getCurrentUser: computed('kredits.provider', function() {
getCurrentUser: computed('ethProvider', function() {
if (isEmpty(this.currentUserAccounts)) { if (isEmpty(this.currentUserAccounts)) {
return RSVP.resolve(); return RSVP.resolve();
} }
@@ -188,8 +181,8 @@ export default Service.extend({
this.kredits.Operator.getById(proposalId) this.kredits.Operator.getById(proposalId)
.then((proposal) => { .then((proposal) => {
proposal = this.buildModel('proposal', proposal); proposal.contributor = this.contributors.findBy('id', proposal.contributorId.toString());
this.proposals.pushObject(proposal); this.proposals.pushObject(Proposal.create(proposal));
}); });
}, },
+6 -1
View File
@@ -4,7 +4,12 @@ import ethers from 'npm:ethers';
export default function(dependentKey, converterMethod) { export default function(dependentKey, converterMethod) {
return computed(dependentKey, { return computed(dependentKey, {
get () { get () {
return this.get(dependentKey)[converterMethod](); let value = this.get(dependentKey);
if (value && ethers.utils.isBigNumber(value)) {
return value[converterMethod]();
} else {
return value;
}
}, },
set (key, value) { set (key, value) {
value = ethers.utils.bigNumberify(value); value = ethers.utils.bigNumberify(value);
+5 -14
View File
@@ -35,7 +35,7 @@ module.exports = function(environment) {
] ]
}, },
contractMetadata: {}, contractMetadata: { networkId: '42' },
web3ProviderUrl: 'https://parity.kosmos.org:8545', web3ProviderUrl: 'https://parity.kosmos.org:8545',
@@ -46,19 +46,8 @@ module.exports = function(environment) {
} }
}; };
ENV.contractMetadata['networkId'] = "42"; if (process.env.NETWORK_ID) {
ENV.contractMetadata['networkId'] = process.env.NETWORK_ID;
if (process.env.OPERATOR_CONTRACT_ADDR) {
ENV.contractMetadata['Operator'] = {
address: process.env.OPERATOR_CONTRACT_ADDR,
networkId: ENV.contractMetadata['networkId']
};
}
if (process.env.TOKEN_CONTRACT_ADDR) {
ENV.contractMetadata['Token'] = {
address: process.env.TOKEN_CONTRACT_ADDR,
networkId: ENV.contractMetadata['networkId']
};
} }
if (process.env.WEB3_PROVIDER_URL) { if (process.env.WEB3_PROVIDER_URL) {
ENV.web3ProviderUrl = process.env.WEB3_PROVIDER_URL; ENV.web3ProviderUrl = process.env.WEB3_PROVIDER_URL;
@@ -70,6 +59,8 @@ module.exports = function(environment) {
// ENV.APP.LOG_TRANSITIONS = true; // ENV.APP.LOG_TRANSITIONS = true;
// ENV.APP.LOG_TRANSITIONS_INTERNAL = true; // ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
// ENV.APP.LOG_VIEW_LOOKUPS = true; // ENV.APP.LOG_VIEW_LOOKUPS = true;
ENV.contractMetadata['networkId'] = '100';
ENV.ipfs = { ENV.ipfs = {
host: 'localhost', host: 'localhost',
port: '5001', port: '5001',