From 3b85069135a7df553bb163015a7d98e5cd3566c2 Mon Sep 17 00:00:00 2001 From: 0Lilian Date: Sun, 4 Sep 2022 18:26:49 +0200 Subject: [PATCH] feat: auto-instanciate provider in priority with connected wallet, else with others --- .../proxies/provider/placeholder.js | 33 +++++++++++++++---- src/composables/wallets/eip-1193.js | 1 - 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/composables/proxies/provider/placeholder.js b/src/composables/proxies/provider/placeholder.js index 99f75507..82fa88fa 100644 --- a/src/composables/proxies/provider/placeholder.js +++ b/src/composables/proxies/provider/placeholder.js @@ -24,11 +24,30 @@ export class TulipeProviderPlaceholder extends TulipePlaceholder { this.OnSafe = OnProviderSafe; } - _autoInstantiateFromWallet () { + async _autoInstantiateFromWallet () { + + // Find if any wallet is already connected in order to retrieve in priority + // the provider exposed by this wallet. + let connectedWallet = null; for (const wallet of Object.values(dapp.wallets)) { - const provider = wallet.getProvider() - if (provider) { - this.proxy.ethersInstance = provider; + if (wallet.lazyConnectAvailable) { + if (await wallet.isConnected()) { + connectedWallet = wallet; + break; + } + } + } + if (connectedWallet) { + this.proxy.ethersInstance = connectedWallet.getProvider(); + } + + // Else search if any wallet expose a + else { + for (const wallet of Object.values(dapp.wallets)) { + const provider = wallet.getProvider() + if (provider) { + this.proxy.ethersInstance = provider; + } } } } @@ -40,9 +59,9 @@ export class TulipeProviderPlaceholder extends TulipePlaceholder { } } - _autoInstantiate () { + async _autoInstantiate () { // Try to auto-instantiate a Provider instance from informations exposed by wallets - this._autoInstantiateFromWallet(); + await this._autoInstantiateFromWallet(); // If ethersInstance is still null, try to auto-instantiate default network in configs. if (!this.proxy.ethersInstance) { @@ -117,7 +136,7 @@ export class TulipeProviderPlaceholder extends TulipePlaceholder { // If ethersInstance is not given during instantiation, try to automatically // create an ethersInstance from informations given by wallets and DApp configs if (!this.proxy.ethersInstance) { - this._autoInstantiate(); + await this._autoInstantiate(); } // If ethersInstance is still null, set status to DISCONNECTED diff --git a/src/composables/wallets/eip-1193.js b/src/composables/wallets/eip-1193.js index 3c5a870e..00327984 100644 --- a/src/composables/wallets/eip-1193.js +++ b/src/composables/wallets/eip-1193.js @@ -17,7 +17,6 @@ export class Eip1193Wallet extends Wallet { try { const accounts = await this.exposedObject.request({ method: 'eth_accounts' }) if (accounts && accounts.length > 0) { - console.log(accounts) return true; } }