103 lines
4.1 KiB
JavaScript
103 lines
4.1 KiB
JavaScript
export class BlockChain {
|
|
|
|
loadJson(url) {
|
|
return fetch(url)
|
|
.then(response => response.json());
|
|
}
|
|
|
|
async initInstance(user, address, jsonUrl) {
|
|
let json = await this.loadJson(jsonUrl);
|
|
return new web3.eth.Contract(
|
|
json.abi,
|
|
address,
|
|
{ from: user }
|
|
);
|
|
}
|
|
|
|
async init() {
|
|
for (let data of contractInfos) {
|
|
this[`${data.name}Instance`] = await this.initInstance(userAddress, data.address, data.json);
|
|
}
|
|
|
|
for (let data of contractInfos) {
|
|
this[`${data.name}InstanceBuy`] = await this.initInstance(userBuyAddress, data.address, data.json);
|
|
}
|
|
// 监听NFT创建成功Event
|
|
this.factoryInstance.events.TokenMinted()
|
|
.on('data', async function(event) {
|
|
console.log('[BE] receive contract mint event: ', event.returnValues);
|
|
});
|
|
// 监听市场交易成功Event
|
|
this.marketInstance.events.MatchTransaction()
|
|
.on('data', async function(event) {
|
|
console.log('[BE] receive contract trade event: ', event.returnValues);
|
|
});
|
|
|
|
// 监听盒子购买成功Event
|
|
this.mallInstance.events.BEBoxPaid()
|
|
.on('data', async function(event) {
|
|
console.log('[BE] receive box payed event: ', event.returnValues);
|
|
});
|
|
// 监听游戏币转账成功Event
|
|
this.coinInstance.events.Transfer()
|
|
.on('data', async function(event) {
|
|
console.log('[BE] receive Transfer event: ', event.returnValues);
|
|
});
|
|
}
|
|
|
|
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) {
|
|
account = account || userAddress;
|
|
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 buy({seller_address,nft_address,payment_token_address,token_id,price}) {
|
|
let nonce = Math.random()*1000|0;
|
|
let addresses = [seller_address, nft_address, payment_token_address];
|
|
let values = [token_id, price, nonce];
|
|
let signStr = web3.utils.soliditySha3(nft_address, token_id, payment_token_address, price, nonce);
|
|
const { signature } = await web3.eth.accounts.sign(signStr, privateKey);
|
|
// 以上是服务端处理的逻辑, 处理完后, 将相关信息下发到客户端, 客户端发起购买请求
|
|
await this.coinInstanceBuy.methods.increaseAllowance(contractInfos.find(o => o.name ==='market' ).address, price).send({ gas: 1000000 });
|
|
return this.marketInstanceBuy.methods.matchTransaction(addresses, values, signature).send({ gas: 1000000 });
|
|
}
|
|
|
|
// 获取历史事件, 可用此方法处理掉单等的情况
|
|
async getPastEvent(instance, eventName, filter) {
|
|
let events = await instance.getPastEvents(eventName, filter);
|
|
return events.map(o => o.returnValues);
|
|
}
|
|
|
|
// 购买盒子
|
|
async buyBox({boxId, type, price, payment_token_address}) {
|
|
let nonce = Math.random()*1000|0;
|
|
let signStr = web3.utils.soliditySha3(type, payment_token_address, price, nonce);
|
|
const accounts = await this.getMetaMaskAccounts();
|
|
let buyerAddress = accounts[0];
|
|
let signature2 = await web3.eth.sign(signStr, buyerAddress);
|
|
signature2 = signature2.replace(/00$/, '1b').replace(/01$/, '1c');
|
|
await this.coinInstanceBuy.methods.increaseAllowance(contractInfos.find(o => o.name ==='mall' ).address, price).send({ gas: 1000000 });
|
|
// 以上是客户端的逻辑
|
|
// 下面这个方法需要服务端根据客户端提交的信息和签名数据来发起请求
|
|
return await this.mallInstance.methods.buyBoxWithSignature(boxId, type, buyerAddress, price, payment_token_address, nonce, signature2).send({gas: 1000000});
|
|
}
|
|
}
|