lotus/lotuspond/front/src/Address.js

111 lines
3.2 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
}
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()
if(!this.props.ts)
setInterval(this.refresh, 2050)
}
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-08-10 01:54:45 +00:00
try {
balance = await this.props.client.call('Filecoin.WalletBalance', [this.props.addr])
actor = await this.props.client.call('Filecoin.ChainGetActor', [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-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-08-21 16:31:14 +00:00
this.setState({balance, actor, actorInfo, minerInfo})
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':
const actstate = await this.props.client.call('Filecoin.ChainReadState', [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-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-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-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>
}
return <span>{addr}{balance}{actInfo}{add1k}{transfer}{minerInfo}</span>
2019-08-10 01:54:45 +00:00
}
}
export default Address