1. Home
  2. Docs
  3. APIs
  4. How to call another smart contract using web3.js?

How to call another smart contract using web3.js?

To call another smart contract, you can use web3.js (or other Ethereum development libraries) to construct and send transactions. Here’s a simple example demonstrating how to call a function in another smart contract using web3.js:
Assuming you have two smart contracts, one being a token contract, and the other being the target contract you want to call. First, ensure that you are correctly connected to the Ethereum network and that you have enough Ether to cover gas fees.

  1. Import the web3.js library and connect to an Ethereum node:
    1
    2
    const { Web3 } = require('web3');
    const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'); // Replace with your Infura project ID or Ethereum node URL
  2. Get your account address and private key:
    1
    2
    const senderAddress = '0xYourAddress'; // Replace with your Ethereum address
    const senderPrivateKey = 'YourPrivateKey'; // Replace with your private key
  3. Create an instance of the target contract and build transaction parameters:
    1
    2
    3
    4
    5
    const targetContractAddress = '0xTargetContractAddress'; // Replace with the target smart contract address
    const targetContractABI = [...]; // Replace with the ABI of the target smart contractconst targetContract = new web3.eth.Contract(targetContractABI, targetContractAddress);const methodName = 'yourFunctionName'; // Replace with the name of the function in the target contract you want to call
    const methodParams = [...]; // If there are parameters, replace with appropriate parameter values

    const txData = targetContract.methods[methodName](...methodParams).encodeABI();
  4. Build and send the transaction:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    web3.eth.accounts.signTransaction({
    from: senderAddress,
    to: targetContractAddress,
    gas: '50000', // Replace with an appropriate gas limit
    gasPrice: '1000000000', // Replace with an appropriate gas price
    data: txData,
    nonce: null, // Replace with an appropriate nonce value
    }, senderPrivateKey)
    .then((signedTx) => {
    return web3.eth.sendSignedTransaction(signedTx.rawTransaction);
    })
    .then((receipt) => {
    console.log(Transaction Hash: ${receipt.transactionHash});
    console.log(Transaction successful!);
    })
    .catch((error) => {
    console.error(Error calling the target contract: ${error});
    });

Please note to replace the following in the sample code:
  • ‘https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID’: Replace with your Infura project ID or Ethereum node URL.
  • ‘0xYourAddress’: Replace with your Ethereum address.
  • ‘YourPrivateKey’: Replace with your private key.
  • ‘0xTargetContractAddress’: Replace with the actual address of the target smart contract.
  • targetContractABI: Replace with the ABI of the target smart contract.
  • ‘yourFunctionName’: Replace with the name of the function in the target contract you want to call.
  • methodParams: If there are parameters, replace with appropriate parameter values.
  • ‘50000’ and ‘1000000000’: Replace with appropriate gas limit and gas price.

This code will build and send a transaction to call a function in the target smart contract.
Tags