From 38ae8048eb000fe2b4a23caa912ed855908ef751 Mon Sep 17 00:00:00 2001 From: emmaodia Date: Wed, 21 Feb 2024 09:31:29 +0100 Subject: [PATCH] update: Build on Fuse - Hardhat Guide. --- .../using-hardhat.mdx | 115 ++++++++++++++++-- 1 file changed, 108 insertions(+), 7 deletions(-) diff --git a/docs/developers/deploying-smart-contracts/using-hardhat.mdx b/docs/developers/deploying-smart-contracts/using-hardhat.mdx index e82cf161..14b49796 100644 --- a/docs/developers/deploying-smart-contracts/using-hardhat.mdx +++ b/docs/developers/deploying-smart-contracts/using-hardhat.mdx @@ -6,31 +6,132 @@ title: Using Hardhat Hardhat is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment. -# How to use hardhat on Fuse and Spark +This Guide will walk you throught the steps on how to deploy an ERC-20 Token Contract to Fuse Blockchain using Hardhat. -Following the instructions [here](https://hardhat.org/hardhat-runner/docs/getting-started#overview), run `npx hardhat` to create a hardhat project. +## Pre-requisites -Then in hardhat.config.js add Fuse and Spark to the network section: +- Solidity programming knowledge +- JavaScript knowledge + +Open a new terminal and run these commands to create a new folder: + +```bash +mkdir hardhat-fuse-guide && cd hardhat-fuse-guide +``` + +Inside the folder, initialize a project by running + +```bash +npm init -y +``` + +In the same directory where you installed Hardhat run: + +```bash +npx hardhat init +``` + +Follow the prompts. A `package.json` file is added to the project dir, and we can then install Hardhat by running: + +Note: + +**Do you want to install this sample project's dependencies with npm (hardhat @nomicfoundation/hardhat-toolbox)? (Y/n) › y ** + +```bash +npm install --save-dev hardhat +``` + +Next install the Hardhat toolbox. The `@nomicfoundation/hardhat-toolbox` plugin bundles all the commonly used packages and Hardhat plugins to start developing with Hardhat. + +```bash +npm install --save-dev @nomicfoundation/hardhat-toolbox +``` + +If you are using VS Code Editor, you can add the folder and look at the contents. In this guide, we will focus on the `hardhat.config.js` file, `Contracts` and `Scripts` directories. Open the Contract folder, delete the default `Lock.sol` file and add a new file `Token.sol`. Update the contents of `Token.sol` with the following code: + +```solidity +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts@4.0.0/token/ERC20/ERC20.sol"; +import "@openzeppelin/contracts@4.0.0/token/ERC20/extensions/ERC20Burnable.sol"; +import "@openzeppelin/contracts@4.0.0/access/Ownable.sol"; + +contract MyToken is ERC20, ERC20Burnable, Ownable { + constructor(address initialOwner) + ERC20("MyToken", "MTK") + Ownable() + {} + + function mint(address to, uint256 amount) public onlyOwner { + _mint(to, amount); + } +} +``` + +To compile your contracts run: + +```bash +$ npx hardhat compile +``` + +It will return the response: + +```bash +Compiling... +Compiled 1 contract successfully +``` + +The compiled artifacts will be saved in the artifacts/ directory by default. If you need to customize the Solidity compiler options, then you can do so through the solidity field in your hardhat.config.js + +The next step is to deploy the contracts to the Fuse Blockchain. Open the `Scripts` directory and update the contents of `deploy.js` + +```javascript +const hre = require("hardhat"); + +async function main() { + const token = await hre.ethers.deployContract("Token"); + + await token.waitForDeployment(); + + console.log(`Contract deployed to ${token.target}`); +} + +main().catch((error) => { + console.error(error); + process.exitCode = 1; +}); +``` + +## Deploy Contract + +Open the `hardhat.config.js` file and update the information on Fuse Network ```typescript title="hardhat.config.js" overflow="wrap" module.exports = { // ... networks: { fuse: { - url: 'https://rpc.fuse.io/', + url: "https://rpc.fuse.io/", accounts: { // put dev menomonic or PK here }, }, spark: { - url: 'https://rpc.fusespark.io/', + url: "https://rpc.fusespark.io/", accounts: { // put dev menomonic or PK here }, }, }, // ... -} +}; +``` + +Open a new terminal and deploy the smart contract to the Fuse network + +```bash +npx hardhat run --network fuse scripts/deploy.ts ``` ## How to verify contracts @@ -66,7 +167,7 @@ etherscan: { } ``` -### Now to verify the contract on Fuse: +## Verify the contract on Fuse ```javascript npx hardhat verify --network fuse DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1"