import { IToken } from "../data/DataModel"; import { renderFromTokenMinimalUnit } from "../util/number.util"; import WalletBase from "./WallerBase"; const {ccclass, property} = cc._decorator; const ETH_TYPE = 'eth' @ccclass export default class OneToken extends WalletBase { @property(cc.Label) symbolLabel: cc.Label = null; @property(cc.Label) countLabel: cc.Label = null; @property(cc.Sprite) icon: cc.Sprite = null; data: IToken = null // LIFE-CYCLE CALLBACKS: // onLoad () {} start () { super.start() this.updateInfo() } // update (dt) {} init(_data: IToken) { this.data = _data } private updateInfo() { if (this.data.symbol) { this.symbolLabel.string = this.data.symbol } else { // TODO: get from remote } if (this.data.decimal === undefined && this.data.type !== ETH_TYPE) { // get from chain this.wallet.erc20Standard.getTokenDecimals(this.data.address) .then(sysmbol => { this.data.symbol = sysmbol this.showBalance() }) } if (this.data.balance !== undefined) { this.showBalance() } else { this.countLabel.string = '-' } if (this.data.type === ETH_TYPE) { this.wallet.getBalance() .then(balance => { this.data.balance = balance this.showBalance() }) } else { const account = this.wallet.currentAccount().address this.wallet.erc20Standard.getBalanceOf(this.data.address, account) .then(balance => { this.data.balance = balance this.showBalance() }) } } private showBalance() { if (this.data.decimal !== undefined && this.data.balance !== undefined) { this.countLabel.string = this.formatMoney() } } formatMoney() { console.log('update balance: ', this.data.balance) const chainData = this.wallet.currentChain let symbol = chainData.symbol if (this.data.balance === '-') { return `-`; } let money = renderFromTokenMinimalUnit(this.data.balance, this.data.decimal, 4) return `${money}`; } }