Deploying to a testnet is cheap (and free). Deploying to BNB mainnet costs real BNB. Before going live, make sure your Hardhat config is pointing to the right network and your wallet is funded.
TL;DR: Deploy a smart contract to BNB Chain mainnet using Hardhat, then verify it on BscScan.
Stack: Hardhat, Solidity, BNB Chain, BscScan, dotenv
Level: Intermediate
Reading time: ~6 min
Configure .env
BSC_URL=https://bsc-dataseed.binance.org/
MNEMONIC=your twelve word mnemonic phrase here
npm i dotenv
Add BNB network to hardhat.config.ts
import dotenv from "dotenv";
dotenv.config();
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
const config: HardhatUserConfig = {
solidity: "0.8.24",
defaultNetwork: "local",
networks: {
local: {
url: "http://127.0.0.1:8545",
chainId: 31337,
accounts: { mnemonic: "test test test test test test test test test test test junk" }
},
bnb: {
url: process.env.BSC_URL,
accounts: { mnemonic: process.env.MNEMONIC }
}
},
};
export default config;
Deploy to BNB Chain
# Add to package.json scripts:
# "deploy:bnb": "npx hardhat ignition deploy ignition/modules/SimpleBank.ts --network bnb"
npm run deploy:bnb
Verify on BscScan
Go to bscscan.com to find your contract, then verify the source code so users can read and interact with it directly from the BscScan interface. Get an API key from bscscan.com and add it to your .env, then run:
npx hardhat verify --network bnb 0xYourContractAddress
What you’ve built
A smart contract deployed to BNB Chain mainnet and verified on BscScan. The source code is publicly visible and users can interact with it directly through the BscScan interface.
Next steps
- Never commit your private key or mnemonic to source control. Add .env to .gitignore before your first commit.
- Estimate gas costs before deployment by testing on BSC testnet first.
- After verification, write a frontend that connects to the deployed contract address using ethers.js or web3.js.
Questions or feedback? Find me on LinkedIn or GitHub.