How to send a transaction to a smart contract or a wallet with web3

We will see how you can send a transaction with web3 to any address on the blockchain, either a smart contract or another wallet.

In this tutorial, we will see how you can send a transaction with web3 to any address on the blockchain, either a smart contract or another wallet.

First, if you haven't already, install the web3 dependency using the following command:

npm install web3

Then you need to connect to the user's wallet using any method you want. You can check out our guides if you want to learn how to connect to a wallet.

The next step is to find the address of the smart contract you want to send your transaction to. It has to be on the same blockchain as the wallet that sends the transaction otherwise your tokens will be lost.

You can find the address on etherscan.io for the Ethereum blockchain or in bscscan.com for the Binance Smart Chain.

In this tutorial, we are going to use the smart contract of the USDT token that you can find here: https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7

We are going to send Ethereum to this smart contract to get USDT.

Here are the transaction parameters you can use to send transactions:

const transactionParameters = {
  nonce: '0x00', // ignored by MetaMask
  
  gasPrice: '0x09184e72a000', // customizable by user during MetaMask confirmation.
  
  gas: '0x2710', // customizable by user during MetaMask confirmation.
  
  to: '0x0000000000000000000000000000000000000000', // Required
  
  from: fromAddress, // must match user's active address.
  
  value: '0x00', // Only required to send ether to the recipient from the initiating external account.
  
  data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', // Optional, but used for defining smart contract creation and interaction.
  
  chainId: '0x3', // Used to prevent transaction reuse across blockchains. Auto-filled by MetaMask.
};
  • nonce: optional and ignored by MetaMask
  • gasPrice: optional and it can be customized by the user during the transaction confirmation
  • gas: optional, same as gasPrice
  • to: required, the address you are sending the transaction to
  • from: required, the address that sends the transaction
  • value: required, the amount of ether you are sending, represented as 10^-18 ether
  • data: optional, but used for defining smart contract creation and interaction. It can be used to send
  • chainId: optional, used to prevent transaction reuse across blockchains. Auto-filled by MetaMask.

Now you need to connect the user's wallet to your website to it for the transactions.

import Web3 from 'web3' // not needed if you use vanilla JS

let account;
let web3;

if (window.ethereum) {
        const accounts = await window.ethereum.request({method: 'eth_requestAccounts'});
        account = accounts[0]
        web3 = new Web3(window.ethereum);
}

You can use this to connect to browser wallets but you can of course use any other method to connect the users wallet to your website. You can use Wallet Connect for example. Check out our guides if you want to learn more.

To send the transaction, we will use this code:

const account = '0x...'
const amount = '0.1'

const transactionParameters = {
  to: '0xdac17f958d2ee523a2206206994597c13d831ec7',
  from: account,
  value: Web3.utils.toHex(Web3.utils.toWei(amount, 'ether')),
};

// txHash is a hex string
// As with any RPC call, it may throw an error
const txHash = await web3.eth.sendTransaction(transactionParameters)

Using this transaction hash, you can check the state of your transaction on the block explorer. For the Ethereum network, that explorer is etherscan.io.

To see you're transaction, take your hash and go to:

https://etherscan.io/tx/YOUR-HASH-HERE

In the code above, we use window.ethereum to communicate with the Ethereum API through the user's browser wallet.

We also use Web3.utils methods to convert an Ethereum amount as a string to Wei which is Ethereum with 18 decimals so 1 Ethereum = 1000000000000000000. Then we convert it to a hexadecimal representation which looks like a wallet address.

The from and to parameters are both addresses, from is the wallet that sends the transaction and to is the recipient.

We will se in another tutorial how you can handle errors that may happen when trying to call the function like the user doesn't confirm the transaction, the wallet isn't on the right network or other.

Thanks for following this tutorial!