From 380ed17fd910789879c021d7264492ad1839f9d7 Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Mon, 15 Apr 2024 01:53:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8A=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/chainapi.js | 20 ++++++++++++---- src/wallet/index.js | 55 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/utils/chainapi.js b/src/utils/chainapi.js index 015cb9b..8d3ae44 100644 --- a/src/utils/chainapi.js +++ b/src/utils/chainapi.js @@ -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() } - let chainRes = await new Wallet()[chainMethods[type]](address, val) - if (!chainRes?.transactionHash) { - throw new Error('Failed to claim task') + + 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 + } } - localStorage.setItem(storeageKey, chainRes.transactionHash) + return storeageKey } diff --git a/src/wallet/index.js b/src/wallet/index.js index b2e6e6b..6ae374b 100644 --- a/src/wallet/index.js +++ b/src/wallet/index.js @@ -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(); + } }