This commit is contained in:
aozhiwei 2022-04-22 09:40:37 +08:00
parent 1cc1b46ff4
commit 57fea86b5d
5 changed files with 270 additions and 172 deletions

View File

@ -0,0 +1,99 @@
const log = require('j7/log');
const bcutils = require('j7/bcutils');
const utils = require('j7/utils');
class BoxOpenedProcess extends BaseService {
async start(conn, event) {
const {err, row} = await conn.ormSelectOne(
't_boxopened_event',
[
['txhash', event['transactionHash']],
['log_index', event['logIndex']],
]
);
if (err) {
log.error('processEvent:' + err);
throw 'processEvent:' + err;
}
if (!row) {
const blockNumber = event['blockNumber'];
const returnValues = event['returnValues'];
const fieldList = [
['box_token_id', returnValues['boxId']],
['txhash', event['transactionHash']],
['block_number', blockNumber],
['log_index', event['logIndex']],
['_to', bcutils.toNormalAddress(returnValues['to'])],
['raw_data', utils.jsonEncode(event)],
['createtime', utils.getUtcTime()],
['modifytime', utils.getUtcTime()],
];
for (let i = 0; i < 3; ++i) {
const id = returnValues['ids'][i];
const type = returnValues['types'][i];
if (id != '0') {
fieldList.push(
['token_id' + (i + 1), id],
);
fieldList.push(['token_type' + (i + 1), type]),
await this.mintNft(
bcutils.toNormalAddress(returnValues['to']),
blockNumber,
id,
type);
}
}
const {err} = await conn.insert(
't_boxopened_event',
fieldList
);
if (err) {
log.error('processEvent:' + err);
throw 'processEvent:' + err;
}
}
}
async mintNft(owner, blockNumber, 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 itemMeta = metaFactory.getMetaByKey('Item', 110001);
if (!itemMeta) {
return;
}
const nftItemId = itemMeta.randLuckBox(tokenType);
if (nftItemId > 0) {
const nowTime = utils.getUtcTime();
const fieldList = [
['token_id', tokenId],
['token_type', tokenType],
['item_id', nftItemId],
['tags', tokenType == bcutils.HERO_TYPE ? '1' : 0],
['game_id', 2006],
['owner_address', bcutils.toNormalAddress(owner)],
['creator_address', bcutils.toNormalAddress(owner)],
['confirm_block_number', blockNumber],
['createtime', nowTime],
['modifytime', nowTime],
];
await conn.insert(
't_nft',
fieldList
);
}
}
}
}
module.exports = BoxOpenedProcess;

View File

