diff --git a/src/utils/chainapi.js b/src/utils/chainapi.js index 8d3ae44..d963a64 100644 --- a/src/utils/chainapi.js +++ b/src/utils/chainapi.js @@ -50,29 +50,22 @@ export const sendToChain = async (type, address, val) => { if (!chainMethods[type]) { throw new Error('Invalid chain method') } - const storeageKey = `${address}_${type}_${val || 'default'}` - if (localStorage.getItem(storeageKey)) { - return - } + 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 { + if (JSON.stringify(err).indexOf('already') === -1 && err.message.indexOf('already') === -1) { throw err } } - return storeageKey + return 'no_need' } // 探索 diff --git a/src/wallet/index.js b/src/wallet/index.js index 42f65fb..8e13d2f 100644 --- a/src/wallet/index.js +++ b/src/wallet/index.js @@ -1,11 +1,10 @@ import Web3 from 'web3'; import { toHexChainId } from '@/utils/utils'; -import { loginNonce, hexToBase58, aesEncrypt, loginWithSignature } from '@/utils/webapi.js'; +import { loginNonce, loginWithSignature } from '@/utils/webapi.js'; import { SiweMessage } from '@/utils/siwe.js'; import { GlobalData } from '@/utils/GlobalData.js'; -import { parseTokenData } from '@/utils/utils.js'; import { setToken } from '@/utils/cookies.js'; import store from '@/store' import { treasureAbi } from './abi.js' @@ -13,7 +12,6 @@ import { treasureAbi } from './abi.js' // Wallet Connect初始化 import { EthereumProvider } from '@walletconnect/ethereum-provider'; -const targetAddress = process.env.VUE_APP_SCRIPTION_ADDRESS const chains = [ { @@ -57,6 +55,36 @@ const connectWc = async () => { await wc.connect() return wc; }; + +const requestChain = async (rpc, method, params) => { + const data = { + id: Date.now(), + jsonrpc: '2.0', + method, + params, + } + const options = { + method: 'POST', + headers: { + 'Content-Type': 'application/json; charset=utf-8', + }, + body: JSON.stringify(data), + } + return fetch(rpc, options).then(res => res.json()) +} + +export const fetchChainStatus = async (address, data) => { + console.log(data) + const params = [ + { + data: data, + from: address, + to: process.env.VUE_APP_CONTRACT, + }, + 'latest', + ] + return requestChain(chainData.rpc, 'eth_call', params) +} export class Wallet { constructor() { if (Wallet.instance) { @@ -214,7 +242,7 @@ export class Wallet { }) // Subscribe to session disconnection - this.provider.on('disconnect', (err) => { + this.provider.on('disconnect', () => { // console.log('disconnect', err) store.commit('wallet/updateConnected', false) }) @@ -304,6 +332,10 @@ export class Wallet { } async chainCheckIn(address) { + let checkResult = await this.fetchCheckInStatus(address) + if (checkResult) { + throw new Error('already checkin') + } let web3 = this.web3; const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address); let gasPrice = await web3.eth.getGasPrice() @@ -314,13 +346,24 @@ export class Wallet { } 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 }); + const days = ((Date.now() / 1000 / 60 / 60 / 24) | 0) + const valStr = days.toString(16).padStart(64, '0') + const addressStr = address.replace('0x', '').padStart(64, '0') + const method = '86cd4926' + let res = await fetchChainStatus(address, `0x${method}${addressStr}${valStr}`) + if (!res.error) { + if (parseInt(res.result) > 0) { + return true + } + } + return false } async chainExplore(address, exploreId) { + let checkResult = await this.fetchExploreStatus(address, exploreId) + if (checkResult) { + throw new Error('already explored') + } let web3 = this.web3; let idBN = web3.utils.toBigInt('0x'+exploreId) const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address); @@ -331,13 +374,23 @@ export class Wallet { } 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 }); + const valStr = exploreId.toString().padStart(64, '0') + const addressStr = address.replace('0x', '').padStart(64, '0') + const method = '36028275' + let res = await fetchChainStatus(address, `0x${method}${addressStr}${valStr}`) + if (!res.error) { + if (parseInt(res.result) > 0) { + return true + } + } + return false } async enhanceBox(address, shareCode) { + let checkResult = await this.fetchEnhanceStatus(address, shareCode) + if (checkResult) { + throw new Error('already enhanced') + } let web3 = this.web3; const codeHex = shareCode.split("") .map(c => c.charCodeAt(0).toString(16).padStart(2, "0")) @@ -351,19 +404,31 @@ export class Wallet { } 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 }); + const shareCodeHex = shareCode + .split('') + .map(c => c.charCodeAt(0).toString(16).padStart(2, '0')) + .join('') + const valStr = shareCodeHex.padStart(64, '0') + const addressStr = address.replace('0x', '').padStart(64, '0') + const method = '9b68ea4c' + let res = await fetchChainStatus(address, `0x${method}${addressStr}${valStr}`) + if (!res.error) { + if (parseInt(res.result) > 0) { + return true + } + } + return false } async chainOpenBox(address, boxId) { + let checkResult = await this.fetchOpenBoxtatus(address, boxId) + if (checkResult) { + throw new Error('already opened') + } let web3 = this.web3; let boxIdBN = web3.utils.toBigInt('0x'+boxId) 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.openBox(boxIdBN).estimateGas({ from: address }); @@ -371,13 +436,23 @@ export class Wallet { } 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 }); + const valStr = boxId.padStart(64, '0') + const addressStr = address.replace('0x', '').padStart(64, '0') + const method = 'd869bb29' + let res = await fetchChainStatus(address, `0x${method}${addressStr}${valStr}`) + if (!res.error) { + if (parseInt(res.result) > 0) { + return true + } + } + return false } async chainClaimTask(address, taskId) { + let checkResult = await this.fetchClaimStatus(address, taskId) + if (checkResult) { + throw new Error('already claimed') + } let web3 = this.web3; const taskIdHex = taskId.split("") .map(c => c.charCodeAt(0).toString(16).padStart(2, "0")) @@ -391,12 +466,19 @@ export class Wallet { } 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(); + const taskIdHex = taskId + .split('') + .map(c => c.charCodeAt(0).toString(16).padStart(2, '0')) + .join('') + const valStr = taskIdHex.padStart(64, '0') + const addressStr = address.replace('0x', '').padStart(64, '0') + const method = '4902f7e0' + let res = await fetchChainStatus(address, `0x${method}${addressStr}${valStr}`) + if (!res.error) { + if (parseInt(res.result) > 0) { + return true + } + } + return false } }