2018-05-18 19:17:46 +00:00
|
|
|
import web3 from "Embark/web3"
|
|
|
|
import EmbarkJS from 'Embark/EmbarkJS';
|
|
|
|
import React from 'react';
|
|
|
|
import { Nav, MenuItem , NavDropdown} from 'react-bootstrap';
|
|
|
|
import Blockies from 'react-blockies';
|
|
|
|
|
|
|
|
import './accountlist.css';
|
|
|
|
|
|
|
|
class AccList extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2018-05-19 06:09:13 +00:00
|
|
|
classNameNavDropdown: props.classNameNavDropdown,
|
|
|
|
defaultAccount: "0x0000000000000000000000000000000000000000",
|
|
|
|
addresses: [],
|
|
|
|
balances: []
|
2018-05-18 19:17:46 +00:00
|
|
|
}
|
|
|
|
__embarkContext.execWhenReady(() => {
|
2018-05-19 06:09:13 +00:00
|
|
|
this.load()
|
2018-05-18 19:17:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-19 06:09:13 +00:00
|
|
|
load() {
|
|
|
|
web3.eth.getAccounts((err, addresses) => {
|
|
|
|
if (addresses) {
|
|
|
|
var defaultAccount = web3.eth.defaultAccount;
|
|
|
|
if(!defaultAccount){
|
|
|
|
web3.eth.defaultAccount = addresses[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
var balances = [];
|
|
|
|
balances.length == addresses.length;
|
|
|
|
addresses.forEach((address, index) => {
|
|
|
|
web3.eth.getBalance(address, 'latest', (err, balance) => {
|
|
|
|
balances[index] = balance;
|
|
|
|
if(index+1 == balances.length){
|
|
|
|
this.setState({
|
|
|
|
balances: balances
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
this.setState({
|
|
|
|
defaultAccount: defaultAccount,
|
|
|
|
addresses: addresses
|
|
|
|
});
|
|
|
|
|
2018-05-18 19:17:46 +00:00
|
|
|
} else {
|
2018-05-19 06:09:13 +00:00
|
|
|
console.log("No addresses available.");
|
2018-05-18 19:17:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setDefaultAccount(index) {
|
2018-05-19 06:09:13 +00:00
|
|
|
var defaultAcc = this.state.addresses[index];
|
2018-05-18 19:17:46 +00:00
|
|
|
if(defaultAcc){
|
|
|
|
web3.eth.defaultAccount = defaultAcc;
|
|
|
|
this.setState({defaultAccount: defaultAcc });
|
2018-05-22 13:49:22 +00:00
|
|
|
this.props.accountUpdateHandler(defaultAcc);
|
2018-05-18 19:17:46 +00:00
|
|
|
} else {
|
|
|
|
console.log("invalid account")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
var accsTitle;
|
|
|
|
var accsList = [];
|
2018-05-19 06:09:13 +00:00
|
|
|
if (this.state.addresses) {
|
2018-05-18 19:17:46 +00:00
|
|
|
accsTitle = this.state.defaultAccount;
|
2018-05-19 06:09:13 +00:00
|
|
|
this.state.addresses.forEach(
|
2018-05-18 19:17:46 +00:00
|
|
|
(name, index) => {
|
|
|
|
accsList.push(
|
|
|
|
<MenuItem key={index} onClick={(e) => this.setDefaultAccount(index) }>
|
|
|
|
<div className="account">
|
|
|
|
<div className="accountIdenticon">
|
|
|
|
<Blockies seed={name} />
|
|
|
|
</div>
|
2018-05-19 06:09:13 +00:00
|
|
|
<div className="accountHexString">
|
2018-05-18 19:17:46 +00:00
|
|
|
{name}
|
2018-05-19 06:09:13 +00:00
|
|
|
</div>
|
|
|
|
<div className="accountBalance">
|
|
|
|
Ξ {this.state.balances[index] / (10**18)}
|
|
|
|
</div>
|
2018-05-18 19:17:46 +00:00
|
|
|
</div>
|
|
|
|
</MenuItem>);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
<div className="accounts">
|
|
|
|
<div className="selectedIdenticon">
|
|
|
|
<Blockies seed={ this.state.defaultAccount } />
|
|
|
|
</div>
|
|
|
|
<div className="accountList">
|
|
|
|
<Nav>
|
2018-05-19 06:09:13 +00:00
|
|
|
<NavDropdown key={1} title={accsTitle} id="basic-nav-dropdown" className={ this.state.classNameNavDropdown }>
|
2018-05-18 19:17:46 +00:00
|
|
|
{accsList}
|
|
|
|
</NavDropdown>
|
|
|
|
</Nav>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AccList;
|