完善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' type: 'eth'|'erc20'
default: boolean default: boolean
symbol?: string symbol?: string
balance?: number balance?: string
decimal: number decimal: number
image?: string image?: string
last?: number last?: number

View File

@ -1,8 +1,10 @@
import { IToken } from "../data/DataModel"; import { IToken } from "../data/DataModel";
import { renderFromTokenMinimalUnit } from "../util/number.util";
import WalletBase from "./WallerBase"; import WalletBase from "./WallerBase";
const {ccclass, property} = cc._decorator; const {ccclass, property} = cc._decorator;
const ETH_TYPE = 'eth'
@ccclass @ccclass
export default class OneToken extends WalletBase { export default class OneToken extends WalletBase {
@ -21,23 +23,65 @@ export default class OneToken extends WalletBase {
// onLoad () {} // onLoad () {}
start () { start () {
super.start()
this.updateInfo()
} }
// update (dt) {} // update (dt) {}
init(_data: IToken) { init(_data: IToken) {
this.data = _data this.data = _data
}
private updateInfo() {
if (this.data.symbol) { if (this.data.symbol) {
this.symbolLabel.string = this.data.symbol this.symbolLabel.string = this.data.symbol
} else { } else {
// TODO: get from remote // 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) { if (this.data.balance !== undefined) {
this.countLabel.string = this.data.balance + '' this.showBalance()
} else { } else {
this.countLabel.string = '-' 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) { addOneToken(data: IToken) {
let node = cc.instantiate(this.tokenPreb) let node = cc.instantiate(this.tokenPreb)
node.getComponent('OneToken').init(data)
this.contentNode.addChild(node) this.contentNode.addChild(node)
node.getComponent('OneToken').init(data)
} }
} }