diff --git a/.env.development b/.env.development index 7a8cd08..670c99f 100644 --- a/.env.development +++ b/.env.development @@ -1,5 +1,6 @@ # API -VUE_APP_API_URL = 'http://192.168.100.22:3006/api' +# VUE_APP_API_URL = 'http://192.168.100.22:3006/api' +VUE_APP_API_URL = 'http://127.0.0.1:3006/api' VUE_APP_WL_URL = 'https://nftwl.counterfire.games/wl_test' VUE_APP_CONFIG_URL = 'https://sepolia.infura.io/v3/b6bf7d3508c941499b10025c0776eaf8' @@ -10,3 +11,5 @@ VUE_APP_AES_PASS = 'f0cbc08f7afcaa3baeae386860141d072b6840e1' VUE_APP_NFT_URL = '0x59e751c2037B710090035B6ea928e0cce80aC03f' # BEB合约 VUE_APP_BEB_URL = '0x1304E6AA241eE3C9ea44Db9e593e85Ae76eC41F1' + +VUE_APP_CHAIN_ID = '5611' diff --git a/.env.production b/.env.production index af935e9..144a626 100644 --- a/.env.production +++ b/.env.production @@ -10,3 +10,5 @@ VUE_APP_AES_PASS = 'f0cbc08f7afcaa3baeae386860141d072b6840e1' VUE_APP_NFT_URL = '0x59e751c2037B710090035B6ea928e0cce80aC03f' # BEB合约 VUE_APP_BEB_URL = '0x1304E6AA241eE3C9ea44Db9e593e85Ae76eC41F1' + +VUE_APP_CHAIN_ID = '5611' \ No newline at end of file diff --git a/src/components/walletDialog/index.vue b/src/components/walletDialog/index.vue index 2bfaabe..b3434da 100644 --- a/src/components/walletDialog/index.vue +++ b/src/components/walletDialog/index.vue @@ -100,7 +100,7 @@ export default { methods: { // 链接钱包 async getLinkWallet(val) { - await addNetwork(val) + // await addNetwork(val) // return if(localStorage.getItem('walletName')) { let res = await linkWallet(val) diff --git a/src/utils/utils.js b/src/utils/utils.js index f459ea1..a3c791d 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -11,4 +11,13 @@ export const parseTokenData = (token) =>{ } catch (err) { return {}; } +} + +/** + * number to hex string + * @param {number} chainId + * @return {string} + */ +export function toHexChainId(chainId) { + return '0x' + chainId.toString(16) } \ No newline at end of file diff --git a/src/utils/webapi.js b/src/utils/webapi.js index 5cf966d..0128f0a 100644 --- a/src/utils/webapi.js +++ b/src/utils/webapi.js @@ -1,4 +1,4 @@ -const API_BASE = 'http://192.168.100.22:3006' +const API_BASE = process.env.VUE_APP_API_URL.replace('/api', ''); import CryptoJS from 'crypto-js' import axios from 'axios' diff --git a/src/views/home/index.vue b/src/views/home/index.vue index 3373d52..687396b 100644 --- a/src/views/home/index.vue +++ b/src/views/home/index.vue @@ -492,7 +492,7 @@ import LinkBtn from "./../../components/linkButton/linkBtnView.vue"; import WalletDialog from "./../../components/walletDialog/index.vue"; import Web3 from "web3"; import { getToken } from './../../utils/cookies.js' -import { getWalletAddress, isWalletConnected } from "./../../wallet/index.js"; +import { getWalletAddress, isWalletConnected, linkWallet } from "./../../wallet/index.js"; import { apiGameStat, apiBeginActivity, @@ -618,6 +618,10 @@ export default { this.myInviteCount = this.userData.inviteCount this.myEnhanceCount = this.userData.enhanceCount this.myAddress = localStorage.getItem("myAddress") + let walletName = localStorage.getItem("walletName") + if (walletName) { + linkWallet(walletName) + } } // if (this.$route.params.name != "undefined") { // this.helpDialogVisible = true; @@ -722,18 +726,44 @@ export default { // 领取任务奖励 async getTaskClaim(id) { let address = localStorage.getItem("myAddress") + const storeageKey = `${address}_claimTask_${id}` if(await isWalletConnected()) { if(address) { - let walletRes = await sendClaimTask(address, id) - if (walletRes) { - let serTimeId = setInterval(async () => { - let res = await apiTaskClaim(id) - if(res.data.status == 3) { - this.getActivitrStatue(id) - clearInterval(serTimeId) + if (!localStorage.getItem(storeageKey)) { + try { + const chainRes = await sendClaimTask(address, id) + console.log(chainRes) + if (chainRes && chainRes.transactionHash) { + localStorage.setItem(storeageKey, chainRes.transactionHash) } - }, 2000) + } catch (err) { + this.$message.error(`error send chain request`) + return + } } + let serTimeId = setInterval(async () => { + try { + let { errcode, errmsg, data } = await apiTaskClaim(id) + if (errcode) { + if (errcode === 12) { + // 已领取, 给个提示就可以了 + this.$message.error(errmsg) + this.getActivitrStatue(id) + clearInterval(serTimeId) + } + } else { + if(data?.status == 3) { + alert(`领取成功, 获得积分: ${data.score}`) + this.getActivitrStatue(id) + clearInterval(serTimeId) + } + } + } catch (err) { + this.$message.error(`claim task reward error: ${err}`) + } + + }, 3000) + } else { this.walletDialogVisible = true } diff --git a/src/wallet/index.js b/src/wallet/index.js index fb5c8c2..e373183 100644 --- a/src/wallet/index.js +++ b/src/wallet/index.js @@ -1,42 +1,57 @@ import Web3 from 'web3' -import {hexToBase58} from './../utils/webapi.js' + +import { toHexChainId } from '@/utils/utils' // Wallet Connect初始化 -// import { EthereumProvider } from '@walletconnect/ethereum-provider' -// const provider = await EthereumProvider.init({ -// projectId: 'e7743d46923911fa8850619b7a7f6d9d', // required -// chains: ['421614'], // required -// showQrModal: true, // requires @walletconnect/modal -// optionalChains: ['421614'], -// // optionalMethods: ['eth_signTypedData', 'eth_signTypedData_v4'], -// metadata: { -// name: 'Counter Fire', -// description: 'Counter Fire', -// url: 'http://192.168.100.216:8030/', -// icons: ['https://www.counterfire.games/favicon.ico'], -// }, -// }); -// 链接Wallet Connect钱包 -export async function getConnect() { - await provider.connect() - let result = await provider.request({ method: 'eth_requestAccounts' }) - return result +import { EthereumProvider } from '@walletconnect/ethereum-provider' + +const chains = [ + { + name: 'opBNB Testnet', + type: 'Testnet', + rpc: 'https://opbnb-testnet-rpc.bnbchain.org', + id: 5611, + symbol: 'tBNB', + explorerurl: 'https://opbnb-testnet.bscscan.com', + }, + { + name: 'opBNB Mainnet', + type: 'Mainnet', + rpc: 'https://opbnb-rpc.publicnode.com', + id: 204, + symbol: 'BNB', + explorerurl: 'https://mainnet.opbnbscan.com', + }, +] +const chain_define = parseInt(process.env.VUE_APP_CHAIN_ID) +const chainData = chains.find((chain) => chain.id === chain_define) + +if (!chainData) { + throw new Error('chain not supported') } -const chainData = { - name: 'Arbitrum Sepolia', - type: 'Testnet', - rpc: 'https://arb-sepolia.g.alchemy.com/v2/mHoYM0SyjeizxvdjShcdOHiCrXOM_mlg|https://arb-sepolia.g.alchemy.com/v2/EKR1je8ZGia332kkemNc4mtXQuFskIq3', - id: 421614, - network: 'ARB_SEPOLIA', - symbol: 'ETH', - explorerurl: 'https://sepolia.arbiscan.io', +const connectWc = async() => { + const wc = await EthereumProvider.init({ + projectId: 'e7743d46923911fa8850619b7a7f6d9d', // required + chains: [chain_define+''], // required + showQrModal: true, // requires @walletconnect/modal + optionalChains: [chain_define+''], + // optionalMethods: ['eth_signTypedData', 'eth_signTypedData_v4'], + metadata: { + name: 'Counter Fire', + description: 'Counter Fire', + url: 'http://192.168.100.216:8030/', + icons: ['https://www.counterfire.games/favicon.ico'], + }, + }); + return wc } +let provider = null export async function getWalletAddress() { - const web3js = window.ethereum - const web3 = await web3js.request({ + + const web3 = await provider.request({ method: 'eth_accounts' }) if (web3 !== 'undefined') { @@ -46,7 +61,7 @@ export async function getWalletAddress() { return await web3[0] } } -}; +} // 签到 @@ -79,9 +94,9 @@ export const dailyCheckIn = async() => { }; // 开箱子 -export const changeChain = async(provider) => { +export const changeChain = async() => { const chainId = chainData.id - const hexChainId = hexToBase58(chainId) + const hexChainId = toHexChainId(chainId) return new Promise(async (resolve, reject) => { const onChainChange = (chainId) => { console.log('switchEthereumChain: ', chainId) @@ -130,39 +145,24 @@ export const changeChain = async(provider) => { }) }; - // 链接钱包 export const linkWallet = async(name) => { if(name == "ethereum") { - const web3js = window.ethereum - if (typeof web3js !== "undefined") { - let res = await ethereum.request({ method: "eth_requestAccounts" }) - // this.getAddress = res[0] - // localStorage.setItem('walletName', name) - // localStorage.setItem('address', res[0]) - return res[0] - } + provider = window.ethereum } else if(name == "okxwallet") { - let res = await okxwallet.request({ method: "eth_requestAccounts" }); - // this.getAddress = res[0] - // localStorage.setItem('walletName', name) - // localStorage.setItem('address', res[0]) - return res[0] + provider = window.okxwallet } else if(name == "connect") { - let res = await getConnect() - console.log(res[0],'getConnect---------------------------------------') - // this.getAddress = res[0] - // localStorage.setItem('walletName', name) - // localStorage.setItem('address', res[0]) - return res[0] + provider = await connectWc() } - location.reload() + let res = await provider.request({ method: "eth_requestAccounts" }) + return res[0] + // location.reload() }; // 断开链接 export async function disconnectLink(name) { if(name == 'ethereum') { - await window.ethereum.request({ + await provider.request({ "method": "wallet_revokePermissions", "params": [ { @@ -171,7 +171,7 @@ export async function disconnectLink(name) { ] }); } else if(name == 'okxwallet') { - await okxwallet.request({ + await provider.request({ "method": "wallet_disconnect", }) } else if(name == 'connect') { @@ -182,10 +182,10 @@ export async function disconnectLink(name) { // 查看钱包 export async function isWalletConnected() { - const ethereum = window.web3; - if (typeof ethereum !== 'undefined') { + + if (provider) { // let res = await window.ethereum.request({ "method": "eth_accounts" }) - let res = await window.okxwallet.request({ method: "eth_accounts" }); + let res = await provider.request({ method: "eth_accounts" }); if(res[0] == undefined) { return false } else { @@ -198,52 +198,5 @@ export async function isWalletConnected() { // 添加网络 export async function addNetwork(name) { - if(name == 'ethereum') { - // const chainId = await ethereum.request({ method: 'eth_chainId' }) - // console.log(chainId,'------------------') - // return - try{ - let res = await window.ethereum.request({ - method: 'wallet_addEthereumChain', - params: [{ - chainId: '0x15eb', - // chainId: '5611', - chainName: 'opBNB Testnet', - rpcUrls: ['https://opbnb-testnet-rpc.bnbchain.org'], - iconUrls: ['https://xdaichain.com/fake/example/url/xdai.png'], - blockExplorerUrls: ['https://opbnb-testnet.bscscan.com'], - nativeCurrency: { - name: "tBNB", - symbol: "tBNB", - decimals: 18 - }, - },] - }) - // ------------------------------------------------------------------------------------------------------------------------------------ - // let res = await window.ethereum.request({ - // method: 'wallet_addEthereumChain', - // params: [{ - // chainId: '0x38', - // chainName: 'opBNB Testnet', - // rpcUrls: ['https://opbnb-rpc.publicnode.com'], - // iconUrls: ['https://xdaichain.com/fake/example/url/xdai.png'], - // blockExplorerUrls: ['https://mainnet.opbnbscan.com'], - // nativeCurrency: { - // name: "BNB", - // symbol: "BNB", - // decimals: 18 - // }, - // },] - // }) - return await res - } catch(e) { - return false - } - } else if(name == 'okxwallet') { - await okxwallet.request({ - "method": "wallet_disconnect", - }) - } else if(name == 'connect') { - await provider.request({method: 'eth_requestDisconnect'}) - } + changeChain() } \ No newline at end of file