Build
Deploy with Hardhat
Add Lib Testnet to a Hardhat project, deploy with a script or Ignition, and interact with contracts from the console.
Hardhat talks to LibChain over standard JSON-RPC. This guide uses Hardhat 2 with @nomicfoundation/hardhat-toolbox; the network settings are the same in Hardhat 3.
#Configure
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox dotenv
npx hardhat initimport 'dotenv/config'
import type { HardhatUserConfig } from 'hardhat/config'
import '@nomicfoundation/hardhat-toolbox'
const config: HardhatUserConfig = {
solidity: {
version: '0.8.28',
settings: { evmVersion: 'cancun', optimizer: { enabled: true, runs: 200 } },
},
networks: {
libTestnet: {
url: 'https://rpc-testnet.libchain.tech',
chainId: 54210,
accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [],
},
},
}
export default configPRIVATE_KEY=0x... # a testnet-only key; add .env to .gitignore#Deploy with a script
import { ethers } from 'hardhat'
async function main() {
const counter = await ethers.deployContract('Counter')
await counter.waitForDeployment()
console.log('Counter deployed at', await counter.getAddress())
}
main().catch((e) => { console.error(e); process.exitCode = 1 })npx hardhat run scripts/deploy.ts --network libTestnet#Deploy with Ignition
import { buildModule } from '@nomicfoundation/hardhat-ignition/modules'
export default buildModule('CounterModule', (m) => {
const counter = m.contract('Counter')
m.call(counter, 'setNumber', [1n])
return { counter }
})npx hardhat ignition deploy ignition/modules/Counter.ts --network libTestnetIgnition waits for confirmations between steps. On LibChain one confirmation is final, so the defaults only add a little waiting.
#Use the console
npx hardhat console --network libTestnetconst wtlib = await ethers.getContractAt(['function totalSupply() view returns (uint256)'], '0x87052f1f17b2b4054b014e93e53d6ce6dc912bda')
ethers.formatEther(await wtlib.totalSupply())hardhat-verify needs a verification backend, which isn't enabled on Lib Testnet yet. See Verify a contract.