Skip to content

Commit

Permalink
feat: handle switch ethereum chain (#2676)
Browse files Browse the repository at this point in the history
* feat: handle switch ethereum chain

* Update packages/shared/src/lib/auxiliary/wallet-connect/handlers/wallet_switchEthereumChain.handler.ts

Co-authored-by: Mark Nardi <[email protected]>

---------

Co-authored-by: Mark Nardi <[email protected]>
  • Loading branch information
nicole-obrien and MarkNerdi authored Jun 28, 2024
1 parent 425fbd2 commit a22001e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { RpcMethod } from '../enums'

export const GENERAL_SUPPORTED_METHODS = [RpcMethod.WalletWatchAsset]
export const GENERAL_SUPPORTED_METHODS = [RpcMethod.WalletWatchAsset, RpcMethod.WalletSwitchEthereumChain]
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export enum RpcMethod {
EthSendTransaction = 'eth_sendTransaction',
EthSendRawTransaction = 'eth_sendRawTransaction',
WalletWatchAsset = 'wallet_watchAsset',
WalletSwitchEthereumChain = 'wallet_switchEthereumChain',
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from './onSessionDelete.handler'
export * from './onSessionProposal.handler'
export * from './onSessionRequest.handler'
export * from './sign_message.handler'
export * from './wallet_switchEthereumChain.handler'
export * from './wallet_watchAsset.handler'
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CallbackParameters, WCRequestInfo } from '../types'
import { handleEthSignTypedData } from './eth_signTypedData.handler'
import { handleEthTransaction } from './eth_transaction.handler'
import { handleSignMessage } from './sign_message.handler'
import { handleWatchAsset } from '@auxiliary/wallet-connect/handlers'
import { handleWalletSwitchEthereumChain, handleWatchAsset } from '@auxiliary/wallet-connect/handlers'
import { DappVerification, RpcMethod } from '../enums'
import { EvmTransactionData, getEvmTransactionFromHexString } from '@core/layer-2'
import { activeProfileId } from '@core/profile/stores'
Expand Down Expand Up @@ -110,6 +110,9 @@ export function onSessionRequest(event: Web3WalletTypes.SessionRequest): void {
case RpcMethod.WalletWatchAsset:
void handleWatchAsset(request.params, requestData)
break
case RpcMethod.WalletSwitchEthereumChain:
void handleWalletSwitchEthereumChain(request.params, requestData)
break
default:
returnResponse({ error: getSdkError('INVALID_METHOD') })
break
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { WCRequestInfo } from '@auxiliary/wallet-connect/types'
import { getEvmNetworks } from '@core/network'
import { Converter } from '@core/utils'
import { getSdkError } from '@walletconnect/utils'

type WalletSwitchEthereumChain = [
{
chainId: `0x${string}`
},
]

export function handleWalletSwitchEthereumChain(params: WalletSwitchEthereumChain, requestInfo: WCRequestInfo): void {
const { responseCallback } = requestInfo

const hexChainId = params[0]?.chainId
if (!hexChainId) {
responseCallback({ error: getSdkError('INVALID_METHOD') })
return
}

const chainId = Converter.hexToDecimal(hexChainId)

const isSupportedEvmChain = getEvmNetworks().some((network) => Number(network.chainId) === chainId)
if (isSupportedEvmChain) {
responseCallback({ result: chainId })
} else {
responseCallback({ error: getSdkError('UNSUPPORTED_CHAINS') })
}
}

0 comments on commit a22001e

Please sign in to comment.