2022-06-20 14:23:26 +08:00

79 lines
2.0 KiB
TypeScript

import { ZError } from "../common/ZError";
import { INFT } from "../data/DataModel";
import JCWallet from "../JCWallet";
import { GET_JSON } from "../lib/Http";
import WalletBase from "./WallerBase";
const {ccclass, property} = cc._decorator;
const BASE_TOKEN_URI = 'https://market.cebg.games/api/nft/info/'
@ccclass
export default class OneNFT extends WalletBase {
@property(cc.Label)
nftIDlabel: cc.Label = null;
@property(cc.Sprite)
icon: cc.Sprite = null;
data: INFT = null
addressAccount: string = ''
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start () {
super.start()
}
// update (dt) {}
async init(data: INFT, refresh: boolean) {
if (!this.wallet) {
this.wallet = new JCWallet()
}
this.addressAccount = this.wallet.currentAccount().address
this.data = data
try {
const tokenId = await this.wallet.erc721Standard.getCollectibleTokenId(
this.data.address,
this.addressAccount,
this.data.index
)
if (this.data.tokenId !== tokenId) {
this.data.tokenId = tokenId
refresh = true
}
} catch (e) {
console.log('error fetch tokenId from chain')
}
if (refresh) {
await this.refreshNFTInfo()
}
this.updateUI()
}
async refreshNFTInfo() {
const url = `${BASE_TOKEN_URI}${this.data.tokenId}`
const info: any = await GET_JSON(url)
console.log(info)
this.data.name = info.name
this.data.desc = info.description
this.data.image = info.image
}
updateUI() {
if (this.data.image) {
let self = this
cc.loader.load(this.data.image, function(err, texture){
self.icon.spriteFrame = new cc.SpriteFrame(texture);
})
}
this.nftIDlabel.string = this.data.tokenId
}
}