2

i need help com many parameters for sign a transaction.

What is "nonce"?

How i calculte gas value?

How i calculate gasPrice?

what is data field?

Chain ID 1 is a Mainnet?

I will using infura.

$transaction = new Transaction([
'nonce' => '0x'.$hexNonce,
'gas' => '0x'.$gas,
'gasPrice' => '0x'.$gasPrice,
'value' => '0x'.$hexValue,
'chainId' => 1,
'data' => '0x0'
]);

4 Answers4

1

1.) Nonce is basically the amount of transactions sent from that account. You don't have to set it manually, you get it using web3.eth.getTransactionCount()

2.) Gas is the amount of gas to be used for that transaction. So you can set it according to what you are trying to do, if you are calling a contract function that performs a lot of computation you can set the gas limit a little higher. If you are unsure about the estimate you can use web3.eth.estimateGas()

3.) gasPrice is the price of gas in wei for that transaction. You can set it whatever you want, but if you don't it defaults to median gas used for the last few blocks.

4.) Data is the data to be sent to the contract function call, or in case of contract creation it is the bytecode or intialisation code of the contract

5.) For chain Id, you can refer to this thread

1
{     
     gas: web3.utils.toHex(3000000), 
     gasPrice: web3.utils.toHex(await web3.eth.getGasPrice()), 
     data: contractData, 
     nonce: web3.utils.toHex(await web3.eth.getTransactionCount(address)), 
     to: contractAddress, 
     chainId: chainId,
     from: address
}

gas Max limit of the gas to used, revert the transaction if used consumed higher then this limit

gasPrice Price per gas unit e.g (300000 * gasPrice), Set Higher for the transactions tobe mined faster.

data Data tobe sent in the transaction. Can be function call or contract init. can be obtained by using getData() if using web3js@0.20 or encodeAbi() web3js1.0`

contract.deploy({data:bytecode}).encodeABI(); //create new contract
contract.methods.setModule(args[0], args[1]).encodeABI(); //method calling

nonce No of transactions from this address + 1

to To whom you want to send, can be contract address, wallet address or leave it empty if creating new contract

chainID Id of the network if chain supports (EIP155) e.g ethereum mainnet = 1, ethereum rinkeby = 4, Expanse = 2

from The sender of the transaction

Hamza Ahmed
  • 587
  • 2
  • 11
1

Nonce is basically the number of transactions published from the same address before. You may find correct value using web3.eth.getTransactionCount function. Note that for some old test networks, initial nonce for an address was some big number rather than zero, but this is not the case anymore for modern test networks.

Gas is basically how much gas units you want to allow your transaction to spend. If transaction will try to spend more gas, it will be reverted, but gas will not be refunded. If transaction will spend less, the rest will be refunded. You may estimate how much gas your transaction will spend using web3.eth.estimateGas function.

Gas price is basically how much Wei you want to pay for each gas unit spent by your transaction. The bigger value you will put here, the more reasons miners will have to include your transaction into next block. You may estimate fair gas price using getGasPrice function.

Value is basically number of Wei you want to send along with transaction (may be zero).

Chain ID is a value that is needed to ensure that your transaction will be valid in certain blockchain only, i.e. only on main net, or only on some test net. You may find current list of valid chain IDs here.

Data is a sequence of bytes you want to send along with your transaction (may be empty).

Mikhail Vladimirov
  • 7,313
  • 1
  • 23
  • 38
0

Here is how you can calculate gas fees and sign txn:

need to calculate gas fees for the yourFunction
    const gasFees =
      await contractInstance.transactionContractInstance.methods
        .yourFunction(
         // all the parameters
        )
        .estimateGas({ from: publicAddress_of_your_desired_wallet });
   const tx = {
      // this is the address responsible for this transaction
      from: chainpalsPlatformAddress,
      // target address, this could be a smart contract address
      to: transactionSmartContractAddress,
      // gas fees for the transaction
      gas: gasFees,
      // this encodes the ABI of the method and the arguments
      data: await contractInstance.transactionContractInstance.methods
        .yourFunction(
       // all the parameters
        )
        .encodeABI(),
    };
  // sign the transaction with a private key. It'll return messageHash, v, r, s, rawTransaction, transactionHash
    const signPromise =
       await contractInstance.web3BSC.eth.accounts.signTransaction(
        tx,
        config.get('WALLET_PRIVATE_KEY'),
      );
    // the rawTransaction here is already serialized so you don't need to serialize it again
    // Send the signed txn
    const sendTxn =
      await contractInstance.web3BSC.eth.sendSignedTransaction(
        signPromise.rawTransaction,
      );