完善erc20的显示

This commit is contained in:
cebgcontract 2022-06-19 17:13:31 +08:00
parent e38ab3e150
commit a33e7e59f4
3 changed files with 49 additions and 5 deletions

View File

@ -3,7 +3,7 @@ export interface IToken {
type: 'eth'|'erc20'
default: boolean
symbol?: string
balance?: number
balance?: string
decimal: number
image?: string
last?: number

View File

@ -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}`;
}
}

View File

@ -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)
}
}