Hardhat
Learn how to use Hardhat to build and deploy smart contracts on Abstract.
YouTube Tutorial: Get Started with Hardhat
Watch a step-by-step tutorial on how to get started with Hardhat.
1. Create a new project
Inside an empty directory, initialize a new Hardhat project using the Hardhat CLI:
Create a new directory and navigate into it:
mkdir my-abstract-project && cd my-abstract-project
Initialize a new Hardhat project within the directory:
npx hardhat init
Select your preferences when prompted by the CLI, or use the recommended setup below.
2. Install the required dependencies
Abstract smart contracts use different bytecode than the Ethereum Virtual Machine (EVM).
Install the required dependencies to compile, deploy and interact with smart contracts on Abstract:
- @matterlabs/hardhat-zksync: A suite of Hardhat plugins for working with Abstract.
- @matterlabs/zksync-contracts: Access system contracts and other utilities.
- zksync-ethers: Recommended package for writing Hardhat scripts to interact with your smart contracts.
npm install -D @matterlabs/hardhat-zksync @matterlabs/zksync-contracts zksync-ethers@6 ethers@6
3. Modify the Hardhat configuration
Update your hardhat.config.ts
file to include the following options:
import { HardhatUserConfig } from "hardhat/config";
import "@matterlabs/hardhat-zksync";
const config: HardhatUserConfig = {
zksolc: {
version: "latest",
settings: {
// Note: This must be true to call NonceHolder & ContractDeployer system contracts
enableEraVMExtensions: false,
},
},
defaultNetwork: "abstractTestnet",
networks: {
abstractTestnet: {
url: "https://api.testnet.abs.xyz",
ethNetwork: "sepolia",
zksync: true,
verifyURL:
"https://api-explorer-verify.testnet.abs.xyz/contract_verification",
},
},
solidity: {
version: "0.8.24",
},
};
export default config;
Using system contracts
To use system contracts,
set the enableEraVMExtensions
flag to true:
zksolc: {
settings: {
// If you plan to interact directly with the NonceHolder or ContractDeployer system contracts
enableEraVMExtensions: true,
},
version: "latest",
},
4. Write a smart contract
Rename the existing contracts/Lock.sol
file to contracts/HelloAbstract.sol
:
mv contracts/Lock.sol contracts/HelloAbstract.sol
Write a new smart contract in the contracts/HelloAbstract.sol
file, or use the example smart contract below:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract HelloAbstract {
function sayHello() public pure virtual returns (string memory) {
return "Hello, World!";
}
}
5. Compile the smart contract
Clear any existing artrifacts:
npx hardhat clean
Use the zksolc compiler (installed in the above steps) to compile smart contracts for Abstract:
npx hardhat compile --network abstractTestnet
You should now see the compiled smart contracts in the generated artifacts-zk
directory.
6. Deploy the smart contract
Add your private key
Create a new configuration variable
called DEPLOYER_PRIVATE_KEY
.
npx hardhat vars set DEPLOYER_PRIVATE_KEY
Enter the private key of a new wallet you created for this step.
✔ Enter value: · ****************************************************************
Write the deployment script
Create a new Hardhat script located at /deploy/deploy.ts
:
mkdir deploy && touch deploy/deploy.ts
Add the following code to the deploy.ts
file:
import { Wallet } from "zksync-ethers";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { Deployer } from "@matterlabs/hardhat-zksync";
import { vars } from "hardhat/config";
// An example of a deploy script that will deploy and call a simple contract.
export default async function (hre: HardhatRuntimeEnvironment) {
console.log(`Running deploy script`);
// Initialize the wallet using your private key.
const wallet = new Wallet(vars.get("DEPLOYER_PRIVATE_KEY"));
// Create deployer object and load the artifact of the contract we want to deploy.
const deployer = new Deployer(hre, wallet);
// Load contract
const artifact = await deployer.loadArtifact("HelloAbstract");
// Deploy this contract. The returned object will be of a `Contract` type,
// similar to the ones in `ethers`.
const tokenContract = await deployer.deploy(artifact);
console.log(
`${
artifact.contractName
} was deployed to ${await tokenContract.getAddress()}`
);
}
Deploy your smart contract
Run the following command to deploy your smart contracts:
npx hardhat deploy-zksync --script deploy.ts
If successful, your output should look similar to the following:
Running deploy script
HelloAbstract was deployed to YOUR_CONTRACT_ADDRESS
Verify your smart contract on the block explorer
Verifying your smart contract is helpful for others to view the code and interact with it from a block explorer. To verify your smart contract, run the following command:
npx hardhat verify --network abstractTestnet YOUR_CONTRACT_ADDRESS
Note: Replace YOUR_CONTRACT_ADDRESS
with the address of your deployed smart contract.
If successful, your output should look similar to the following:
npx hardhat verify --network abstractTestnet YOUR_CONTRACT_ADDRESS
Your verification ID is: 123
Contract successfully verified on ZKsync block explorer!
Was this page helpful?