Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Feature) Reorder contract executors: owners are first #239

Merged
merged 12 commits into from
Dec 29, 2018
108 changes: 97 additions & 11 deletions old-ui/app/components/send/choose-contract-executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,46 @@ import SendHeader from './send-header'
import SendError from './send-error'
import actions from '../../../../ui/app/actions'
import { ifContractAcc } from '../../util'
import Web3 from 'web3'

const ownerABI = [{
'constant': true,
'inputs': [],
'name': 'owner',
'outputs': [
{
'name': '',
'type': 'address',
},
],
'payable': false,
'type': 'function',
'stateMutability': 'view',
}]

const getOwnersABI = [{
'constant': true,
'inputs': [],
'name': 'getOwners',
'outputs': [
{
'name': '',
'type': 'address[]',
},
],
'payable': false,
'type': 'function',
'stateMutability': 'view',
}]

class ChooseContractExecutor extends Component {
constructor (props) {
super(props)
this.state = {
web3: new Web3(global.ethereumProvider),
selectedExecutor: '',
accountsCells: [],
owners: [],
nextDisabled: true,
}
}
Expand All @@ -30,6 +63,8 @@ class ChooseContractExecutor extends Component {
identities: PropTypes.object,
keyrings: PropTypes.array,
error: PropTypes.string,
showLoadingIndication: PropTypes.func,
hideLoadingIndication: PropTypes.func,
}

render () {
Expand Down Expand Up @@ -61,7 +96,8 @@ class ChooseContractExecutor extends Component {
)
}

componentDidMount () {
componentDidMount = async () => {
await this.getAllOwners()
this.generateListOfAccounts()
}

Expand All @@ -88,22 +124,25 @@ class ChooseContractExecutor extends Component {
return buttonContainer
}

generateListOfAccounts () {
generateListOfAccounts = () => {
const { keyrings, identities } = this.props
const accountsCells = []
keyrings.forEach((keyring) => {
if (!ifContractAcc(keyring)) {
keyring.accounts.forEach((address) => {
const identity = identities[address]
accountsCells.push(
<ExecutorCell
key={Math.random()}
address={address}
identity={identity}
isAccountSelected={this.isAccountSelected(address)}
onClick={(e, isSelected) => this.selectExecutor(e, isSelected, address)}
/>
)
const executorCell = <ExecutorCell
key={Math.random()}
address={address}
identity={identity}
isAccountSelected={this.isAccountSelected(address)}
onClick={(e, isSelected) => this.selectExecutor(e, isSelected, address)}
/>
if (this.state.owners.includes(address)) {
accountsCells.unshift(executorCell)
} else {
accountsCells.push(executorCell)
}
})
}
})
Expand All @@ -113,6 +152,51 @@ class ChooseContractExecutor extends Component {
})
}

getAllOwners = () => {
this.props.showLoadingIndication()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May it be possible to add a catch to the promise so you can call hideLoadingIndicator if for any reason the Promise.all fails?

or you can refactor to use async/awaits? whatever fits best.

Copy link
Collaborator Author

@vbaranov vbaranov Dec 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fernandomg! Catch was added.

return new Promise((resolve) => {
Promise.all([this.getOwner(), this.getOwners()])
.then(([owner, owners]) => {
if (!owners) {
owners = []
}
if (owner !== '0x' && !owners.includes(owner)) {
owners.push(owner)
}
this.setState({ owners })
this.props.hideLoadingIndication()
resolve()
})
.catch(_ => {
this.props.hideLoadingIndication()
resolve()
})
})
}

getOwner = () => {
return this.getOwnersCommon('owner', ownerABI)
}

getOwners = () => {
return this.getOwnersCommon('getOwners', getOwnersABI)
}

getOwnersCommon = (method, abi) => {
const { web3 } = this.state
const { txParams } = this.props

return new Promise((resolve) => {
try {
web3.eth.contract(abi).at(txParams.to)[method].call((err, output) => {
resolve(output)
})
} catch (e) {
resolve('')
}
})
}

componentDidUpdate (prevProps, prevState) {
if (prevState.selectedExecutor !== this.state.selectedExecutor) {
this.generateListOfAccounts()
Expand Down Expand Up @@ -171,6 +255,8 @@ function mapStateToProps (state) {

function mapDispatchToProps (dispatch) {
return {
showLoadingIndication: () => dispatch(actions.showLoadingIndication()),
hideLoadingIndication: () => dispatch(actions.hideLoadingIndication()),
hideWarning: () => dispatch(actions.hideWarning()),
signTx: (txParams) => dispatch(actions.signTx(txParams)),
setSelectedAddress: (address) => dispatch(actions.setSelectedAddress(address)),
Expand Down
Loading