Cover photo

Reef dApp Academy: Deploy and Interact with Contracts using Hardhat

πŸ‘‹ Hey there, my name is Filip! Glad you are here & welcome to Reef Academy. These resources are made for you to learn about the Reef ecosystem and ensure a smooth process to get you ready for building on Reef.

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.

πŸ’‘ What is hardhat?
Hardhat is an Ethereum development environment. Compile your contracts and run them on a development network. You can check the official documentation here.

πŸ’‘ What is Reef hardhat?
A custom version of hardhat specifically built for Reef. This means you can use all your known environment tooling from Ethereum, as Reef is fully EVM compatible. Check out our implementation here.

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

πŸ’‘ To get initial REEF tokens on the testnet, visit Reef's Discord server, find the #testnet-faucet channel, and use the following command:

/faucet REEF_ADDRESS

πŸ’‘ We have set up a testnet faucet to automatically distribute REEF to developers and testnet users. 2000 REEF should be more than enough to get you started deploying your first smart contract. Now we are ready to go!

  1. Node.js runtime environment for Javascript

node --version

πŸ’‘ Not installed? Here is how to: node.js package manager


  1. Git installed (source version control)

git --version

πŸ’‘ Not installed? Here is how to: install git


  1. Yarn package manager

yarn --version

πŸ’‘ Not installed? Here is how to: install yarn


πŸŽ— If you have questions at any point feel free to reach out in the Reef Discord Dev Chat!


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

Step 1: Visit hardhat-reef-examples on GitHub

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

πŸ’‘ What is GitHub?
Git is a DevOps tool used for source code management. It is a free and open-source version control system used to handle small to very large projects efficiently. Git is used to track changes in the source code, enabling multiple developers to work
together on non-linear development.

Step 2: Checkout the hardhat-reef-examples repository

Step 3: Choose your directory & clone the repository via HTTPS

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

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

yarn

πŸ’‘ What is yarn?
Yarn is a software packaging system developed for the Node.js JavaScript runtime environment. It is an alternative to the npm package manager. Yarn was created as a collaboration of Facebook, Exponent, Google, and Tilde to solve consistency, security, and performance problems with large codebases.

Step 5: 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.

πŸ’‘ Today we are using the contract flipper.sol. It has two functions, which are flip() and get().

πŸ’‘ The script deploy.js is developed to deploy the contract flipper.sol. To use the script and deploy our smart contract, we need to look at some configs again in the next step.

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

⚠ Important: define your mnemonic seed in hardhat.config.js under reef_testnet. We need to make sure who the owner is and where to deploy our smart contract!

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 || "",
      },
    },
  },
};

πŸ’‘ Copy paste your MNEMONIC SEED into the hardhat.config.js file

Step 7: 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);
  });

πŸ’‘ We can see that we need hardhat const hre = require("hardhat"); and that the script awaits a signer as defined (”testnet_account”). We just defined this in the step before!

Step 8: Deploy your contract with yarn & deploy.js

⚠ Important: Make sure here to select the correct testnet flag. If not set, the system will try to deploy the contract to a local node (which we don’t have set up).

πŸ’‘ What do you mean by the correct flag?
--network reef_testnet

πŸ’‘ What if I want to deploy on mainnet?
--network reef_mainnet

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

βœ… Success! You deployed your first contract on Reef testnet. Congrats!

πŸ’‘ Now, copy-paste the flipper_contract_address from your terminal, as we need it in the next steps!

Step 9: 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);
  });

πŸ’‘ Copy paste your contract address to const flipperAddress to specify the contract you want to interact with (returned from previous script).

πŸ’‘ Change const alice = await hre.reef.getSignerByName**("alice")** to const alice = await hre.reef.getSignerByName("reef_testnet") to correspond to your account name in hardhat.config.js

Step 10: 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

βœ… Success! You interacted with your first smart contract on Reef testnet. Congrats!


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

πŸ“– You learned how to install and use our Reef hardhat implementation. You also learned how to deploy a smart contract and interact with it! Congrats on the first steps to entering the ecosystem.

βœ… Use Reef Extension and Reef Accounts using Reef Testnet

βœ… Get REEF tokens for Reef Testnet

βœ… Installed hardhat, configured scripts, and used to deploy & interact with your first smart contract on Reef Testnet


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.

Loading...
highlight
Collect this post to permanently own it.
Reef Chain News logo
Subscribe to Reef Chain News and never miss a post.
#reefchain#hardhat#solidity
  • Loading comments...