Truffle contract deployment failed, invalid sender

Viewed 4126

I'm trying to deploy a contract to the ropsten testnet using truffle, but I get the following error:

Deploying 'Migrations'
   ----------------------

Error:  *** Deployment Failed ***

"Migrations" -- invalid sender.

    at /home/usr/.npm/lib/node_modules/truffle/build/webpack:/packages/deployer/src/deployment.js:365:1
    at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.2.5 (core: 5.2.5)
Node v10.19.0

When deploying to ganache locally, it works fine. Also I'm pretty sure my truffle-config.js is correct, it's the same as all the online tutorials, but since I'm here, I guess I'm not completely sure :). The address that hd-wallet is using is also correct (verified with the console.log statement in truffle-config.js) and it has 5 ETH balance, so more than enough. I have 2 migration scripts, it gives exactly the same error with each script.

truffle-config.js:

require("dotenv").config();
const HDWalletProvider = require("@truffle/hdwallet-provider");

module.exports = {
    networks: {
        ropsten: {
            provider: () => {
                var provider = new HDWalletProvider({
                    mnemonic: process.env.MNEMONIC,
                    providerOrUrl: `https://ropsten.infura.io/v3/${process.env.INFURA_KEY}`,
                    derivationPath: "m/44'/60'/0'/0/",
                    addressIndex: 0,
                });
                console.log(provider.getAddress());
                return provider;
            },
            network_id: 3,
            gas: 5500000,
            confirmations: 2,
            timeoutBlocks: 200,
            skipDryRun: true,
        },
        development: {
            host: "127.0.0.1",
            port: 7545,
            network_id: "*",
        },
    },
    compilers: {
        solc: {
            version: "0.6.0",
            optimizer: {
                enabled: true,
                runs: 200,
            },
        },
    },
};

1_initial_migration.js:

const Migrations = artifacts.require("Migrations");

module.exports = function (deployer) {
  deployer.deploy(Migrations);
};

2_deploy.js:

const Token = artifacts.require("Token");

module.exports = (deployer) => {
    deployer.deploy(Token);
};

Token.sol:

//SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Token is ERC20 {

    address minter;

    // minterChanged event
    event minterChanged(address indexed from, address to);
    
    constructor() public payable ERC20("Decentralized Bank Currency", "DCB") {

        minter = msg.sender;
    }

    function transferMinterRole(address bank) public returns(bool) {
        require(msg.sender == minter);
        minter = bank;

        emit minterChanged(msg.sender, minter);
        return true;
    }

    function mint(address account, uint256 amount) public {

        require(msg.sender == minter);
        _mint(account, amount);
    }
}

Escrow.sol:

//SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0 ;

contract Escrow {
    address agent;

    mapping(address => uint256) public deposits;

    modifier onlyAgent() {
        require(msg.sender == agent);
        _; // return void
    }

    constructor() public {
        // solidity heeft globale var msg
        agent = msg.sender;
    }

    function deposit(address payee) payable public onlyAgent {
        uint256 amount = msg.value;
        deposits[payee] = deposits[payee] + amount;
    }


    function withdras(address payable payee) public onlyAgent {
        uint256 payment = deposits[payee];
        deposits[payee] = 0;

        payee.transfer(payment);
    }
}
5 Answers

Try a different version @truffle/hdwallet-provider Works for me with 1.2.3

npm uninstall @truffle/hdwallet-provider npm install @truffle/hdwallet-provider@1.2.3

With the latest version (1.2.4) there was the same error (invalid sender).

According to my observation, this issue only occurs when you try to deploy any contract on ropsten testnet. If you're deploying with a local node like running with ganache-cli, it functions well even with the latest version (>1.2.3)

The reason here is they change the constructor method to add a chainId parameter to specified which chain you're signing with your transaction.

Solution: Update your code of initializing the HDWalletProvider.

    ropsten: {
      provider: () =>
        new HDWalletProvider({
          mnemonic,
          providerOrUrl:
            'wss://ropsten.infura.io/ws/v3/.....',
          chainId: 3,
        }),
      network_id: 3, // Ropsten's id
      gas: 5500000, // Ropsten has a lower block limit than mainnet
      confirmations: 0, // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 200, // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true, // Skip dry run before migrations? (default: false for public nets )
    },

This works fine with me on ropsten

I am using @truffle/hdwallet-provider 1.3.0 and still got the same error.

Got it fixed by changing the initialization of HDWalletProvider.

ropsten: {
    provider: function () {
        return new HDWalletProvider(
            {
                privateKeys: ["YourPrivateKey"],
                providerOrUrl: "https://ropsten.infura.io/v3/InfuraKey",
                chainId: 3,
            }
        )
    },
    network_id: '3',
}

(Replace YourPrivateKey and InfuraKey with your private key and infura api key)

As an alternative solution, inside truffle-config.js use:

const HDWalletProvider = require('truffle-hdwallet-provider');

instead of

const HDWalletProvider = require('@truffle/hdwallet-provider');

It's just another way of downgrading the @truffle/hdwallet-provider while your package.json can still have:

"dependencies": {
    "@truffle/hdwallet-provider": "^1.3.1"
}
Related