DePayFi / web3-mock

🤡 JavaScript library to mock web3 responses either by emulating web3 wallets or web3 RPC requests.
https://depay.com
MIT License
87 stars 20 forks source link

Switch to custom chain id #33

Closed matstyler closed 1 month ago

matstyler commented 6 months ago

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`
0xNe0x1 commented 6 months ago

Hi @matstyler,

Thank you for using DePay's open-source tools.

Unfortunately, DePay currently does not support custom chains, and this limitation extends to our open-source tools as well.

You could fork https://github.com/DePayFi/web3-blockchains and use the fork to add your custom chain. That might work.

Best