Skip to main content

👷 Deploy to Etherlink with Hardhat

Hardhat is a suite of tools for working with Ethereum contracts and dApps. You can use Hardhat to deploy contracts to Etherlink just like you use it on other networks:

  1. Set up a new Hardhat project by running npx hardhat init.

  2. Add Etherlink as a network by putting the Etherlink RPC node and your private key in the hardhat.config.js file, as in this example:

    import "@nomicfoundation/hardhat-toolbox";
    import 'dotenv/config';

    const config = {
    solidity: "0.8.19",
    networks: {
    etherlinkTest: {
    url: "https://node.ghostnet.etherlink.com",
    accounts: [process.env.MY_PRIVATE_KEY],
    }
    }
    };

    export default config;
  3. Create a contract or use the default contract in the contracts folder.

  4. Compile the contract by running npx hardhat compile.

  5. Update the default scripts/deploy.js file to deploy your contract. For example, if your contract is named "MyContract.sol" and accepts a string value and an integer value in the constructor, the deployment script looks like this example:

    import { ethers } from "hardhat";

    async function deployMyContract() {
    const deployedContract = await ethers.deployContract("MyContract", ["Hello", 5]);
    await deployedContract.waitForDeployment();
    }

    deployMyContract().catch((error) => {
    console.error(error);
    process.exitCode = 1;
    });
  6. Run the deployment script on the Etherlink network with this command:

    npx hardhat run --network etherlinkTest scripts/deploy.js

    This command refers to the etherlinkTest network in the Hardhat config file.

  7. Copy the address of the deployed contract from the log.

Now you can interact with the contract from Hardhat. For example, if the contract has a function named "DoSomething," you can call it with this code:

import { ethers } from "hardhat";

async function callMyContract() {
const MyContract = await ethers.getContractFactory("MyContract");
const contract = MyContract.attach(
// The deployed contract address
"0xcDd461a8dD3d519111Fe7a4ac2b9563AcEbF5B7c"
);

await contract.DoSomething();
}

callMyContract().catch((error) => {
console.error(error);
process.exitCode = 1;
});

Then you can run this script on the Etherlink network with the command npx hardhat run --network etherlinkTest scripts/myScript.js.