Hardhat Framework

Explains on how to update the smartcontract configuration inside the Hardhat Framework.

We assume that you installed the `seascape` module in your Hardhat project. If not then please follow the Seascape SDK/Installation instructions.

We assume that you already defined how to configure the smartcontracts path. So that you can give it to the website/backend developers. For more reminding please check the Configuration format page.

We also assume that you got the credentials and setted up the .env with the CDN credentials. For more information check the CDN Write module page.

Scripts

Hardhat uses the scripts to deploy the smartcontracts. Therefore we need to update the scripts that deploy the smartcontracts, by adding the CDN Write calls. By convention the scripts in the hardhat framework are kept inside the /scripts directory.

In the top of script files for your smartcontracts, import the CDNWrite module from seascape package. I show example for Javascript and Typescript versions.

// Javascript
let { CdnWrite } = require("seascape");
// Typescript
import { CdnWrite } from "seascape";

What we need to do now is to convert this main function that deploys the smartcontract into the asynchronous function. And wait when the smartcontract is deployed:

// Javascript/Typescript
const smartcontractName = "Greeter";
const Greeter = await ethers.getContractFactory(smartcontractName);
const greeter = await Greeter.deploy("Hello, Hardhat and Seascape JS!");
await greeter.deployed();    // wait for transaction confirmation.

The reason that we have to wait until the deployment end is simple. We will add after it, the code that will update the Seascape CDN. But for that we need to be sure that smartcontract is deployed successfully.

So, let's show the full deployment script that updates the CDN right after the smartcontract deployment:

import { ethers } from "hardhat";
import { CdnWrite } from "seascape";

async function main() {
  let smartcontractName = 'Greeter';
  
  // deploy the smartcontract
  const Greeter = await ethers.getContractFactory(smartcontractName);
  const greeter = await Greeter.deploy("Hello, Hardhat and Seascape JS!");
  let tx = await greeter.deployed();

  // prepare the configurations
  const addresses = await ethers.getSigners();
  let networkId = await addresses[0].getChainId();
  
  let hardhatConfig = {
      networkId: networkId,
      projectName: 'greeter', 
      projectEnv: 'beta', 
      contractType: 'main', 
      contractName: smartcontractName, 
      deployedInstance: greeter
  };

  let cdnUpdated = CdnWrite.setHardhatSmartcontract(hardhatConfig);

  if (!cdnUpdated) {
    console.log("Please update the cdn");
  } else {
    console.log("Greeter smartcontract deployed to:", greeter.address);
  }
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

After deployment, we prepare the parameter of the configuration. The only thing that we need to pass to the CDN updating function is the networkId. The rest of the parameters (owner, txid, abi) are fetched from the smartcontract instance provided by the hardhat framework.

Check the <project name> and <project env>. Those are indicating the path of the configuration file.

Then, we set the configuration onto CDN using the setHardhatSmartcontract() function of CdnWrite module.

The uploaded smartcontract config will be available for checking at:

Once the smartcontract configuration is deployed onto the CDN, as the Smartcontract writer should share the project name, project environment, Smartcontract name, the methods to call, the logs to listen to the appropriate developers.

Last updated