Do we need to sign the transaction by web3.eth.signTransaction after initiating transaction via web3.eth.sendTransaction.

for example:

when I do:

         from: accs[0],
         to: wallet[0].address,
         value: '10000000000'

I get this:

{ transactionHash: '0x3b6b08f9b47780f86f54c575e587841e3ec73a2d284928673d731ae9e55419ed',
  transactionIndex: 0,
  blockHash: '0x6e16c78b4b93c7be0c7829c0b45af03d8c52837c8a70c55a4783b3e18cd05273',
  blockNumber: 1,
  gasUsed: 21000,
  cumulativeGasUsed: 21000,
  contractAddress: null,
  logs: [],
  status: true }

But I didn't get any balance in my wallet[0].address

  • What client are you using? Is mining enabled?
    – Ismael
    Aug 7 '19 at 17:12
  • I am just testing on locahost : Ganache. Aug 9 '19 at 5:26
  • In Ganache your transactions should be mined immediately since it is a client for testing. Either your transaction was erroneous somehow or your are not checking the balance correctly. Check the result of getTransaction and getTransactionReceipt with the transaction hash generated.
    – Ismael
    Aug 9 '19 at 16:49

Do we need to sign the transaction by web3.eth.signTransaction after initiating transaction via web3.eth.sendTransaction?

To me, the fact that you've used the word after implies that you are totally baffled here... unless you actually meant before...

In either case, here is the deal:

You may use web3.eth.sendTransaction({from: account, ...}) only if you have unlocked this account on the node that you are connect to.

This is typically the case when you use Ganache in order to test your contract - it unlocks several accounts for you, according to your configuration (the default being 10 accounts with private keys 0x000...1 to 0x000...a).

You can also unlock accounts explicitly via web3.eth.personal.unlockAccount, though it is generally not a recommended working-mode, since your unlocked accounts will also be available to anyone hacking your node (so you need to protect it in various way, for example, via https).

Otherwise, you need to use the following functions in the following order:

  1. web3.eth.accounts.signTransaction
  2. web3.eth.sendSignedTransaction

Here is what I usually do (tested on web3 v1.0.0-beta.34):

async function send(web3, transaction) {
    while (true) {
        try {
            const options = {
                to   : transaction._parent._address,
                data : transaction.encodeABI(),
                gas  : (await web3.eth.getBlock("latest")).gasLimit
            const signed  = await web3.eth.accounts.signTransaction(options, PRIVATE_KEY);
            const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
            return transactionReceipt;
        catch (error) {
            console.log("Press enter to try again...");
            await new Promise(function(resolve, reject) {
                process.stdin.once("data", function(data) {

And here is a usage example (executed from an async function):

const receipt = await send(web3, myContract.methods.myFunc(arg1, arg2, arg3));
  • This function works great, thanks! Curious why you wrapped the whole thing in "while (true)"? That ran like an infinite loop for me and didn't seem to serve a purpose.
    – Julian
    Aug 22 '21 at 6:19

I'd assume you are using web3 v1.2.

  • signTransaction will sign a transaction but it will not send it, returning an hexadecimal string with the signed transaction instead. You can use sendSignedTransaction to send it to the network.
  • sendTransaction will sign and send the transaction to the network.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.