Search code examples
ethereumblockchainsolidityweb3js

How can I send transaction with no gas fee on private chain


I want to send transaction with no gas fee.

I made a private chain which and started geth with gas price is 0 like below.

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'

However, it is not supposed to need gas fee, but error message shows that intrinsic gas too low. My code in like below

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);

Are there any problem of my code? Could give me any advise, please?


Solution

  • The transaction can't be included in a block when there is no block available. You have to activate mining so blocks can be mined and your sent transaction can be included.

    You have to add --mine --mine.threads 1 This will activate mining and create 1 thread to mine for new blocks. Also --unlock must be used to unlock your account (in this case account 0, which is the coinbase). In order to successfully unlock your account, you have to provide the password of account 0 in a .sec file. The file contains only the password without any spaces or new lines. After you have created the file add the following: --password <path of the .sec file> If you are working on your private chain add --allow-insecure-unlock, because otherwise the unlock won't work. You can look up the reason in another post if you want.

    So all in all you should add the following:

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

    The option "gasprice" is marked as deprecated when looking at "geth help". You should use --miner.gasprice '0'.