How to deploy smart contracts on Ethereum testnet using web3.js

Web3.js is an open source library of javascript built by the Ethereum foundation to allow end users to interact with an Ethereum node using HTTP, IPC, or WebSocket.

We can use the Web3.js library to write transactions to the Ethereum network. The networks can be localIt is a personal blockchain that is also mainly used for testing purposes., testnetIt is an instance of blockchain powered by the same underlying code as the actual blockchain network. It is mainly used for testing purposes., and mainnetIt is the actual network of the blockchain..

Ethereum testnet

The Ethereum testnet uses nearly identical underlying technology as the Ethereum mainnet. It is primarily used by developers to test their decentralized applications in a live setting without the threat of losing any money, as all the tesetnet Ether has no value.

There are multiple testnet networks. Each differs from the other based on the consensus algorithm used. The most commonly used tesetnets are listed below:

  • Ropsten

  • Kovan

  • Rinkeby

  • Goerli

In this Answer, we'll learn about the Ropsten network in detail:

Example

We'll consider an example where we deploy a smart contract on the Ropsten network. Smart contract deployment is a write operation on the blockchain ledger, and all the write operations are considered transactions. In this example, we'll try to build the transaction, sign it, and broadcast it using web3.js.

To perform the transaction, follow the steps given below:

  1. Create an account on "MetaMask" and switch to the Ropsten test network.

  2. Fill the account with Ropsten Ether using a faucet. Now, the account should look like this:

A screenshot of the MetaMask account displaying the balance before the transaction

  1. Now, copy the address of the account written under “Account1” and paste it into the code given below.

  2. Then fetch the private key of “Account1” by clicking the three dots next to the account name and the “Account Details,” and then enter the password of the MetaMask account. Then copy and paste the private key into the code.

  3. Now, execute that code. A transaction hash will be generated, copy that and go to ropsten.etherescan.io and paste this hash into the search bar. We'll be able to see a page like the one given in the image below. We can check the "from” address to verify the same address that we pasted into the code.

The transaction details on Etherscan
  1. Now, click on the value in front of “To” to fetch the details of the smart deployed. There we can find the smart contract address at the top, which we can use to call the functions of the smart contract.

  2. To interact with the deployed smart contract, we can go to remix.ethereum.org and open the Deploy and Run transaction tab from the sidebar.

  3. Click "environment" and select "Injected Provider - MetaMask." Then, connect the MetaMask account with Ropsten Ethereum to remix.

  4. Now, paste the smart contract address in front of the "To Address" button, and remix will fetch the contract below and allow us to interact with the functions. Try to set the value 2525 using the set function and then call the get function and see the output 2525 under the function. We can also try the addition function, among others.

A screenshot of remix where the user can interact with deployed smart contract

Code

To perform the transaction, some fields are required, which are given below:

  1. rpcURL: We need a network node to connect to the Ropsten network. However, to avoid this complex task, we can use Infura’s free API key to connect to the testnet.

  2. Addresses: The account address (public key) can be copied from MetaMask (as explained above).

  3. privateKey: The account’s private key can also be copied from MetaMask (as explained above).

  4. Data: This is the compiled representation of a smart contract. The compilation process produces an ABI of the contract, the data field contains the hexadecimal notation of the smart contract's ABI. This data can be fetched after compiling the smart contract in the remix editor. Click the Compilation details under the "compile" button. Then find WEB 3 DEPLOY and copy the data field. This contains the smart contract data.

Note: The following code is not executable because the Ether in the account is limited and will drain after some transactions.

var Tx = require('ethereumjs-tx')
const Web3 = require('web3')
const web3 = new Web3('https://ropsten.infura.io/v3/YOUR_API_KEY')
const account = 'ACCOUNT_ADDRESS'
const privateKey = Buffer.from('ACCOUNT_PRIVATE_KEY', 'hex')
const data = '0x608060405234801561001057600080fd5b50610425806100206000396000f3fe608060405234801561001057600080fd5b50600436106100625760003560e01c8063165c4a16146100675780633ef5e4451461009757806360fe47b1146100c75780636d4ce63c146100e3578063771602f714610101578063f88e9fbf14610131575b600080fd5b610081600480360381019061007c9190610207565b610161565b60405161008e9190610256565b60405180910390f35b6100b160048036038101906100ac9190610207565b610177565b6040516100be9190610256565b60405180910390f35b6100e160048036038101906100dc9190610271565b61018d565b005b6100eb610197565b6040516100f89190610256565b60405180910390f35b61011b60048036038101906101169190610207565b6101a0565b6040516101289190610256565b60405180910390f35b61014b60048036038101906101469190610207565b6101b6565b6040516101589190610256565b60405180910390f35b6000818361016f91906102cd565b905092915050565b600081836101859190610327565b905092915050565b8060008190555050565b60008054905090565b600081836101ae919061035b565b905092915050565b600081836101c491906103be565b905092915050565b600080fd5b6000819050919050565b6101e4816101d1565b81146101ef57600080fd5b50565b600081359050610201816101db565b92915050565b6000806040838503121561021e5761021d6101cc565b5b600061022c858286016101f2565b925050602061023d858286016101f2565b9150509250929050565b610250816101d1565b82525050565b600060208201905061026b6000830184610247565b92915050565b600060208284031215610287576102866101cc565b5b6000610295848285016101f2565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006102d8826101d1565b91506102e3836101d1565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561031c5761031b61029e565b5b828202905092915050565b6000610332826101d1565b915061033d836101d1565b92508282039050818111156103555761035461029e565b5b92915050565b6000610366826101d1565b9150610371836101d1565b92508282019050808211156103895761038861029e565b5b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b60006103c9826101d1565b91506103d4836101d1565b9250826103e4576103e361038f565b5b82820490509291505056fea264697066735822122034169ff4f96361f20ea1c8f86cf2057213b2300867b1ef82b26d462bfcb970f564736f6c63430008100033'
async function GetNonce()
{
return await web3.eth.getTransactionCount(account)
}
async function SendTransaction()
{
var Nonce = await GetNonce()
// Build the transaction
const txObject = {
nonce: web3.utils.toHex(Nonce),
gasLimit: web3.utils.toHex(1000000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
data: data
}
// Sign the transaction
const tx = new Tx(txObject)
tx.sign(privateKey)
const serializedTx = tx.serialize()
const raw = '0x' + serializedTx.toString('hex')
//Broadcast the transaction
web3.eth.sendSignedTransaction(raw, (err, txHash) => {
console.log('txHash:', txHash)
// Now go check etherscan to see the transaction!
})
}
SendTransaction()

Explanation

The explanation of the code above is as follows:

  • Line 20: The txObject contains the transaction object with the fields required to perform the transaction. The values of these fields must be in Hexadecimal. That is why toHex() is used.

  • Line 31: The seralize() converts the transaction object into an RLP encoding of the transaction. The encoding ensures that the data can be sent in bytes from one application to another, irrespective of their language.

  • Line 35: The sendSignedTransaction() broadcasts the serialized transaction object to perform the transaction. The transaction hash txHash is returned, which is printed in the call-back function.

Note: There is no "to" field in the transaction object because the transaction is sent to the whole network instead of one account in a simple Ether transaction.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved