Truffle 合约部署失败,发件人无效

Truffle contract deployment failed, invalid sender

我正在尝试使用 truffle 将合约部署到 ropsten 测试网,但出现以下错误:

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

在本地部署到 ganache 时,它​​工作正常。另外我很确定我的 truffle-config.js 是正确的,它和所有在线教程一样,但是既然我在这里,我想我不完全确定 :)。 hd-wallet 使用的地址也是正确的(通过 truffle-config.js 中的 console.log 语句验证)并且它有 5 个 ETH 余额,所以绰绰有余。我有 2 个迁移脚本,每个脚本都给出完全相同的错误。

松露-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);
    }
}

看看这个: https://github.com/trufflesuite/truffle/issues/3935

似乎 truffle 可能不符合 eip 155。已合并 PR 以提供帮助,但我认为此问题尚未从 HDWallet 端解决。

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

尝试不同的版本@truffle/hdwallet-provider 适用于 1.2.3

npm 卸载@truffle/hdwallet-provider npm 安装@truffle/hdwallet-provider@1.2.3

最新版本 (1.2.4) 出现了同样的错误(发件人无效)。

根据我的观察,这个问题只会在你尝试在 ropsten 测试网上部署任何合约时出现。如果您使用像 运行 ganache-cli 这样的本地节点进行部署,即使使用最新版本 (>1.2.3)

,它也能正常运行

这里的原因是他们更改了构造函数方法以添加一个 chainId 参数来指定您要与交易签署的链。

解决方案:更新初始化 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 )
    },

我在 ropsten 上工作正常

我正在使用@truffle/hdwallet-provider 1.3.0,但仍然遇到同样的错误。

已通过更改 HDWalletProvider 的初始化修复它。

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

(用您的私钥和 infura api 密钥替换 YourPrivateKey 和 InfuraKey)

作为替代解决方案,在 truffle-config.js 中使用:

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

而不是

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

这只是降级 @truffle/hdwallet-provider 的另一种方式,而您的 package.json 仍然可以:

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