Skip to content
This repository has been archived by the owner on Sep 6, 2023. It is now read-only.

Commit

Permalink
update web3-provider and minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzwfu committed Aug 10, 2023
1 parent 96452c3 commit 76784f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
33 changes: 14 additions & 19 deletions packages/connectors/src/venly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<VenlyProviderOptions, 'reloadOnDisconnect'> & {
Expand Down Expand Up @@ -48,17 +49,20 @@ export class VenlyConnector extends Connector<VenlyProvider, Options> {

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' })

const account = await this.getAccount()
// 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
Expand All @@ -83,6 +87,7 @@ export class VenlyConnector extends Connector<VenlyProvider, Options> {
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)
Expand All @@ -91,8 +96,7 @@ export class VenlyConnector extends Connector<VenlyProvider, Options> {
}

async getAccount() {
const provider = await this.getProvider()
const accounts = await provider.request({
const accounts = await this.provider.request({
method: 'eth_accounts',
})
// return checksum address
Expand All @@ -106,17 +110,13 @@ export class VenlyConnector extends Connector<VenlyProvider, Options> {
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<WalletClient> {
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({
Expand Down Expand Up @@ -147,11 +147,6 @@ export class VenlyConnector extends Connector<VenlyProvider, Options> {
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,
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 76784f6

Please sign in to comment.