This commit is contained in:
yuyongdong 2024-04-15 01:54:39 +08:00
commit 97a1115fe6
2 changed files with 64 additions and 11 deletions

View File

@ -44,6 +44,8 @@ const chainMethods = {
'check': 'chainCheckIn',
'chest_enhance': 'enhanceBox',
}
export const sendToChain = async (type, address, val) => {
if (!chainMethods[type]) {
throw new Error('Invalid chain method')
@ -55,11 +57,21 @@ export const sendToChain = async (type, address, val) => {
if (store.state.wallet.chainId+'' !== process.env.VUE_APP_CHAIN_ID){
await new Wallet().changeChain()
}
try {
let chainRes = await new Wallet()[chainMethods[type]](address, val)
if (!chainRes?.transactionHash) {
throw new Error('Failed to claim task')
}
localStorage.setItem(storeageKey, chainRes.transactionHash)
} catch (err) {
if (JSON.stringify(err).indexOf('already') != -1) {
localStorage.setItem(storeageKey, 'already')
} else {
throw err
}
}
return storeageKey
}

View File

@ -63,13 +63,13 @@ export class Wallet {
return Wallet.instance;
}
Wallet.instance = this;
if (!this.provider) {
let walletName = localStorage.getItem('walletName');
if (!walletName) {
return;
}
this.init(walletName);
}
// if (!this.provider) {
// let walletName = localStorage.getItem('walletName');
// if (!walletName) {
// return;
// }
// this.init(walletName);
// }
}
async init(walletName) {
this.walletName = walletName;
@ -307,6 +307,13 @@ export class Wallet {
return instance.methods.dailyCheckin().send({ from: address, gasPrice, gasLimit });
}
async fetchCheckInStatus(address) {
let web3 = this.web3;
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
let days = (Date.now() / 1000 / 60 / 60 / 24) | 0;
return instance.methods.checkinHistory(address, days).call({ from: address });
}
async chainExplore(address, exploreId) {
let web3 = this.web3;
let idBN = web3.utils.toBigInt('0x'+exploreId)
@ -317,6 +324,13 @@ export class Wallet {
return instance.methods.explore(idBN).send({ from: address, gasPrice, gasLimit });
}
async fetchExploreStatus(address, exploreId) {
let web3 = this.web3;
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
let idBN = web3.utils.toBigInt('0x'+exploreId)
return instance.methods.exploreHistory(address, idBN).call({ from: address });
}
async enhanceBox(address, shareCode) {
let web3 = this.web3;
const codeHex = shareCode.split("")
@ -330,6 +344,16 @@ export class Wallet {
return instance.methods.enhanceBox(codeBn).send({ from: address, gasPrice, gasLimit });
}
async fetchEnhanceStatus(address, shareCode) {
let web3 = this.web3;
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
const codeHex = shareCode.split("")
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
.join("");
let codeBn = web3.utils.toBigInt('0x'+codeHex)
return instance.methods.enhanceHistory(address, codeBn).call({ from: address });
}
async chainOpenBox(address, boxId) {
let web3 = this.web3;
let boxIdBN = web3.utils.toBigInt('0x'+boxId)
@ -340,6 +364,13 @@ export class Wallet {
return instance.methods.openBox(boxIdBN).send({ from: address, gasPrice, gasLimit });
}
async fetchOpenBoxtatus(address, boxId) {
let web3 = this.web3;
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
let boxIdBN = web3.utils.toBigInt('0x'+boxId)
return instance.methods.openBoxHistory(address, boxIdBN).call({ from: address });
}
async chainClaimTask(address, taskId) {
let web3 = this.web3;
const taskIdHex = taskId.split("")
@ -352,4 +383,14 @@ export class Wallet {
let gasLimit = await instance.methods.claimTaskReward(taskIdBN).estimateGas({ from: address });
return instance.methods.claimTaskReward(taskIdBN).send({ from: address, gasPrice, gasLimit });
}
async fetchClaimStatus(address, taskId) {
let web3 = this.web3;
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
const taskIdHex = taskId.split("")
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
.join("");
let taskIdBN = web3.utils.toBigInt('0x'+taskIdHex)
return instance.methods.claimTaskHistory(address, taskIdBN).call();
}
}