Skip to content

Commit

Permalink
feat: auto-instanciate provider in priority with connected wallet, el…
Browse files Browse the repository at this point in the history
…se with others
  • Loading branch information
0Lilian committed Sep 4, 2022
1 parent 61ee43a commit 3b85069
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
33 changes: 26 additions & 7 deletions src/composables/proxies/provider/placeholder.js
Expand Up @@ -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;
}
}
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/composables/wallets/eip-1193.js
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 3b85069

Please sign in to comment.