Reef dApp Academy: Deploy and Interact with Contracts using Hardhat

Reef dApp Academy: Deploy and Interact with Contracts using Hardhat


Briefing - What is this tutorial all about?

If you're new to developing solutions on Reef, you will definitely come across Reef hardhat, which offers you deployment and interaction with your smart contracts, as known from Ethereum. We will explain the process in 10 steps.

Relevance - Why do I need this tutorial?

We will explain the most important steps to get your system up & running and deploying your first smart contract on Reef. This will spare you a lot of time and provide a smooth starting experience on Reef testnet.

Prerequisites - What do I need to get started?

  1. Reef chain extension
  2. Reef extension account for testnet
  3. How to get REEF tokens for testnet
!drip REEF_ADDRESS

  1. Node.js runtime environment for Javascript
node --version

  1. Git installed (source version control)
git --version

  1. Yarn package manager
yarn --version


πŸš€ Getting started - we are ready to go!

Step1: Visit hardhat-reef-examples on GitHub

https://github.com/reef-defi/hardhat-reef-examples/tree/9d33f8920f0603c8e7c9524e8d2ddc814176c080

Step2: Checkout the hardhat-reef-examples repository

Step3: Choose your directory & clone the repository via HTTPS

git clone <https://github.com/reef-defi/hardhat-reef-examples.git>

Step4: Change directory to hardhat-reef-examples & install dependencies with yarn command

yarn

Step5: Import your project into your favorite IDE & let us look at the code

The code is basically a starter package to deploy and interact with your contracts. The folder contracts contains different smart contract examples. The folder scripts contains code to run and test your smart contract examples.

Step6: Add your mnemonic seed in hardhat.config.js(under reef_testnet)

module.exports = {
  solidity: "0.8.4",
  defaultNetwork: "reef",
  networks: {
    reef: {
      url: "ws://substrate-node:9944",
      scanUrl: "<http://api:8000>",
    },
    reef_testnet: {
      url: "wss://rpc-testnet.reefscan.com/ws",
      scanUrl: "<https://testnet.reefscan.com>", // Localhost verification testing: <http://localhost:3000>
      seeds: {
        testnet_account: process.env.MNEMONIC_TESTNET || "", // SEED GOES HERE
      },
    },
    reef_mainnet: {
      url: "wss://rpc.reefscan.com/ws",
      scanUrl: "wss://reefscan.com",
      seeds: {
        mainnet_account: process.env.MNEMONIC_MAINNET || "",
      },
    },
  },
};

Step7: Now let us check out the deploy.js script again

const hre = require("hardhat");

async function main() {
  // define your testnet_account in hardhat.config.js
  const alice = await hre.reef.getSignerByName("testnet_account");
  await alice.claimDefaultAccount();

  const Flipper = await hre.reef.getContractFactory("Flipper", alice);
  const flipper = await Flipper.deploy(false);

  console.log("Deploy done");
  console.log("Save the address to change the values in existing contract");
  console.log({
    flipper_contract_address: flipper.address,
    deploy_hash: flipper.deployTransaction,
  });
  console.log("Initial value:", await flipper.get());

  // Flip value
  console.log("Flipping value ...");
  await flipper.flip();

  // Check new value
  console.log("New value:", await flipper.get());
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Step8: Deploy your contract with yarn & deploy.js

yarn hardhat run scripts/flipper/deploy.js --network reef_testnet

Step9: After the contract is deployed, you can interact with it using the flip.js

const hre = require("hardhat");

async function main() {
  // Get a Flipper contract on already deployed address
  const flipperAddress = (process.env.FLIPPER_ADDRESS) ? process.env.FLIPPER_ADDRESS :  "0x0230135fDeD668a3F7894966b14F42E65Da322e4";
  
  const alice = await hre.reef.getSignerByName("alice")
  const flipper = await hre.reef.getContractAt("Flipper", flipperAddress, alice);

  // Call flip()
  console.log("Current value:", await flipper.get());
  await flipper.flip();
  console.log("New value after flip():", await flipper.get());
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Step10: Run the script to interact with your smart contract

Now you can use the following command to run the script flip.js and interact with your smart contract flipper.sol

yarn hardhat run scripts/flipper/flip.js --network reef_testnet

Debriefing - What are the most important takeaways from this tutorial?


What's next?

Check out more tutorials about the Reef ecosystem here on our blog. Learn how to claim your EVM address, code a simple smart contract from scratch or how to port your dApp from Ethereum.