【问题标题】:Why "invalid sender" (-32000) when trying to deploy an Ethereum smart contract from node script?为什么在尝试从节点脚本部署以太坊智能合约时出现“无效发件人”(-32000)?
【发布时间】:2021-06-18 04:34:56
【问题描述】:

尝试将我的智能合约部署到 rinkeby 网络,但我收到此错误消息 { code: -32000, message: 'invalid sender' }.

我尝试通过 Remix 部署我的合约,它运行良好,但我对收到此错误的原因有点迷茫。

const HDWalletProvider = require("@truffle/hdwallet-provider"); // "^1.2.4"
const Web3 = require("web3"); // "^1.3.4"
const compiledFactory = require("./build/factory.json");
const abi = compiledFactory.abi;
const bytecode = compiledFactory.evm.bytecode.object;

const provider = new HDWalletProvider({
  mnemonic: {
    phrase:
      "twelve word mnemonic phrase twelve word mnemonic phrase twelve word mnemonic phrase",
  },
  providerOrUrl: "https://rinkeby.infura.io/v3/12345678",
});
const web3 = new Web3(provider);

const deploy = async () => {
  const accounts = await web3.eth.getAccounts();

  console.log("Attempting to deploy from account", accounts[0]);
  try {
    const result = await new web3.eth.Contract(abi)
      .deploy({ data: bytecode })
      .send({ from: accounts[0], gas: "1000000" });
    console.log("Contract deployed to", result.options.address);
  } catch (e) {
    console.error(e);
  }
};

deploy();

【问题讨论】:

    标签: ethereum web3 truffle web3js


    【解决方案1】:

    让它工作。问题是,交易必须在发送之前签名。这是更新的部署功能。

    const deploy = async () => {
      const accounts = await web3.eth.getAccounts();
      const deploymentAccount = accounts[0];
      const privateKey = provider.wallets[
        deploymentAccount.toLowerCase()
      ].privateKey.toString("hex");
    
      console.log("Attempting to deploy from account", deploymentAccount);
    
      try {
        let contract = await new web3.eth.Contract(abi)
          .deploy({
            data: bytecode,
            arguments: [],
          })
          .encodeABI();
    
        let transactionObject = {
          gas: 4000000,
          data: contract,
          from: deploymentAccount,
          // chainId: 3,
        };
    
        let signedTransactionObject = await web3.eth.accounts.signTransaction(
          transactionObject,
          "0x" + privateKey
        );
    
        let result = await web3.eth.sendSignedTransaction(
          signedTransactionObject.rawTransaction
        );
        console.log("Contract deployed to", result.contractAddress);
      } catch (e) {
        console.error(e);
      }
    
      process.exit(1);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 2019-04-07
      • 1970-01-01
      • 2020-01-28
      相关资源
      最近更新 更多