Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Dapps use defaultAccount instead of own selectors #4386

Merged
merged 15 commits into from
Feb 3, 2017
Merged
20 changes: 13 additions & 7 deletions js/src/api/subscriptions/personal.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ export default class Personal {
}

_accountsInfo = () => {
return Promise
.all([
this._api.parity.accountsInfo(),
this._api.parity.allAccountsInfo()
])
.then(([info, allInfo]) => {
return this._api.parity
.accountsInfo()
.then((info) => {
this._updateSubscriptions('parity_accountsInfo', null, info);
this._updateSubscriptions('parity_allAccountsInfo', null, allInfo);

return this._api.parity
.allAccountsInfo()
.catch(() => {
// NOTE: This fails on non-secure APIs, swallow error
return {};
})
.then((allInfo) => {
this._updateSubscriptions('parity_allAccountsInfo', null, allInfo);
});
});
}

Expand Down
10 changes: 5 additions & 5 deletions js/src/dapps/basiccoin/Application/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class Application extends Component {
}

componentDidMount () {
this.attachInstance();
return this.attachInstance();
}

render () {
Expand Down Expand Up @@ -80,12 +80,12 @@ export default class Application extends Component {
}

attachInstance () {
Promise
return Promise
.all([
attachInstances(),
api.parity.accountsInfo()
api.parity.accountsInfo(),
attachInstances()
])
.then(([{ managerInstance, registryInstance, tokenregInstance }, accountsInfo]) => {
.then(([accountsInfo, { managerInstance, registryInstance, tokenregInstance }]) => {
accountsInfo = accountsInfo || {};
this.setState({
loading: false,
Expand Down
42 changes: 8 additions & 34 deletions js/src/dapps/basiccoin/Deploy/Deployment/deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import React, { Component, PropTypes } from 'react';

import { api } from '../../parity';
import AddressSelect from '../../AddressSelect';
import Container from '../../Container';
import styles from './deployment.css';

Expand Down Expand Up @@ -122,36 +121,13 @@ export default class Deployment extends Component {
}

renderForm () {
const { accounts } = this.context;
const { baseText, name, nameError, tla, tlaError, totalSupply, totalSupplyError } = this.state;
const hasError = !!(nameError || tlaError || totalSupplyError);
const error = `${styles.input} ${styles.error}`;
const addresses = Object.keys(accounts);

// <div className={ styles.input }>
// <label>global registration</label>
// <select onChange={ this.onChangeRegistrar }>
// <option value='no'>No, only for me</option>
// <option value='yes'>Yes, for everybody</option>
// </select>
// <div className={ styles.hint }>
// register on network (fee: { globalFeeText }ETH)
// </div>
// </div>

return (
<Container>
<div className={ styles.form }>
<div className={ styles.input }>
<label>deployment account</label>
<AddressSelect
addresses={ addresses }
onChange={ this.onChangeFrom }
/>
<div className={ styles.hint }>
the owner account to deploy from
</div>
</div>
<div className={ nameError ? error : styles.input }>
<label>token name</label>
<input
Expand Down Expand Up @@ -206,12 +182,6 @@ export default class Deployment extends Component {
);
}

onChangeFrom = (event) => {
const fromAddress = event.target.value;

this.setState({ fromAddress });
}

onChangeName = (event) => {
const name = event.target.value;
const nameError = name && (name.length > 2) && (name.length < 32)
Expand Down Expand Up @@ -271,7 +241,7 @@ export default class Deployment extends Component {

onDeploy = () => {
const { managerInstance, registryInstance, tokenregInstance } = this.context;
const { base, deployBusy, fromAddress, globalReg, globalFee, name, nameError, tla, tlaError, totalSupply, totalSupplyError } = this.state;
const { base, deployBusy, globalReg, globalFee, name, nameError, tla, tlaError, totalSupply, totalSupplyError } = this.state;
const hasError = !!(nameError || tlaError || totalSupplyError);

if (hasError || deployBusy) {
Expand All @@ -281,14 +251,18 @@ export default class Deployment extends Component {
const tokenreg = (globalReg ? tokenregInstance : registryInstance).address;
const values = [base.mul(totalSupply), tla, name, tokenreg];
const options = {
from: fromAddress,
value: globalReg ? globalFee : 0
};

this.setState({ deployBusy: true, deployState: 'Estimating gas for the transaction' });

managerInstance
.deploy.estimateGas(options, values)
return api.parity
.defaultAccount()
.then((defaultAddress) => {
options.from = defaultAddress;

return managerInstance.deploy.estimateGas(options, values);
})
.then((gas) => {
this.setState({ deployState: 'Gas estimated, Posting transaction to the network' });

Expand Down
16 changes: 16 additions & 0 deletions js/src/dapps/basiccoin/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ let registryInstance;

const registries = {};
const subscriptions = {};

let defaultSubscriptionId;
let nextSubscriptionId = 1000;
let isTest = false;

Expand Down Expand Up @@ -65,6 +67,20 @@ export function unsubscribeEvents (subscriptionId) {
delete subscriptions[subscriptionId];
}

export function subscribeDefaultAddress (callback) {
return api
.subscribe('parity_defaultAccount', callback)
.then((subscriptionId) => {
defaultSubscriptionId = subscriptionId;

return defaultSubscriptionId;
});
}

export function unsubscribeDefaultAddress () {
return api.unsubscribe(defaultSubscriptionId);
}

function pollEvents () {
const loop = Object.values(subscriptions);
const timeout = () => setTimeout(pollEvents, 1000);
Expand Down
84 changes: 37 additions & 47 deletions js/src/dapps/githubhint/Application/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
import React, { Component } from 'react';

import { api } from '../parity';
import { attachInterface } from '../services';
import { attachInterface, subscribeDefaultAddress, unsubscribeDefaultAddress } from '../services';
import Button from '../Button';
import Events from '../Events';
import IdentityIcon from '../IdentityIcon';
import Loading from '../Loading';

import styles from './application.css';
Expand All @@ -32,7 +31,7 @@ let nextEventId = 0;

export default class Application extends Component {
state = {
fromAddress: null,
defaultAddress: null,
loading: true,
url: '',
urlError: null,
Expand All @@ -47,19 +46,32 @@ export default class Application extends Component {
registerType: 'file',
repo: '',
repoError: null,
subscriptionId: null,
events: {},
eventIds: []
}

componentDidMount () {
attachInterface()
.then((state) => {
this.setState(state, () => {
this.setState({ loading: false });
});
return Promise
.all([
attachInterface(),
subscribeDefaultAddress((error, defaultAddress) => {
if (!error) {
this.setState({ defaultAddress });
}
})
])
.then(([state]) => {
this.setState(Object.assign({}, state, {
loading: false
}));
});
}

componentWillUnmount () {
return unsubscribeDefaultAddress();
}

render () {
const { loading } = this.state;

Expand All @@ -75,12 +87,14 @@ export default class Application extends Component {
}

renderPage () {
const { fromAddress, registerBusy, url, urlError, contentHash, contentHashError, contentHashOwner, commit, commitError, registerType, repo, repoError } = this.state;
const { defaultAddress, registerBusy, url, urlError, contentHash, contentHashError, contentHashOwner, commit, commitError, registerType, repo, repoError } = this.state;

let hashClass = null;

if (contentHashError) {
hashClass = contentHashOwner !== fromAddress ? styles.hashError : styles.hashWarning;
hashClass = contentHashOwner !== defaultAddress
? styles.hashError
: styles.hashWarning;
} else if (contentHash) {
hashClass = styles.hashOk;
}
Expand Down Expand Up @@ -166,20 +180,13 @@ export default class Application extends Component {
}

renderButtons () {
const { accounts, fromAddress, urlError, repoError, commitError, contentHashError, contentHashOwner } = this.state;
const account = accounts[fromAddress];
const { defaultAddress, urlError, repoError, commitError, contentHashError, contentHashOwner } = this.state;

return (
<div className={ styles.buttons }>
<div className={ styles.addressSelect }>
<Button invert onClick={ this.onSelectFromAddress }>
<IdentityIcon address={ account.address } />
<div>{ account.name || account.address }</div>
</Button>
</div>
<Button
onClick={ this.onClickRegister }
disabled={ (contentHashError && contentHashOwner !== fromAddress) || urlError || repoError || commitError }
disabled={ (contentHashError && contentHashOwner !== defaultAddress) || urlError || repoError || commitError }
>register url</Button>
</div>
);
Expand Down Expand Up @@ -294,11 +301,11 @@ export default class Application extends Component {
}

onClickRegister = () => {
const { commit, commitError, contentHashError, contentHashOwner, fromAddress, url, urlError, registerType, repo, repoError } = this.state;
const { defaultAddress, commit, commitError, contentHashError, contentHashOwner, url, urlError, registerType, repo, repoError } = this.state;

// TODO: No errors are currently set, validation to be expanded and added for each
// field (query is fast to pick up the issues, so not burning atm)
if ((contentHashError && contentHashOwner !== fromAddress) || repoError || urlError || commitError) {
if ((contentHashError && contentHashOwner !== defaultAddress) || repoError || urlError || commitError) {
return;
}

Expand Down Expand Up @@ -368,13 +375,15 @@ export default class Application extends Component {
}

registerContent (contentRepo, contentCommit) {
const { contentHash, fromAddress, instance } = this.state;
const { defaultAddress, contentHash, instance } = this.state;

contentCommit = contentCommit.substr(0, 2) === '0x' ? contentCommit : `0x${contentCommit}`;
contentCommit = contentCommit.substr(0, 2) === '0x'
? contentCommit
: `0x${contentCommit}`;

const eventId = nextEventId++;
const values = [contentHash, contentRepo, contentCommit];
const options = { from: fromAddress };
const options = { from: defaultAddress };

this.setState({
eventIds: [eventId].concat(this.state.eventIds),
Expand All @@ -383,7 +392,7 @@ export default class Application extends Component {
contentHash,
contentRepo,
contentCommit,
fromAddress,
defaultAddress,
registerBusy: true,
registerState: 'Estimating gas for the transaction',
timestamp: new Date()
Expand Down Expand Up @@ -421,19 +430,19 @@ export default class Application extends Component {
}

registerUrl (contentUrl) {
const { contentHash, fromAddress, instance } = this.state;
const { contentHash, defaultAddress, instance } = this.state;

const eventId = nextEventId++;
const values = [contentHash, contentUrl];
const options = { from: fromAddress };
const options = { from: defaultAddress };

this.setState({
eventIds: [eventId].concat(this.state.eventIds),
events: Object.assign({}, this.state.events, {
[eventId]: {
contentHash,
contentUrl,
fromAddress,
defaultAddress,
registerBusy: true,
registerState: 'Estimating gas for the transaction',
timestamp: new Date()
Expand Down Expand Up @@ -470,25 +479,6 @@ export default class Application extends Component {
);
}

onSelectFromAddress = () => {
const { accounts, fromAddress } = this.state;
const addresses = Object.keys(accounts);
let index = 0;

addresses.forEach((address, _index) => {
if (address === fromAddress) {
index = _index;
}
});

index++;
if (index >= addresses.length) {
index = 0;
}

this.setState({ fromAddress: addresses[index] });
}

lookupHash (url) {
const { instance } = this.state;

Expand Down
Loading