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
+2 -4
View File
@@ -24,10 +24,8 @@ export default Component.extend({
this.reset();
},
isValidAccount: computed('kredits.ethProvider', 'account', function() {
// TODO: add proper address validation
return this.account !== '';
}),
// TODO: add proper address validation
isValidAccount: notEmpty('account'),
isValidName: notEmpty('name'),
isValidURL: notEmpty('url'),
isValidGithubUID: notEmpty('github_uid'),
+2 -2
View File
@@ -19,13 +19,13 @@ export default Component.extend({
contributors: [],
isValidRecipient: notEmpty('contributorId'),
isValidContributor: notEmpty('contributorId'),
isValidAmount: computed('amount', function() {
return parseInt(this.amount, 10) > 0;
}),
isValidDescription: notEmpty('description'),
isValidUrl: notEmpty('url'),
isValid: and('isValidRecipient',
isValid: and('isValidContributor',
'isValidAmount',
'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 { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import { isEmpty, isPresent } 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 { alias, notEmpty } from '@ember/object/computed';
import { isEmpty } from '@ember/utils';
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({
ethProvider: null,
currentUserAccounts: null, // default to not having an account. this is the wen web3 is loaded.
currentUser: null,
currentUserIsContributor: computed('currentUser', function() {
return isPresent(this.currentUser);
}),
currentUserIsContributor: notEmpty('currentUser'),
currentUserIsCore: alias('currentUser.isCore'),
hasAccounts: computed('currentUserAccounts', function() {
return !isEmpty(this.currentUserAccounts);
}),
hasAccounts: notEmpty('currentUserAccounts'),
accountNeedsUnlock: computed('currentUserAccounts', function() {
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
initEthProvider: function() {
return new RSVP.Promise((resolve) => {
@@ -35,32 +32,38 @@ export default Service.extend({
if (typeof window.web3 !== 'undefined') {
console.debug('[kredits] Using user-provided instance, e.g. from Mist browser or Metamask');
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) => {
this.set('currentUserAccounts', accounts);
this.set('ethProvider', ethProvider);
resolve(ethProvider);
});
} else {
console.debug('[kredits] Creating new instance from npm module class');
let providerUrl = localStorage.getItem('config:web3ProviderUrl') || config.web3ProviderUrl;
networkId = parseInt(config.contractMetadata.networkId);
ethProvider = new ethers.providers.JsonRpcProvider(providerUrl, {chainId: networkId});
this.set('ethProvider', ethProvider);
ethProvider = new ethers.providers.JsonRpcProvider(
config.web3ProviderUrl,
{ chainId: networkId }
);
resolve(ethProvider);
}
window.ethProvider = ethProvider;
});
},
setup() {
return this.initEthProvider().then((ethProvider) => {
let signer = ethProvider.getSigner();
return Kredits.setup(ethProvider, signer, config.ipfs).then((kredits) => {
let ethSigner;
if (ethProvider.getSigner) {
ethSigner = ethProvider.getSigner();
}
return Kredits.setup(ethProvider, ethSigner, config.ipfs).then((kredits) => {
this.set('kredits', kredits);
// TODO: Cleanup
if (this.currentUserAccounts.length > 0) {
if (this.currentUserAccounts && this.currentUserAccounts.length > 0) {
this.getCurrentUser.then((contributorData) => {
this.set('currentUser', contributorData);
});
@@ -84,15 +87,6 @@ export default Service.extend({
.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) {
console.debug('[kredits] add contributor', attributes);
@@ -143,8 +137,7 @@ export default Service.extend({
});
},
// TODO: Cleanup
getCurrentUser: computed('ethProvider', function() {
getCurrentUser: computed('kredits.provider', function() {
if (isEmpty(this.currentUserAccounts)) {
return RSVP.resolve();
}
@@ -188,8 +181,8 @@ export default Service.extend({
this.kredits.Operator.getById(proposalId)
.then((proposal) => {
proposal = this.buildModel('proposal', proposal);
this.proposals.pushObject(proposal);
proposal.contributor = this.contributors.findBy('id', proposal.contributorId.toString());
this.proposals.pushObject(Proposal.create(proposal));
});
},
+6 -1
View File
@@ -4,7 +4,12 @@ import ethers from 'npm:ethers';
export default function(dependentKey, converterMethod) {
return computed(dependentKey, {
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) {
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',
@@ -46,19 +46,8 @@ module.exports = function(environment) {
}
};
ENV.contractMetadata['networkId'] = "42";
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.NETWORK_ID) {
ENV.contractMetadata['networkId'] = process.env.NETWORK_ID;
}
if (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_INTERNAL = true;
// ENV.APP.LOG_VIEW_LOOKUPS = true;
ENV.contractMetadata['networkId'] = '100';
ENV.ipfs = {
host: 'localhost',
port: '5001',