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 {
constructor() {
this.netId = 0;
constructor(netId) {
this.netId = netId;
}
async initInstance(user, address, jsonUrl) {
@ -24,40 +24,65 @@ class BlockChain {
}
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.accounts.wallet.add(metaFactory.getPrivateKey());
for (const data of metaFactory.getContracts()) {
this.web3.eth.accounts.wallet.add(this.getPrivateKey());
for (const data of this.contractsConf) {
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);
}
async getNetId() {
return await this.web3.eth.getChainId();
getNetId() {
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) {
switch (Number(tokenType)) {
case bcutils.HERO_TYPE:
{
return metaFactory.getContractByName('HERO')['address'];
return this.getContractByName('HERO')['address'];
}
break;
case bcutils.EQUIP_TYPE:
{
return metaFactory.getContractByName('WEAPON')['address'];
return this.getContractByName('WEAPON')['address'];
}
break;
case bcutils.CHIP_TYPE:
{
return metaFactory.getContractByName('CHIP')['address'];
return this.getContractByName('CHIP')['address'];
}
break;
case bcutils.FRAGMENT_TYPE:
{
return metaFactory.getContractByName('shard')['address'];
return this.getContractByName('shard')['address'];
}
break;
default:

View File

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