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
docker pull purestake/moonbeam:v0.14.2
docker run --rm --name moonbeam_development --network host \ purestake/moonbeam:v0.14.2 \ --dev
sudo apt install curl git
curl https://deb.nodesource.com/setup_12.x | bash -
sudo apt install nodejs
node --version
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
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] } } };
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); });
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" }
npx hardhat run scripts/deploy.js --network dev
If you see this picture, then you have not installed a private key. Check config file and private key file
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!');
https://docs.moonbeam.network/builders/get-started/moonbeam-dev/