import React from 'react'
import CID from 'cids'
import * as multihash from "multihashes";
import State from "./State";
import methods from "./chain/methods";
function truncAddr(addr) {
if (addr.length > 21) {
return {addr.substr(0, 18) + '..'}
}
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 = {}
let actorInfo
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])
actorInfo = await this.actorInfo(actor)
} catch (err) {
console.log(err)
balance = -1
}
this.setState({balance, actor, actorInfo})
}
openState() {
this.props.mountWindow((onClose) => )
}
async actorInfo(actor) {
const c = new CID(actor.Code['/'])
const mh = multihash.decode(c.multihash) // TODO: check identity
let method =
if(this.props.method !== undefined && mh.digest.toString()) {
method = .{methods[mh.digest.toString()][this.props.method]}
}
let info = ({mh.digest.toString()}{method})
switch(mh.digest.toString()) {
case 'paych':
const actstate = await this.props.client.call('Filecoin.ChainReadState', [actor, this.props.ts || null])
info = ({mh.digest.toString()}{method} to )
}
return info
}
render() {
let add1k =
if(this.props.add1k) {
add1k = this.props.add1k(this.props.addr)}>[+1k]
}
let addr = truncAddr(this.props.addr)
let actInfo = (?)
if(this.state.balance >= 0) {
actInfo = this.state.actorInfo
addr = {addr}
}
let balance = : {this.state.balance}
if(this.props.nobalance) {
balance =
}
let transfer =
if(this.props.transfer) {
transfer = {this.props.transfer}FIL
}
return {addr}{balance} {actInfo}{add1k}{transfer}
}
}
export default Address