Skip to content

Commit

Permalink
Implement wallet_switchEthereumChain (#2111)
Browse files Browse the repository at this point in the history
* Implement wallet_switchEthereumChain

* Run prettier

---------

Co-authored-by: Chase Fleming <1666730+chasefleming@users.noreply.github.com>
  • Loading branch information
chasefleming and chasefleming authored Feb 4, 2025
1 parent 9a57857 commit 31df38b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
23 changes: 19 additions & 4 deletions packages/fcl-ethereum-provider/src/network/network-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
switchMap,
} from "../util/observable"
import * as fcl from "@onflow/fcl"
import {AddEthereumChainParams} from "../types/eth"
import {AddEthereumChainParams, SwitchEthereumChainParams} from "../types/eth"

export type ChainIdStore = {
isLoading: boolean
Expand Down Expand Up @@ -86,9 +86,24 @@ export class NetworkManager {
* No-op implementation for wallet_addEthereumChain.
* Since FCL does support dynamic chain additions.
*/
public async wallet_addEthereumChain(
_chainConfig: AddEthereumChainParams
): Promise<null> {
public async addChain(_chainConfig: AddEthereumChainParams): Promise<null> {
return null
}

public async switchChain(params: SwitchEthereumChainParams): Promise<null> {
const activeChainId = await this.getChainId()
if (activeChainId === null) {
throw new Error("No active chain configured.")
}

// Convert the chainId from hex (e.g., "0x64") to a number.
const requestedChainId = parseInt(params.chainId, 16)

if (requestedChainId !== activeChainId) {
throw new Error(
"Network switch error: The requested chain ID does not match the currently configured network."
)
}
return null
}
}
12 changes: 11 additions & 1 deletion packages/fcl-ethereum-provider/src/rpc/rpc-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AddEthereumChainParams,
PersonalSignParams,
SignTypedDataParams,
SwitchEthereumChainParams,
TypedData,
} from "../types/eth"
import {signTypedData} from "./handlers/eth-signtypeddata"
Expand Down Expand Up @@ -73,7 +74,16 @@ export class RpcProcessor {
}
const chainConfig = params[0] as AddEthereumChainParams

return await this.networkManager.wallet_addEthereumChain(chainConfig)
return await this.networkManager.addChain(chainConfig)
case "wallet_switchEthereumChain":
// Expect params to be an array with one object.
if (!params || !Array.isArray(params) || !params[0]) {
throw new Error(
"wallet_switchEthereumChain requires an array with a chain configuration object."
)
}
const switchParams = params[0] as SwitchEthereumChainParams
return await this.networkManager.switchChain(switchParams)
default:
return await this.gateway.request({
chainId,
Expand Down
4 changes: 4 additions & 0 deletions packages/fcl-ethereum-provider/src/types/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ export interface AddEthereumChainParams {
blockExplorerUrls?: string[]
iconUrls?: string[]
}

export interface SwitchEthereumChainParams {
chainId: string // Hex string, e.g., "0x64"
}

0 comments on commit 31df38b

Please sign in to comment.