diff --git a/src/chain/ChainManager.ts b/src/chain/ChainManager.ts index 3be4411..9093c97 100644 --- a/src/chain/ChainManager.ts +++ b/src/chain/ChainManager.ts @@ -7,6 +7,7 @@ import { Message } from 'element-ui' import { Chain } from '@/chain/Chain' import { AVAILABLE_CHAINS, IChainData } from '@/configs/config_chain' import { AllChains } from '@/configs/allchain' +import { parsePrice } from '@/utils/chain.util' @singleton export default class ChainManager { @@ -136,4 +137,28 @@ export default class ChainManager { console.log('balance: ', balance) return balance } + + // 转账 + public async transferToAccount({ to, amount, chainId, address } : { + to: string + amount: number + chainId: number + address: string}) { + const self = this + if (chainId !== this.bc.currentChain) { + return new Promise((resolve, reject) => { + this.bc.switchEthereumChain(chainId, function() { + self.bc.transferToAccount(to, amount, address) + .then(res => { + resolve && resolve(res) + }) + .catch(err => { + reject && reject(err) + }) + }) + }) + } else { + return this.bc.transferToAccount(to, amount, address) + } + } } diff --git a/src/chain/blockchain.ts b/src/chain/blockchain.ts index 9a7487d..98e9d70 100644 --- a/src/chain/blockchain.ts +++ b/src/chain/blockchain.ts @@ -58,7 +58,11 @@ export class Blockchain { } public get hexChainId() { - return '0x' + this.currentChain.toString(16) + return this.toHexChainId(this.currentChain) + } + + public toHexChainId(chainId: number) { + return '0x' + chainId.toString(16) } public async chainSelected(id: number) { @@ -213,13 +217,11 @@ export class Blockchain { } public async getCoinInstance(address: string) { - if (this.coinInstanceMap.has(address)) { - return this.coinInstanceMap.get(address) - } else { + if (!this.coinInstanceMap.has(address)) { const coinInstance = await this.initInstance({ abi: ERC20ABI, address, account: AppModule.accountId }) this.coinInstanceMap.set(address, coinInstance) - return coinInstance } + return this.coinInstanceMap.get(address) } public clearCachedProvider() { @@ -285,6 +287,8 @@ export class Blockchain { console.log('chainChanged', chainId) const chainIdNum = parseInt(chainId) await this.checkChain(chainIdNum) + this.currentChain = chainIdNum + this.saveProvider() }) // Subscribe to session disconnection @@ -293,26 +297,35 @@ export class Blockchain { }) } - async switchEthereumChain() { + async switchEthereumChain(chainId?: number, cb?: () => void) { + chainId = chainId || this.currentChain + const hexChainId = this.toHexChainId(chainId) + const onChainChange = (chainId: string) => { + console.log('switchEthereumChain: ', chainId) + this.provider.removeListener('chainChanged', onChainChange) + cb && cb() + } + this.provider.on('chainChanged', onChainChange) try { await this.provider.request({ method: 'wallet_switchEthereumChain', - params: [{ chainId: this.hexChainId }] + params: [{ chainId: hexChainId }] }) + console.log('switch chain success') } catch (e: any) { console.log('switch chain error: ', e) if (e.code === 4902) { try { - const data = this.chainMap.get(this.currentChain)! + const data = this.chainMap.get(chainId)! await this.provider.request({ method: 'wallet_addEthereumChain', params: [ { - chainId: this.hexChainId, + chainId: hexChainId, chainName: data.name, nativeCurrency: { name: data.symbol, - symbol: data.symbol, + symbol: data.symbol || 18, decimals: data.decimals }, blockExplorerUrls: [data.explorerurl], @@ -320,8 +333,10 @@ export class Blockchain { } ] }) + console.log('add chain success') } catch (addError) { console.error('add chain error: ', addError) + this.provider.removeListener('chainChanged', onChainChange) } } // console.error(e) @@ -382,10 +397,19 @@ export class Blockchain { */ public async getBalance(address: string, account: string | null) { account = account || AppModule.accountId - const coinInstance: any = this.getCoinInstance(address) + const coinInstance: any = await this.getCoinInstance(address) return await coinInstance.methods.balanceOf(account).call() } + // 转账 + public async transferToAccount(account: string, amount: number, address: string) { + const amountBN = this.web3.utils.toBN(this.web3.utils.toWei(amount + '')) + const coinInstance: any = await this.getCoinInstance(address) + return coinInstance.methods + .transfer(account, amountBN) + .send({ gas: 1000000 }) + } + public async signData(signObj: any, signer: string) { const msgParams = JSON.stringify(signObj) const from = signer diff --git a/src/components/market/wallet/CoinCard.vue b/src/components/market/wallet/CoinCard.vue index 43c1eb2..1eda424 100644 --- a/src/components/market/wallet/CoinCard.vue +++ b/src/components/market/wallet/CoinCard.vue @@ -12,7 +12,7 @@ alt="coin" class="topRight">
-