-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChainCard.js
More file actions
82 lines (73 loc) · 2.18 KB
/
Copy pathChainCard.js
File metadata and controls
82 lines (73 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React from "react";
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
import Web3 from "web3";
class SimpleCard extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.setBlock = this.setBlock.bind(this);
this.setBalance = this.setBalance.bind(this);
this.hideBlockTick = this.hideBlockTick.bind(this);
this.handleError = this.handleError.bind(this);
}
setBlock(block) {
if (!block) return;
this.setState(block);
this.setState({ tick: true });
setTimeout(this.hideBlockTick, 300);
}
setBalance(balance) {
this.setState({ balance });
}
hideBlockTick() {
this.setState({ tick: false });
}
handleError(e) {
console.log(`Error on ${this.props.provider.name}`, e);
}
componentDidMount() {
const web3 = new Web3(this.props.provider.ws);
web3.eth
.isSyncing()
.then(this.setBlock)
.catch(this.handleError);
web3.eth
.getBlock("latest")
.then(this.setBlock)
.catch(this.handleError);
web3.eth
.subscribe("newBlockHeaders")
.on("data", this.setBlock)
.on("error", this.handleError);
// Subscribe to addr changes
window.addEventListener("addrChange", e => {
const { addr } = e.detail;
if (!web3.utils.isAddress(addr)) return;
web3.eth
.getBalance(addr)
.then(web3.utils.fromWei)
.then(this.setBalance)
.catch(this.handleError);
});
}
render() {
const chainName = this.props.provider.name;
let { number, tick, highestBlock, currentBlock, balance } = this.state; // this.state = block
let block = !number && highestBlock && currentBlock ? "Syncing" : number;
if (balance && balance !== "0") balance = parseFloat(balance).toFixed(6);
return (
<Card
className="card"
style={{ backgroundColor: tick ? "#22bcb2bf" : null }}
>
<CardContent>
<div className="card-name">{chainName}</div>
<div className="card-block">{block || "..."}</div>
<h2 className="card-balance">{balance}</h2>
</CardContent>
</Card>
);
}
}
export default SimpleCard;