This commit is contained in:
aozhiwei 2023-06-13 19:10:13 +08:00
parent 3d6d31e088
commit 2d24984588
2 changed files with 43 additions and 12 deletions

View File

@ -10,8 +10,8 @@ const C = require("./C");
class BlockChain { class BlockChain {
constructor() { constructor(netId) {
this.netId = 0; this.netId = netId;
} }
async initInstance(user, address, jsonUrl) { async initInstance(user, address, jsonUrl) {
@ -24,40 +24,65 @@ class BlockChain {
} }
async init() { async init() {
this.web3 = new Web3(metaFactory.getWeb3Conf()['block_server']); this.web3Conf = metaFactory.getWeb3Conf(this.netId);
this.contractsConf = metaFactory.getContractsConf(this.netId);
this.web3 = new Web3(this.getRpcUrl());
this.web3.eth.handleRevert = true; this.web3.eth.handleRevert = true;
this.web3.eth.accounts.wallet.add(metaFactory.getPrivateKey()); this.web3.eth.accounts.wallet.add(this.getPrivateKey());
for (const data of metaFactory.getContracts()) { for (const data of this.contractsConf) {
this[`${data.name}Instance`] = await this.initInstance this[`${data.name}Instance`] = await this.initInstance
(metaFactory.getUserAddress(), data.address, data.json); (this.getUserAddress(), data.address, data.json);
} }
event.emitEvent(C.BC_INITIALIZED_EVENT); event.emitEvent(C.BC_INITIALIZED_EVENT);
} }
async getNetId() { getNetId() {
return await this.web3.eth.getChainId(); return this.netId;
}
getRpcUrl() {
return this.web3Conf['block_server'];
}
getUserAddress() {
return this.web3Conf['user_address'];
}
getPrivateKey() {
return this.web3Conf['private_key'];
}
getContractByName(name) {
let contract = null;
this.contractsConf.forEach((item) => {
if (item['name'] == name) {
contract = item;
}
});
return contract;
} }
getNftAddress(tokenType) { getNftAddress(tokenType) {
switch (Number(tokenType)) { switch (Number(tokenType)) {
case bcutils.HERO_TYPE: case bcutils.HERO_TYPE:
{ {
return metaFactory.getContractByName('HERO')['address']; return this.getContractByName('HERO')['address'];
} }
break; break;
case bcutils.EQUIP_TYPE: case bcutils.EQUIP_TYPE:
{ {
return metaFactory.getContractByName('WEAPON')['address']; return this.getContractByName('WEAPON')['address'];
} }
break; break;
case bcutils.CHIP_TYPE: case bcutils.CHIP_TYPE:
{ {
return metaFactory.getContractByName('CHIP')['address']; return this.getContractByName('CHIP')['address'];
} }
break; break;
case bcutils.FRAGMENT_TYPE: case bcutils.FRAGMENT_TYPE:
{ {
return metaFactory.getContractByName('shard')['address']; return this.getContractByName('shard')['address'];
} }
break; break;
default: default:

View File

@ -146,8 +146,14 @@ function traverseMetaList(name, cb) {
} }
} }
function getWeb3Conf(netId) {
}
exports.init = init; exports.init = init;
exports.getMetaByKey = getMetaByKey; exports.getMetaByKey = getMetaByKey;
exports.traverseMetaList = traverseMetaList; exports.traverseMetaList = traverseMetaList;
exports.callMetaStatic = callMetaStatic; exports.callMetaStatic = callMetaStatic;
exports.getWeb3Conf = getWeb3Conf;