Developers

Deploy Smart Contract

Smart contracts are computer programs that help automate business tasks. They're an essential part of blockchain technology, which allows for secure and decentralized transactions. To deploy a smart contract there are several steps you need to follow. You need to create the contract, set up the development environment, compile the code, and then deploy it to Syncport using a virtual wallet (Metamask). Let's look at each of these steps in more detail.

Prerequisites

Before you begin deploying smart contracts to your rollup, ensure you have the following:

  • Node.js and npm installed on your system

  • Completed the "Adding Syncport Testnet to a wallet" tutorial

  • Completed the "Bridge funds" tutorial, to cover the gas fees for the contract deployment transaction


Step 1: Initiating Our Project

To get started, let's create a folder for our project. Open your command line and type:

Shell


Once you're inside the project folder, we can initialize the project using npm init.

Shell

Step 2: Downloading and Setting Up Hardhat

Hardhat is a powerful development environment designed for Ethereum software development. It lets you compile, deploy, test, and debug your smart contracts and dApps locally before deploying them to the live chain.

Inside our rollup-contract-deployment project, run:

Shell

Step 3: Creating a Hardhat Project

Inside our rollup-contract-deployment project folder, run:

Shell

After running the above command, a welcome message will appear, giving you different options to choose from. Initialize a "empty-hardhat-config-js" project, and this will generate a hardhat.config.js file.

Step 4: Creating Project Folders

To keep your project neat and tidy, create two new folders:

Shell


  • contracts/: This is where you'll store all of your smart contract code files.

  • deployments/: This folder will keep all the necessary scripts for deploying and interacting with your contract.


Step 5: Writing Contract

To create your first smart contract, follow these simple steps:

  1. Open your project in your code editor. In this tutorial, we'll be using VS Code.

  2. Navigate to the "contracts" folder and create a new file called FirstContract.sol.

  3. Copy and paste the following Hello World smart contract from the Ethereum Foundation into your FirstContract.sol file.

Shell


This smart contract is straightforward and easy to use. When it's created, it stores a message. You can update this message by calling the "update" function.

Step 6: Integrate Metamask and Alchemy with Your Project

To send any transaction from your virtual wallet, you need to sign it with your unique private key. You can safely store your private key (as well as your metamask private key) in an environment file.

To get started with using environment files, you'll need to install the dotenv package in your project directory.

Shell

Note: Make sure to name your environment file .env - this is the standard naming convention.

Shell


For your metamask private key, follow these instructions

Step 7: Installing Ethers.js

Ethers.js is a library that simplifies Ethereum interaction by wrapping standard JSON-RPC methods with more user-friendly methods. And with Hardhat's plugin integration, you can easily extend its functionality.

Shell

Step 8: Updating hardhat.config.js File

In your project directory, you will find a file named hardhat.config.js, update this file to match the provided code:

Javascript

require('dotenv').config();
require("@nomiclabs/hardhat-ethers");

const { API_URL, PRIVATE_KEY } = process.env;

module.exports = {
  solidity: "0.7.3",
  defaultNetwork: "rollup",
  networks: {
    hardhat: {},
    rollup: {
      url: API_URL,
      accounts: [`0x${PRIVATE_KEY}`]
    }
  },
}

Step 9: Compiling Contract

To confirm that everything is working properly, we will compile our contract using the built-in compile task in hardhat.

Shell

Step 10: Writing Deploy Script

Go to the /deployments folder and create a new file named deploy.js. Then, copy and paste the following contents into the file:

Javascript

async function main() {
  const HelloWorld = await ethers.getContractFactory("HelloWorld");
  const hello_world = await HelloWorld.deploy("Hello World!");
  console.log("Contract Deployed to Address:", hello_world.address);
}
main()
  .then(() => process.exit(0))
  .catch(error => {
    console.error(error);
    process.exit(1);
  });

Step 11: Deploying Contract

Let's deploy our smart contract. Open your command line and enter the following command:

Shell

After executing the previous command, you should see something like this:

Contract Deployed to Address: 0x34417A0CA9b3aebd...770a716fa8

Go to Explorer to verify