From 750d56f118f8b0a99465d7363d2697638e5b0759 Mon Sep 17 00:00:00 2001 From: cebgcontract <99630598+cebgcontract@users.noreply.github.com> Date: Mon, 7 Mar 2022 13:46:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=99=BB=E5=BD=95=E6=B5=81?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/core/ChainModal.vue | 28 ++++++---- src/utils/ChainManager.ts | 7 +-- src/utils/blockchain.ts | 87 ++++++++++++++++++++---------- 3 files changed, 82 insertions(+), 40 deletions(-) diff --git a/src/components/core/ChainModal.vue b/src/components/core/ChainModal.vue index c89acc7..f45c927 100644 --- a/src/components/core/ChainModal.vue +++ b/src/components/core/ChainModal.vue @@ -1,6 +1,6 @@ diff --git a/src/utils/ChainManager.ts b/src/utils/ChainManager.ts index a87257f..8ddd480 100644 --- a/src/utils/ChainManager.ts +++ b/src/utils/ChainManager.ts @@ -4,20 +4,20 @@ import { getNonce } from '@/api/User' import { AppModule } from '@/store/modules/app' import { UserModule } from '@/store/modules/user' import { Message } from 'element-ui' -import { EventBus, NEED_NONCE } from '@/utils/event-bus' @singleton export default class ChainManager { bc: BlockChain constructor() { this.bc = new BlockChain() - EventBus.$on(NEED_NONCE, this.checkNance.bind(this)) + // EventBus.$on(NEED_NONCE, this.checkNance.bind(this)) } public async init() { if (this.bc.isWalletConnect) { try { await this.bc.connect() + await this.getNance() } catch (err) { console.log('connect chain error: ', err) } @@ -33,9 +33,10 @@ export default class ChainManager { } public async login() { - if (!AppModule.accountId) { + if (!AppModule.step) { try { await this.bc.connect(true) + await this.checkNance() } catch (err) { Message({ message: err.message, diff --git a/src/utils/blockchain.ts b/src/utils/blockchain.ts index 2e18e33..556724c 100644 --- a/src/utils/blockchain.ts +++ b/src/utils/blockchain.ts @@ -5,18 +5,17 @@ import Web3 from 'web3' import { ERC20ABI, MALL_ADDRESS } from '@/configs/config_chain' import { ACCOUNT_CHANGE, - CHAIN_SELECTED, EventBus, NEED_CHANGE_CHAIN, NEED_LOGIN, NEED_NONCE, - SHOW_CHAIN_MODAL, - WALLET_SELECTED + SHOW_CHAIN_MODAL } from '@/utils/event-bus' import { UserModule } from '@/store/modules/user' import { chains, IChainData } from '@/configs/chains' import { isMobile } from '@/utils/resize' import { hasMetamask } from '@/utils/chain.util' import { AllChains } from '@/configs/allchain' +import { MessageBox } from 'element-ui' const EIP721_DOMAIN_DATA = [ { name: 'name', type: 'string' }, @@ -51,8 +50,6 @@ export class BlockChain { this.coinInstanceMap = new Map() // AppModule.updateChainID(chainId) EventBus.$on(NEED_LOGIN, this.connect.bind(this)) - EventBus.$on(WALLET_SELECTED, this.walletSelected.bind(this)) - EventBus.$on(CHAIN_SELECTED, this.chainSelected.bind(this)) } loadJson(url: string) { @@ -80,11 +77,6 @@ export class BlockChain { } } - public async walletSelected(type: number) { - this.walletType = type - await this.connectWallet(true) - } - public async connectWallet(isManual: boolean) { console.log('begin connect to wallet: ', this.walletType, this.currentChain) if (this.walletType === 1) { @@ -96,16 +88,15 @@ export class BlockChain { return } this.web3 = new Web3(this.provider) - this.subscribeToEvents() const chainId = await this.web3.eth.getChainId() - if (!this.chainMap.has(chainId)) { - EventBus.$emit(NEED_CHANGE_CHAIN) - return - } + await this.checkChain(chainId) + + this.subscribeToEvents() const accounts = await this.web3.eth.getAccounts() if (accounts && accounts.length > 0) { AppModule.updateAccount(accounts[0]) } + if (!this.currentChain) this.currentChain = chainId this.saveProvider() AppModule.updateChainID(chainId) AppModule.updateWalletStatus(true) @@ -119,24 +110,68 @@ export class BlockChain { return { account: accounts[0], chainId } } + private async checkChain(chainId: number) { + if (!this.chainMap.has(chainId)) { + if (this.walletType === 1) { + await this.selectChain() + } else if (this.walletType === 2) { + await this.disconnect() + this.walletType = 2 + await MessageBox.alert('You need to connect to supported network', 'Wrong Network', { + confirmButtonText: 'Confirm' + }) + await this.connectWallet(true) + } + } + } + public async connect(isManual = false) { if (isMobile()) { this.walletType = 2 } else { if (hasMetamask()) { if (isManual && !this.walletType) { - EventBus.$emit(SHOW_CHAIN_MODAL) - return + this.walletType = await this.selectWallet() } } else { this.walletType = 2 } } - if (this.isWalletConnect) { - await this.connectWallet(false) - } else { - EventBus.$emit(NEED_CHANGE_CHAIN) - } + if (isManual || this.isWalletConnect) { await this.connectWallet(isManual) } + } + + private selectWallet(): Promise { + return new Promise((resolve, reject) => { + EventBus.$emit(SHOW_CHAIN_MODAL, { + confirm: (id: number) => { + console.log('select wallet: ', id) + resolve && resolve(id) + }, + cancel: (reason: any) => { + console.log('cancel select wallet: ', reason) + reject && reject(reason) + } + }) + }) + } + + private selectChain(): Promise { + return new Promise((resolve, reject) => { + EventBus.$emit(NEED_CHANGE_CHAIN, { + confirm: async(id: number) => { + console.log('select chain: ', id) + this.currentChain = id + if (this.provider) { + await this.switchEthereumChain() + } + resolve && resolve(id) + }, + cancel: (reason: any) => { + console.log('cancel select chain: ', reason) + reject && reject(reason) + } + }) + }) } public async connectMetaMask() { @@ -161,7 +196,6 @@ export class BlockChain { public async connectWalletConnect() { const provider = new WalletConnectProvider({ infuraId: process.env.VUE_APP_WALLET_INFURAID, - chainId: 97, rpc: this.rpc }) try { @@ -244,8 +278,7 @@ export class BlockChain { if (accounts && accounts.length > 0) { if (AppModule.accountId !== accounts[0]) { await UserModule.LogOut() - AppModule.updateAccount(accounts[0]) - EventBus.$emit(ACCOUNT_CHANGE) + location.reload() } } }) @@ -254,9 +287,7 @@ export class BlockChain { this.provider.on('chainChanged', async(chainId: string) => { console.log('chainChanged', chainId) const chainIdNum = parseInt(chainId) - if (chainIdNum !== AppModule.chainId) { - EventBus.$emit(NEED_CHANGE_CHAIN) - } + await this.checkChain(chainIdNum) }) // Subscribe to session disconnection