Truffle contract deployment failed, invalid sender
Asked Answered
E

5

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);
    }
}
Epicontinental answered 21/3, 2021 at 17:17 Comment(0)
M
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

Mic answered 27/4, 2021 at 17:12 Comment(3)
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 :)Epicontinental
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.Gunilla
Life saver! Thank you!Hahnemann
E
22

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

npm uninstall @truffle/hdwallet-provider npm install @truffle/[email protected]

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

Expositor answered 25/3, 2021 at 9:57 Comment(12)
You saved my life! Thanks!Furtek
You saved another life.Yeh
You still saved another life.Brieta
A 4th life saved!Pilkington
A 5th one. Thank youMadras
6th person :DDDDefiance
The root reason is the chainId parameter is now required. Please see my answer. Downgrading the version will solve this but it's not the perfect choice.Mic
This only treats the symptoms, I checked and verified that Yija Su's solution fixes the root cause.Epicontinental
You are the hero we needAudry
Not the hero we deserveGermiston
One more here. I saw this error with version 1.3.0, but winding back to 1.2.3 fixed!Gunilla
And another one! To the moon :diamond: :fingers:Hitormiss
M
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

Mic answered 27/4, 2021 at 17:12 Comment(3)
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 :)Epicontinental
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.Gunilla
Life saver! Thank you!Hahnemann
B
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

Boltrope answered 22/3, 2021 at 9:19 Comment(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.Epicontinental
Z
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)

Zonda answered 4/5, 2021 at 21:24 Comment(0)
M
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"
}
Miscarry answered 9/5, 2021 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.