6

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);
    }
}
Robbe Louwet
  • 97
  • 2
  • 8

5 Answers5

22

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).

Alexander Dyomin
  • 391
  • 1
  • 1
  • 3
12

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

Yijia Su
  • 314
  • 1
  • 5
  • I just checked with hdwallet-provider 1.3.0, this indeed fixes the root problem. I guess many people prefer the quick and easy solution :) – Robbe Louwet Apr 28 '21 at 08:11
  • Thanks for this solution. Unfortunately it doesn't work for BSC, when I try to update my `HDWalletProvider` and provide the `chainId`, I get the following error: `Error: Chain with ID 97 not supported`. Rolling back to 1.2.3 DOES fix it though. – StackG May 04 '21 at 02:07
  • Life saver! Thank you! – tinker May 04 '21 at 16:55
1

Check this out: https://github.com/trufflesuite/truffle/issues/3935

Seems that truffle may not be properly eip 155 compliant. A PR was merged to help but I don't think this issue is yet resolved from the HDWallet end.

https://github.com/trufflesuite/truffle/issues/3913 https://github.com/trufflesuite/truffle/pull/3923

AudioLeaf
  • 11
  • 1
  • Thanks! That does seem to be the most likely scenario. I'll try setting up geth and deploy using the --rpc.allow-unprotected-txs arg. If it works I'll let you know. – Robbe Louwet Mar 22 '21 at 09:54
0

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)

Dennis Stücken
  • 1,296
  • 9
  • 10
0

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"
}
blackgreen
  • 34,072
  • 23
  • 111
  • 129