Skip to content

Writing contracts overview

Writing Solidity smart contracts in Hardhat is as simple as writing a .sol file in your contracts/ folder.

For example, you could create the file contracts/HelloWorld.sol:

contracts/HelloWorld.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract HelloWorld {
string public greet = "Hello World!";
}

To build it, run:

Terminal window
npx hardhat build

Hardhat will analyze all your files in your contracts directory, including their dependencies, and compile them with the highest Solidity version that’s compatible with each of them.

Once the build process is successful, Hardhat will emit the build artifacts generated as a result. These can be used to interact with your contracts from TypeScript tests, your deployments, and advanced Solidity tests.

You can import files in Hardhat 3 using relative import paths. For example, we can modify our example above to import another file:

contracts/HelloWorld.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import { BaseContract } from "./BaseContract.sol";
contract HelloWorld is BaseContract {
string public greet = "Hello World!";
}

This default behavior is intentional, as it ensures that your modules are better supported by different tools, prevents accidental clashes with unscoped remappings, and ensures that your code behaves correctly when distributed through npm and/or git submodules. To learn how to customize this behavior, check the guides in the next section.