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

Hardhat Developer Workflow Tutorial #524

Merged
merged 68 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
cd5b7dc
intro and structure
themacexpert Jan 18, 2023
c718e45
hardhat tutorial checkin
themacexpert Jan 25, 2023
5effff2
update hardhat tutorial
themacexpert Jan 26, 2023
b13cc21
spelling and grammar
themacexpert Jan 26, 2023
a2eca99
formatting and various updates
themacexpert Jan 26, 2023
63840ae
merge tutorials
themacexpert Feb 1, 2023
651451e
Update tutorials/hardhat-tutorial.md
themacexpert Feb 1, 2023
bffebe1
Update tutorials/hardhat-tutorial.md
themacexpert Feb 2, 2023
a977d6c
Update tutorials/hardhat-tutorial.md
themacexpert Feb 2, 2023
e84ac49
Update tutorials/hardhat-tutorial.md
themacexpert Feb 2, 2023
48a4c62
Update tutorials/hardhat-tutorial.md
themacexpert Feb 2, 2023
aa191d2
Update tutorials/hardhat-tutorial.md
themacexpert Feb 2, 2023
72c2080
Update tutorials/hardhat-tutorial.md
themacexpert Feb 2, 2023
3790b02
Update tutorials/hardhat-tutorial.md
themacexpert Feb 2, 2023
20cc042
feedback updates
themacexpert Feb 2, 2023
da7e8d7
remove punctuation from end of comments
themacexpert Feb 2, 2023
de5a428
formatting consistency
themacexpert Feb 2, 2023
d978601
feedback updates
themacexpert Feb 2, 2023
b8de7d7
feedback updates
themacexpert Feb 2, 2023
8083037
feedback updates
themacexpert Feb 2, 2023
ab7ef7d
longer introduction and feedback updates
themacexpert Feb 2, 2023
439dae5
seo description
themacexpert Feb 2, 2023
5900665
add prereqs
themacexpert Feb 2, 2023
01ab626
feedback updates
themacexpert Feb 2, 2023
2abf0c6
feedback updates
themacexpert Feb 2, 2023
e9cd3a2
feedback updates
themacexpert Feb 2, 2023
2ccc0a0
feedback updates
themacexpert Feb 2, 2023
909f9b0
feedback updates
themacexpert Feb 2, 2023
e822381
image updates and tutorial updates
themacexpert Feb 2, 2023
5c57f37
add secrets.json image
themacexpert Feb 2, 2023
e59b05d
update images and feedback updates
themacexpert Feb 2, 2023
f6bbe9b
feedback updates
themacexpert Feb 2, 2023
2984de1
move test file to code snippet and more
themacexpert Feb 2, 2023
f8ef43c
feedback updates
themacexpert Feb 2, 2023
a6ccba5
remove fixtures and revise test files
themacexpert Feb 2, 2023
b790dac
feedback updates
themacexpert Feb 2, 2023
5a65745
feedback updates
themacexpert Feb 2, 2023
2939bbd
change moonbaseAlpha to moonbase
themacexpert Feb 2, 2023
9785de3
feedback updates
themacexpert Feb 2, 2023
84ba510
feedback updates
themacexpert Feb 2, 2023
fa2b07f
clean up and clarify test
themacexpert Feb 3, 2023
50928a5
feedback updates
themacexpert Feb 3, 2023
e2a1147
add moonbeam deployment steps
themacexpert Feb 3, 2023
6d5dbc2
add deployment and verif steps for moonbeam
themacexpert Feb 3, 2023
bec8abd
spelling and grammar
themacexpert Feb 3, 2023
be48ba5
improve verification steps
themacexpert Feb 3, 2023
7cdd818
improve introduction
themacexpert Feb 3, 2023
e50b236
solidity version
themacexpert Feb 3, 2023
89157a1
revise images
themacexpert Feb 3, 2023
887c5c7
seo stuff
themacexpert Feb 3, 2023
a5ef6e9
update test file etc
themacexpert Feb 3, 2023
5f2ddc9
clean up
themacexpert Feb 3, 2023
e8bf5a7
apply formatting changes and update config section
eshaben Feb 7, 2023
d1868a6
revise testing section to recommend moonbeam local dev node
themacexpert Feb 8, 2023
77e6eae
formatting updates
themacexpert Feb 8, 2023
e06b1cc
Update .snippets/text/hardhat/hardhat-configuration-file.md
themacexpert Mar 9, 2023
ab49b9e
Update tutorials/hardhat-tutorial.md
themacexpert Mar 9, 2023
902e6e8
Update tutorials/hardhat-tutorial.md
themacexpert Mar 9, 2023
276e300
Update tutorials/hardhat-tutorial.md
themacexpert Mar 9, 2023
2808cd7
Update tutorials/hardhat-tutorial.md
themacexpert Mar 9, 2023
f833362
feedback updates
themacexpert Mar 9, 2023
5541105
feedback updates
themacexpert Mar 9, 2023
8212b1e
merge master
themacexpert Mar 9, 2023
cd98505
feedback updates
themacexpert Mar 9, 2023
f895283
feedback updates
themacexpert Mar 9, 2023
4b1e952
feedback updates
themacexpert Mar 9, 2023
55ba68d
updates
themacexpert Mar 9, 2023
fccf62b
update images, add disclaimers, and add minor updates
eshaben Mar 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
85 changes: 85 additions & 0 deletions .snippets/code/hardhat/dao-js-test-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Import Hardhat and Hardhat Toolbox
const { ethers } = require("hardhat");
require("@nomicfoundation/hardhat-toolbox");

