Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wallet info #2099

Merged
merged 7 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/core/src/controllers/AccountController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { subscribeKey as subKey } from 'valtio/utils'
import { proxy, ref, subscribe as sub } from 'valtio/vanilla'
import { CoreHelperUtil } from '../utils/CoreHelperUtil.js'
import type { CaipAddress } from '../utils/TypeUtil.js'
import type { CaipAddress, ConnectedWalletInfo } from '../utils/TypeUtil.js'
import type { Balance } from '@web3modal/common'
import { BlockchainApiController } from './BlockchainApiController.js'
import { SnackController } from './SnackController.js'
Expand All @@ -19,6 +19,7 @@ export interface AccountControllerState {
addressExplorerUrl?: string
smartAccountDeployed?: boolean
tokenBalance?: Balance[]
connectedWalletInfo?: ConnectedWalletInfo
}

type StateKey = keyof AccountControllerState
Expand Down Expand Up @@ -86,6 +87,10 @@ export const AccountController = {
}
},

setConnectedWalletInfo(connectedWalletInfo: AccountControllerState['connectedWalletInfo']) {
state.connectedWalletInfo = connectedWalletInfo
},

async fetchTokenBalance() {
try {
if (state.address) {
Expand All @@ -110,5 +115,6 @@ export const AccountController = {
state.profileImage = undefined
state.addressExplorerUrl = undefined
state.tokenBalance = []
state.connectedWalletInfo = undefined
}
}
15 changes: 14 additions & 1 deletion packages/core/src/utils/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export interface CaipNetwork {
imageUrl?: string
}

export type ConnectedWalletInfo =
| {
name?: string
icon?: string
[key: string]: unknown
}
| undefined

export interface LinkingRecord {
redirect: string
href: string
Expand All @@ -38,7 +46,12 @@ export type Connector = {
imageId?: string
explorerId?: string
imageUrl?: string
info?: { rdns?: string }
info?: {
uuid?: string
name?: string
icon?: string
rdns?: string
}
provider?: unknown
}

Expand Down
3 changes: 2 additions & 1 deletion packages/ethers/exports/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export {
useWeb3ModalTheme,
useWeb3Modal,
useWeb3ModalState,
useWeb3ModalEvents
useWeb3ModalEvents,
useWalletInfo
} from '@web3modal/scaffold-react'

// -- Universal Exports -------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion packages/ethers/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export {
useWeb3ModalTheme,
useWeb3Modal,
useWeb3ModalState,
useWeb3ModalEvents
useWeb3ModalEvents,
useWalletInfo
} from '@web3modal/scaffold-vue'

// -- Universal Exports -------------------------------------------------------
Expand Down
54 changes: 41 additions & 13 deletions packages/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,11 @@ interface Info {
rdns: string
}

interface Wallet {
interface EIP6963ProviderDetail {
info: Info
provider: Provider
}

interface IEIP6963Provider {
name: string
provider: Provider
}

interface ExternalProvider extends EthereumProvider {
_addresses?: string[]
}
Expand All @@ -88,7 +83,7 @@ interface ExternalProvider extends EthereumProvider {
export class Web3Modal extends Web3ModalScaffold {
private hasSyncedConnectedAccount = false

private EIP6963Providers: IEIP6963Provider[] = []
private EIP6963Providers: EIP6963ProviderDetail[] = []

private walletConnectProvider?: EthereumProvider

Expand Down Expand Up @@ -516,10 +511,10 @@ export class Web3Modal extends Web3ModalScaffold {
const currentActiveWallet = window?.localStorage.getItem(EthersConstantsUtil.WALLET_ID)
if (currentActiveWallet) {
const currentProvider = this.EIP6963Providers.find(
provider => provider.name === currentActiveWallet
provider => provider.info.name === currentActiveWallet
)
if (currentProvider) {
this.setEIP6963Provider(currentProvider.provider, currentProvider.name)
this.setEIP6963Provider(currentProvider.provider, currentProvider.info.name)
}
}
}
Expand Down Expand Up @@ -831,7 +826,7 @@ export class Web3Modal extends Web3ModalScaffold {
this.setIsConnected(isConnected)

this.setCaipAddress(caipAddress)

this.syncConnectedWalletInfo()
await Promise.all([
this.syncProfile(address),
this.syncBalance(address),
Expand Down Expand Up @@ -924,6 +919,39 @@ export class Web3Modal extends Web3ModalScaffold {
}
}

private syncConnectedWalletInfo() {
const currentActiveWallet = window?.localStorage.getItem(EthersConstantsUtil.WALLET_ID)
const providerType = EthersStoreUtil.state.providerType

if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID) {
if (currentActiveWallet) {
const currentProvider = this.EIP6963Providers.find(
provider => provider.info.name === currentActiveWallet
)

if (currentProvider) {
this.setConnectedWalletInfo({
...currentProvider.info
})
}
}
} else if (providerType === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID) {
const provider = EthersStoreUtil.state.provider as unknown as EthereumProvider

if (provider.session) {
this.setConnectedWalletInfo({
...provider.session.peer.metadata,
name: provider.session.peer.metadata.name,
icon: provider.session.peer.metadata.icons?.[0]
})
}
} else if (currentActiveWallet) {
this.setConnectedWalletInfo({
name: currentActiveWallet
})
}
}

public async switchNetwork(chainId: number) {
const provider = EthersStoreUtil.state.provider
const providerType = EthersStoreUtil.state.providerType
Expand Down Expand Up @@ -1107,7 +1135,7 @@ export class Web3Modal extends Web3ModalScaffold {
}
}

private eip6963EventHandler(event: CustomEventInit<Wallet>) {
private eip6963EventHandler(event: CustomEventInit<EIP6963ProviderDetail>) {
if (event.detail) {
const { info, provider } = event.detail
const connectors = this.getConnectors()
Expand All @@ -1127,8 +1155,8 @@ export class Web3Modal extends Web3ModalScaffold {
})

const eip6963ProviderObj = {
name: info.name,
provider
provider,
info
}

this.EIP6963Providers.push(eip6963ProviderObj)
Expand Down
3 changes: 2 additions & 1 deletion packages/ethers5/exports/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export {
useWeb3ModalTheme,
useWeb3Modal,
useWeb3ModalState,
useWeb3ModalEvents
useWeb3ModalEvents,
useWalletInfo
} from '@web3modal/scaffold-react'

// -- Universal Exports -------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion packages/ethers5/exports/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export {
useWeb3ModalTheme,
useWeb3Modal,
useWeb3ModalState,
useWeb3ModalEvents
useWeb3ModalEvents,
useWalletInfo
} from '@web3modal/scaffold-vue'

// -- Universal Exports -------------------------------------------------------
Expand Down
51 changes: 40 additions & 11 deletions packages/ethers5/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,11 @@ interface Info {
rdns: string
}

interface Wallet {
interface EIP6963ProviderDetail {
info: Info
provider: Provider
}

interface IEIP6963Provider {
name: string
provider: Provider
}

interface ExternalProvider extends EthereumProvider {
_addresses?: string[]
}
Expand All @@ -79,7 +74,7 @@ interface ExternalProvider extends EthereumProvider {
export class Web3Modal extends Web3ModalScaffold {
private hasSyncedConnectedAccount = false

private EIP6963Providers: IEIP6963Provider[] = []
private EIP6963Providers: EIP6963ProviderDetail[] = []

private walletConnectProvider?: EthereumProvider

Expand Down Expand Up @@ -485,10 +480,10 @@ export class Web3Modal extends Web3ModalScaffold {
const currentActiveWallet = window?.localStorage.getItem(EthersConstantsUtil.WALLET_ID)
if (currentActiveWallet) {
const currentProvider = this.EIP6963Providers.find(
provider => provider.name === currentActiveWallet
provider => provider.info.name === currentActiveWallet
)
if (currentProvider) {
this.setEIP6963Provider(currentProvider.provider, currentProvider.name)
this.setEIP6963Provider(currentProvider.provider, currentProvider.info.name)
}
}
}
Expand Down Expand Up @@ -714,6 +709,7 @@ export class Web3Modal extends Web3ModalScaffold {
this.setIsConnected(isConnected)

this.setCaipAddress(caipAddress)
this.syncConnectedWalletInfo()

await Promise.all([
this.syncProfile(address),
Expand Down Expand Up @@ -804,6 +800,39 @@ export class Web3Modal extends Web3ModalScaffold {
}
}

private syncConnectedWalletInfo() {
const currentActiveWallet = window?.localStorage.getItem(EthersConstantsUtil.WALLET_ID)
const providerType = EthersStoreUtil.state.providerType

if (providerType === ConstantsUtil.EIP6963_CONNECTOR_ID) {
if (currentActiveWallet) {
const currentProvider = this.EIP6963Providers.find(
provider => provider.info.name === currentActiveWallet
)

if (currentProvider) {
this.setConnectedWalletInfo({
...currentProvider.info
})
}
}
} else if (providerType === ConstantsUtil.WALLET_CONNECT_CONNECTOR_ID) {
const provider = EthersStoreUtil.state.provider as unknown as EthereumProvider

if (provider.session) {
this.setConnectedWalletInfo({
...provider.session.peer.metadata,
name: provider.session.peer.metadata.name,
icon: provider.session.peer.metadata.icons?.[0]
})
}
} else if (currentActiveWallet) {
this.setConnectedWalletInfo({
name: currentActiveWallet
})
}
}

public async switchNetwork(chainId: number) {
const provider = EthersStoreUtil.state.provider
const providerType = EthersStoreUtil.state.providerType
Expand Down Expand Up @@ -954,7 +983,7 @@ export class Web3Modal extends Web3ModalScaffold {
this.setConnectors(w3mConnectors)
}

private eip6963EventHandler(event: CustomEventInit<Wallet>) {
private eip6963EventHandler(event: CustomEventInit<EIP6963ProviderDetail>) {
if (event.detail) {
const { info, provider } = event.detail
const connectors = this.getConnectors()
Expand All @@ -973,7 +1002,7 @@ export class Web3Modal extends Web3ModalScaffold {
})

const eip6963ProviderObj = {
name: info.name,
info,
provider
}

Expand Down
16 changes: 15 additions & 1 deletion packages/scaffold-react/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react'
import { useEffect, useState, useSyncExternalStore } from 'react'
import type {
W3mAccountButton,
W3mButton,
Expand Down Expand Up @@ -86,6 +86,20 @@ export function useWeb3Modal() {
return { open, close }
}

export function useWalletInfo() {
if (!modal) {
throw new Error('Please call "createWeb3Modal" before using "useWeb3Modal" hook')
}

const walletInfo = useSyncExternalStore(
modal.subscribeWalletInfo,
modal.getWalletInfo,
modal.getWalletInfo
)

return { walletInfo }
}

export function useWeb3ModalState() {
if (!modal) {
throw new Error('Please call "createWeb3Modal" before using "useWeb3ModalState" hook')
Expand Down
18 changes: 18 additions & 0 deletions packages/scaffold-vue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ export function useWeb3Modal() {
})
}

export function useWalletInfo() {
if (!modal) {
throw new Error('Please call "createWeb3Modal" before using "useWeb3Modal" composable')
}

const walletInfo = ref(modal.getWalletInfo())

const unsubscribe = modal.subscribeWalletInfo(newValue => {
walletInfo.value = newValue
})

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

return { walletInfo }
}

export function useWeb3ModalState() {
if (!modal) {
throw new Error('Please call "createWeb3Modal" before using "useWeb3ModalState" composable')
Expand Down
Loading
Loading