Deploy a contract

We will deploy your first Contract on Specular together using two of our favorite tools, all of this while covering each part of the process step by step.

Let's dive in! Happy hacking!

Choose your preferred environment


One of our favorite tools for development is Foundry. Foundry is a smart contract development toolchain with which you can manage dependencies, compile, run tests, deploy, and interact with the Specular chain from the CLI while using Solidity!

Prerequisites

  1. JavaScript Knowledge: Some familiarity with JavaScript is assumed.

  2. Node.js and NPM/Yarn: Make sure you have Node.js and either npm or Yarn installed on your computer. You can install them from the official Node.js website: Node.js.

  3. Rust: Rust is required for certain functionalities of Foundry. You can install Rust by running the following command in your terminal:

Install Rust

Drop this command in your terminal to install Rust! If you have any problems, head over here to understand them better!

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Install Foundry

Foundryup is the Foundry toolchain installer. You can find more about it here but what we cover here is enough to get you started.

$ curl -L https://foundry.paradigm.xyz | bash
$ foundryup

These commands will install Foundry's amazing precomipled binaries, forge, cast, avail and chisel. For this mini tutorial, we will focus on using forge. With forge, we can easily test, build, and deploy our smart contracts. Let's dive in and make the most of this powerful tool!

Create a Project

Now that you have your environment set up, let's create a template project by running the following command:

$ forge init hello_specular

Notice that a project has been initialized, and it looks like this:

Foundry is indeed amazing as it creates this fantastic template that consists of the following folders:

  1. src: This folder contains the smart contracts.

  2. test: Here, you can find tests for the smart contract functionality.

  3. script: In this folder, you'll see the contract deployment scripts.

    For the mini tutorial, we'll be using the existing Counter contract for our Specular deployment. It's a simple contract used for setting up and incrementing the value of an integer with the functions setNumber() and increment().

pragma solidity ^0.8.13;

contract Counter {
    uint256 public number;

    function setNumber(uint256 newNumber) public {
        number = newNumber;
    }

    function increment() public {
        number++;
    }
}

Build our contracts

The following command compiles our contracts located in the src folder and generates a new out folder where all the compiled contracts artifacts reside.

$ forge build

After "building" our Foundry project file structure is updated and our Counter contract is now ready to be deployed!

Test our contracts

Before spending any funds on deploying the contract, it is a common practice to conduct testing. This way, we can check if there are any errors in our code syntax or semantics.

Running the following command will execute the test files written in Solidity under the test folder. This will help ensure that our smart contract functions as intended and validate its correctness before proceeding with the deployment:

$ forge test

Deploy

Our deployment process is going to be fairly simple, involving just one significant command execution. If the smart contract were more complex, for example, requiring parameters upon creation or invoking other contracts, we would have used the deployment script located under the scripts folder.

For the deployment, you will need to have your private key, which, for safety purposes, should be stored in a variable. This helps protect your private key from accidental exposure.

Let's proceed with the deployment, ensuring that we handle sensitive information like private keys securely throughout the process. Safety first!

Be careful, never share or upload your private key publicly! As long as you keep it in your CLI you are safe.

Export your private key as a variable with the follwing command:

$ export PRIVATE_KEY=<YOUR_PRIVATE_KEY>

Finally deploy it to Specular using the RPC URL and your previously exported private key:

forge create --rpc-url https://devnet.specular.network/ --private-key $PRIVATE_KEY src/Counter.sol:Counter --legacy

Once you are done, you should receive the address your contract is deployed to and the transactions hash!

You can take a closer look at the transaction using the transaction hash on our block explorer.

Congratulations! You just deployed your first contract on our network!

Last updated