1
This commit is contained in:
parent
56b7845137
commit
32b3751834
@ -4,7 +4,6 @@ const config = require('j7/config');
|
||||
|
||||
event.addListener(event.APP_INITIALIZED_EVENT, async () => {
|
||||
require('./metadata/factory').init();
|
||||
await require('./blockchain').init();
|
||||
require('./middlewares/factory').init();
|
||||
require('./controllers/factory').init();
|
||||
require('./models/factory').init();
|
||||
|
@ -1,70 +0,0 @@
|
||||
const util = require('util');
|
||||
const Web3 = require('web3');
|
||||
const utils = require('j7/utils');
|
||||
const event = require('j7/event');
|
||||
const sync = require("j7/sync");
|
||||
const log = require("j7/log");
|
||||
const metaFactory = require('./metadata/factory');
|
||||
const constant = require("./constant");
|
||||
|
||||
class BlockChain {
|
||||
|
||||
constructor() {
|
||||
this.currBlockNumber = 0;
|
||||
this.refreshCond = new sync.Cond();
|
||||
this.lastRefreshTime = utils.getUtcTime();
|
||||
this.actived = false;
|
||||
}
|
||||
|
||||
async initInstance(user, address, jsonUrl) {
|
||||
const json = utils.readJsonFromFile(jsonUrl);
|
||||
return new this.web3.eth.Contract(
|
||||
json.abi,
|
||||
address,
|
||||
{ from: user }
|
||||
);
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.web3 = new Web3(metaFactory.getWeb3Conf()['block_server']);
|
||||
this.web3.eth.accounts.wallet.add(metaFactory.getPrivateKey());
|
||||
for (const data of metaFactory.getContracts()) {
|
||||
this[`${data.name}Instance`] = await this.initInstance
|
||||
(metaFactory.getUserAddress(), data.address, data.json);
|
||||
}
|
||||
setTimeout(this.refreshBlockNumber.bind(this), 1000 * 0.01);
|
||||
await this.mustBeActive();
|
||||
event.emitEvent(constant.BC_INITIALIZED_EVENT);
|
||||
}
|
||||
|
||||
async mustBeActive() {
|
||||
while (!this.actived) {
|
||||
await utils.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
async refreshBlockNumber() {
|
||||
const logClass = 'refreshBlockNumber';
|
||||
while (true) {
|
||||
try {
|
||||
this.currBlockNumber = await this.web3.eth.getBlockNumber();
|
||||
this.actived = true;
|
||||
this.lastRefreshTime = utils.getUtcTime();
|
||||
} catch (e) {
|
||||
this.actived = false;
|
||||
log.warning(util.format('%s err:%s',
|
||||
logClass,
|
||||
e
|
||||
));
|
||||
}
|
||||
await this.refreshCond.wait(1000 * 3);
|
||||
}
|
||||
}
|
||||
|
||||
getCurrBlockNumber() {
|
||||
return this.currBlockNumber;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = new BlockChain();
|
@ -1,8 +0,0 @@
|
||||
const utils = require('j7/utils');
|
||||
const basewrap = require('./basewrap');
|
||||
|
||||
class Contract extends basewrap.BaseWrap {
|
||||
|
||||
}
|
||||
|
||||
module.exports = Contract;
|
@ -1,8 +0,0 @@
|
||||
const utils = require('j7/utils');
|
||||
const basewrap = require('./basewrap');
|
||||
|
||||
class MarketDb extends basewrap.BaseWrap {
|
||||
|
||||
}
|
||||
|
||||
module.exports = MarketDb;
|
@ -1,8 +0,0 @@
|
||||
const utils = require('j7/utils');
|
||||
const basewrap = require('./basewrap');
|
||||
|
||||
class Web3 extends basewrap.BaseWrap {
|
||||
|
||||
}
|
||||
|
||||
module.exports = Web3;
|
@ -75,31 +75,10 @@ function init() {
|
||||
}
|
||||
console.log(configDir, utils.getArgv('env'));
|
||||
|
||||
let resDir = './res/';
|
||||
if (utils.isOnlineEnv()) {
|
||||
resDir = '../res/';
|
||||
} else if (utils.getArgv('env') == 'dev'){
|
||||
resDir = './res_dev/';
|
||||
}
|
||||
resDir = '/data/conf/webadmin2006/';
|
||||
|
||||
registerMetaClass(configDir + 'config.json',
|
||||
'',
|
||||
'Config'
|
||||
);
|
||||
registerMetaClass(configDir + 'web3.json',
|
||||
'',
|
||||
'Web3'
|
||||
);
|
||||
registerMetaClass(configDir + 'contract.json',
|
||||
'',
|
||||
'Contract'
|
||||
);
|
||||
|
||||
registerMetaClass(configDir + 'marketdb_mysql.json',
|
||||
'',
|
||||
'MarketDb'
|
||||
);
|
||||
load();
|
||||
{
|
||||
traverseMetaList('MarketDb', (dbConf, idx) => {
|
||||
@ -138,10 +117,6 @@ function traverseMetaList(name, cb) {
|
||||
}
|
||||
}
|
||||
|
||||
function getWeb3Conf() {
|
||||
return getMetaByKey('Web3', '0');
|
||||
}
|
||||
|
||||
function getContracts() {
|
||||
return getMetaList('Contract');
|
||||
}
|
||||
@ -160,23 +135,8 @@ function getContractByAddress(address) {
|
||||
return contract;
|
||||
}
|
||||
|
||||
function getUserAddress() {
|
||||
return getWeb3Conf()['user_address'];
|
||||
}
|
||||
|
||||
function getPrivateKey() {
|
||||
return getWeb3Conf()['private_key'];
|
||||
}
|
||||
|
||||
exports.init = init;
|
||||
|
||||
exports.getMetaByKey = getMetaByKey;
|
||||
exports.traverseMetaList = traverseMetaList;
|
||||
exports.callMetaStatic = callMetaStatic;
|
||||
|
||||
exports.getWeb3Conf = getWeb3Conf;
|
||||
exports.getContracts = getContracts;
|
||||
exports.getContractByName = getContractByName;
|
||||
exports.getContractByAddress = getContractByAddress;
|
||||
exports.getUserAddress = getUserAddress;
|
||||
exports.getPrivateKey = getPrivateKey;
|
||||
|
Loading…
x
Reference in New Issue
Block a user