This commit is contained in:
aozhiwei 2023-07-07 15:42:58 +08:00
parent de1c856ca3
commit b61f866682

View File

@ -0,0 +1,169 @@
const log = require('j7/log');
const app = require('j7/app');
const bcutils = require('j7/bcutils');
const utils = require('j7/utils');
const j7event = require('j7/event');
const metaFactory = require('../../metadata/factory');
const C = require('../../C');
const gameapi = require('../gameapi');
/*
操作成功的事件
event TokenMinted(
address indexed to,
uint256 indexed nonce,
uint256 startTime,
uint256[] ids
)
*/
class Activate721Nft {
async start(instance, conn, event) {
console.log('Activate721Nft', event);
const blockNumber = event['blockNumber'];
const returnValues = event['returnValues'];
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(instance, 'activate721nft error transId:' + transId);
return;
}
const itemUniId = params[0];
const itemId = params[1];
console.log(params);
await this.mintNft(
instance,
conn,
transId,
bcutils.toNormalAddress(to),
blockNumber,
tokenId,
itemUniId,
itemId);
await gameapi.confirmTransactionDb(this.instance, transId);
}
async mintNft(instance, conn, transId, owner, blockNumber, tokenId, itemUniId, itemId) {
const itemMeta = metaFactory.getMetaByKey('Item', itemId);
if (!itemMeta) {
this.throwError(instance, 'activate721nft error transId:1');
return;
}
const tokenType = itemMeta.getNftType();
if (tokenType == bcutils.NONE_TYPE) {
this.throwError(instance, 'activate721nft error transId:2');
return;
}
await this.updateGameDbInfo(instance, conn, transId, itemUniId, itemId, tokenId, tokenType);
const {err, row} = await conn.ormSelectOne(
't_nft',
[
['token_id', tokenId],
]
);
if (err) {
log.error('processEvent:' + err);
throw 'processEvent:' + err;
}
if (!row) {
const nowTime = utils.getUtcTime();
const fieldList = [
['token_id', tokenId],
['token_type', tokenType],
['item_id', itemId],
['game_id', 2006],
['owner_address', bcutils.toNormalAddress(owner)],
['creator_address', bcutils.toNormalAddress(owner)],
['confirm_block_number', blockNumber],
['createtime', nowTime],
['modifytime', nowTime],
];
const {err} = await conn.insert(
't_nft',
fieldList
);
if (err) {
log.error('processEvent:' + err);
throw 'processEvent:' + err;
}
try {
j7event.emitEvent(C.CREATE_EXEC_CONFIRM_OWNER_EVENT, tokenId);
} catch (err) {
log.warning('processEvent:' + err);
}
}
}
async updateGameDbInfo(instance, marketDbConn, transId, itemUniId, itemId, tokenId, tokenType) {
const {err, conn} = await app.getDbConn('GameDb0');
if (err) {
this.throwError(instance, 'gamedb connection err:' + err);
return;
}
try {
try {
let tblName = '';
switch (tokenType) {
case bcutils.HERO_TYPE:
{
tblName = 't_hero';
}
break;
case bcutils.EQUIP_TYPE:
{
tblName = 't_gun';
}
break;
case bcutils.CHIP_TYPE:
{
tblName = 't_chip';
}
break;
default:
{
}
break;
}
if (!tblName) {
this.throwError(instance, 'error token_type:' + tokenType);
return;
}
await conn.update(
tblName,
[
['idx', itemUniId],
],
[
['token_id', tokenId],
['!account_id', () => {
return 'null';
}],
]
);
} finally{
conn.release();
}
} catch (e) {
this.throwError(instance, 'updateGameDbInfo err:' + e);
return;
}
}
throwError(instance, err) {
const errMsg = 'processActivate721NftEvent:' + utils.jsonEncode(instance) + ' err:' + err;
throw errMsg;
}
}
module.exports = Activate721Nft;