Skip to content

Commit

Permalink
Refactor accounts data structure in store
Browse files Browse the repository at this point in the history
  • Loading branch information
ssteiger committed Jun 15, 2019
1 parent 3f37615 commit 13b0721
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 29 deletions.
17 changes: 10 additions & 7 deletions app/components/Accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ export default class Accounts extends Component<Props> {
}

handleChange = (value: Number) => {
const { web3, web3AccountsSetMain } = this.props
const account = web3.accounts.find(obj => obj.address === value)
const {
accounts: { all: allAccounts=[], selected: selectedAccount={} },
web3AccountsSetMain
} = this.props
const account = allAccounts.find(obj => obj.address === value)
delete account.index
web3AccountsSetMain(account)
}

copyToClipboard = () => {
const { web3: { selectedAccount: { address }='' } } = this.props
const {accounts: { all: allAccounts=[], selected: selectedAccount={} }} = this.props
const { address } = selectedAccount
copy(address)
message.success(`copied '${address}' to clipboard`)
}
Expand All @@ -40,9 +44,8 @@ export default class Accounts extends Component<Props> {

render() {
const { convertWeiToEth } = this
const { web3 } = this.props
const { selectedAccount={} } = web3
const { address='' } = selectedAccount
const { accounts: { all: allAccounts=[], selected={} } } = this.props
const { address } = selected

return (
<React.Fragment>
Expand All @@ -55,7 +58,7 @@ export default class Accounts extends Component<Props> {
onChange={this.handleChange}
>
{
web3.accounts.map((account, index) => (
allAccounts.map((account, index) => (
<Select.Option key={`account-opt-${account.address}`} value={account.address}>
<Tag color='green'>{convertWeiToEth(account.balance)} ETH</Tag>
{account.address}
Expand Down
5 changes: 2 additions & 3 deletions app/components/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ export default class Sidebar extends Component<Props> {
render() {
const { Header, Content, } = Layout
const { Title } = Typography

const {
web3,
accounts: { all: allAccounts=[], selected: selectedAccount={} },
sidebarWidth,
rpcServer,
compilerUrl,
Expand Down Expand Up @@ -105,7 +104,7 @@ export default class Sidebar extends Component<Props> {
style={{ backgroundColor:'#fff', marginTop:'100px' }}
>
{
web3.accounts.map((account, key) => {
allAccounts.map((account, key) => {
return (
<List.Item key={`item-${key}`} actions={[<a onClick={() => {removeAccount(account)}}>remove</a>]}>
<List.Item.Meta
Expand Down
8 changes: 6 additions & 2 deletions app/components/contract-form/Constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export default class Constructor extends Component<Props> {

handleSubmit = (event) => {
event.preventDefault()
const { web3, file, deployContract } = this.props
const {
accounts: { all: allAccounts=[], selected: selectedAccount={} },
file,
deployContract,
} = this.props
const inputFields = $(event.target).find('input')

let inputs = {}
Expand All @@ -28,7 +32,7 @@ export default class Constructor extends Component<Props> {
}
})

deployContract({ file, inputs, account: web3.selectedAccount })
deployContract({ file, inputs, account: selectedAccount })
}

renderInputs = (constructorAbi) => {
Expand Down
18 changes: 15 additions & 3 deletions app/components/contract-form/Functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,19 @@ export default class Functions extends Component<Props> {
*/

// TODO:
functionCallResult = (abiFunc) => {

getEncodeFunctionSignature= (abiPart) => {
// https://web3js.readthedocs.io/en/1.0/web3-eth-abi.html#example
const { accounts: { all: allAccounts=[], selected: selectedAccount={} } } = this.props
/*
const encodeFunctionSignature = web3.eth.abi.encodeFunctionSignature({
name: abiPart.name,
type: abiPart.type,
inputs: abiPart.inputs,
})
console.log({ encodeFunctionSignature })
return encodeFunctionSignature
*/
return abiPart
}

render() {
Expand All @@ -263,6 +274,7 @@ export default class Functions extends Component<Props> {
<Text strong>functions</Text>
{
abi.map((abiPart, key) => {
console.log(abiPart)
if (abiPart.type === 'function') {
return (
<Form
Expand All @@ -277,7 +289,7 @@ export default class Functions extends Component<Props> {
{renderEthInput(abiPart)}
<Button block htmlType='submit'>call</Button>
<Text style={{ color:'#52c41a', overflowWrap:'break-word' }}>
{functionCallResults[abiPart.name]}
{functionCallResults[this.getEncodeFunctionSignature(abiPart)]}
</Text>
</Form>
)
Expand Down
14 changes: 5 additions & 9 deletions app/reducers/web3.js → app/reducers/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,27 @@
import type { Action } from './types'

import {
WEB3_INIT,
WEB3_ACCOUNTS_LOAD_ALL,
WEB3_ACCOUNTS_SET_MAIN,
WEB3_ACCOUNTS_LOAD_BALANCES,
} from '../constants/actions'

const initialState = {
accounts: [],
all: [],
selected: {},
}

export default function web3(state: Object = initialState, action: Action) {
export default function accounts(state: Object = initialState, action: Action) {
switch (action.type) {
case WEB3_INIT:
return {
...state
}
case WEB3_ACCOUNTS_LOAD_ALL:
return {
...state,
accounts: action.accounts,
all: action.accounts,
}
case WEB3_ACCOUNTS_SET_MAIN:
return {
...state,
selectedAccount: action.account,
selected: action.account,
}
default:
return {
Expand Down
6 changes: 4 additions & 2 deletions app/reducers/functionCallResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export default function functionCallResults(state: Object = initialState, action
...state,
}
switch (action.type) {
case FUNCTION_CALL_RESULTS_UPDATE:
newState[action.payload.functionDetails.name] = action.payload.result + ''
case FUNCTION_CALL_RESULTS_UPDATE: {
const { payload: { functionDetails, result } } = action
newState[functionDetails.signature] = result + ''
return newState
}
case FUNCTION_CALL_RESULTS_RESET:
delete newState[action.functionName]
return newState
Expand Down
4 changes: 2 additions & 2 deletions app/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { combineReducers } from 'redux'
import { connectRouter } from 'connected-react-router'

import web3 from './web3'
import accounts from './accounts'
import settings from './settings'
import sidebar from './sidebar'
import selectedFile from './selectedFile'
Expand All @@ -12,7 +12,7 @@ import functionCallResults from './functionCallResults'
export default function createRootReducer(history: History) {
return combineReducers({
router: connectRouter(history),
web3,
accounts,
settings,
sidebar,
selectedFile,
Expand Down
2 changes: 1 addition & 1 deletion app/sagas/settings/generateRandomAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function* generateRandomAccount(action) {
}
const account = yield call(generateRandomAccountPromise)
const query_find = { _id: 'accounts' }
const query_update = { $push: {accounts: account} }
const query_update = { $push: { accounts: account } }
yield call(promiseDbUpdate, Settings, query_find, query_update)
yield put({ type: WEB3_INIT })
message.success('account created')
Expand Down

0 comments on commit 13b0721

Please sign in to comment.