Introduction
Creator L2 is a high-performance Layer 2 blockchain built on the OP Stack, offering developers scalability, low transaction fees, and a robust environment for building decentralized applications (dApps). In this guide, we’ll walk you through deploying a smart contract on Creator L2 using Solidity, Hardhat, and Viem.
Prerequisites
Before deploying a smart contract on Creator, ensure you have the following:
- Node.js installed (v16 or later)
- Hardhat (Ethereum development environment)
- Metamask or any EVM-compatible wallet
- Test ETH on Creator L2 (available via the Creator faucet)
Step 1: Set Up Your Development Environment
Start by creating a new project and installing dependencies:
mkdir creator-smart-contract && cd creator-smart-contract
npm init -y
npm install --save-dev hardhat
Initialize Hardhat:
npx hardhat
Choose “Create an empty hardhat.config.js” and proceed.
Step 2: Create a Smart Contract
Create a new directory for contracts and write a simple Solidity contract:
mkdir contracts && touch contracts/MyContract.sol
Edit MyContract.sol
:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract MyContract {
string public message;
constructor(string memory _message) {
message = _message;
}
}
Step 3: Configure Hardhat for Creator L2
Modify hardhat.config.js
:
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.19",
networks: {
creator: {
url: "https://rpc.creatorchain.io",
accounts: ["YOUR_PRIVATE_KEY"]
}
}
};
Replace YOUR_PRIVATE_KEY
with your wallet’s private key.
Step 4: Compile and Deploy
Compile the contract:
npx hardhat compile
Deploy using a deployment script (scripts/deploy.js
):
const hre = require("hardhat");
async function main() {
const MyContract = await hre.ethers.getContractFactory("MyContract");
const contract = await MyContract.deploy("Hello, Creator!");
await contract.deployed();
console.log("Contract deployed at:", contract.address);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
Run the deployment script:
npx hardhat run scripts/deploy.js --network creator
Step 5: Interacting with the Contract using Viem
To interact with your contract in a frontend app, install Viem:
npm install viem
Use Viem to read the contract state:
import { createPublicClient, http } from 'viem';
import { creatorTestnet } from 'viem/chains';
const client = createPublicClient({
chain: creator,
transport: http()
});
const contractAddress = "YOUR_DEPLOYED_CONTRACT_ADDRESS";
const abi = [
"function message() public view returns (string)"
];
async function getMessage() {
const message = await client.readContract({
address: contractAddress,
abi,
functionName: "message"
});
console.log("Contract message:", message);
}
getMessage();
Conclusion
You’ve successfully deployed a smart contract on Creator L2 and interacted with it using Viem. This is just the beginning — start building your dApps on Creator today!
For more resources, visit Creator’s Documentation.
#Creator #SmartContract #Web3 #Superchain
Connect with Us on Socials:
Website | Twitter | Blog | Discord | Telegram