修改上链流程, 将计算gas消耗改为本地计算

This commit is contained in:
CounterFire2023 2024-04-28 16:17:42 +08:00
parent f371007b8b
commit 30e1fb6b6a

View File

@ -331,6 +331,22 @@ export class Wallet {
return new web3.eth.Contract(abi, address, { from: user });
}
async estimateGas(address, data) {
const params = [
{
data,
from: address,
to: process.env.VUE_APP_CONTRACT,
},
'latest',
]
let res = await requestChain(chainData.rpc, 'eth_estimateGas', params)
if (res.error) {
throw new Error(res.error.message)
}
return res.result
}
async chainCheckIn(address) {
let checkResult = await this.fetchCheckInStatus(address)
if (checkResult) {
@ -341,7 +357,8 @@ export class Wallet {
let gasPrice = await web3.eth.getGasPrice()
// console.log('chainCheckIn gasPrice: ', gasPrice)
gasPrice = gasPrice * 2n
let gasLimit = await instance.methods.dailyCheckin().estimateGas();
let gasLimit = await this.estimateGas(address, '0xd8edeb1b');
console.log('gasLimit: ', gasLimit, 'gasPrice:', gasPrice)
return instance.methods.dailyCheckin().send({ from: address, gasPrice, gasLimit });
}
@ -369,7 +386,10 @@ export class Wallet {
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
let gasPrice = await web3.eth.getGasPrice()
gasPrice = gasPrice * 2n
let gasLimit = await instance.methods.explore(idBN).estimateGas({ from: address });
// 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 });
}
@ -399,7 +419,10 @@ export class Wallet {
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
let gasPrice = await web3.eth.getGasPrice()
gasPrice = gasPrice * 2n
let gasLimit = await instance.methods.enhanceBox(codeBn).estimateGas({ from: address });
// 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 });
}
@ -431,7 +454,10 @@ export class Wallet {
let gasPrice = await web3.eth.getGasPrice()
gasPrice = gasPrice * 2n
let gasLimit = await instance.methods.openBox(boxIdBN).estimateGas({ from: address });
// 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 });
}
@ -461,7 +487,10 @@ export class Wallet {
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
let gasPrice = await web3.eth.getGasPrice()
gasPrice = gasPrice * 2n
let gasLimit = await instance.methods.claimTaskReward(taskIdBN).estimateGas({ from: address });
// 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 });
}