From a33e7e59f4a14c0a95ad38df510c33003f7d6c37 Mon Sep 17 00:00:00 2001 From: cebgcontract <99630598+cebgcontract@users.noreply.github.com> Date: Sun, 19 Jun 2022 17:13:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84erc20=E7=9A=84=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/comp/wallet/scripts/data/DataModel.ts | 2 +- assets/comp/wallet/scripts/ui/OneToken.ts | 50 ++++++++++++++++++-- assets/comp/wallet/scripts/ui/TokenList.ts | 2 +- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/assets/comp/wallet/scripts/data/DataModel.ts b/assets/comp/wallet/scripts/data/DataModel.ts index dd01c68..935cbd9 100644 --- a/assets/comp/wallet/scripts/data/DataModel.ts +++ b/assets/comp/wallet/scripts/data/DataModel.ts @@ -3,7 +3,7 @@ export interface IToken { type: 'eth'|'erc20' default: boolean symbol?: string - balance?: number + balance?: string decimal: number image?: string last?: number diff --git a/assets/comp/wallet/scripts/ui/OneToken.ts b/assets/comp/wallet/scripts/ui/OneToken.ts index f2da19a..fe01de7 100644 --- a/assets/comp/wallet/scripts/ui/OneToken.ts +++ b/assets/comp/wallet/scripts/ui/OneToken.ts @@ -1,8 +1,10 @@ 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 { @@ -21,23 +23,65 @@ export default class OneToken extends WalletBase { // 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.countLabel.string = this.data.balance + '' + this.showBalance() } else { this.countLabel.string = '-' - //TODO: get from remote + } + 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}`; + } } diff --git a/assets/comp/wallet/scripts/ui/TokenList.ts b/assets/comp/wallet/scripts/ui/TokenList.ts index 0d5726b..659c750 100644 --- a/assets/comp/wallet/scripts/ui/TokenList.ts +++ b/assets/comp/wallet/scripts/ui/TokenList.ts @@ -48,7 +48,7 @@ export default class TokenList extends WalletBase { addOneToken(data: IToken) { let node = cc.instantiate(this.tokenPreb) - node.getComponent('OneToken').init(data) this.contentNode.addChild(node) + node.getComponent('OneToken').init(data) } }