Skip to content

Commit

Permalink
chore: fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuascan committed Feb 3, 2025
1 parent bfb2d08 commit 0e0b906
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/lib/connectors/universalWalletConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function universalWalletConnector({

async connect() {
await this.magic?.wallet.connectWithUI()
const provider = await this.getProvider() as RPCProviderModule
const provider = (await this.getProvider()) as RPCProviderModule
const chainId = await this.getChainId()
provider &&
registerProviderEventListeners(
Expand Down Expand Up @@ -147,48 +147,59 @@ export function universalWalletConnector({
},
switchChain: async function ({ chainId }: { chainId: number }) {
if (!options.networks) {
throw new Error('switch chain not supported: please provide networks in options')
throw new Error(
'switch chain not supported: please provide networks in options',
)
}
const normalizedChainId = normalizeChainId(chainId)
const chain = chains.find((x) => x.id === normalizedChainId)

if (!chain)
throw new Error(`Unsupported chainId: ${chainId}`)
if (!chain) throw new Error(`Unsupported chainId: ${chainId}`)

const network = options.networks.find((x) => typeof x === 'object' && x.chainId
? normalizeChainId(x.chainId) === normalizedChainId
: normalizeChainId(x as bigint | number | string) === normalizedChainId)
const network = options.networks.find((x) =>
typeof x === 'object' && x.chainId
? normalizeChainId(x.chainId) === normalizedChainId
: normalizeChainId(x as bigint | number | string) ===
normalizedChainId,
)

if (!network)
throw new Error(`Unsupported chainId: ${chainId}`)
if (!network) throw new Error(`Unsupported chainId: ${chainId}`)

const account = await this.getAccount()
const provider = await this.getProvider() as RPCProviderModule
const provider = (await this.getProvider()) as RPCProviderModule

if (provider?.off) {
provider.off('accountsChanged', this.onAccountsChanged)
provider.off('chainChanged', this.onChainChanged)
provider.off('disconnect', this.onDisconnect)
}

const newOptions: MagicOptions = {...options, connectorType: 'universal'}
const newOptions: MagicOptions = {
...options,
connectorType: 'universal',
}
newOptions.magicSdkConfiguration!.network = network

const { getAccount, getMagicSDK, getProvider, onAccountsChanged, } = magicConnector({

const { getAccount, getMagicSDK, getProvider, onAccountsChanged } =
magicConnector({
chains,
options: newOptions,
})
})

this.getAccount = getAccount
this.magic = getMagicSDK()
this.getProvider = getProvider
this.onAccountsChanged = onAccountsChanged

registerProviderEventListeners(this.magic!.rpcProvider, this.onChainChanged, this.onDisconnect)

registerProviderEventListeners(
this.magic!.rpcProvider,
this.onChainChanged,
this.onDisconnect,
)
this.onChainChanged(chain.id.toString())
this.onAccountsChanged([account])
return chain
},
},
onDisconnect: () => {
config.emitter.emit('disconnect')
},
Expand Down

0 comments on commit 0e0b906

Please sign in to comment.