var Web3 = require('web3'); var metamgr = require('./metamgr'); var utils = require('./utils'); class BlockChain { async initInstance(user, address, jsonUrl) { let json = utils.readJsonFromFile(jsonUrl); return new this.web3.eth.Contract( json.abi, address, { from: user } ); } async init() { this.web3 = new Web3(metamgr.getServerConf()['block_server']); let userAddress = metamgr.getServerConf()['user_address']; let userBuyAddress = metamgr.getServerConf()['user_buy_address']; for (let data of metamgr.getContracts()) { this[`${data.name}Instance`] = await this.initInstance(userAddress, data.address, data.json); } for (let data of metamgr.getContracts()) { this[`${data.name}InstanceBuy`] = await this.initInstance(userBuyAddress, data.address, data.json); } } async mintHero(tokenId) { let gas = await this.factoryInstance.methods.mintHeroTo(userAddress, tokenId).estimateGas({ gas: 1000000 }); console.log(`mintHeroTo estimateGas: ${gas}`); return this.factoryInstance.methods.mintHeroTo(userAddress, tokenId).send({ gas: 1000000 }); } async mintEquip(tokenId) { return this.factoryInstance.methods.mintEquipTo(userAddress, tokenId).send({ gas: 1000000 }); } async mintChip(tokenId) { return this.factoryInstance.methods.mintChipTo(userAddress, tokenId).send({ gas: 1000000 }); } async getBalance(account) { let banlace = await this.coinInstance.methods.balanceOf(account).call(); return banlace; } async transferToAccount(account, amount) { return this.coinInstance.methods.transfer(account, amount).send({gas: 1000000}); } async getPastEvent(instance, eventName, filter) { let events = await instance.getPastEvents(eventName, filter); return events.map(o => o.returnValues); } } var _instance = new BlockChain(); module.exports = _instance;