Using Hardhat to Deploy To Creator’s Magnet Testnet
Hardhat is an Ethereum development environment that aids developers in managing and automating the repetitive processes that come with developing smart contracts and DApps. Hardhat can communicate directly with Creator’s Ethereum API, allowing it to be used to deploy smart contracts on the platform.
By launching Creator’s Magnet TestNet recently, this post will show you how to compile, deploy, and debug Ethereum smart contracts using Hardhat.
Let’s get started!!
Step 1: To start a new project, we need to create a directory for it:
mkdir hardhat-demo && cd hardhat-demo
After that, initialize the project by running:
npm init -y
You will notice a newly created package.json, which will continue to grow as you install project dependencies.
To get started with Hardhat, we will install it in our newly created project directory:
npm install hardhat
Once installed, run: npx hardhat
This will create a Hardhat config file (hardhat.config.js) in our project directory.
After running the command, choose Create an empty hardhat.config.js:
Step 2: Store the contract file:
We are going to store our contract in the contracts directory. Create it:
mkdir contracts && cd contracts
The smart contract that we’ll deploy as an example will be called Box which let people store a value that can be later retrieved.
We will save this file as contracts/Box.sol:
Step 3: Modify Hardhat Configuration File
We are going to modify our Hardhat configuration file in this phase so that we can compile and deploy this contract to Creator Chain.
If you have not yet done so, create a MetaMask Account, connect to Creator Chain. We will use the private key of the account created to deploy the contract.
We start by requiring the ethers plugin, which brings the ethers.js library that allows you to interact with the blockchain in a simple way. We can install ethers plugin by running:
npm install @nomiclabs/hardhat-ethers ethers
Inside the module.exports, we need to provide the Solidity version (0.8.1 according to our contract file), and the network details
The Hardhat configuration file should look be as the following image:
Next, let’s create a secrets.json, where the private key mentioned before is stored. Make sure to add the file to your project’s .gitignore, and to never reveal your private key. The secrets.json file must contain a private key entry, for example:
{
“privateKey”: “YOUR-PRIVATE-KEY-HERE”
}
Congratulations! We are ready for deployment!
Step 4: Compiling Solidity
Our contract, Box.sol, uses Solidity 0.8.1. Make sure the Hardhat configuration file is correctly set up with this solidity version. If so, we can compile the contract by running:
npx hardhat compile
Step 5: Deploying the Contract
In order to deploy the Box smart contract, we will need to write a simple deployment script. First, let’s create a new directory (scripts). Inside the newly created directory, add a new file deploy.js.
mkdir scripts && cd scripts
touch deploy.js
We start by creating a local instance of the contract with the getContractFactory() method. Next, let’s use the deploy() method that exists within this instance to initiate the smart contract. Lastly, we wait for its deployment by using deployed(). Once deployed, we can fetch the address of the contract inside the box instantiation.
Using the run command, we can now deploy the Box contract to Create Chain:
npx hardhat run — network creatorchain scripts/deploy.js
Congratulations, your contract is live! Save the address, as we will use it to interact with this contract instance in the next step.
Step 6: Interacting with the Contract
Let’s use Hardhat to interact with our newly deployed contract in Creator Chain. To do so, launch hardhat console by running:
npx hardhat console — network creatorchain
Then, add the following lines of code one line at a time. First, we create a local instance of the Box.solcontract once again. Don’t worry about the undefined output you will get after each line is executed:
const Box = await ethers.getContractFactory(‘Box’);
Next, let’s connect this instance to an existing one by passing in the address we obtained when deploying the contract:
const box = await
Box.attach(‘0xe46fd03a3443705BE8192f3AB7867431b9401e89’);
After attaching to the contract, we are ready to interact with it. While the console is still in session, let’s call the store method and store a simple value:
await box.store(5)
The transaction will be signed by your Creatorchain account and broadcast to the network. The output should look similar to:
Notice your address labeled from, the address of the contract, and the data that is being passed. Now, let’s retrieve the value by running:
(await box.retrieve()).toNumber()
We should see 5 or the value you have stored initially.
Congratulations, you have completed deploying a smart contract on Creator Chain using Hardhat!
You also can interact with Creator’s Magnet Testnet using MetaMask or Remix. If you still don’t know how to do it, read the full instructions as the link below:
About Creator
Creator is a Block chain as a Service (BaaS) platform that provides No Code Smart Contract, Low Code DApp and other services to empower the DeFi and NFT World. Creator’s Blockchain features shall broaden the gate for every game studio and developers to enter the gamified NFT and Blockchain industry.
Creator has an incredibly friendly UI and multiple enhanced background services. Hence, with or without IT/ Blockchain skills, by filling out some initial information and a few clicks, anyone can simply go live a Smart Contract or DApp.
Creator’s chain is based on the Polkadot/Substrate for true interoperability, user-driven network governance and customizations that focus on Defi, NFT and other services.
Contact us via this Email address: info@creatorchain.network
Find out more about Creator:
Website | Telegram |Telegram ANN| Twitter | Medium | LinkedIn | Git