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, len) {
if (addr.length > len) {
return {addr.substr(0, len - 3) + '..'}
}
return addr
}
let sheet = document.createElement('style')
document.body.appendChild(sheet);
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
let minerInfo
let nonce
try {
balance = await this.props.client.call('Filecoin.WalletBalance', [this.props.addr])
actor = await this.props.client.call('Filecoin.StateGetActor', [this.props.addr, this.props.ts || null])
actorInfo = await this.actorInfo(actor)
if(this.props.miner) {
minerInfo = await this.props.client.call('Filecoin.StateMinerPower', [this.props.addr, this.props.ts || null])
}
if(this.props.nonce) {
nonce = await this.props.client.call('Filecoin.MpoolGetNonce', [this.props.addr])
}
} catch (err) {
console.log(err)
balance = -1
}
this.setState({balance, actor, actorInfo, minerInfo, nonce})
}
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.StateReadState', [actor, this.props.ts || null])
info = ({mh.digest.toString()}{method} to )
}
return info
}
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))
}
render() {
let add1k =
if(this.props.add1k) {
add1k = this.props.add1k(this.props.addr)}>[+1k]
if (this.props.add10k) {
add1k = {add1k} [+10k]
}
}
let addr = truncAddr(this.props.addr, this.props.short ? 12 : 17)
let actInfo = (?)
if(this.state.balance >= 0) {
actInfo = this.state.actorInfo
addr = {addr}
}
addr = sheet.sheet.insertRule(`.pondaddr-${this.props.addr}, .pondaddr-${this.props.addr} * { color: #11ee11; }`, 0)}
onMouseLeave={() => sheet.sheet.deleteRule(0)}
>{addr}
let nonce =
if(this.props.nonce) {
nonce = Nc:{this.state.nonce}{nonce}
}
let balance = : {this.state.balance}
if(this.props.nobalance) {
balance =
}
if(this.props.short) {
actInfo =
balance =
}
let transfer =
if(this.props.transfer) {
transfer = {this.props.transfer}FIL
}
let minerInfo =
if(this.state.minerInfo) {
minerInfo = Power: {this.state.minerInfo.MinerPower} ({this.state.minerInfo.MinerPower/this.state.minerInfo.TotalPower*100}%)
}
return {addr}{balance}{actInfo}{nonce}{add1k}{transfer}{minerInfo}
}
}
export default Address