aozhiwei fb1a72bec0 1
2023-07-11 17:31:36 +08:00

121 lines
3.2 KiB
JavaScript

const log = require('j7/log');
const app = require('j7/app');
const bcutils = require('j7/bcutils');
const utils = require('j7/utils');
const constant = require('common/constant');
const bcconst = require('common/bcconst');
const bchelper = require('common/bchelper');
const metaFactory = require('../../../metadata/factory');
const BaseEventProcess = require('../common/BaseEventProcess');
/*
操作成功的事件
event TokenMinted(
address indexed to,
uint256 indexed nonce,
uint256 startTime,
uint256[] ids
)
*/
class Activate721Nft extends BaseEventProcess {
async start() {
const returnValues = this.getReturnValues();
const transId = returnValues['nonce'];
const to = returnValues['to'];
if (returnValues['ids'].length != 1) {
this.throwError('error returnValues:' + utils.jsonEncode(returnValues));
return;
}
const tokenId = returnValues['ids'][0];
const params = bcutils.extractParamsFromTransId(transId);
if (!params || params.length != 3) {
this.throwError(' error transId:' + utils.jsonEncode(returnValues));
return;
}
const itemUniId = params[0];
const itemId = params[1];
await this.mintNft(
transId,
bcutils.toNormalAddress(to),
tokenId,
itemUniId,
itemId);
await gameapi.confirmTransactionDb(transId);
}
async mintNft(transId, owner, tokenId, itemUniId, itemId) {
const itemMeta = metaFactory.getMetaByKey('Item', itemId);
if (!itemMeta) {
this.throwError('error transId: itemId:' + itemId);
return;
}
const tokenType = itemMeta.getNftType();
if (tokenType == bcconst.BC_NFT_NONE) {
this.throwError('error transId:2');
return;
}
await this.updateGameDbInfo(transId, itemUniId, itemId, tokenId, tokenType);
const nowTime = utils.getUtcTime();
const fieldList = [
['token_id', tokenId],
['token_type', tokenType],
['item_id', itemId],
['owner_address', bcutils.toNormalAddress(owner)],
['creator_address', bcutils.toNormalAddress(owner)],
['confirm_block_number', this.getBlockNumber()],
['net_id', this.getNetId()],
['contract_address', this.getContractAddress()],
['createtime', nowTime],
['modifytime', nowTime],
];
const {err} = await this.bcNftDbConn(
'upsert',
't_nft',
[
['token_id', tokenId],
['net_id', this.getNetId()],
['contract_address', this.getContractAddress()],
],
[
],
fieldList
);
console.log(this.getNetId(), this.getContractAddress());
if (err) {
this.throwError('mintNft transId:' + transId);
}
}
async updateGameDbInfo(transId, itemUniId, itemId, tokenId, tokenType) {
const tblName = bchelper.getNftTableName(tokenType);
if (!tblName) {
this.throwError('error token_type:' + tokenType);
return;
}
const {err} = await this.gameDbConn(
'update',
tblName,
[
['idx', itemUniId],
],
[
['token_id', tokenId],
['!account_id', () => {
return 'null';
}],
]
);
if (err) {
this.throwError('updateGameDbInfo transId:' + transId);
}
}
}
module.exports = Activate721Nft;