November 24, 2021

Creating and deploying a Moonbeam smart contract using hardhat

In this document, we will explain how to create and deploy a Moonbeam smart contract using a local node, using a hardhat

1. To run the local Moonbeam Development Node, we will use docker in our example. Let's prepare our server to use docker:

sudo apt update
sudo apt install wget -y
wget -O get-docker.sh https://get.docker.com
sudo sh get-docker.sh
sudo apt install -y docker-compose
rm -f get-docker.sh

2. Install the local Moonbeam Development Node:

docker pull purestake/moonbeam:v0.14.2
We make a pull request for the current version
docker run --rm --name moonbeam_development --network host \
purestake/moonbeam:v0.14.2 \
--dev
This is the conclusion we should get We launched a local Moonbeam Development Node

3. The local node is running, now let's install Node.js:

sudo apt install curl git
curl https://deb.nodesource.com/setup_12.x | bash  -
sudo apt install nodejs

Сheck that Node.js is installed and its version:

node --version

4. Install hardhat

mkdir hardhat-dev && cd hardhat-dev
npm init --yes
npm install --save-dev hardhat

5. Create a hardhat project, for example, the default parameters are used. You can set the parameters that you need:

npx hardhat
Launch hardhat
The example uses the default values for the sample project

6. Now compile the project

npx hardhat compile

7. Now let's prepare files for deploying a smart contract. We need a config file, a script deployment file and a private key file.
First, let's set up the network to the config file:

nano hardhat.config.js
  networks: {
    dev: {
      url: "http://127.0.0.1:9933",
      chainId: 1281,
      accounts: [privateKey]
    }
  }

!help The config file should look like this:

require("@nomiclabs/hardhat-waffle");

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/**
 * @type import('hardhat/config').HardhatUserConfig
 */
const { privateKey } = require('./secrets.json');


module.exports = {
  solidity: "0.8.4",

  networks: {
    dev: {
      url: "http://127.0.0.1:9933",
      chainId: 1281,
      accounts: [privateKey]
    }
  }
};
The config file looks like this

Now create the deployment script

nano scripts/deploy.js
async function main() {
    //  We get the contract to deploy
    const Greeter = await ethers.getContractFactory('Greeter');
    console.log('Deploying...');

    // Instantiating a new smart contract
    const greeter = await Greeter.deploy('Hi!');

    // Waiting for the deployment to resolve
    await greeter.deployed();
    console.log('Greeter deployed to:', greeter.address);
}

main()
   .then(() => process.exit(0))
   .catch((error) => {
      console.error(error);
      process.exit(1);
   });
The deployment script should look like this

Create a file with a private key from the metamask.
You can get acquainted with the keys for development here.

nano secrets.json
{
   "privateKey": "0x99b3c12287537e38c90a9219d4cb074a89a16e9cdb20bf85728ebd97c343e342"
}
Your private key should look like this

8. Launching the deployment

npx hardhat run scripts/deploy.js --network dev
Conclusion of a successful deployment
Check the logs that our deployment was successful and passed the block

Errors you may encounter:

If you see this picture, then you have not installed a private key. Check config file and private key file

Error due to lack of private key

If you see such a picture, then you missed the argument in the deployment file. In our example, this is an argument const greeter = await Greeter.deploy ('Hi!');

Error for missing argument

Useful links :

https://docs.moonbeam.network/builders/get-started/moonbeam-dev/

https://hardhat.org/tutorial/

https://remix.ethereum.org/