How to Deploy Smart Contracts on Base Blockchain

Base is a layer-two (L-2) chain developed by Coinbase, built on OP Stack, a layer-two scaling solution for Ethereum. It's an optimistic rollup chain built on open-sourced OP Stack technology. Base offers a secure, low-cost, and developer-friendly way for anyone to build decentralized apps (DApps) on-chain.

Currently, Base mainnet has not officially launched and users can use its testnet for testing. Base’s mainnet is expected to launch sometime in August.

In this guide, we will walk you through the process of deploying a Non-Fungible Token (NFT) smart contract (ERC-721) on the Base test network using a developer tool called Hardhat.

Getting Started

Before we dive into the deployment process, there are a few prerequisites you need to have in place:

  1. Node v18+: Ensure you have Node version 18 or above installed. You can download it from here.
  2. Coinbase Wallet: To deploy a smart contract, you will need a web3 wallet. You can create a wallet by downloading the Coinbase Wallet browser extension.
  3. Wallet funds: Deploying contracts to the blockchain requires a gas fee. Therefore, you will need to fund your wallet with ETH to cover those gas fees. You can fund your wallet with Base Goerli ETH using options like the Coinbase Faucet | Base Goerli.

Setting Up Your Project

Before deploying smart contracts to Base, you need to set up your development environment by creating a Node.js project. Follow these steps:

  1. Create a new Node.js project by running:

npm init --y

  1. Install Hardhat and create a new Hardhat project by running:

npm install --save-dev hardhat

npx hardhat

Configuring Hardhat with Base

To deploy smart contracts to the Base network, you will need to configure your Hardhat project and add the Base network. This involves adding Base as a network to your project's hardhat.config.ts file and installing the @nomicfoundation/hardhat-toolbox plugin.

Here is a sample configuration:

import { HardhatUserConfig } from 'hardhat/config'; import '@nomicfoundation/hardhat-toolbox'; require('dotenv').config(); const config: HardhatUserConfig = { solidity: { version: '0.8.17', }, networks: { // for mainnet 'base-mainnet': { url: 'https://mainnet.base.org', accounts: [process.env.WALLET_KEY as string], gasPrice: 1000000000, }, // for testnet 'base-goerli': { url: 'https://goerli.base.org', accounts: [process.env.WALLET_KEY as string], gasPrice: 1000000000, }, // for local dev environment 'base-local': { url: 'http://localhost:8545', accounts: [process.env.WALLET_KEY as string], gasPrice: 1000000000, }, }, defaultNetwork: 'hardhat', }; export default config;
import { HardhatUserConfig } from 'hardhat/config';import '@nomicfoundation/hardhat-toolbox';require('dotenv').config();‍const config: HardhatUserConfig = {  solidity: {    version: '0.8.17',  },  networks: {    // for mainnet    'base-mainnet': {      url: 'https://mainnet.base.org',      accounts: [process.env.WALLET_KEY as string],      gasPrice: 1000000000,    },    // for testnet    'base-goerli': {      url: 'https://goerli.base.org',      accounts: [process.env.WALLET_KEY as string],      gasPrice: 1000000000,    },    // for local dev environment    'base-local': {      url: 'http://localhost:8545',      accounts: [process.env.WALLET_KEY as string],      gasPrice: 1000000000,    },  },  defaultNetwork: 'hardhat',};‍export default config;‍

To install @nomicfoundation/hardhat-toolbox, run:

npm install --save-dev @nomicfoundation/hardhat-toolbox

Creating and Compiling the Smart Contract

Next, you will create a smart contract for Base and compile it. Below is a simple NFT smart contract (ERC-721) written in Solidity.

Here is the sample contract:

// SPDX-License-Identifier: MITpragma solidity ^0.8.17;import "@openzeppelin/contracts/token/ERC721/ERC721.sol";import "@openzeppelin/contracts/utils/Counters.sol";‍contract NFT is ERC721 {  using Counters for Counters.Counter;  Counters.Counter private currentTokenId;‍  constructor() ERC721("NFT Name", "NFT") {}‍  function mint(address recipient)    public    returns (uint256)  {    currentTokenId.increment();    uint256 tokenId = currentTokenId.current();    _safeMint(recipient, tokenId);    return tokenId;  }}

You will need to add the OpenZeppelin Contracts library to your project and compile the contract using Hardhat.

npm install --save @openzeppelin/contracts

npx hardhat compile

Deploying the Smart Contract

Once your contract has been successfully compiled, you can deploy the contract to the Base Goerli test network. This involves modifying the scripts/deploy.ts in your project and running a specific command.

Here is a sample deployment script:

import { ethers } from 'hardhat';async function main() {  const nft = await ethers.deployContract('NFT');  await nft.waitForDeployment();  console.log('NFT Contract Deployed at ' + nft.target);}// We recommend this pattern to be able to use async/await everywhere// and properly handle errors.main().catch((error) => {  console.error(error);  process.exitCode = 1;});

To deploy the contract to the Base Goerli test network, run:

npx hardhat run scripts/deploy.ts --network base-goerli

Verifying the Smart Contract

If you want to interact with your contract on the block explorer, you, or someone, needs to verify it first.

To verify your contract, run:

npx hardhat verify --network base-goerli <deployed address>

Interacting with the Smart Contract

Once your contract is verified, you can use the Read Contract and Write Contract tabs to interact with the deployed contract. You'll need to connect your wallet first, by clicking the Connect button.

By following this guide, you should be able to deploy and interact with a smart contract on the Base blockchain.

Looking to Build on Base?

If you wants to build a project on Base blockchain, and looking for development services — Schedule a free consultation with our team today, or email us to discuss your project.

Check Out Our Blogs

STAY UP TO DATE
Get our newsletter
Thank you!
Your submission has been received!
Oops! Something went wrong while submitting the form.
Link Arrow