Skip to content

Commit

Permalink
Merge branch 'V3' of github.com:WalletConnect/web3modal into V3
Browse files Browse the repository at this point in the history
  • Loading branch information
svenvoskamp committed Dec 15, 2023
2 parents af9d2be + 3cb12f8 commit 543666c
Show file tree
Hide file tree
Showing 19 changed files with 277 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/utils/CoreHelperUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export const CoreHelperUtil = {
}
}

return formattedBalance ? `${formattedBalance} ${symbol}` : '0.000'
return formattedBalance ? `${formattedBalance} ${symbol}` : `0.000 ${symbol}`
},

isRestrictedRegion() {
Expand Down
8 changes: 8 additions & 0 deletions packages/ethers/exports/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ export function useWeb3ModalAccount() {
}
}

export function useWeb3ModalError() {
const { error } = useSnapshot(EthersStoreUtil.state)

return {
error
}
}

export {
useWeb3ModalTheme,
useWeb3Modal,
Expand Down
20 changes: 20 additions & 0 deletions packages/ethers/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,26 @@ export function useWeb3ModalAccount() {
}
}

export function useWeb3ModalError() {
if (!modal) {
throw new Error('Please call "createWeb3Modal" before using "useWeb3ModalError" composition')
}

const error = ref(modal.getError())

const unsubscribe = modal.subscribeProvider(state => {
error.value = state.error
})

onUnmounted(() => {
unsubscribe?.()
})

return {
error
}
}

export {
useWeb3ModalTheme,
useWeb3Modal,
Expand Down
55 changes: 43 additions & 12 deletions packages/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import type { CombinedProvider } from '@web3modal/scaffold-utils/ethers'
// -- Types ---------------------------------------------------------------------
export interface Web3ModalClientOptions extends Omit<LibraryOptions, 'defaultChain' | 'tokens'> {
ethersConfig: ProviderType
chains: Chain[]
siweConfig?: Web3ModalSIWEClient
chains?: Chain[]
defaultChain?: Chain
chainImages?: Record<number, string>
connectorImages?: Record<string, string>
Expand Down Expand Up @@ -95,7 +95,7 @@ export class Web3Modal extends Web3ModalScaffold {

private projectId: string

private chains?: Chain[]
private chains: Chain[]

private metadata?: Metadata

Expand Down Expand Up @@ -127,7 +127,11 @@ export class Web3Modal extends Web3ModalScaffold {
switchCaipNetwork: async caipNetwork => {
const chainId = HelpersUtil.caipNetworkIdToNumber(caipNetwork?.id)
if (chainId) {
await this.switchNetwork(chainId)
try {
await this.switchNetwork(chainId)
} catch (error) {
EthersStoreUtil.setError(error)
}
}
},

Expand Down Expand Up @@ -192,19 +196,31 @@ export class Web3Modal extends Web3ModalScaffold {
if (!InjectedProvider) {
throw new Error('connectionControllerClient:connectInjected - provider is undefined')
}
await InjectedProvider.request({ method: 'eth_requestAccounts' })
this.setInjectedProvider(ethersConfig)
try {
await InjectedProvider.request({ method: 'eth_requestAccounts' })
this.setInjectedProvider(ethersConfig)
} catch (error) {
EthersStoreUtil.setError(error)
}
} else if (id === ConstantsUtil.EIP6963_CONNECTOR_ID && info && provider) {
await provider.request({ method: 'eth_requestAccounts' })
this.setEIP6963Provider(provider, info.name)
try {
await provider.request({ method: 'eth_requestAccounts' })
this.setEIP6963Provider(provider, info.name)
} catch (error) {
EthersStoreUtil.setError(error)
}
} else if (id === ConstantsUtil.COINBASE_CONNECTOR_ID) {
const CoinbaseProvider = ethersConfig.coinbase
if (!CoinbaseProvider) {
throw new Error('connectionControllerClient:connectCoinbase - connector is undefined')
}
await CoinbaseProvider.request({ method: 'eth_requestAccounts' })

this.setCoinbaseProvider(ethersConfig)
try {
await CoinbaseProvider.request({ method: 'eth_requestAccounts' })
this.setCoinbaseProvider(ethersConfig)
} catch (error) {
EthersStoreUtil.setError(error)
}
} else if (id === ConstantsUtil.EMAIL_CONNECTOR_ID) {
this.setEmailProvider()
}
Expand Down Expand Up @@ -343,6 +359,10 @@ export class Web3Modal extends Web3ModalScaffold {
return address ? getOriginalAddress(address) : undefined
}

public getError() {
return EthersStoreUtil.state.error
}

public getChainId() {
return EthersStoreUtil.state.chainId
}
Expand Down Expand Up @@ -374,7 +394,11 @@ export class Web3Modal extends Web3ModalScaffold {
} else {
const walletConnectProvider = provider as unknown as EthereumProvider
if (walletConnectProvider) {
await walletConnectProvider.disconnect()
try {
await walletConnectProvider.disconnect()
} catch (error) {
EthersStoreUtil.setError(error)
}
}
}
}
Expand All @@ -399,22 +423,27 @@ export class Web3Modal extends Web3ModalScaffold {
return map
}, {})
: ({} as Record<number, string>),
optionalChains: this.chains ? [0, ...this.chains.map(chain => chain.chainId)] : [0],
optionalChains: [...this.chains.map(chain => chain.chainId)] as [number],
metadata: {
name: this.metadata ? this.metadata.name : '',
description: this.metadata ? this.metadata.description : '',
url: this.metadata ? this.metadata.url : '',
icons: this.metadata ? this.metadata.icons : ['']
}
}

this.walletConnectProvider = await EthereumProvider.init(walletConnectProviderOptions)

await this.checkActiveWalletConnectProvider()
}

