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
|
|
|
|
|
|
|
function truncAddr(addr) {
|
|
|
|
if (addr.length > 21) {
|
|
|
|
return <abbr title={addr}>{addr.substr(0, 18) + '..'}</abbr>
|
|
|
|
}
|
|
|
|
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-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-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-16 02:34:11 +00:00
|
|
|
this.setState({balance, actor, actorInfo})
|
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> <a href="#" onClick={() => this.props.add1k(this.props.addr)}>[+1k]</a></span>
|
2019-08-10 01:54:45 +00:00
|
|
|
}
|
|
|
|
let addr = truncAddr(this.props.addr)
|
|
|
|
|
|
|
|
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-15 15:46:34 +00:00
|
|
|
let balance = <span>: {this.state.balance}</span>
|
|
|
|
if(this.props.nobalance) {
|
|
|
|
balance = <span></span>
|
|
|
|
}
|
|
|
|
|
2019-08-16 17:37:04 +00:00
|
|
|
let transfer = <span></span>
|
|
|
|
if(this.props.transfer) {
|
|
|
|
transfer = <span> {this.props.transfer}FIL</span>
|
|
|
|
}
|
|
|
|
|
|
|
|
return <span>{addr}{balance} {actInfo}{add1k}{transfer}</span>
|
2019-08-10 01:54:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Address
|