Thanks to visit codestin.com
Credit goes to github.com

Skip to content

switchNetwork bug when rejecting switch right after adding network #265

@thomasvalera

Description

@thomasvalera

Issue Description

The switchNetwork function of the useChain() hook has a bug when the user rejects the switch after adding the network.
The switchNetwork function should return a Promise.reject but instead returns Promise.resolve making it seem like the network was switched.

This is a serious bug as it will continue execution and could send a payment from the wrong wallet on the wrong chain without anybody noticing.

Steps + code to reproduce

  1. Remove the network from your Metamask
  2. Execute switchNetwork from the useChain() hook
  3. Confirm the prompt to add the new network
  4. Reject the prompt to switch

The code is pretty simple:

const {switchNetwork} = useChain();

return switchNetwork(POLYGON_CHAIN_ID)
.then(() => {
    // The network has been added and switched, continue execution
}).catch((error) => {
    // The network has not been added or has not been switched, handle error
})

Actual Outcome

The then clause is executed, making it seems like the network was switched successfully

Expected Outcome

The catch clause should be executed as the network was not switched

Environment

Client

  • react-moralis version: 1.4.0
  • Moralis SDK version: 1.10.0
  • Operating system: MacOs 12.4
  • Browser: Brave v1.41.100

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions