Hardhat

How can I send transactions to a contract using Hardhat


Sending transactions to a smart contract is a key part of interacting with Ethereum decentralized applications (dApps). In this guide, we will explain how to send transactions to a deployed contract using Hardhat. We will use the MyToken contract as an example to demonstrate the process.

1. Prerequisites

Before you start sending transactions, ensure that you have:

  • A Hardhat project set up with a deployed smart contract.
  • The contract's ABI (Application Binary Interface) and address.
  • A local Ethereum network running (e.g., using Hardhat's built-in node).

2. Understanding Transactions

In Ethereum, a transaction is a signed data package that is sent from one account to another. Transactions can:

  • Transfer Ether from one account to another.
  • Call a function on a smart contract, which may modify its state.

3. Sending Transactions to a Contract

To send transactions to a smart contract, you can create a script in your Hardhat project. Below, we will create a script that demonstrates how to send tokens using the transfer function of the MyToken contract.

3.1. Create a Script

Create a new script file in the scripts directory named sendTransaction.js:

const hre = require(`hardhat`);
async function main() {
    // Replace with your deployed contract address
    const tokenAddress = `YOUR_DEPLOYED_TOKEN_ADDRESS`; // e.g., `0x5B...B7D`
    // Get the contract instance
    const Token = await hre.ethers.getContractFactory(`MyToken`);
    const token = Token.attach(tokenAddress);
    // Get signers (accounts)
    const [owner, addr1] = await hre.ethers.getSigners();
    // Amount to transfer
    const amount = hre.ethers.utils.parseUnits(`100`, 18); // 100 tokens
    console.log(`Transferring ${amount.toString()} tokens from ${owner.address} to ${addr1.address}`);
    // Send transaction to transfer tokens
    const transferTx = await token.transfer(addr1.address, amount);
    
    // Wait for the transaction to be mined
    await transferTx.wait();
    console.log(`Transfer successful!`);
}
main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

3.2. Explanation of the Script

In this script:

  • Getting the Contract Instance: We use hre.ethers.getContractFactory to get the contract factory and then attach it to the deployed contract address using Token.attach(tokenAddress).
  • Getting Signers: We retrieve the available accounts (signers) using hre.ethers.getSigners(). The first account (usually the owner) will send the transaction.
  • Preparing the Transaction: We specify the amount of tokens to transfer using hre.ethers.utils.parseUnits to ensure the value is in the correct format.
  • Sending the Transaction: We call the transfer function on the contract instance to send the transaction. This function modifies the state of the contract, hence it requires gas fees.
  • Waiting for Confirmation: We use await transferTx.wait() to wait until the transaction is mined and confirmed on the blockchain.

3.3. Running the Script

To run the script, execute the following command in your terminal:

npx hardhat run scripts/sendTransaction.js --network localhost

Make sure to replace YOUR_DEPLOYED_TOKEN_ADDRESS in the script with the actual address of your deployed token. You should see a confirmation of the token transfer in the console output.

4. Conclusion

Sending transactions to a smart contract using Hardhat is a straightforward process that allows you to interact with your deployed contracts efficiently. By following the steps outlined in this guide, you can easily send transactions

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.