Skip to content

Commit

Permalink
Merge pull request #71 from XDeFi-tech/fix/connect-all-chains-at-once
Browse files Browse the repository at this point in the history
fix: [XDEFI-4530] request accounts from connected chains at once
  • Loading branch information
Amurmurmur committed Oct 2, 2023
2 parents 2c9d00f + e9cee89 commit c75a352
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xdefi/wallets-connector",
"version": "2.0.3",
"version": "2.1.0",
"description": "Cross chain wallets connector with react hooks",
"author": "garageinc",
"license": "MIT",
Expand Down
55 changes: 29 additions & 26 deletions src/controllers/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,38 @@ export class ProviderController {
if (providerHasChains) {
const cachedChains = this.injectedChains[providerId]
const connectList = cachedChains?.length ? cachedChains : chains
const providerChains = connectList.filter(
(chain) => providerHasChains[chain]
)

for (const chain of connectList) {
const promises = []
for (const chain of providerChains) {
const providerChain = providerHasChains[chain]
if (providerChain) {
try {
const accounts = await providerChain.methods.getAccounts()
if (!accounts.length) {
throw new Error(
`Provider ${providerId} returned empty accounts for chain: ${chain}`
)
}

results.push({
chain: chain as IChainType,
accounts: accounts
})
} catch (e) {
console.error(e)

if (
e.message ===
`Provider ${providerId} returned empty accounts for chain: ${chain}`
) {
throw new Error(e.message)
}
if (e?.code === 4001) {
throw new Error(e.code)
}
promises.push(providerChain.methods.getAccounts())
}

try {
const accounts = await Promise.all(promises)

accounts.forEach((accounts, index) => {
const chain = providerChains[index] as IChainType
if (!accounts.length) {
throw new Error(`Provider is not connected`)
}

results.push({
chain,
accounts
})
})
} catch (e) {
console.error(e)

if (e.message === `Provider is not connected`) {
throw new Error(e.message)
}
if (e?.code === 4001) {
throw new Error(e.code)
}
}
} else {
Expand Down
14 changes: 12 additions & 2 deletions src/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,18 @@ export class WalletsConnector {
}
})

// GarageInc | 3.02.2022, handle for all subscription hooks in react app
setTimeout(() => this.init())
if (document?.readyState === 'complete') {
this.init()
}

const documentStateChange = async (event: Event) => {
if ((event?.target as Document)?.readyState === 'complete') {
this.init()
document?.removeEventListener('readystatechange', documentStateChange)
}
}

document?.addEventListener('readystatechange', documentStateChange)
}

private init() {
Expand Down

0 comments on commit c75a352

Please sign in to comment.