From 76784f68d41156ddb0d515e20edd6408923ef3bc Mon Sep 17 00:00:00 2001 From: David Fu Date: Thu, 10 Aug 2023 11:54:35 +0400 Subject: [PATCH] update web3-provider and minor fixes --- packages/connectors/package.json | 2 +- packages/connectors/src/venly.ts | 33 ++++++++++++++------------------ pnpm-lock.yaml | 14 +++++++------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/packages/connectors/package.json b/packages/connectors/package.json index d819aa17..2d0920ad 100644 --- a/packages/connectors/package.json +++ b/packages/connectors/package.json @@ -25,7 +25,7 @@ "@ledgerhq/connect-kit-loader": "^1.1.0", "@safe-global/safe-apps-provider": "^0.17.1", "@safe-global/safe-apps-sdk": "^8.0.0", - "@venly/web3-provider": "^3.1.0", + "@venly/web3-provider": "^3.2.3", "@walletconnect/ethereum-provider": "2.9.2", "@walletconnect/utils": "2.9.2", "@walletconnect/legacy-provider": "^2.0.0", diff --git a/packages/connectors/src/venly.ts b/packages/connectors/src/venly.ts index 2009c249..9de735a6 100644 --- a/packages/connectors/src/venly.ts +++ b/packages/connectors/src/venly.ts @@ -11,6 +11,7 @@ import { import { Connector } from './base' import { ChainNotConfiguredForConnectorError } from './errors' +import type { WalletClient } from './types' import { normalizeChainId } from './utils/normalizeChainId' type Options = Omit & { @@ -48,10 +49,12 @@ export class VenlyConnector extends Connector { async connect({ chainId }: { chainId?: number } = {}) { try { - const provider = await this.getProvider() - provider.on('accountsChanged', this.onAccountsChanged) - provider.on('chainChanged', this.onChainChanged) - provider.on('disconnect', this.onDisconnect) + if (!this.provider) + this.provider = await this.client.createProvider(this.options) + + this.provider.on('accountsChanged', this.onAccountsChanged) + this.provider.on('chainChanged', this.onChainChanged) + this.provider.on('disconnect', this.onDisconnect) this.emit('message', { type: 'connecting' }) @@ -59,6 +62,7 @@ export class VenlyConnector extends Connector { // Switch to chain if provided let id = await this.getChainId() let unsupported = this.isChainUnsupported(id) + chainId = chainId ?? this.chains[0]?.id if (chainId && id !== chainId) { const chain = await this.switchChain(chainId) id = chain.id @@ -83,6 +87,7 @@ export class VenlyConnector extends Connector { async disconnect() { if (!this.provider) return + await this.client.logout() this.provider.removeListener('accountsChanged', this.onAccountsChanged) this.provider.removeListener('chainChanged', this.onChainChanged) this.provider.removeListener('disconnect', this.onDisconnect) @@ -91,8 +96,7 @@ export class VenlyConnector extends Connector { } async getAccount() { - const provider = await this.getProvider() - const accounts = await provider.request({ + const accounts = await this.provider.request({ method: 'eth_accounts', }) // return checksum address @@ -106,17 +110,13 @@ export class VenlyConnector extends Connector { return normalizeChainId(chainId) } - async getProvider() { - if (!this.provider) - this.provider = await this.client.createProvider(this.options) + getProvider() { return this.provider } - async getWalletClient({ chainId }: { chainId?: number } = {}) { - const [provider, account] = await Promise.all([ - this.getProvider(), - this.getAccount(), - ]) + async getWalletClient({ chainId }: { chainId?: number } = {}): Promise { + const provider = this.getProvider() + const account = await this.getAccount() const chain = this.chains.find((x) => x.id === chainId) if (!provider) throw new Error('provider is required.') return createWalletClient({ @@ -147,11 +147,6 @@ export class VenlyConnector extends Connector { method: 'wallet_switchEthereumChain', params: [{ chainId: id }], }) - this.provider = this.client._provider - this.provider.on('accountsChanged', this.onAccountsChanged) - this.provider.on('chainChanged', this.onChainChanged) - this.provider.on('disconnect', this.onDisconnect) - return ( this.chains.find((x) => x.id === chainId) ?? { id: chainId, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8c2b6e4f..436d56e9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -96,8 +96,8 @@ importers: specifier: ^8.0.0 version: 8.0.0(typescript@5.0.4) '@venly/web3-provider': - specifier: ^3.1.0 - version: 3.1.0 + specifier: ^3.2.3 + version: 3.2.3 '@walletconnect/ethereum-provider': specifier: 2.9.2 version: 2.9.2(@walletconnect/modal@2.6.1)(lokijs@1.5.12) @@ -1082,21 +1082,21 @@ packages: '@types/node': 18.11.18 dev: false - /@venly/connect@2.2.0: - resolution: {integrity: sha512-fSG+loJ7q6Oy8FCIPPIba3z6Ss6TEJ4ZRGDSoQw7Od0JCaSPPJ1MOA9/4MplVIbj3eCqR/FS11tq0K1jpQZavQ==} + /@venly/connect@2.2.1: + resolution: {integrity: sha512-4U5TFtTuwsA2stK2Q6D0p0WyfLy6CFp1z+XsiilBTRZ4JKgHuLkhumeDm2UW0r46Fl92h0TAWepGWlFLfb0TqQ==} dependencies: '@types/node': 12.20.55 es6-promise: 4.2.8 keycloak-js: 11.0.2 dev: false - /@venly/web3-provider@3.1.0: - resolution: {integrity: sha512-yPZJPGDTiwEq1YnEU2gL1NxjteQPFRGWeCKuvYd7sy3EH9fUcohwhkJbVuKipL5eGpAW5j4AyABjpkb79XZcxQ==} + /@venly/web3-provider@3.2.3: + resolution: {integrity: sha512-/poFuMlznWXAn9LlrSqtBCDAnmKHtt/NhibKSO8LAh5M3pUpAaIvjRHQtekoFLvxnjUExM/cccmw00yCB8JUFw==} dependencies: '@ethersproject/bignumber': 5.7.0 '@metamask/eth-json-rpc-middleware': 11.0.1 '@metamask/eth-json-rpc-provider': 1.0.0 - '@venly/connect': 2.2.0 + '@venly/connect': 2.2.1 eth-block-tracker: 7.1.0 eth-json-rpc-filters: 6.0.0 json-rpc-engine: 6.1.0