【问题标题】:How can I send transaction with no gas fee on private chain如何在私有链上发送无gas费的交易
【发布时间】:2019-12-16 17:56:45
【问题描述】:

我想发送不收取gas费的交易。

我创建了一个私有链,并开始使用 gas 价格为 0 的 geth,如下所示。

geth --datadir node1/ --syncmode 'full' --port 30311 --rpc --rpcaddr '0.0.0.0' --rpcport 8545 --rpccorsdomain "*" --rpcvhosts "*" --rpcapi 'personal,db,eth,net,web3,txpool,miner'  --networkid 1515 --gasprice '0'

但是,它应该不需要gas费,但错误消息显示intrinsic gas too low。 我的代码如下所示

const customCommon = Common.forCustomChain(
      'mainnet',
      {
        name: 'privatechain',
        networkId: 1515,
        chainId: 1515,
      },
      'petersburg',
    )
    const functionAbi = await this.state.contract.methods.setGreeting(this.state.text).encodeABI()
    console.log(this.state.nonce)
    var details = await {
      nonce : this.state.nonce,
      gasPrice : 0,
      gas : 0,
      gasLimit: 0,
      from : this.state.web3.eth.coinbase,
      to: this.state.address,
      value : 0,
      data : functionAbi,
    };
    const transaction = await new EthereumTx(details, { common: customCommon },);  
    await transaction.sign(this.state.pk)

    var rawdata = await '0x' + transaction.serialize().toString('hex');
    console.log(rawdata)

    await this.state.web3.eth.sendSignedTransaction(rawdata)
    .on('transactionHash', function(hash){
      console.log(['transferToStaging Trx Hash:' + hash]);
    })
    .on('receipt', function(receipt){
      console.log(['transferToStaging Receipt:', receipt]);
    })
    .on('error', console.error);

我的代码有问题吗?请给我一些建议好吗?

【问题讨论】:

    标签: blockchain ethereum solidity web3


    【解决方案1】:

    当没有可用区块时,交易不能包含在区块中。您必须激活挖矿,这样才能挖出区块并包含您发送的交易。

    您必须添加--mine --mine.threads 1 这将激活挖掘并创建 1 个线程来挖掘新块。 此外,--unlock 必须用于解锁您的帐户(在本例中为帐户 0,即 coinbase)。 为了成功解锁您的帐户,您必须在 .sec 文件中提供帐户 0 的密码。该文件仅包含密码,没有任何空格或换行符。 创建文件后,添加以下内容: --password <path of the .sec file> 如果您正在使用您的私有链添加--allow-insecure-unlock,否则解锁将无法正常工作。如果需要,您可以在另一个帖子中查找原因。

    所以总而言之,您应该添加以下内容:

    --mine --miner.threads 1 --unlock --allow-insecure-unlock --password <path of the .sec file>

    在查看“geth help”时,选项“gasprice”被标记为已弃用。 你应该使用--miner.gasprice '0'

    【讨论】:

      猜你喜欢
      • 2016-06-06
      • 2022-11-03
      • 2019-05-21
      • 2022-06-14
      • 2019-10-26
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      相关资源
      最近更新 更多