Lesson 14 of 21
In Progress

Working with External Libraries and Contracts

As a blockchain developer, you’ll often need to use external libraries or contracts in your projects. These libraries or contracts can provide useful functionality or data that you can use in your own contracts.

In this article, we’ll provide a guide for working with external libraries and contracts in your blockchain projects. We’ll cover how to import libraries and contracts, how to use them in your own contracts, and how to manage dependencies and updates.

Prerequisites

Before you begin, you’ll need to install and set up Hardhat. To install Hardhat, you’ll need Node.js and npm. To set up Hardhat, you’ll need to create a hardhat.config.js file in the root directory of your project.

Importing Libraries and Contracts

To use an external library or contract in your project, you’ll need to import it into your code. In Solidity, you can import libraries or contracts using the import statement.

To import a library, you’ll need to specify the path to the library’s source code. For example, to import a library called MyLibrary, you can use the following code:

import "./MyLibrary.sol";

To import a contract, you’ll need to specify the contract’s name and the path to its source code. For example, to import a contract called MyContract, you can use the following code:

import "./MyContract.sol" as MyContract;

Using Libraries and Contracts

Once you’ve imported a library or contract, you can use it in your own code. To use a library, you can call its functions like any other function. For example, to call a function called doSomething in the MyLibrary library, you can use the following code:

MyLibrary.doSomething();

To use a contract, you’ll need to create an instance of the contract. You can do this using the new keyword and the contract’s constructor function. For example, to create an instance of the MyContract contract, you can use the following code:

MyContract contract = new MyContract();

Once you’ve created an instance of a contract, you can call its functions like any other function. For example, to call a function called doSomething in the MyContract contract, you can use the following code:

contract.doSomething();

Managing Dependencies

When you use external libraries or contracts in your project, you’ll need to manage their dependencies. A dependency is a library or contract that is required by another library or contract.

To manage dependencies, you’ll need to use a dependency management tool. One popular tool is Hardhat’s @hardhat/dependency-tracking module.

To use the @hardhat/dependency-tracking module, you’ll need to install it and create a dependencies.json file in the root directory of your project. The dependencies.json file should specify the dependencies of your project.

For example, to specify that your project depends on the MyLibrary and MyContract contracts, you can use the following dependencies.json file:

{
  "dependencies": {
    "MyLibrary": "1.0.0",
    "MyContract": "2.0.0"
  }
}

To install the dependencies of your project, you can use the following command:

hardhat install

This command will install the dependencies of your project and update the dependencies.json file to reflect the installed versions.

Updating Dependencies

As you develop your project, you may need to update your dependencies to new versions. To update a dependency, you’ll need to update the version number in the dependencies.json file and then run the hardhat install command.

For example, to update the MyLibrary dependency to version 2.0.0, you can use the following dependencies.json file:

{
  "dependencies": {
    "MyLibrary": "2.0.0",
    "MyContract": "2.0.0"
  }
}

Then, you can use the following command to update the dependency:

hardhat install

Managing Contract Updates

When you use external contracts in your project, you’ll need to manage updates to those contracts. To do this, you’ll need to use a deployment management tool.

One popular tool for deployment management is Hardhat’s @hardhat/deploy module. This module provides a set of functions that you can use to deploy or update contracts on the Ethereum blockchain.

To use the @hardhat/deploy module, you’ll need to install it and create a migrations directory in the root directory of your project. The migrations directory should contain files that define your deployment scripts.

For example, to deploy the MyContract contract, you can create a migrations/2_deploy_contracts.js file with the following contents:

const MyContract = artifacts.require("MyContract");

async function migrate(deployer) {
  await deployer.deploy(MyContract);
}

module.exports = migrate;

To deploy or update the contracts in the migrations directory, you can use the following command:

hardhat run migrate

Conclusion

In this article, we provided a guide for working with external libraries and contracts in your blockchain projects. We covered how to import libraries and contracts, how to use them in your own code, and how to manage

dependencies and updates. We hope this article has been helpful in your journey as a blockchain developer. Happy coding!

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Write a Solidity contract called MyContract that imports a library called MyLibrary and calls a function called doSomething in the library.

import "./MyLibrary.sol";

contract MyContract {
  function doSomething() public {
    MyLibrary.doSomething();
  }
}

Create an instance of the MyContract contract and call its doSomething function.

const MyContract = artifacts.require("MyContract");

async function migrate(deployer) {
  const contract = await deployer.deploy(MyContract);
  await contract.doSomething();
}

module.exports = migrate;

Update the dependencies.json file to specify that your project depends on the MyLibrary library version 2.0.0 and the MyContract contract version 3.0.0.

{
  "dependencies": {
    "MyLibrary": "2.0.0",
    "MyContract": "3.0.0"
  }
}

Create a migrations/2_deploy_contracts.js file that deploys the MyContract contract and then calls its doSomething function.

const MyContract = artifacts.require("MyContract");

async function migrate(deployer) {
  const contract = await deployer.deploy(MyContract);
  await contract.doSomething();
}

module.exports = migrate;

Create a migrations/3_update_contracts.js file that updates the MyContract contract and then calls its doSomething function.

const MyContract = artifacts.require("MyContract");

async function migrate(deployer) {
  const contract = await deployer.deploy(MyContract);
  await contract.doSomething();
}

module.exports = migrate;