修改上链请求, 上链前先获取链上状态, 不在保存本地状态
This commit is contained in:
parent
aec6d87ddb
commit
ecba478f32
@ -50,29 +50,22 @@ export const sendToChain = async (type, address, val) => {
|
|||||||
if (!chainMethods[type]) {
|
if (!chainMethods[type]) {
|
||||||
throw new Error('Invalid chain method')
|
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){
|
if (store.state.wallet.chainId+'' !== process.env.VUE_APP_CHAIN_ID){
|
||||||
await new Wallet().changeChain()
|
await new Wallet().changeChain()
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let chainRes = await new Wallet()[chainMethods[type]](address, val)
|
let chainRes = await new Wallet()[chainMethods[type]](address, val)
|
||||||
if (!chainRes?.transactionHash) {
|
if (!chainRes?.transactionHash) {
|
||||||
throw new Error('Failed to claim task')
|
throw new Error('Failed to claim task')
|
||||||
}
|
}
|
||||||
localStorage.setItem(storeageKey, chainRes.transactionHash)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (JSON.stringify(err).indexOf('already') != -1) {
|
if (JSON.stringify(err).indexOf('already') === -1 && err.message.indexOf('already') === -1) {
|
||||||
localStorage.setItem(storeageKey, 'already')
|
|
||||||
} else {
|
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return storeageKey
|
return 'no_need'
|
||||||
}
|
}
|
||||||
|
|
||||||
// 探索
|
// 探索
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
import Web3 from 'web3';
|
import Web3 from 'web3';
|
||||||
|
|
||||||
import { toHexChainId } from '@/utils/utils';
|
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 { SiweMessage } from '@/utils/siwe.js';
|
||||||
import { GlobalData } from '@/utils/GlobalData.js';
|
import { GlobalData } from '@/utils/GlobalData.js';
|
||||||
import { parseTokenData } from '@/utils/utils.js';
|
|
||||||
import { setToken } from '@/utils/cookies.js';
|
import { setToken } from '@/utils/cookies.js';
|
||||||
import store from '@/store'
|
import store from '@/store'
|
||||||
import { treasureAbi } from './abi.js'
|
import { treasureAbi } from './abi.js'
|
||||||
@ -13,7 +12,6 @@ import { treasureAbi } from './abi.js'
|
|||||||
// Wallet Connect初始化
|
// Wallet Connect初始化
|
||||||
import { EthereumProvider } from '@walletconnect/ethereum-provider';
|
import { EthereumProvider } from '@walletconnect/ethereum-provider';
|
||||||
|
|
||||||
const targetAddress = process.env.VUE_APP_SCRIPTION_ADDRESS
|
|
||||||
|
|
||||||
const chains = [
|
const chains = [
|
||||||
{
|
{
|
||||||
@ -57,6 +55,36 @@ const connectWc = async () => {
|
|||||||
await wc.connect()
|
await wc.connect()
|
||||||
return wc;
|
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 {
|
export class Wallet {
|
||||||
constructor() {
|
constructor() {
|
||||||
if (Wallet.instance) {
|
if (Wallet.instance) {
|
||||||
@ -214,7 +242,7 @@ export class Wallet {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Subscribe to session disconnection
|
// Subscribe to session disconnection
|
||||||
this.provider.on('disconnect', (err) => {
|
this.provider.on('disconnect', () => {
|
||||||
// console.log('disconnect', err)
|
// console.log('disconnect', err)
|
||||||
store.commit('wallet/updateConnected', false)
|
store.commit('wallet/updateConnected', false)
|
||||||
})
|
})
|
||||||
@ -304,6 +332,10 @@ export class Wallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async chainCheckIn(address) {
|
async chainCheckIn(address) {
|
||||||
|
let checkResult = await this.fetchCheckInStatus(address)
|
||||||
|
if (checkResult) {
|
||||||
|
throw new Error('already checkin')
|
||||||
|
}
|
||||||
let web3 = this.web3;
|
let web3 = this.web3;
|
||||||
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
||||||
let gasPrice = await web3.eth.getGasPrice()
|
let gasPrice = await web3.eth.getGasPrice()
|
||||||
@ -314,13 +346,24 @@ export class Wallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetchCheckInStatus(address) {
|
async fetchCheckInStatus(address) {
|
||||||
let web3 = this.web3;
|
const days = ((Date.now() / 1000 / 60 / 60 / 24) | 0)
|
||||||
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
const valStr = days.toString(16).padStart(64, '0')
|
||||||
let days = (Date.now() / 1000 / 60 / 60 / 24) | 0;
|
const addressStr = address.replace('0x', '').padStart(64, '0')
|
||||||
return instance.methods.checkinHistory(address, days).call({ from: address });
|
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) {
|
async chainExplore(address, exploreId) {
|
||||||
|
let checkResult = await this.fetchExploreStatus(address, exploreId)
|
||||||
|
if (checkResult) {
|
||||||
|
throw new Error('already explored')
|
||||||
|
}
|
||||||
let web3 = this.web3;
|
let web3 = this.web3;
|
||||||
let idBN = web3.utils.toBigInt('0x'+exploreId)
|
let idBN = web3.utils.toBigInt('0x'+exploreId)
|
||||||
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
||||||
@ -331,13 +374,23 @@ export class Wallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetchExploreStatus(address, exploreId) {
|
async fetchExploreStatus(address, exploreId) {
|
||||||
let web3 = this.web3;
|
const valStr = exploreId.toString().padStart(64, '0')
|
||||||
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
const addressStr = address.replace('0x', '').padStart(64, '0')
|
||||||
let idBN = web3.utils.toBigInt('0x'+exploreId)
|
const method = '36028275'
|
||||||
return instance.methods.exploreHistory(address, idBN).call({ from: address });
|
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) {
|
async enhanceBox(address, shareCode) {
|
||||||
|
let checkResult = await this.fetchEnhanceStatus(address, shareCode)
|
||||||
|
if (checkResult) {
|
||||||
|
throw new Error('already enhanced')
|
||||||
|
}
|
||||||
let web3 = this.web3;
|
let web3 = this.web3;
|
||||||
const codeHex = shareCode.split("")
|
const codeHex = shareCode.split("")
|
||||||
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
|
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
|
||||||
@ -351,19 +404,31 @@ export class Wallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetchEnhanceStatus(address, shareCode) {
|
async fetchEnhanceStatus(address, shareCode) {
|
||||||
let web3 = this.web3;
|
const shareCodeHex = shareCode
|
||||||
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
.split('')
|
||||||
const codeHex = shareCode.split("")
|
.map(c => c.charCodeAt(0).toString(16).padStart(2, '0'))
|
||||||
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
|
.join('')
|
||||||
.join("");
|
const valStr = shareCodeHex.padStart(64, '0')
|
||||||
let codeBn = web3.utils.toBigInt('0x'+codeHex)
|
const addressStr = address.replace('0x', '').padStart(64, '0')
|
||||||
return instance.methods.enhanceHistory(address, codeBn).call({ from: address });
|
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) {
|
async chainOpenBox(address, boxId) {
|
||||||
|
let checkResult = await this.fetchOpenBoxtatus(address, boxId)
|
||||||
|
if (checkResult) {
|
||||||
|
throw new Error('already opened')
|
||||||
|
}
|
||||||
let web3 = this.web3;
|
let web3 = this.web3;
|
||||||
let boxIdBN = web3.utils.toBigInt('0x'+boxId)
|
let boxIdBN = web3.utils.toBigInt('0x'+boxId)
|
||||||
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
||||||
|
|
||||||
let gasPrice = await web3.eth.getGasPrice()
|
let gasPrice = await web3.eth.getGasPrice()
|
||||||
gasPrice = gasPrice * 2n
|
gasPrice = gasPrice * 2n
|
||||||
let gasLimit = await instance.methods.openBox(boxIdBN).estimateGas({ from: address });
|
let gasLimit = await instance.methods.openBox(boxIdBN).estimateGas({ from: address });
|
||||||
@ -371,13 +436,23 @@ export class Wallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetchOpenBoxtatus(address, boxId) {
|
async fetchOpenBoxtatus(address, boxId) {
|
||||||
let web3 = this.web3;
|
const valStr = boxId.padStart(64, '0')
|
||||||
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
const addressStr = address.replace('0x', '').padStart(64, '0')
|
||||||
let boxIdBN = web3.utils.toBigInt('0x'+boxId)
|
const method = 'd869bb29'
|
||||||
return instance.methods.openBoxHistory(address, boxIdBN).call({ from: address });
|
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) {
|
async chainClaimTask(address, taskId) {
|
||||||
|
let checkResult = await this.fetchClaimStatus(address, taskId)
|
||||||
|
if (checkResult) {
|
||||||
|
throw new Error('already claimed')
|
||||||
|
}
|
||||||
let web3 = this.web3;
|
let web3 = this.web3;
|
||||||
const taskIdHex = taskId.split("")
|
const taskIdHex = taskId.split("")
|
||||||
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
|
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
|
||||||
@ -391,12 +466,19 @@ export class Wallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fetchClaimStatus(address, taskId) {
|
async fetchClaimStatus(address, taskId) {
|
||||||
let web3 = this.web3;
|
const taskIdHex = taskId
|
||||||
const instance = this.initInstance(web3, process.env.VUE_APP_CONTRACT, treasureAbi, address);
|
.split('')
|
||||||
const taskIdHex = taskId.split("")
|
.map(c => c.charCodeAt(0).toString(16).padStart(2, '0'))
|
||||||
.map(c => c.charCodeAt(0).toString(16).padStart(2, "0"))
|
.join('')
|
||||||
.join("");
|
const valStr = taskIdHex.padStart(64, '0')
|
||||||
let taskIdBN = web3.utils.toBigInt('0x'+taskIdHex)
|
const addressStr = address.replace('0x', '').padStart(64, '0')
|
||||||
return instance.methods.claimTaskHistory(address, taskIdBN).call();
|
const method = '4902f7e0'
|
||||||
|
let res = await fetchChainStatus(address, `0x${method}${addressStr}${valStr}`)
|
||||||
|
if (!res.error) {
|
||||||
|
if (parseInt(res.result) > 0) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user