diff --git a/.env.development b/.env.development index 28e706c..1b7d5b3 100644 --- a/.env.development +++ b/.env.development @@ -1,4 +1,9 @@ VUE_APP_WALLET_INFURAID='e7743d46923911fa8850619b7a7f6d9d' +VUE_APP_BASE_API='http://game2006api-test.kingsome.cn' VUE_APP_CHAIN_ID=1338 VUE_APP_CHAIN_RPC='http://192.168.100.22:8545' -VUE_APP_BASE_API='http://game2006api-test.kingsome.cn' +VUE_APP_CHAIN_NAME='Smart Chain - Testnet' +VUE_APP_CHAIN_EXPLORERURL = 'https://testnet.bscscan.com' +VUE_APP_CHAIN_CURRENCY_NAME='name' +VUE_APP_CHAIN_CURRENCY_SYMBOL='BNB' +VUE_APP_CHAIN_CURRENCY_DECIMALS=18 diff --git a/.env.production b/.env.production index df61874..84565ff 100644 --- a/.env.production +++ b/.env.production @@ -2,3 +2,8 @@ VUE_APP_WALLET_INFURAID='e7743d46923911fa8850619b7a7f6d9d' VUE_APP_CHAIN_ID=97 VUE_APP_CHAIN_RPC='https://data-seed-prebsc-1-s1.binance.org:8545' VUE_APP_BASE_API='https://game2006api.kingsome.cn' +VUE_APP_CHAIN_NAME='Smart Chain - Testnet' +VUE_APP_CHAIN_EXPLORERURL = 'https://testnet.bscscan.com' +VUE_APP_CHAIN_CURRENCY_NAME='name' +VUE_APP_CHAIN_CURRENCY_SYMBOL='BNB' +VUE_APP_CHAIN_CURRENCY_DECIMALS=18 diff --git a/src/components/market/NftInfo.vue b/src/components/market/NftInfo.vue index 066941e..5135090 100644 --- a/src/components/market/NftInfo.vue +++ b/src/components/market/NftInfo.vue @@ -1,14 +1,16 @@ diff --git a/src/store/modules/app.ts b/src/store/modules/app.ts index d90d52d..2aa27e3 100644 --- a/src/store/modules/app.ts +++ b/src/store/modules/app.ts @@ -87,6 +87,10 @@ class App extends VuexModule implements IAppState { this.canBuy = val } + public get hexChainId() { + return '0x' + this.chainId.toString(16) + } + public get accountShow() { if (this.accountId.length >= 10) { return this.accountId.substring(0, 8) + '...' + this.accountId.substring(this.accountId.length - 8) diff --git a/src/utils/blockchain.ts b/src/utils/blockchain.ts index 5e67be7..3018cfd 100644 --- a/src/utils/blockchain.ts +++ b/src/utils/blockchain.ts @@ -3,6 +3,7 @@ import WalletConnectProvider from '@walletconnect/web3-provider' import Web3Modal, { isMobile } from 'web3modal' import { AppModule } from '@/store/modules/app' import Web3 from 'web3' +import { MessageBox } from 'element-ui' @singleton export class BlockChain { @@ -12,6 +13,7 @@ export class BlockChain { constructor() { const chainId = parseInt(process.env.VUE_APP_CHAIN_ID!) + AppModule.updateChainID(chainId) const rpc: any = { } rpc[chainId] = process.env.VUE_APP_CHAIN_RPC! const providerOptions = { @@ -43,14 +45,31 @@ export class BlockChain { return !!this.web3Modal.cachedProvider } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore public async connect() { - // Enable session (triggers QR Code modal) try { this.provider = await this.web3Modal.connect() this.subscribeToEvents() this.web3 = new Web3( this.provider) const accounts = await this.web3.eth.getAccounts() const chainId = await this.web3.eth.getChainId() + if (chainId !== AppModule.chainId) { + try { + await MessageBox.confirm( + 'You need to connect to supported network', + 'Wrong Network', + { + confirmButtonText: 'Switch to BSC network', + cancelButtonText: 'Cancel', + type: 'warning' + } + ) + await this.switchEthereumChain() + } catch (err) { + return this.disconnect() + } + } console.log('chainId: ', chainId) console.log('accountsLogin: ', accounts) if (accounts && accounts.length > 0) { @@ -68,7 +87,6 @@ export class BlockChain { } this.web3Modal.clearCachedProvider() AppModule.updateAccount('') - AppModule.updateChainID(0) AppModule.updateWalletStatus(false) } @@ -92,6 +110,39 @@ export class BlockChain { }) } + async switchEthereumChain() { + try { + await window.ethereum.request({ + method: 'wallet_switchEthereumChain', + params: [{ chainId: AppModule.hexChainId }] + }) + } catch (e: any) { + if (e.code === 4902) { + try { + await window.ethereum.request({ + method: 'wallet_addEthereumChain', + params: [ + { + chainId: AppModule.hexChainId, + chainName: process.env.VUE_APP_CHAIN_NAME, + nativeCurrency: { + name: process.env.VUE_APP_CHAIN_CURRENCY_NAME, + symbol: process.env.VUE_APP_CHAIN_CURRENCY_SYMBOL, + decimals: parseInt(process.env.VUE_APP_CHAIN_CURRENCY_DECIMALS!) + }, + blockExplorerUrls: [process.env.VUE_APP_CHAIN_EXPLORERURL], + rpcUrls: [process.env.VUE_APP_CHAIN_RPC] + } + ] + }) + } catch (addError) { + console.error(addError) + } + } + // console.error(e) + } + } + public async signPresale({ type, paymentTokenAddress, price, buyerAddress }: {type: number paymentTokenAddress: string