Skip to content

Commit

Permalink
fix: typing issues in client
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir committed Dec 19, 2023
1 parent 94adb7f commit 04c3cdf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 156 deletions.
68 changes: 12 additions & 56 deletions packages/wagmi-v2/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type Hex, type Chain } from 'viem' // TODO: THIS DOES NOT COVER 6963
import { type Hex, type Chain } from 'viem'
import { EthereumProvider } from '@walletconnect/ethereum-provider'
import { mainnet } from 'viem/chains'
import {
connect,
Expand Down Expand Up @@ -26,7 +27,6 @@ import type {
} from '@web3modal/scaffold'
import { Web3ModalScaffold } from '@web3modal/scaffold'
import type { Web3ModalSIWEClient } from '@web3modal/siwe'
// import type { EIP6963Connector } from './connectors/EIP6963Connector.js'
import { ConstantsUtil, PresetsUtil, HelpersUtil } from '@web3modal/scaffold-utils'
import { getCaipDefaultChain } from './utils/helpers.js'
import { WALLET_CHOICE_KEY } from './utils/constants.js'
Expand Down Expand Up @@ -57,18 +57,6 @@ interface Web3ModalState extends PublicStateControllerState {
selectedNetworkId: number | undefined
}

// interface Info {
// uuid: string
// name: string
// icon: string
// rdns: string
// }

// interface Wallet {
// info: Info
// provider: EIP1193Provider
// }

// -- Client --------------------------------------------------------------------
export class Web3Modal extends Web3ModalScaffold {
private hasSyncedConnectedAccount = false
Expand Down Expand Up @@ -115,13 +103,15 @@ export class Web3Modal extends Web3ModalScaffold {
'networkControllerClient:getApprovedCaipNetworks - connector is undefined'
)
}
const provider: any = await connector.getProvider()
const provider = (await connector.getProvider()) as Awaited<
ReturnType<(typeof EthereumProvider)['init']>
>
const ns = provider?.signer?.session?.namespaces
const nsMethods = ns?.[ConstantsUtil.EIP155]?.methods
const nsChains = ns?.[ConstantsUtil.EIP155]?.chains
const nsChains = ns?.[ConstantsUtil.EIP155]?.chains as CaipNetworkId[]

return {
supportsAllNetworks: nsMethods?.includes(ConstantsUtil.ADD_CHAIN_METHOD),
supportsAllNetworks: Boolean(nsMethods?.includes(ConstantsUtil.ADD_CHAIN_METHOD)),
approvedCaipNetworkIds: nsChains
}
}
Expand All @@ -142,8 +132,7 @@ export class Web3Modal extends Web3ModalScaffold {
connector.onMessage = event => {
if (event.type === 'display_uri') {
onUri(event.data as string)
// TODO: How to do this?
// connector.removeAllListeners()
// TOD0: How to do this? connector.removeAllListeners()
}
}

Expand Down Expand Up @@ -219,7 +208,6 @@ export class Web3Modal extends Web3ModalScaffold {

this.syncConnectors(wagmiConfig)
this.syncEmailConnector(wagmiConfig)
// this.listenEIP6963Connector(wagmiConfig)
this.listenEmailConnector(wagmiConfig)

watchAccount(this.wagmiConfig, { onChange: this.syncAccount })
Expand Down Expand Up @@ -262,10 +250,11 @@ export class Web3Modal extends Web3ModalScaffold {
}

private async syncAccount({ address, isConnected, chainId }: GetAccountReturnType) {
const chain = this.wagmiConfig.chains.find((chain: Chain) => chain.id === chainId)
const chain = this.wagmiConfig.chains.find((c: Chain) => c.id === chainId)

this.resetAccount()
this.syncNetwork() // TODO: Check with Sven. Now network is synced when acc is synced.
// TOD0: Check with Sven. Now network is synced when acc is synced.
this.syncNetwork()
if (isConnected && address && chain) {
const caipAddress: CaipAddress = `${ConstantsUtil.EIP155}:${chain.id}:${address}`
this.setIsConnected(isConnected)
Expand All @@ -284,10 +273,9 @@ export class Web3Modal extends Web3ModalScaffold {

private async syncNetwork() {
const { address, isConnected, chainId } = getAccount(this.wagmiConfig)
const chain = this.wagmiConfig.chains.find((chain: Chain) => chain.id === chainId)
const chain = this.wagmiConfig.chains.find((c: Chain) => c.id === chainId)

if (chain) {
const chainId = String(chain.id)
const caipChainId: CaipNetworkId = `${ConstantsUtil.EIP155}:${chainId}`
this.setCaipNetwork({
id: caipChainId,
Expand Down Expand Up @@ -381,38 +369,6 @@ export class Web3Modal extends Web3ModalScaffold {
}
}

// private eip6963EventHandler(connector: EIP6963Connector, event: CustomEventInit<Wallet>) {
// if (event.detail) {
// const { info, provider } = event.detail
// const connectors = this.getConnectors()
// const existingConnector = connectors.find(c => c.name === info.name)
// if (!existingConnector) {
// this.addConnector({
// id: ConstantsUtil.EIP6963_CONNECTOR_ID,
// type: 'ANNOUNCED',
// imageUrl:
// info.icon ?? this.options?.connectorImages?.[ConstantsUtil.EIP6963_CONNECTOR_ID],
// name: info.name,
// provider,
// info
// })
// connector.isAuthorized({ info, provider })
// }
// }
// }

// private listenEIP6963Connector(wagmiConfig: Web3ModalClientOptions['wagmiConfig']) {
// const connector = wagmiConfig.connectors.find(
// c => c.id === ConstantsUtil.EIP6963_CONNECTOR_ID
// ) as EIP6963Connector | undefined

// if (typeof window !== 'undefined' && connector) {
// const handler = this.eip6963EventHandler.bind(this, connector)
// window.addEventListener(ConstantsUtil.EIP6963_ANNOUNCE_EVENT, handler)
// window.dispatchEvent(new Event(ConstantsUtil.EIP6963_REQUEST_EVENT))
// }
// }

private async listenEmailConnector(wagmiConfig: Web3ModalClientOptions['wagmiConfig']) {
const connector = wagmiConfig.connectors.find(c => c.id === ConstantsUtil.EMAIL_CONNECTOR_ID)

Expand Down
91 changes: 0 additions & 91 deletions packages/wagmi-v2/src/connectors/EIP6963Connector.ts

This file was deleted.

12 changes: 3 additions & 9 deletions packages/wagmi-v2/src/utils/defaultWagmiCoreConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import type { Config, CreateConnectorFn } from '@wagmi/core'
import { type Chain } from 'viem/chains'
import { createConfig } from '@wagmi/core'

// import { EIP6963Connector } from '../connectors/EIP6963Connector.js'

import { createClient, http } from 'viem'
import { coinbaseWallet, walletConnect, injected } from '@wagmi/connectors'
import { emailConnector } from '../connectors/EmailConnectorv2'
import { emailConnector } from '../connectors/EmailConnectorv2.js'

export interface ConfigOptions {
projectId: string
Expand All @@ -33,7 +31,6 @@ export function defaultWagmiConfig({
metadata,
enableInjected,
enableCoinbase,
// enableEIP6963,
enableEmail,
enableWalletConnect
}: ConfigOptions): Config {
Expand All @@ -48,10 +45,6 @@ export function defaultWagmiConfig({
connectors.push(injected({ shimDisconnect: true }))
}

// if (enableEIP6963 !== false) {
// connectors.push(new EIP6963Connector({ chains }))
// }

if (enableCoinbase !== false) {
connectors.push(coinbaseWallet({ appName: metadata?.name ?? 'Unknown' }))
}
Expand All @@ -66,7 +59,8 @@ export function defaultWagmiConfig({
client: ({ chain }) =>
createClient({
chain,
transport: http() // TODO: How do we add WC transport?
// TOD0: How to use WC transport? Do we need it for analytics?
transport: http()
}),
connectors,
multiInjectedProviderDiscovery: true
Expand Down

0 comments on commit 04c3cdf

Please sign in to comment.