Skip to content

Commit

Permalink
Merge pull request #239 from poanetwork/contract-executors-reorder
Browse files Browse the repository at this point in the history
(Feature) Reorder contract executors: owners are first
  • Loading branch information
vbaranov authored Dec 29, 2018
2 parents 798256f + 0b2fb88 commit 9359e12
Show file tree
Hide file tree
Showing 5 changed files with 633 additions and 513 deletions.
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()
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

0 comments on commit 9359e12

Please sign in to comment.