// Import Chai to use its assertion functions here
const { expect } = require("chai");

// Indicate the collator the DAO wants to delegate to
const targetCollator = "0x4c5A56ed5A4FF7B09aA86560AfD7d383F4831Cce";

// The describe function receives the name of a section of your test suite, and a
// callback. The callback must define the tests of that section. This callback
// can't be an async function
describe("Dao contract", function () {
async function deployDao() {
// Get the contract factory and signers here
const [deployer, member1] = await ethers.getSigners();
const delegationDao = await ethers.getContractFactory("DelegationDAO");

// Deploy the staking DAO and wait for the deployment transaction to be confirmed
const deployedDao = await delegationDao.deploy(
targetCollator,
deployer.address
);
await deployedDao.deployed();

// Add a new member to the DAO
await deployedDao.grant_member(member1.address);

// Return the deployed DAO to allow the tests to access and interact with it
return { deployedDao };
}

// You can nest calls to create subsections
describe("Deployment", function () {
// Mocha's it function is used to define each of your tests.
// It receives the test name, and a callback function
//
// If the callback function is async, Mocha will await it
it("should store the correct target collator in the DAO", async function () {
// Set up our test environment by calling deployDao
const { deployedDao } = await deployDao();

// The expect function receives a value and wraps it in an assertion object.
// This test will pass if the DAO stored the correct target collator
expect(await deployedDao.target()).to.equal(targetCollator);
});

// The following test cases should be added here
it("should initially have 0 funds in the DAO", async function () {
const { deployedDao } = await deployDao();

// This test will pass if the DAO has no funds as expected before any contributions
expect(await deployedDao.totalStake()).to.equal(0);
});

it("should not allow non-admins to grant membership", async function () {
const { deployedDao } = await deployDao();

// We ask ethers for two accounts back this time
const [deployer, member1] = await ethers.getSigners();

// We use connect to call grant_member from member1's account instead of admin.
// This test will succeed if the function call reverts and fails if the call succeeds
await expect(
deployedDao
.connect(member1)
.grant_member("0x0000000000000000000000000000000000000000")
).to.be.reverted;
});

it("should only allow members to access member-only functions", async function () {
const { deployedDao } = await deployDao();

// We ask ethers for two accounts back this time
const [deployer, member1] = await ethers.getSigners();

// This test will succeed if the DAO member can call the member-only function.
// We use connect here to call the function from the account of the new member
expect(await deployedDao.connect(member1).check_free_balance()).to.equal(
0
);
});
});
});
27 changes: 27 additions & 0 deletions .snippets/text/hardhat/creating-a-hardhat-project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
You will need to create a Hardhat project if you don't already have one. You can create one by completing the following steps:

1. Create a directory for your project
```
mkdir hardhat && cd hardhat
```
2. Initialize the project which will create a `package.json` file
```
npm init -y
```
3. Install Hardhat
```
npm install hardhat
```
4. Create a project
```
npx hardhat
```

!!! note
`npx` is used to run executables installed locally in your project. Although Hardhat can be installed globally, it is recommended to install it locally in each project so that you can control the version on a project by project basis.

5. A menu will appear which will allow you to create a new project or use a sample project. For this example, you can choose **Create an empty hardhat.config.js**

![Hardhat Create Project](/images/builders/build/eth-api/dev-env/hardhat/hardhat-1.png)

This will create a Hardhat config file (`hardhat.config.js`) in your project directory.
6 changes: 6 additions & 0 deletions .snippets/text/hardhat/hardhat-configuration-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Next you can take the following steps to modify the `hardhat.config.js` file and add Moonbase Alpha as a network:

1. Import plugins. The Hardhat Ethers plugin comes out of the box with Hardhat, so you don't need to worry about installing it yourself
2. Import the `secrets.json` file
3. Inside the `module.exports`, you need to provide the Solidity version (`0.8.1` according to our contract file)
themacexpert marked this conversation as resolved.
Show resolved Hide resolved
4. Add the Moonbase Alpha network configuration
34 changes: 34 additions & 0 deletions .snippets/text/hardhat/using-the-hardhat-etherscan-plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

To get started with the Hardhat Etherscan plugin, you will need to first install the plugin library:

```
npm install --save-dev @nomiclabs/hardhat-etherscan
```

!!! note
Support for Moonbeam-based networks was added in version 3.0.1 of `@nomiclabs/hardhat-etherscan`. You can double check what version you're using by looking under the `devDependencies` section of your `package.json` and updating to version 3.0.1 or greater if needed.

