63 lines
1.5 KiB
TypeScript
63 lines
1.5 KiB
TypeScript
import { formatPrice } from "../common/chain.util";
|
|
import { ZMonitor } from "../decorator/AutoUpdateUI";
|
|
import JCWallet from "../JCWallet";
|
|
import WalletBase from "./WallerBase";
|
|
|
|
const {ccclass, property} = cc._decorator;
|
|
|
|
@ccclass
|
|
export default class WalletInfo extends WalletBase {
|
|
|
|
@property(cc.Label)
|
|
addressLabel: cc.Label = null;
|
|
|
|
@property(cc.Label)
|
|
balanceLabel: cc.Label = null;
|
|
|
|
@ZMonitor()
|
|
accountId: string = '';
|
|
|
|
@ZMonitor()
|
|
balance: string = ''
|
|
|
|
private wallet: JCWallet
|
|
|
|
// LIFE-CYCLE CALLBACKS:
|
|
|
|
// onLoad () {}
|
|
|
|
start () {
|
|
this.wallet = new JCWallet()
|
|
this.accountId = this.wallet.currentAccount().address
|
|
this.updateUI()
|
|
this.wallet.getBalance()
|
|
.then(v => {
|
|
console.log('update balance: ', v);
|
|
this.balance = v + ''
|
|
})
|
|
}
|
|
|
|
// update (dt) {}
|
|
updateUI(): void {
|
|
super.updateUI()
|
|
this.addressLabel.string = this.formatAddress()
|
|
this.balanceLabel.string = this.formatMoney()
|
|
}
|
|
|
|
formatAddress() {
|
|
if (this.accountId.length >= 10) {
|
|
return this.accountId.substring(0, 8) + '...' + this.accountId.substring(this.accountId.length - 8)
|
|
} else if (this.accountId.length > 0 && this.accountId.length < 10) {
|
|
return this.accountId
|
|
} else {
|
|
return ''
|
|
}
|
|
}
|
|
|
|
formatMoney() {
|
|
console.log('update balance: ', this.balance)
|
|
let money = formatPrice(this.balance, 18, 4)
|
|
return `${money} KCS`;
|
|
}
|
|
}
|