Merge pull request #58 from 67P/cleanup/kredits-service
Cleanup kredits service
This commit was merged in pull request #58.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import Component from '@ember/component';
|
||||
import { computed } from '@ember/object';
|
||||
import { and, notEmpty } from '@ember/object/computed';
|
||||
import { inject as injectService } from '@ember/service';
|
||||
|
||||
@@ -24,10 +23,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'),
|
||||
|
||||
@@ -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'),
|
||||
|
||||
|
||||
+36
-40
@@ -1,66 +1,72 @@
|
||||
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() {
|
||||
getEthProvider: function() {
|
||||
return new RSVP.Promise((resolve) => {
|
||||
let ethProvider;
|
||||
let networkId;
|
||||
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) => {
|
||||
return this.getEthProvider().then((ethProvider) => {
|
||||
let ethSigner;
|
||||
|
||||
if (ethProvider.getSigner) {
|
||||
ethSigner = ethProvider.getSigner();
|
||||
}
|
||||
|
||||
let kredits = new Kredits(ethProvider, ethSigner);
|
||||
return kredits
|
||||
.init()
|
||||
.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 +90,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 +140,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 +184,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));
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
+11
-19
@@ -35,7 +35,7 @@ module.exports = function(environment) {
|
||||
]
|
||||
},
|
||||
|
||||
contractMetadata: {},
|
||||
contractMetadata: { networkId: '42' },
|
||||
|
||||
web3ProviderUrl: 'https://parity.kosmos.org:8545',
|
||||
|
||||
@@ -46,30 +46,15 @@ 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.WEB3_PROVIDER_URL) {
|
||||
ENV.web3ProviderUrl = process.env.WEB3_PROVIDER_URL;
|
||||
}
|
||||
|
||||
if (environment === 'development') {
|
||||
// ENV.APP.LOG_RESOLVER = true;
|
||||
// ENV.APP.LOG_ACTIVE_GENERATION = true;
|
||||
// ENV.APP.LOG_TRANSITIONS = true;
|
||||
// ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
|
||||
// ENV.APP.LOG_VIEW_LOOKUPS = true;
|
||||
ENV.contractMetadata['networkId'] = '100';
|
||||
ENV.web3ProviderUrl = 'http://localhost:7545';
|
||||
|
||||
ENV.ipfs = {
|
||||
host: 'localhost',
|
||||
port: '5001',
|
||||
@@ -93,5 +78,12 @@ module.exports = function(environment) {
|
||||
// here you can enable a production-specific feature
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return ENV;
|
||||
};
|
||||
|
||||
@@ -42,6 +42,19 @@ module('Serializers contributor', function() {
|
||||
let expected = {
|
||||
name: 'Satoshi Nakamoto',
|
||||
kind: 'person',
|
||||
accounts: [
|
||||
{
|
||||
"site": "github.com",
|
||||
"uid": 123,
|
||||
"url": "https://github.com/therealsatoshi",
|
||||
"username": "therealsatoshi"
|
||||
},
|
||||
{
|
||||
"site": "wiki.kosmos.org",
|
||||
"url": "https://wiki.kosmos.org/User:Satoshi",
|
||||
"username": "Satoshi"
|
||||
}
|
||||
],
|
||||
github_uid: 123,
|
||||
github_username: 'therealsatoshi',
|
||||
wiki_username: 'Satoshi',
|
||||
|
||||
Reference in New Issue
Block a user