You can add your Moonscan API key to the `secrets.json` file alongside your private key. For this example, you'll need a [Moonbeam Moonscan](https://moonscan.io/){target=_blank} API key. If you want to verify a contract on Moonriver, you'll need a [Moonriver Moonscan](https://moonriver.moonscan.io/){target=_blank} API key.

From within your Hardhat project, open your `hardhat.config.js` file. You'll need to import the `hardhat-etherscan` plugin, your Moonscan API key, and add the config for Etherscan:

```js
require("@nomiclabs/hardhat-etherscan");

const { privateKey, moonbeamMoonscanAPIKey, moonriverMoonscanAPIKey } = require('./secrets.json');

module.exports = {
networks: {
moonbeam: { ... },
moonriver: { ... },
moonbaseAlpha: { ... }
},
etherscan: {
apiKey: {
moonbeam: moonbeamMoonscanAPIKey, // Moonbeam Moonscan API Key
moonriver: moonriverMoonscanAPIKey, // Moonriver Moonscan API Key
moonbaseAlpha: moonbeamMoonscanAPIKey, // Moonbeam Moonscan API Key
}
}
};
```
11 changes: 3 additions & 8 deletions builders/build/eth-api/dev-env/hardhat.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,10 @@ Then add your private key to it:

Make sure to add the file to your project's `.gitignore`, and to never reveal your private key.

!!! note
!!! remember
Please always manage your private keys with a designated secret manager or similar service. Never save or commit your private keys inside your repositories.

Next you can take the following steps to modify the `hardhat.config.js` file and add Moonbase Alpha as a network:

1. Import the Ethers plugin
2. Import the `secrets.json` file
3. Inside the `module.exports`, you need to provide the Solidity version (`0.8.1` according to our contract file)
4. Add the Moonbase Alpha network configuration
--8<-- 'text/hardhat/hardhat-configuration-file.md'

```js
// 1. Import the Ethers plugin required to interact with the contract
Expand Down Expand Up @@ -213,7 +208,7 @@ touch deploy.js

Next, you need to write your deployment script which can be done using `ethers`. Because you'll be running it with Hardhat, you don't need to import any libraries.

To get started start, take the following steps:
To get started, take the following steps:

1. Create a local instance of the contract with the `getContractFactory` method
2. Use the `deploy` method that exists within this instance to instantiate the smart contract
Expand Down
34 changes: 1 addition & 33 deletions builders/build/eth-api/verify-contracts/etherscan-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,7 @@ You will then be prompted to enter in an **AppName** for your API key and once y

The example in this section of the guide will be based off of the `Box.sol` contract that was created in the [Using Hardhat to Deploy To Moonbeam](/builders/build/eth-api/dev-env/hardhat/){target=_blank} guide.

To get started with the Hardhat Etherscan plugin, you will need to first install the plugin library:

```
npm install --save-dev @nomiclabs/hardhat-etherscan
```

!!! note
Support for Moonbeam-based networks was added in version 3.0.1 of `@nomiclabs/hardhat-etherscan`. You can double check what version you're using by looking under the `devDependencies` section of your `package.json` and updating to version 3.0.1 or greater if needed.

You can add your Moonscan API key to the `secrets.json` file alongside your private key. For this example, you'll need a [Moonbeam Moonscan](https://moonscan.io/){target=_blank} API key. If you want to verify a contract on Moonriver, you'll need a [Moonriver Moonscan](https://moonriver.moonscan.io/){target=_blank} API key.

From within your Hardhat project, open your `hardhat.config.js` file. You'll need to import the `hardhat-etherscan` plugin, your Moonscan API key, and add the config for Etherscan:

```js
require("@nomiclabs/hardhat-etherscan");

const { privateKey, moonbeamMoonscanAPIKey, moonriverMoonscanAPIKey } = require('./secrets.json');

module.exports = {
networks: {
moonbeam: { ... },
moonriver: { ... },
moonbase: { ... }
},
etherscan: {
apiKey: {
moonbeam: moonbeamMoonscanAPIKey, // Moonbeam Moonscan API Key
moonriver: moonriverMoonscanAPIKey, // Moonriver Moonscan API Key
moonbaseAlpha: moonbeamMoonscanAPIKey, // Moonbeam Moonscan API Key
}
}
};
```
--8<-- 'text/hardhat/using-the-hardhat-etherscan-plugin.md'

To verify the contract, you will run the `verify` command and pass in the address of the deployed contract and the network where it's deployed:

Expand Down
Binary file added images/index-pages/tutorials/hardhat-tutorial.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tutorials/hardhat/hardhat-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tutorials/hardhat/hardhat-2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tutorials/hardhat/hardhat-3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tutorials/hardhat/hardhat-4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tutorials/hardhat/hardhat-5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tutorials/hardhat/hardhat-6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tutorials/hardhat/hardhat-7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tutorials/hardhat/hardhat-8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tutorials/hardhat/hardhat-banner.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tutorials/.pages
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ nav:
- index.md
- remote-staking-xcm.md
- batch-approve-swap.md
- hardhat-tutorial.md
- foundry-start-to-end.md
- truffle-dev-life-cycle.md

- truffle-dev-life-cycle.md