Skip to content

Commit

Permalink
chore: remove core usage on client side
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir committed Mar 1, 2024
1 parent 417ed09 commit 796184b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/core/src/controllers/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ export const AccountController = {
state.profileName = undefined
state.profileImage = undefined
state.addressExplorerUrl = undefined
state.smartAccountDeployed = undefined
}
}
3 changes: 1 addition & 2 deletions packages/ethers/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import type { EthereumProviderOptions } from '@walletconnect/ethereum-provider'
import type { Eip1193Provider } from 'ethers'
import { W3mFrameProvider, W3mFrameHelpers, W3mFrameRpcConstants } from '@web3modal/wallet'
import type { CombinedProvider } from '@web3modal/scaffold-utils/ethers'
import { AccountController } from '@web3modal/core'

// -- Types ---------------------------------------------------------------------
export interface Web3ModalClientOptions extends Omit<LibraryOptions, 'defaultChain' | 'tokens'> {
Expand Down Expand Up @@ -618,7 +617,7 @@ export class Web3Modal extends Web3ModalScaffold {
EthersStoreUtil.setProvider(this.emailProvider as unknown as CombinedProvider)
EthersStoreUtil.setIsConnected(true)
const { isDeployed, address: smartAccountAddress } = await this.initSmartAccount(chainId)
AccountController.setSmartAccountDeployed(isDeployed)
this.setSmartAccountDeployed(isDeployed)
if (isDeployed && smartAccountAddress) {
EthersStoreUtil.setAddress(smartAccountAddress as Address)
} else {
Expand Down
4 changes: 4 additions & 0 deletions packages/scaffold/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ export class Web3ModalScaffold {
AccountController.setAddressExplorerUrl(addressExplorerUrl)
}

protected setSmartAccountDeployed: (typeof AccountController)['setSmartAccountDeployed'] =
isDeployed => {
AccountController.setSmartAccountDeployed(isDeployed)
}
// -- Private ------------------------------------------------------------------
private async initControllers(options: ScaffoldOptions) {
NetworkController.setClient(options.networkControllerClient)
Expand Down
5 changes: 5 additions & 0 deletions packages/wagmi/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ export class Web3Modal extends Web3ModalScaffold {
if (isLoginEmailUsed) {
this.setIsConnected(false)
}

provider.onInitSmartAccount((isDeployed: boolean) => {
this.setSmartAccountDeployed(isDeployed)
})

provider.onRpcRequest(request => {
if (W3mFrameHelpers.checkIfRequestExists(request)) {
if (!W3mFrameHelpers.checkIfRequestIsAllowed(request)) {
Expand Down
2 changes: 0 additions & 2 deletions packages/wagmi/src/connectors/EmailConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { SwitchChainError, getAddress } from 'viem'
import type { Address } from 'viem'

import { ConstantsUtil } from '@web3modal/scaffold-utils'
import { AccountController } from '@web3modal/core'

// -- Types ----------------------------------------------------------------------------------------
interface W3mFrameProviderOptions {
Expand Down Expand Up @@ -38,7 +37,6 @@ export function emailConnector(parameters: EmailParameters) {

if (smartAccountEnabledNetworks.includes(chainId)) {
const { address: smartAccountAddress, isDeployed } = await provider.initSmartAccount()
AccountController.setSmartAccountDeployed(isDeployed)
if (isDeployed) {
return {
accounts: [smartAccountAddress as Address],
Expand Down
12 changes: 11 additions & 1 deletion packages/wallet/src/W3mFrameProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,16 @@ export class W3mFrameProvider {
})
}

public onInitSmartAccount(callback: (isDeployed: boolean) => void) {
this.w3mFrame.events.onFrameEvent(event => {
if (event.type === W3mFrameConstants.FRAME_INIT_SMART_ACCOUNT_SUCCESS) {
callback(event.payload.isDeployed)
} else if (event.type === W3mFrameConstants.FRAME_INIT_SMART_ACCOUNT_ERROR) {
callback(false)
}
})
}

// -- Promise Handlers ------------------------------------------------
private onConnectEmailSuccess(
event: Extract<W3mFrameTypes.FrameEvent, { type: '@w3m-frame/CONNECT_EMAIL_SUCCESS' }>
Expand Down Expand Up @@ -637,7 +647,7 @@ export class W3mFrameProvider {
}

private setLastUsedChainId(chainId: number) {
W3mFrameStorage.set(W3mFrameConstants.LAST_USED_CHAIN_KEY, `${chainId}`)
W3mFrameStorage.set(W3mFrameConstants.LAST_USED_CHAIN_KEY, String(chainId))
}

private getLastUsedChainId() {
Expand Down

0 comments on commit 796184b

Please sign in to comment.