web3-mock icon indicating copy to clipboard operation
web3-mock copied to clipboard

Switch to custom chain id

Open matstyler opened this issue 2 months ago • 1 comments

Use case

I add new, custom network (with custom network id):

return Web3Mock.mock({
          blockchain,
          wallet,
          network: {
            add: networkInfo
          }
        })

Then I want to switch to that network:

Web3Mock.mock({
          blockchain,
          wallet,
          network: {
            switchTo: networkName
          }
        })

        window.ethereum.request({
          method: 'wallet_switchEthereumChain',
          // Mock do not support custom network IDs
          params: [{ chainId }]
        })

Unfortunately switch to custom chan id is not working as @depay/web3-mock support only existing chain ids.

Error

`Error: page.evaluate: TypeError: fixedId.match is not a function

    at Object.findById (<anonymous>:22960:17)
    at switchNetwork (<anonymous>:23396:30)
    at request$2 (<anonymous>:23613:16)
    at Object.request (<anonymous>:23670:16)
    at eval (eval at evaluate (:226:30), <anonymous>:9:23)
    at UtilityScript.evaluate (<anonymous>:228:17)
    at UtilityScript.<anonymous> (<anonymous>:1:44)
    at Object.findById (/Users/matstyler/Documents/apps/synpress/wallets/ethereum-wallet-mock/<anonymous>:22960:17)
    at switchNetwork (/Users/matstyler/Documents/apps/synpress/wallets/ethereum-wallet-mock/<anonymous>:23396:30)
    at request$2 (/Users/matstyler/Documents/apps/synpress/wallets/ethereum-wallet-mock/<anonymous>:23613:16)
    at Object.request (/Users/matstyler/Documents/apps/synpress/wallets/ethereum-wallet-mock/<anonymous>:23670:16)
    at eval (/Users/matstyler/Documents/apps/synpress/wallets/ethereum-wallet-mock/eval at evaluate (:226:30), <anonymous>:9:23)
    at UtilityScript.evaluate (/Users/matstyler/Documents/apps/synpress/wallets/ethereum-wallet-mock/<anonymous>:228:17)
    at UtilityScript.<anonymous> (/Users/matstyler/Documents/apps/synpress/wallets/ethereum-wallet-mock/<anonymous>:1:44)
    at EthereumWalletMock.switchNetwork (/Users/matstyler/Documents/apps/synpress/wallets/ethereum-wallet-mock/src/ethereum-wallet-mock.ts:159:22)
    at /Users/matstyler/Documents/apps/synpress/wallets/ethereum-wallet-mock/test/e2e/metamask/switchNetwork.spec.ts:12:20`

matstyler avatar Apr 21 '24 18:04 matstyler