📔Deploying Contracts
Deploying Contracts using Hardhat
You can deploy your contracts in 3Base using hardhat.
What is Hardhat?
Hardhat is a development environment for Ethereum. It helps developer manage and automate building smart contracts. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.
Creating a Hardhat Project
Create a directory for you project
//make adirectory and go inside it
mkdir 3Base_Project && cd 3Base_ProjectInitialize the project with hardhat
npx hardhat init Choose the Configuration of your hardhat project, a sample typescript project(recommended)

Creating Your Smart Contract
Add your contracts to the contracts folder or
Create a contracts directory, if there is none
mkdir contracts && cd contractsCreate your contract in the contracts directory
touch your_contract.solCreating Your Configuration File
Create a secrets.json file to store your private key
touch secrets.jsonAdd your private key to secrets.json
{
"privateKey": "YOUR_PRIVATE_KEY_HERE"
}Add secrets.json to .gitignore, to avoid pushing your private key to github
Modify the hardhat.config.ts file as below
// hardhat.config.ts
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
const { privateKey } = require("./secrets.json");
const config: HardhatUserConfig = {
solidity: {
version: "0.8.24",
},
networks: {
three_base: {
url: `RPC URL`,//INSERT YOUR RPC URL HERE
accounts: [privateKey],
timeout: 60000,
},
},
};
export default config;Deploying Your Smart Contract
Compile your contract
npx hardhat compileCreate a scripts directory and create your deployment scripts
mkdir scripts && cd scripts
touch deploy.tsCreate 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.waitForDeployment();
// 4. Use the contract instance to get the contract address
const contract_address = await your_contract.getAddress();
console.log("Your_Contract deployed to:", contract_address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Deploy your_contract.sol using the command below
npx hardhat run scripts/deploy.tsLast updated
Was this helpful?

