Hardhat
What is Hardhat?
Hardhat (opens in a new tab) is a development environment for Ethereum that helps developers manage and automate the common tasks involved in building smart contracts and decentralized applications. It can directly interact with Xterio Chain (BNB)'s Ethereum API, allowing for the deployment of smart contracts into the Xterio Chain (BNB).
Additionally, Hardhat is a comprehensive set of tools for creating Ethereum-based software, which includes various components that aid in editing, compiling, debugging, and deploying smart contracts and decentralized applications. All of these components work together to create a complete development environment.
In this example, let's deploy a simple contract in Xterio Testnet.
Prerequisites
Before we start, make sure you have met the following conditions:
-
Have some tBNB on Xterio Testnet Network. You can follow our guide on Bridging for Xterio Chain (BNB).
-
Node.js and npm must be installed on your computer. If it's not, follow this guide (opens in a new tab).
Creating a Hardhat Project
-
Create a directory for your project:
mkdir hardhat && cd hardhat
-
Initialize the project, which will create a
package.json
filenpm init -y
-
Install Hardhat:
npm install --save-dev hardhat
-
Start a new Hardhat project with TypeScript:
npx hardhat init
-
Create an empty
hardhat.config.js
and install the Ethers plugin to use the Ethers.js library to interact with the network.npm install @nomiclabs/hardhat-ethers ethers
Creating Your Smart contract
- Create
contracts
directory:mkdir contracts && cd contracts
- Create
your_contract.sol
file in contracts directorytouch your_contract.sol
Creating Your Configuration File
Modify the Hardhat configuration file and create a secure file to store your private key in.
- Create a
secrets.json
file to store your private keytouch secrets.json
- Add your private key to
secrets.json
{ "privateKey": "YOUR-PRIVATE-KEY-HERE" }
- Add the file to your project's
.gitignore
, and never reveal your private key. - Modify the
hardhat.config.js
file- Import the Ethers.js plugin
- Import the
secrets.json
file - Inside the
module.exports
add the Xterio Chain (BNB) configuration
require('@nomiclabs/hardhat-ethers'); const { privateKey } = require('./secrets.json'); module.exports = { solidity: "0.8.1", defaultNetwork: "rinkeby", networks: { rinkeby: { url: "https://eth-rinkeby.alchemyapi.io/v2/123abc123abc123abc123abc123abcde", accounts: [privateKey] }, xterioTestnet: { url: "https://xterio-testnet.alt.technology/", // XterioTestnet RPC URL Here chainId: 1637450, // XterioTestnet Chain ID Here } }, }
Notice:
Avoid uploading your
hardhat.config.ts
file to GitHub or disclosing your private key to others. It's a frequent error to accidentally push your private key to GitHub, so ensure you includehardhat.config.ts
in your.gitignore
file to prevent this.
Deploying Your Smart Contract
- Compile the contract
npx hardhat compilejs
- Create a new directory for the script and name it scripts and add a new file to it called
deploy.js
mkdir scripts && cd scripts touch deploy.js
- Create a deployment script, like the one below
async function main() { // 1. Get the contract to deploy const Your_Contract = await ethers.getContractFactory('your_contract'); console.log('Deploying Your_Contract...'); // 2. Instantiating a new smart contract const your_contract = await Your_Contract.deploy(); // 3. Waiting for the deployment to resolve await your_contract.deployed(); // 4. Use the contract instance to get the contract address console.log('Your_Contract deployed to:', your_contract.address); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
- Deploy
your_contract.sol
using the command belownpx hardhat run scripts/deploy.js --network xterioTestnet