StratoVM: Deploy Your Smart Contract Using HARDHAT

Stratoswap Finance
4 min readJun 26, 2024

--

image: Hardhat

StratoVM is an innovative Layer 2 solution on top of Bitcoin, providing scalability and smart contract functionality while leveraging Bitcoin’s security.

Previously you learn about how to deploy your smart contract using Remix IDE. You can read it here: https://medium.com/@stratoswapfinance/how-to-deploy-smart-contract-on-stratovm-testnet-using-remix-step-by-step-guide-32e526594436

Now on this guide StratoSwap team will walk you through deploying a smart contract on the StratoVM Testnet using Hardhat, a popular Ethereum development environment.

Prerequisites

  1. Node.js v18 or above and npm: Ensure you have Node.js and npm installed. You can download them from Node.js.
  2. MetaMask: Set up MetaMask and configure it for the StratoVM Testnet: https://medium.com/@stratoswapfinance/how-to-add-stratovm-testnet-to-metamask-step-by-step-guide-6a5f8aa8e3ca
  3. Testnet BTC: Obtain some Testnet BTC for deployment gas fees. https://medium.com/@StratoVM_/a-comprehensive-guide-claiming-faucet-earning-the-discord-role-and-minting-stratovm-nfts-23998db11c74

Step-by-Step Guide to deploy smart contract on StratoVM Network using Hardhat

Step 1: Set Up Your Hardhat Project

  1. Create a new directory for your project and navigate into it:
mkdir stratovm-hardhat
cd stratovm-hardhat

2. Initialize a new Hardhat project:

npm init -y
npm install --save-dev hardhat
npx hardhat

3. Follow the prompts. This will create a sample Hardhat project with a basic structure.

✔ What do you want to do? · Create a TypeScript project
✔ Hardhat project root: ·
✔ Do you want to add a .gitignore? (Y/n) · y
✔ Do you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)? (Y/n) · y

Step 2: Configure Hardhat for StratoVM

Edit hardhat.config.ts to add StratoVM Testnet configuration:

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

const config: HardhatUserConfig = {
solidity: "0.8.0",
networks: {
stratovm: {
url: "https://bitcoin-l2-349313-testnet.tc.l2aas.com/rpc",
accounts: ["Replace with your MetaMask private key"]
}
}
};

export default config;

⚠️ Security Tip: Use a .env file to securely store and load your private key. Never expose your private key in the code. It is recommended to installdotenv

Step 3: Write the Smart Contract

  1. Create a new file and name it TestToken.sol inside the contracts folder. You can delete Lock.sol file which is generated automatically by Hardhat.
  2. Copy and paste this code inside TestToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract TestToken {
string public name = "Test Token"; // Replace with your desired token name
string public symbol = "tTKN"; // Replace with your desired token symbol
uint8 public decimals = 18; // Replace with your desired token decimals
uint256 public totalSupply;
address public owner;

mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);

constructor() {
owner = msg.sender;
totalSupply = 1000000 * 10 ** uint256(decimals); // Replace with your desired token suply
balanceOf[owner] = totalSupply;
emit Transfer(address(0), owner, totalSupply);
}

function transfer(address _to, uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value, "Insufficient balance");
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}

function approve(address _spender, uint256 _value) public returns (bool success) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}

function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(_value <= balanceOf[_from], "Insufficient balance");
require(_value <= allowance[_from][msg.sender], "Allowance exceeded");
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
}

3. Compile the contract using Hardhat

npx hardhat compile

Step 4: Write the Deployment Script

  1. Create a folder and name it scripts. Inside the scripts folder create a new file and name it deploy.js
mkdir scripts
touch scripts/deploy.js

2. Copy and paste this code inside deploy.js

async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);

const Token = await ethers.getContractFactory("TestToken");
const token = await Token.deploy();

console.log("Token contract deployed to:", token.address);
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});

Step 5: Deploy the Smart Contract

  1. Deploy your contract by running this code:
npx hardhat run scripts/deploy.js --network stratovm

2. Check explorer to confirm the deployment

You can copy contract address and paste it on your metamask to see your balance

CONGRATULATIONS!!!

You’ve successfully deployed an ERC-20 token on the StratoVM Testnet using Hardhat! This tutorial covered setting up Hardhat, writing a smart contract, and deploying it to StratoVM. This example is a foundation for more complex dApps you might want to build on StratoVM.

Pros and cons: Unlike Remix, which uses a web-based environment, Hardhat requires local installations and some familiarity with the development environment. Hardhat offers a highly customizable setup, along with advanced testing and debugging features, but it can be quite challenging for new developers.

Additional Resources

Stay Connected with StratoSwap:

Website | dApp | Discord | Twitter | Docs | Medium

--

--