@ -3,14 +3,44 @@ const utils = require('j7/utils');
const bcutils = require('j7/bcutils');
const log = require('j7/log');
const event = require('j7/event');
const serviceFactory = require('./factory');
const BaseService = require('./baseservice');
const factory = require('./factory');
const metaFactory = require('../metadata/factory');
const bc = require('../blockchain');
const C = require('../C');
const LIMIT_COUNT = 100;
class EventCenter {
class EventCenter extends BaseService {
instances = [
{
'name': 'heroInstance',
'last_block_number': 0,
'eventName': 'Transfer',
},
{
'name': 'equipInstance',
'last_block_number': 0,
'eventName': 'Transfer',
},
{
'name': 'chipInstance',
'last_block_number': 0,
'eventName': 'Transfer',
},
{
'name': 'luckboxInstance',
'last_block_number': 0,
'eventName': 'Transfer',
}
];
boxInstance = {
'name': 'boxproxyInstance',
'last_block_number': 0,
'eventName': 'BoxOpened',
};
async init() {
this.lastIdx = 0;
@ -34,14 +64,30 @@ class EventCenter {
});
this.confirmOwner();
this.pullNftTransferEvent();
this.pullBoxOpenedEvent();
await this.initEventProcess();
}
async initEventProcess() {
{
this.instances.forEach(async (item) => {
factory.create('EventProcess', null)
.init(item, async (event) = {
await factory.create('NftTransferProcess', null).start(this.conn, event);
})
});
}
{
factory.create('EventProcess', null)
.init(this.boxInstance, async (event) = {
await factory.create('BoxOpenedProcess', null).start(this.conn, event);
});
}
}
async addConfirmOwnerRequest(tokenId) {
const pendingRequest = utils.getVal(this.pendingConfirmOwnerHash, tokenId);
if (!pendingRequest) {
const newRequest = new serviceFactory.create('ExecConfirmOwner', null);
const newRequest = new factory.create('ExecConfirmOwner', null);
this.pendingConfirmOwnerHash[tokenId] = newRequest;
newRequest.init(tokenId);
return;
@ -86,173 +132,6 @@ class EventCenter {
}
}
async pullNftTransferEvent() {
const instances = [
{
'name': 'heroInstance',
'last_block_number': 0,
},
{
'name': 'equipInstance',
'last_block_number': 0,
},
{
'name': 'chipInstance',
'last_block_number': 0,
},
{
'name': 'luckboxInstance',
'last_block_number': 0,
}
];
while (true) {
for (let i in instances) {
const instance = instances[i];
console.log(instance);
const events = await bc[instance['name']].getPastEvents(
'Transfer',
{
fromBlock: instance['last_block_number'] + 1,
//toBlock: 'lea',
},
);
for (let ii in events) {
const event = events[ii];
if (event['blockNumber'] > instance['last_block_number']) {
instance['last_block_number'] = event['blockNumber']
}
const {err, row} = await this.conn.ormSelectOne(
't_nft_transfer',
[
['txhash', event['transactionHash']],
['log_index', event['logIndex']],
]
);
if (!err) {
if (!row) {
const returnValues = event['returnValues'];
await this.conn.insert(
't_nft_transfer',
[
['token_id', returnValues['tokenId']],
['txhash', event['transactionHash']],
['block_number', event['blockNumber']],
['log_index', event['logIndex']],
['_from', bcutils.toNormalAddress(returnValues['from'])],
['_to', bcutils.toNormalAddress(returnValues['to'])],
['raw_data', utils.jsonEncode(event)],
['createtime', utils.getUtcTime()],
['modifytime', utils.getUtcTime()],
]
);
}
}
}
}//end for
await utils.sleep(2000 + utils.randRange(500, 1500));
}
}
async pullBoxOpenedEvent() {
return;
const instance = {
'name': 'boxproxyInstance',
'last_block_number': 0,
};
while (true) {
const events = await bc[instance['name']].getPastEvents(
'BoxOpened',
{
fromBlock: instance['last_block_number'] + 1,
//toBlock: 'lea',
},
);
for (let ii in events) {
const event = events[ii];
if (event['blockNumber'] > instance['last_block_number']) {
instance['last_block_number'] = event['blockNumber']
}
const {err, row} = await this.conn.ormSelectOne(
't_boxopened_event',
[
['txhash', event['transactionHash']],
['log_index', event['logIndex']],
]
);
if (!err) {
if (!row) {
const blockNumber = event['blockNumber'];
const returnValues = event['returnValues'];
const fieldList = [
['box_token_id', returnValues['boxId']],
['txhash', event['transactionHash']],
['block_number', blockNumber],
['log_index', event['logIndex']],
['_to', bcutils.toNormalAddress(returnValues['to'])],
['raw_data', utils.jsonEncode(event)],
['createtime', utils.getUtcTime()],
['modifytime', utils.getUtcTime()],
];
for (let iii = 0; iii < 3; ++iii) {
const id = returnValues['ids'][iii];
const type = returnValues['types'][iii];
if (id != '0') {
fieldList.push(
['token_id' + (iii + 1), id],
);
fieldList.push(['token_type' + (iii + 1), type]),
await this.mintNft(bcutils.toNormalAddress(returnValues['to']), blockNumber, id, type);
}
}
await this.conn.insert(
't_boxopened_event',
fieldList
);
}
}
}
console.log(events);
await utils.sleep(2000 + utils.randRange(500, 1500));
}
}
async mintNft(owner, blockNumber, tokenId, tokenType) {
const {err, row} = await this.conn.ormSelectOne(
't_nft',
[
['token_id', tokenId],
]
);
if (!err) {
if (!row) {
const itemMeta = metaFactory.getMetaByKey('Item', 110001);
if (!itemMeta) {
return;
}
const nftItemId = itemMeta.randLuckBox(tokenType);
if (nftItemId > 0) {
const nowTime = utils.getUtcTime();
const fieldList = [
['token_id', tokenId],
['token_type', tokenType],
['item_id', nftItemId],
['tags', tokenType == bcutils.HERO_TYPE ? '1' : 0],
['game_id', 2006],
['owner_address', bcutils.toNormalAddress(owner)],
['creator_address', bcutils.toNormalAddress(owner)],
['confirm_block_number', blockNumber],
['createtime', nowTime],
['modifytime', nowTime],
];
await this.conn.insert(
't_nft',
fieldList
);
}
}
}
}
}
module.exports = EventCenter;

View File

@ -0,0 +1,73 @@
const app = require('j7/app');
const utils = require('j7/utils');
const bcutils = require('j7/bcutils');
const log = require('j7/log');
const bc = require('../blockchain');
class EventProcess extends BaseService {
async init(instance, cb) {
this.instance = instance;
this.cb = cb;
}
async start() {
while (true) {
await this.pullEvent();
await utils.sleep(8000 + utils.randRange(500, 1500));
}
}
async pullEvent() {
const logClass = this.getInstanceName() + ' pullEvent:';
while (true) {
try {
const fromBlock = await getFromBlock();
const toBlock = fromBlock + 3000;
const events = await bc[this.getInstanceName()].getPastEvents(
this.instance['eventName'],
{
fromBlock: fromBlock,
toBlock: toBlock,
},
);
await this.processEvents(events, toBlock);
await this.saveLastBlockNumber(toBlock);
return;
} catch (err) {
log.error(logClass + err);
}
}
await utils.sleep(2000 + utils.randRange(500, 1500));
}
getInstanceName() {
return this.instance['name'];
}
async processEvents(events, toBlock) {
this.instance['last_block_number'] = toBlock;
for (let i in events) {
while (true) {
try {
await cb(event);
break;
} catch (err) {
}
await utils.sleep(8000 + utils.randRange(500, 1500));
}
}
}
async getFromBlock() {
}
async saveLastBlockNumber(blockNumber) {
}
}
module.exports = EventProcess;

View File

@ -15,6 +15,9 @@ function init() {
add(['Present'], 'present');
add(['EventCenter'], 'event_center');
add(['ExecConfirmOwner'], 'exec_confirm_owner');
add(['EventProcess'], 'event_process');
add(['BoxOpenedProcess'], 'boxopened_process');
add(['NftTransferProcess'], 'nft_transfer_process');
create('Present', null).init();
create('EventCenter', null).init();
}

View File

@ -0,0 +1,44 @@
const log = require('j7/log');
const bcutils = require('j7/bcutils');
const utils = require('j7/utils');
class NftTransferProcess extends BaseService {
async start(conn, event) {
const {err, row} = await conn.ormSelectOne(
't_nft_transfer',
[
['txhash', event['transactionHash']],
['log_index', event['logIndex']],
]
);
if (err) {
log.error('processEvent:' + err);
throw 'processEvent:' + err;
}
if (!row) {
const returnValues = event['returnValues'];
const {err} = await conn.insert(
't_nft_transfer',
[
['token_id', returnValues['tokenId']],
['txhash', event['transactionHash']],
['block_number', event['blockNumber']],
['log_index', event['logIndex']],
['_from', bcutils.toNormalAddress(returnValues['from'])],
['_to', bcutils.toNormalAddress(returnValues['to'])],
['raw_data', utils.jsonEncode(event)],
['createtime', utils.getUtcTime()],
['modifytime', utils.getUtcTime()],
]
);
if (err) {
log.error('processEvent:' + err);
throw 'processEvent:' + err;
}
}
};
}
module.exports = NftTransferProcess;