为啥在尝试从节点脚本部署以太坊智能合约时出现“无效发件人”(-32000)?

Posted

技术标签:

【中文标题】为啥在尝试从节点脚本部署以太坊智能合约时出现“无效发件人”(-32000)?【英文标题】: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();

【问题讨论】:

【参考方案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);
;

【讨论】:

以上是关于为啥在尝试从节点脚本部署以太坊智能合约时出现“无效发件人”(-32000)?的主要内容,如果未能解决你的问题,请参考以下文章

二.私有以太坊网络部署智能合约

以太坊私链账户下智能合约的部署与调用——使用RemixGolangGeth

智能合约实战 gas gasprice gaslimit

智能合约实战 gas gasprice gaslimit

以太坊。 gas * price + value 资金不足

合约代码无法存储,请检查您的gas限制:以太坊智能合约部署失败: