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

Commit

Permalink
Use default account for sending transactions (#5588)
Browse files Browse the repository at this point in the history
* Add default account for Wallet Creation

* Use default account by default, save current used account

* Fix tests
  • Loading branch information
ngotchac authored and gavofyork committed May 16, 2017
1 parent 48ddd8b commit 8642300
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 7 deletions.
3 changes: 2 additions & 1 deletion js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import ContextProvider from '~/ui/ContextProvider';
import muiTheme from '~/ui/Theme';
import MainApplication from './main';

import { patchApi } from '~/util/tx';
import { loadSender, patchApi } from '~/util/tx';
import { setApi } from '~/redux/providers/apiActions';

import './environment';
Expand Down Expand Up @@ -66,6 +66,7 @@ if (window.location.hash && window.location.hash.indexOf(AUTH_HASH) === 0) {
const api = new SecureApi(`${urlScheme}${parityUrl}`, token);

patchApi(api);
loadSender(api);
ContractInstances.create(api);

const store = initStore(api, hashHistory);
Expand Down
6 changes: 5 additions & 1 deletion js/src/modals/CreateWallet/createWallet.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ let component;
let onClose;

function createApi () {
api = {};
api = {
parity: {
getNewDappsDefaultAddress: sinon.stub().resolves('')
}
};

return api;
}
Expand Down
12 changes: 10 additions & 2 deletions js/src/modals/CreateWallet/createWalletStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { wallet as walletCode, walletLibrary as walletLibraryCode, walletLibrary

import { validateUint, validateAddress, validateName } from '~/util/validation';
import { toWei } from '~/api/util/wei';
import { deploy } from '~/util/tx';
import { deploy, getSender, loadSender, setSender } from '~/util/tx';
import WalletsUtils from '~/util/wallets';

const STEPS = {
Expand Down Expand Up @@ -120,10 +120,17 @@ export default class CreateWalletStore {
this.api = api;

this.step = this.stepsKeys[0];
this.wallet.account = Object.values(accounts)[0].address;
this.wallet.account = getSender() || Object.values(accounts)[0].address;
this.validateWallet(this.wallet);
this.onClose = onClose;
this.onSetRequest = onSetRequest;

loadSender(this.api)
.then((defaultAccount) => {
if (defaultAccount !== this.wallet.account) {
this.onChange({ account: defaultAccount });
}
});
}

@action onTypeChange = (type) => {
Expand Down Expand Up @@ -221,6 +228,7 @@ export default class CreateWalletStore {

const contract = this.api.newContract(walletAbi);

setSender(account);
this.wallet = this.getWalletWithMeta(this.wallet);
this.onClose();
return deploy(contract, options, [ owners, required, daylimit ])
Expand Down
12 changes: 10 additions & 2 deletions js/src/modals/DeployContract/deployContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { bindActionCreators } from 'redux';
import { Button, GasPriceEditor, IdentityIcon, Portal, Warning } from '~/ui';
import { CancelIcon } from '~/ui/Icons';
import { ERRORS, validateAbi, validateCode, validateName, validatePositiveNumber } from '~/util/validation';
import { deploy, deployEstimateGas } from '~/util/tx';
import { deploy, deployEstimateGas, getSender, loadSender, setSender } from '~/util/tx';
import { setRequest } from '~/redux/providers/requestsActions';

import DetailsStep from './DetailsStep';
Expand Down Expand Up @@ -94,7 +94,7 @@ class DeployContract extends Component {
description: '',
descriptionError: null,
extras: false,
fromAddress: Object.keys(this.props.accounts)[0],
fromAddress: getSender() || Object.keys(this.props.accounts)[0],
fromAddressError: null,
name: '',
nameError: ERRORS.invalidName,
Expand All @@ -110,6 +110,13 @@ class DeployContract extends Component {
if (abi && code) {
this.setState({ abi, code });
}

loadSender(this.context.api)
.then((defaultAccount) => {
if (defaultAccount !== this.state.fromAddress) {
this.setState({ fromAddress: defaultAccount });
}
});
}

componentWillReceiveProps (nextProps) {
Expand Down Expand Up @@ -467,6 +474,7 @@ class DeployContract extends Component {

const contract = api.newContract(abiParsed);

setSender(fromAddress);
this.onClose();
deploy(contract, options, params, true)
.then((requestId) => {
Expand Down
2 changes: 2 additions & 0 deletions js/src/modals/ExecuteContract/executeContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { CancelIcon, NextIcon, PrevIcon } from '~/ui/Icons';
import { MAX_GAS_ESTIMATION } from '~/util/constants';
import { validateAddress, validateUint } from '~/util/validation';
import { parseAbiType } from '~/util/abi';
import { setSender } from '~/util/tx';

import AdvancedStep from './AdvancedStep';
import DetailsStep from './DetailsStep';
Expand Down Expand Up @@ -307,6 +308,7 @@ class ExecuteContract extends Component {
value: api.util.toWei(amount || 0)
});

setSender(fromAddress);
func.postTransaction(options, values);
this.onClose();
}
Expand Down
32 changes: 32 additions & 0 deletions js/src/util/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@

import WalletsUtils from '~/util/wallets';

/**
* The sender is by default (when the UI loads) the
* default dapp address. It can then be modified when
* sending transactions....
*/
let currentSender = '';
let hasCurrentSenderChanged = false;

export function getSender () {
currentSender;
}

export function loadSender (api) {
// If the current sender has been changed
// then don't bother checking changes of the
// default sender
if (hasCurrentSenderChanged) {
return Promise.resolve(currentSender);
}

return api.parity.getNewDappsDefaultAddress()
.then((defaultAccount) => {
currentSender = defaultAccount;
return defaultAccount;
});
}

export function setSender (sender) {
currentSender = sender;
hasCurrentSenderChanged = true;
}

export function trackRequest (api, options, statusCallback) {
const { requestId, transactionHash } = options;
const txHashPromise = transactionHash
Expand Down
10 changes: 9 additions & 1 deletion js/src/views/Contract/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { setVisibleAccounts } from '~/redux/providers/personalActions';
import { Actionbar, Button, Page, Portal } from '~/ui';
import { CancelIcon, DeleteIcon, EditIcon, PlayIcon, VisibleIcon } from '~/ui/Icons';
import Editor from '~/ui/Editor';
import { getSender, loadSender } from '~/util/tx';

import Header from '../Account/Header';
import Delete from '../Address/Delete';
Expand All @@ -52,7 +53,7 @@ class Contract extends Component {

state = {
contract: null,
fromAddress: '',
fromAddress: getSender(),
showDeleteDialog: false,
showEditDialog: false,
showExecuteDialog: false,
Expand All @@ -76,6 +77,13 @@ class Contract extends Component {
api
.subscribe('eth_blockNumber', this.queryContract)
.then(blockSubscriptionId => this.setState({ blockSubscriptionId }));

loadSender(api)
.then((defaultAccount) => {
if (defaultAccount !== this.state.fromAddress) {
this.onFromAddressChange(null, defaultAccount);
}
});
}

componentWillReceiveProps (nextProps) {
Expand Down

0 comments on commit 8642300

Please sign in to comment.