Sending a raw transaction
You can get more control over the transaction and data sent to the Ethereum blockchain. Usually, we send a transaction with send transaction methods. You can also sign a transaction with the private key and send it directly. You will learn various ways to do this in this recipe.
Getting ready
You need to have web3.js installed on your application to use these methods. Some methods may need additional libraries, which are explained along with the respective methods. Ensure that you have a geth- or Ganache-based Ethereum network to connect and test these methods.
How to do it...
We will now learn how we can send raw transactions:
- You need to sign your transaction before broadcasting it to an Ethereum network. If you are using the existing version of web3.js (0.2x.x), this operation is not built in. Install
ethereumjs-tx
via npm to sign transactions easily:
$ npm install ethereumjs-tx --save
- Once it is installed, import it into your project and create the transaction object...