import React from 'react'
import CID from 'cids'
import ReactTooltip from 'react-tooltip'
import * as multihash from "multihashes"
import State from "./State"
import methods from "./chain/methods"
import Fil from "./Fil";
function truncAddr(addr, len) {
if (!addr) {
return ""
}
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) {
this.updates = setInterval(this.refresh, 2050)
this.props.client.on('close', () => clearInterval(this.updates))
}
}
componentWillUnmount() {
clearInterval(this.updates)
}
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
}
addColl = async () => {
const coll = await this.props.client.call('Filecoin.StatePledgeCollateral', [null])
this.props.addN(this.props.addr, coll)
}
render() {
let add20k =
if(this.props.addN) {
add20k = this.props.addN(this.props.addr, 200000)}>[+200k]
if (this.props.add10k) {
add20k = {add20k} this.props.addN(this.props.addr, 2000000)}>[+2M]
add20k = {add20k} this.props.addN(this.props.addr, 20000000)}>[+20M]
add20k = {add20k} this.addColl()}>[+C]
}
}
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 !important; }`, 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 = {actInfo}: {this.state.balance}
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}{add20k}{transfer}{minerInfo}
}
}
export default Address