Deploy on Tangle using Hardhat
Pre-requisites and Assumptions
This guide to deploying a smart contract on a Substrate-based blockchain network with EVM compatibility (similar to Moonbeam) using Hardhat assumes a basic understanding of Blockchain, Smart Contracts, Solidity, Hardhat and Substrate: You should be familiar with Ethereum as it forms the basis of any EVM-compatible blockchain.
If any of the above assumptions do not hold true, we recommend taking the time to fill in the gaps in your knowledge and setup before proceeding. This will ensure that you can follow along with the guide effectively.
Setup
-
Install Node.js and npm Make sure you have Node.js and npm installed. The recommended versions are Node v14.17.6 and npm v6.14.15.
-
Install Hardhat Create a new directory and initialize an npm project. Then install Hardhat using npm:
mkdir <project-name>
cd <project-name>
npm init -y
npm install --save-dev hardhat
- Create a new Hardhat project Run the following command to create a new Hardhat project:
npx hardhat
Select "Create an empty hardhat.config.js" when prompted.
Configure Hardhat for Tangle Network
- Install necessary plugins
Install
@nomiclabs/hardhat-ethers
,@nomiclabs/hardhat-waffle
,ethereum-waffle
, andethers
plugins:
npm install --save-dev @nomiclabs/hardhat-ethers @nomiclabs/hardhat-waffle ethereum-waffle ethers
- Update hardhat.config.js Open hardhat.config.js and replace its content with the following configuration, updating the placeholder fields:
require("@nomiclabs/hardhat-waffle");
module.exports = {
solidity: "0.8.0",
networks: {
tangle: {
url: "<Tangle RPC URL>",
accounts: [`0x${<PRIVATE_KEY>}`],
chainId: <Tangle ChainId>,
gasPrice: 10000000000,
}
}
};
Tangle Chain Information
You can view the latest details on networks, chainIDs, RPC URLs on the Network Information and Configurations page.
Replace <Tangle RPC URL>
with the RPC URL of the Tangle Network. Replace <PRIVATE_KEY>
with the private key of the account that will be used to deploy the contracts. <Tangle ChainId>
should be replaced with the ChainId of the Tangle Network.
Deploy Contracts
- Compile Contracts Assuming your contracts are in the contracts folder, you can compile them by running:
npx hardhat compile
- Create a deployment script Create a new directory named scripts in your project root, then create a file in this directory, say deploy.js, with the following content:
async function main() {
const [deployer] = await ethers.getSigners();
console.log(
"Deploying contracts with the account:",
deployer.address
);
console.log("Account balance:", (await deployer.getBalance()).toString());
const Contract = await ethers.getContractFactory("<YourContract>");
const contract = await Contract.deploy(<YourContractParameters>);
console.log("Contract address:", contract.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Replace <YourContract>
with the name of your contract and <YourContractParameters>
with the parameters required by your contract's constructor.
- Run the deployment script You can now deploy your contract to Tangle Network using Hardhat by running:
npx hardhat run --network tangle scripts/deploy.js
After running this command, Hardhat will execute the deployment script using the account and network configuration provided in hardhat.config.js
.
Make sure you update <Tangle RPC URL>
, <PRIVATE_KEY>
, <Tangle ChainId>
, <YourContract>
, and <YourContractParameters>
with your actual values.