HardHat deployment fails with ProviderError: invalid sender when switching between Polygon networks

I’m having some issues with deploying my smart contracts using HardHat on the Polygon network. Typically, everything functions correctly, but I encounter problems when switching between different network setups.

For example, I face issues when I switch from the mainnet to the testnet or the other way around. Specifically, after shifting to the Mumbai testnet via rpc-mumbai.maticvigil.com, I receive the error message: ProviderError: invalid sender.

To resolve this, I modified my hardhat.config.js to use matic-mumbai.chainstacklabs.com instead, enabling me to deploy contracts and mint NFTs without any errors.

Is this a known issue for others who develop on Polygon with HardHat? I’m curious if certain RPC endpoints might be causing this complication.

Oh interesting, I’ve been seeing similar weirdness but wasn’t sure if it was just me!

Quick question - same wallet/private key for both mainnet and testnet? I wonder if there’s some caching issue with account state when jumping between networks.

Also, what’s your hardhat config like? Separate network configs for each polygon network, or manually switching RPC URLs? When I have both Mumbai and polygon mainnet in the same config, hardhat sometimes gets confused about which chain ID to use.

Tried adding a delay between network switches? Sounds weird but I do npx hardhat accounts --network mumbai to “wake up” the connection before deploying. Might be placebo but seems to help :man_shrugging:

What hardhat version are you running? Been thinking about updating but worried about breaking more stuff lol

This issue with Polygon and HardHat is not uncommon. It generally stems from inconsistent RPC endpoints. When switching from one network to another, ensure that your local environment is aware of the change, as it may still be referencing the previous network’s nonce, leading to the ‘invalid sender’ error. I recommend cleaning your deployment artifacts with npx hardhat clean before switching networks, as this has resolved similar issues for me. Additionally, consider using more reliable RPC providers like Chainstack or Alchemy over less stable ones like Maticvigil to avoid these complications.