Start a conversation

Send transaction Tutorial

Using our blockchain gateway and web3.js, we can send smart contracts, read and write data directly from the blockchain network. Web3.js is a collection of libraries which allows you interact with a local or remote ethereum node, using a HTTP or IPC connection. 

We will be using ropsten for this tutorial. Ropsten is the test network for ethereum which you can read more about here. By the end of this tutorial, you would have used our blockchain gateway to send a transaction to Ethereum!


Installing dependencies

To use web3.js, there are some dependencies required. 

Check for node js by running:  

node -v

That should return the version of node you have e.g  v13.1.0 . 

Otherwise, install node. Also install npm

Once you have installed node and npm, then you can install web3, by running this in your terminal:  

npm install web3

 Install ethereumjs-tx. This is a library we would use this to send the transaction.

npm install ethereumjs-tx


Creating your TbgNodes network

Go to our blockchain gateway and login to your TbgNodes account. There you can create a new network. To create your TbgNodes account using our blockchain network, login here. Your network details page would look something like the screenshot below. Here you can get your URL, username and password, which we will need to send the transaction to ethereum. 



Assigning variables

In this step we would assign all the variables needed to send our transaction. 

First, add the username and password from the network you created  to the URL above, so that the url becomes something like this: https://username:password@ropsten-archive.nodes.unchain.io/v0/1acb8698-a404-4a4a-8e20-cd40caa7f671

Then, in your js file, assign web3 to a variable by adding:

const Web3 = require('web3')

Also, assign the address above to rpCURL constant:

const rpcURL = https://username:password@ropsten-archive.nodes.unchain.io/v0/1acb8698-a404-4a4a-8e20-cd40caa7f671

 Then instantiate your web3 js connection by adding:  

const web3 = new Web3(rpcURL);


Setting up your Ethereum Account

Here, we assign our ethereum account details. 

Assign your ethereum address and your private key to your account variable as shown in the code snippet below. 

Your ethereum address and your private key are gotten from your ethereum account, which you can create using the metamask browser plugin or the app

The ethereum account provides you with a public address and a private key, which you can use for transactions with the blockchain. If you already have a public ethereum address and private key from an already creates ethereum account then you can also just use that here.

const account = {
    address: 'YOUR_ETHEREUM_ADDRESS_GOES HERE',
    privateKey: 'YOUR_PRIVATE_KEY_GOES HERE' 
};


Finally, assign variable Tx to the module:

var Tx = require('ethereumjs-tx').Transaction;


Signing and sending the transaction 

Using the asynchronous transact function below, we do the following:

-  Get the transaction count from the address

- Create our transaction object

- Sign and serialize the transaction

- Send the signed transaction

const transact = async () => {
// get the transaction count
    const txnCount = await web3.eth.getTransactionCount(account.address);

// create the transaction object
    const rawTx = {
        nonce: web3.utils.toHex(txnCount),
        gasLimit: web3.utils.toHex(21008),
        gasPrice: web3.utils.toHex(web3.utils.toWei('100', 'gwei')),
        to: account.address,
        value: '0x00'
    };

    // sign and serialize the transaction
    const tx = new EthTx(rawTx, {chain: 'ropsten'});
    const privKey = Buffer.from(account.privateKey, 'hex');
    tx.sign(privKey);

    let serializedTx = tx.serialize();

   // send the signed transaction
    web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
        .once('transactionHash', console.log)
        .on('receipt', console.log)
        .on('error', err => console.error(err))
        .catch((err) => console.log);
};


Finally, we run the transact function to get back our transaction hash and receipt of sent transaction:

transact();


If all goes well, it should show the transaction hash and the receipt in the conseole log! To see the full code sample, go here

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Support Engineer

  2. Posted
  3. Updated

Comments