private async getWalletConnectProvider() {
if (!this.walletConnectProvider) {
await this.createProvider()
try {
await this.createProvider()
} catch (error) {
EthersStoreUtil.setError(error)
}
}

return this.walletConnectProvider
Expand Down Expand Up @@ -739,11 +768,13 @@ export class Web3Modal extends Web3ModalScaffold {
this.setIsConnected(isConnected)

this.setCaipAddress(caipAddress)

await Promise.all([
this.syncProfile(address),
this.syncBalance(address),
this.getApprovedCaipNetworksData()
])

this.hasSyncedConnectedAccount = true
} else if (!isConnected && this.hasSyncedConnectedAccount) {
this.resetWcConnection()
Expand Down
8 changes: 8 additions & 0 deletions packages/ethers5/exports/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ export function useWeb3ModalAccount() {
}
}

export function useWeb3ModalError() {
const { error } = useSnapshot(EthersStoreUtil.state)

return {
error
}
}

export {
useWeb3ModalTheme,
useWeb3Modal,
Expand Down
20 changes: 20 additions & 0 deletions packages/ethers5/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ export function useWeb3ModalAccount() {
}
}

export function useWeb3ModalError() {
if (!modal) {
throw new Error('Please call "createWeb3Modal" before using "useWeb3ModalError" composition')
}

const error = ref(modal.getError())

const unsubscribe = modal.subscribeProvider(state => {
error.value = state.error
})

onUnmounted(() => {
unsubscribe?.()
})

return {
error
}
}

export {
useWeb3ModalTheme,
useWeb3Modal,
Expand Down
48 changes: 38 additions & 10 deletions packages/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type { EthereumProviderOptions } from '@walletconnect/ethereum-provider'
export interface Web3ModalClientOptions extends Omit<LibraryOptions, 'defaultChain' | 'tokens'> {
ethersConfig: ProviderType
siweConfig?: Web3ModalSIWEClient
chains?: Chain[]
chains: Chain[]
defaultChain?: Chain
chainImages?: Record<number, string>
connectorImages?: Record<string, string>
Expand Down Expand Up @@ -87,7 +87,7 @@ export class Web3Modal extends Web3ModalScaffold {

private projectId: string

private chains?: Chain[]
private chains: Chain[]

private metadata?: Metadata

Expand Down Expand Up @@ -117,7 +117,11 @@ export class Web3Modal extends Web3ModalScaffold {
switchCaipNetwork: async caipNetwork => {
const chainId = HelpersUtil.caipNetworkIdToNumber(caipNetwork?.id)
if (chainId) {
await this.switchNetwork(chainId)
try {
await this.switchNetwork(chainId)
} catch (error) {
EthersStoreUtil.setError(error)
}
}
},

Expand Down Expand Up @@ -182,19 +186,31 @@ export class Web3Modal extends Web3ModalScaffold {
if (!InjectedProvider) {
throw new Error('connectionControllerClient:connectInjected - provider is undefined')
}
await InjectedProvider.request({ method: 'eth_requestAccounts' })
this.setInjectedProvider(ethersConfig)
try {
await InjectedProvider.request({ method: 'eth_requestAccounts' })
this.setInjectedProvider(ethersConfig)
} catch (error) {
EthersStoreUtil.setError(error)
}
} else if (id === ConstantsUtil.EIP6963_CONNECTOR_ID && info && provider) {
await provider.request({ method: 'eth_requestAccounts' })
try {
await provider.request({ method: 'eth_requestAccounts' })
} catch (error) {
EthersStoreUtil.setError(error)
}
this.setEIP6963Provider(provider, info.name)
} else if (id === ConstantsUtil.COINBASE_CONNECTOR_ID) {
const CoinbaseProvider = ethersConfig.coinbase
if (!CoinbaseProvider) {
throw new Error('connectionControllerClient:connectCoinbase - connector is undefined')
}
await CoinbaseProvider.request({ method: 'eth_requestAccounts' })

this.setCoinbaseProvider(ethersConfig)
try {
this.setCoinbaseProvider(ethersConfig)
await CoinbaseProvider.request({ method: 'eth_requestAccounts' })
} catch (error) {
EthersStoreUtil.setError(error)
}
}
},

Expand Down Expand Up @@ -321,6 +337,10 @@ export class Web3Modal extends Web3ModalScaffold {
return address ? utils.getAddress(address) : address
}

public getError() {
return EthersStoreUtil.state.error
}

public getChainId() {
return EthersStoreUtil.state.chainId
}
Expand Down Expand Up @@ -373,21 +393,27 @@ export class Web3Modal extends Web3ModalScaffold {
return map
}, {})
: ({} as Record<number, string>),
optionalChains: this.chains ? [0, ...this.chains.map(chain => chain.chainId)] : [0],
optionalChains: [...this.chains.map(chain => chain.chainId)] as [number],
metadata: {
name: this.metadata ? this.metadata.name : '',
description: this.metadata ? this.metadata.description : '',
url: this.metadata ? this.metadata.url : '',
icons: this.metadata ? this.metadata.icons : ['']
}
}

this.walletConnectProvider = await EthereumProvider.init(walletConnectProviderOptions)

await this.checkActiveWalletConnectProvider()
}

private async getWalletConnectProvider() {
if (!this.walletConnectProvider) {
await this.createProvider()
try {
await this.createProvider()
} catch (error) {
EthersStoreUtil.setError(error)
}
}

return this.walletConnectProvider
Expand Down Expand Up @@ -682,11 +708,13 @@ export class Web3Modal extends Web3ModalScaffold {
this.setIsConnected(isConnected)

this.setCaipAddress(caipAddress)

await Promise.all([
this.syncProfile(address),
this.syncBalance(address),
this.getApprovedCaipNetworksData()
])

this.hasSyncedConnectedAccount = true
} else if (!isConnected && this.hasSyncedConnectedAccount) {
this.resetWcConnection()
Expand Down
6 changes: 6 additions & 0 deletions packages/scaffold-utils/src/EthersStoreUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface EthersStoreUtilState {
providerType?: 'walletConnect' | 'injected' | 'coinbaseWallet' | 'eip6963' | 'w3mEmail'
address?: Address
chainId?: number
error?: unknown
isConnected: boolean
}

Expand Down Expand Up @@ -57,11 +58,16 @@ export const EthersStoreUtil = {
state.isConnected = isConnected
},

setError(error: EthersStoreUtilState['error']) {
state.error = error
},

reset() {
state.provider = undefined
state.address = undefined
state.chainId = undefined
state.providerType = undefined
state.isConnected = false
state.error = undefined
}
}
Loading

0 comments on commit 543666c

Please sign in to comment.