diff --git a/src/wallet/index.js b/src/wallet/index.js index 98e1a11..108aecf 100644 --- a/src/wallet/index.js +++ b/src/wallet/index.js @@ -1,4 +1,5 @@ import Web3 from 'web3'; +import { pollTillDefinedAndReturnIntervalId } from 'web3-utils'; import { toHexChainId } from '@/utils/utils'; import { loginNonce, loginWithSignature } from '@/utils/webapi.js'; @@ -323,7 +324,9 @@ export class Wallet { return web3.eth.sendTransaction(sendObj) } get web3() { - return new Web3(this.provider); + let web3 = new Web3(this.provider); + web3.eth.transactionConfirmationBlocks = 6 + return web3 } initInstance(web3, address, abi, user, ) { @@ -363,11 +366,9 @@ export class Wallet { } let web3 = this.web3; const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address); - let gasPrice = await this.fetchGasPrice() // console.log('chainCheckIn gasPrice: ', gasPrice) let gasLimit = await this.estimateGas(address, '0xd8edeb1b'); - console.log('gasLimit: ', gasLimit, 'gasPrice:', gasPrice) - return instance.methods.dailyCheckin().send({ from: address, gasPrice, gasLimit }); + return this.toChain(instance.methods.dailyCheckin(), address, gasLimit) } async fetchCheckInStatus(address) { @@ -384,6 +385,55 @@ export class Wallet { return false } + async fetchTransactionReceipt(hash) { + const params = [hash] + return requestChain(chainData.rpc, 'eth_getTransactionReceipt', params) + } + + async toChain(method, address, gasLimit) { + let gasPrice = await this.fetchGasPrice(); + console.log('gasLimit: ', gasLimit, 'gasPrice:', gasPrice) + let returned = false; + let self = this + return new Promise((resolve, reject) => { + method.send({ from: address, gasPrice, gasLimit }) + .on('transactionHash', function (hash) { + console.log('transactionHash: ', hash); + let tryCount = 0 + let intervalId = setInterval(async function() { + try { + tryCount ++; + let res = await self.fetchTransactionReceipt(hash) + console.log('fetchTransactionReceipt: ', res.result) + if (res && res.result && !returned) { + clearInterval(intervalId) + returned = true + resolve && resolve(res.result) + } else if (tryCount > 20 || returned) { + clearInterval(intervalId) + } + } catch (err) { + console.log('fetchTransactionReceipt error: ', err) + } + }, 1000) + }) + // .on('confirmation', function (confirmationNumber, receipt) { + // console.log('confirmation: ', confirmationNumber, receipt); + // }) + .on('receipt', function (receipt) { + console.log('receipt: ', receipt); + if (!returned) { + returned = true + resolve && resolve(receipt); + } + }) + .on('error', function (error, receipt) { + console.log('error: ', error, receipt); + reject && reject(error); + }); + }); + } + async chainExplore(address, exploreId) { let checkResult = await this.fetchExploreStatus(address, exploreId) if (checkResult) { @@ -392,12 +442,10 @@ export class Wallet { let web3 = this.web3; let idBN = web3.utils.toBigInt('0x'+exploreId) const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address); - let gasPrice = await this.fetchGasPrice() // let gasLimit = await instance.methods.explore(idBN).estimateGas({ from: address }); const dataStr = '0x6457e389' + exploreId.padStart(64, '0'); const gasLimit = await this.estimateGas(address, dataStr) - console.log('gasLimit: ', gasLimit, 'gasPrice:', gasPrice) - return instance.methods.explore(idBN).send({ from: address, gasPrice, gasLimit }); + return this.toChain(instance.methods.explore(idBN), address, gasLimit) } async fetchExploreStatus(address, exploreId) { @@ -424,12 +472,10 @@ export class Wallet { .join(""); let codeBn = web3.utils.toBigInt('0x'+codeHex) const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address); - let gasPrice = await this.fetchGasPrice() // let gasLimit = await instance.methods.enhanceBox(codeBn).estimateGas({ from: address }); const dataStr = '0x44a17e06' + codeHex.padStart(64, '0'); const gasLimit = await this.estimateGas(address, dataStr) - console.log('gasLimit: ', gasLimit, 'gasPrice:', gasPrice) - return instance.methods.enhanceBox(codeBn).send({ from: address, gasPrice, gasLimit }); + return this.toChain(instance.methods.enhanceBox(codeBn), address, gasLimit) } async fetchEnhanceStatus(address, shareCode) { @@ -458,12 +504,10 @@ export class Wallet { let boxIdBN = web3.utils.toBigInt('0x'+boxId) const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address); - let gasPrice = await this.fetchGasPrice() // let gasLimit = await instance.methods.openBox(boxIdBN).estimateGas({ from: address }); const dataStr = '0xb1e5e2b7' + boxId.padStart(64, '0'); const gasLimit = await this.estimateGas(address, dataStr) - console.log('gasLimit: ', gasLimit, 'gasPrice:', gasPrice) - return instance.methods.openBox(boxIdBN).send({ from: address, gasPrice, gasLimit }); + return this.toChain(instance.methods.openBox(boxIdBN), address, gasLimit) } async fetchOpenBoxtatus(address, boxId) { @@ -490,12 +534,10 @@ export class Wallet { .join(""); let taskIdBN = web3.utils.toBigInt('0x'+taskIdHex) const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address); - let gasPrice = await this.fetchGasPrice() // let gasLimit = await instance.methods.claimTaskReward(taskIdBN).estimateGas({ from: address }); const dataStr = '0x4052a9c7' + taskIdHex.padStart(64, '0'); const gasLimit = await this.estimateGas(address, dataStr) - console.log('gasLimit: ', gasLimit, 'gasPrice:', gasPrice) - return instance.methods.claimTaskReward(taskIdBN).send({ from: address, gasPrice, gasLimit }); + return this.toChain(instance.methods.claimTaskReward(taskIdBN), address, gasLimit) } async fetchClaimStatus(address, taskId) {