lotus/lotuspond/front/src/Address.js

141 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-08-10 01:54:45 +00:00
import React from 'react'
import CID from 'cids'
import * as multihash from "multihashes";
import State from "./State";
2019-08-16 17:37:04 +00:00
import methods from "./chain/methods";
2019-08-10 01:54:45 +00:00
2019-08-19 14:47:09 +00:00
function truncAddr(addr, len) {
if (addr.length > len) {
return <abbr title={addr}>{addr.substr(0, len - 3) + '..'}</abbr>
2019-08-10 01:54:45 +00:00
}
return addr
}
2019-09-06 22:35:31 +00:00
let sheet = document.createElement('style')
document.body.appendChild(sheet);
2019-08-10 01:54:45 +00:00
class Address extends React.Component {
constructor(props) {
super(props)
this.openState = this.openState.bind(this)
this.state = {balance: -2}
this.refresh = this.refresh.bind(this)
}
componentDidMount() {
this.refresh()
2019-09-17 12:03:28 +00:00
if(!this.props.ts) {
2019-09-17 12:53:43 +00:00
this.updates = setInterval(this.refresh, 2050)
this.props.client.on('close', () => clearInterval(this.updates))
2019-09-17 12:03:28 +00:00
}
2019-08-10 01:54:45 +00:00
}
2019-09-17 12:53:43 +00:00
componentWillUnmount() {
clearInterval(this.updates)
}
2019-08-10 01:54:45 +00:00
async refresh() {
let balance = 0
let actor = {}
2019-08-16 02:34:11 +00:00
let actorInfo
2019-08-21 16:31:14 +00:00
let minerInfo
2019-09-06 22:35:31 +00:00
let nonce
2019-08-10 01:54:45 +00:00
try {
balance = await this.props.client.call('Filecoin.WalletBalance', [this.props.addr])
2019-09-10 10:25:46 +00:00
actor = await this.props.client.call('Filecoin.StateGetActor', [this.props.addr, this.props.ts || null])
2019-08-16 17:37:04 +00:00
2019-08-16 02:34:11 +00:00
actorInfo = await this.actorInfo(actor)
2019-08-21 16:31:14 +00:00
if(this.props.miner) {
minerInfo = await this.props.client.call('Filecoin.StateMinerPower', [this.props.addr, this.props.ts || null])
}
2019-09-06 22:35:31 +00:00
if(this.props.nonce) {
nonce = await this.props.client.call('Filecoin.MpoolGetNonce', [this.props.addr])
}
2019-08-10 01:54:45 +00:00
} catch (err) {
2019-08-16 02:34:11 +00:00
console.log(err)
2019-08-10 01:54:45 +00:00
balance = -1
}
2019-09-06 22:35:31 +00:00
this.setState({balance, actor, actorInfo, minerInfo, nonce})
2019-08-10 01:54:45 +00:00
}
openState() {
this.props.mountWindow((onClose) => <State addr={this.props.addr} actor={this.state.actor} client={this.props.client} onClose={onClose}/>)
}
2019-08-16 02:34:11 +00:00
async actorInfo(actor) {
const c = new CID(actor.Code['/'])
const mh = multihash.decode(c.multihash) // TODO: check identity
2019-08-16 17:37:04 +00:00
let method = <span></span>
if(this.props.method !== undefined && mh.digest.toString()) {
method = <span>.{methods[mh.digest.toString()][this.props.method]}</span>
}
let info = <span>({mh.digest.toString()}{method})</span>
2019-08-16 02:34:11 +00:00
switch(mh.digest.toString()) {
case 'paych':
2019-09-10 10:25:46 +00:00
const actstate = await this.props.client.call('Filecoin.StateReadState', [actor, this.props.ts || null])
2019-08-16 17:37:04 +00:00
info = <span>({mh.digest.toString()}{method} to <Address nobalance={true} client={this.props.client} addr={actstate.State.To} mountWindow={this.props.mountWindow}/>)</span>
2019-08-16 02:34:11 +00:00
}
return info
}
2019-09-06 22:35:31 +00:00
add10k = async () => {
[...Array(10).keys()].map(() => async () => await this.props.add1k(this.props.addr)).reduce(async (p, c) => [await p, await c()], Promise.resolve(null))
}
2019-08-10 01:54:45 +00:00
render() {
let add1k = <span/>
if(this.props.add1k) {
2019-08-16 02:34:11 +00:00
add1k = <span>&nbsp;<a href="#" onClick={() => this.props.add1k(this.props.addr)}>[+1k]</a></span>
2019-09-06 22:35:31 +00:00
if (this.props.add10k) {
add1k = <span>{add1k}&nbsp;<a href="#" onClick={this.add10k}>[+10k]</a></span>
}
2019-08-10 01:54:45 +00:00
}
2019-08-19 14:47:09 +00:00
let addr = truncAddr(this.props.addr, this.props.short ? 12 : 17)
2019-08-10 01:54:45 +00:00
let actInfo = <span>(?)</span>
if(this.state.balance >= 0) {
2019-08-16 02:34:11 +00:00
actInfo = this.state.actorInfo
2019-08-10 01:54:45 +00:00
addr = <a href="#" onClick={this.openState}>{addr}</a>
}
2019-09-06 22:35:31 +00:00
addr = <span className={`pondaddr-${this.props.addr}`}
2019-09-19 14:27:01 +00:00
onMouseEnter={() => sheet.sheet.insertRule(`.pondaddr-${this.props.addr}, .pondaddr-${this.props.addr} * { color: #11ee11 !important; }`, 0)}
2019-09-06 22:35:31 +00:00
onMouseLeave={() => sheet.sheet.deleteRule(0)}
>{addr}</span>
let nonce = <span/>
if(this.props.nonce) {
nonce = <span>&nbsp;<abbr title={"Next nonce"}>Nc:{this.state.nonce}</abbr>{nonce}</span>
}
2019-08-19 14:47:09 +00:00
let balance = <span>:&nbsp;{this.state.balance}&nbsp;</span>
2019-08-15 15:46:34 +00:00
if(this.props.nobalance) {
2019-08-19 14:47:09 +00:00
balance = <span/>
}
if(this.props.short) {
actInfo = <span/>
balance = <span/>
2019-08-15 15:46:34 +00:00
}
2019-08-19 14:47:09 +00:00
let transfer = <span/>
2019-08-16 17:37:04 +00:00
if(this.props.transfer) {
transfer = <span>&nbsp;{this.props.transfer}FIL</span>
}
2019-08-21 16:31:14 +00:00
let minerInfo = <span/>
if(this.state.minerInfo) {
minerInfo = <span>&nbsp;Power: {this.state.minerInfo.MinerPower} ({this.state.minerInfo.MinerPower/this.state.minerInfo.TotalPower*100}%)</span>
}
2019-09-06 22:35:31 +00:00
return <span>{addr}{balance}{actInfo}{nonce}{add1k}{transfer}{minerInfo}</span>
2019-08-10 01:54:45 +00:00
}
}
export default Address