Time to read: 1 min
Deploy Your First Contract
In this module, you will write, compile, deploy, and interact with your first smart contract on the Rootstock Testnet.
Step 1 — Write the Contract
Create: contracts/HelloRootstock.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract HelloRootstock {
string public message;
constructor(string memory _message) {
message = _message;
}
function updateMessage(string memory _newMessage) public {
message = _newMessage;
}
}
Step 2 — Configure Hardhat
Edit: hardhat.config.js
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.20",
networks: {
rootstock_testnet: {
url: "https://public-node.testnet.rsk.co",
chainId: 31,
accounts: [process.env.PRIVATE_KEY]
}
}
};
Add your private key to .env:
PRIVATE_KEY=your_private_key_here
Step 3 — Create Deployment Script
Create: scripts/deploy.js
async function main() {
const Contract = await ethers.getContractFactory("HelloRootstock");
const contract = await Contract.deploy("Hello from Rootstock!");
await contract.waitForDeployment();
console.log("Contract deployed at:", await contract.getAddress());
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Step 4 — Compile and Deploy
Compile:
npx hardhat compile
Deploy:
npx hardhat run scripts/deploy.js --network rootstock_testnet
Expected output:
Compiled successfully
Contract deployed at: 0x1234...abcd
Check contract on the Rootstock Testnet Explorer.
Step 5 — Interact With the Contract
Use Hardhat console:
npx hardhat console --network rootstock_testnet
In console:
const c = await ethers.getContractAt(
"HelloRootstock",
"0xYourContractAddress"
);
await c.message(); // read
await c.updateMessage("Updated!"); // write
Summary
You now know how to:
- Write a smart contract
- Configure Hardhat for Rootstock
- Deploy to Testnet
- Interact via terminal
Next: Build Your First dApp