Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: Build on Fuse - Hardhat Guide. #122

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 108 additions & 7 deletions docs/developers/deploying-smart-contracts/using-hardhat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]/token/ERC20/ERC20.sol";
import "@openzeppelin/[email protected]/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/[email protected]/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
Expand Down Expand Up @@ -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"
Expand Down