KRNL Platform Guidebook
KRNL Platformkrnl.xyz
  • Introduction
    • Introduction
    • How Does kOS Work?
  • Setup
    • Getting Started with KRNL
    • Quick Start (CLI)
    • Quick Start (Online IDE)
      • Quick Start (Remix IDE)
      • Quick Start (Atlas IDE)
    • create-krnl-app
    • Platform Registration
  • Kernel
    • What are Kernels?
    • Kernel Registration
      • Kernel Registration (on-chain)
      • Kernel Registration (off-chain)
    • Supported OpenAPI (Off-chain Kernel)
    • Staking
  • Smart Contract
    • Steps for Smart Contract Developers
    • Choosing Kernel(s)
    • Token Authority
      • What is a Token Authority?
      • How to Build a Token Authority?
      • Generic Token Authority
      • How to Deploy a Token Authority on Oasis?
    • Decoding Kernel Responses in Solidity
    • How to Integrate Your Smart Contract with kOS?
    • Smart Contract Registration
  • DApp
    • dApp Registration
    • KRNL SDK Installation
    • Usage
    • KRNL Node RPC
  • Miscellaneous
    • Overview
    • What is the KRNL Platform?
    • What are You Trying to BUIDL?
    • Smart Contract Fundamentals
      • Why Do I Have to Register a Smart Contract?
      • How to Deploy a Smart Contract?
    • Recommended Kernels List
  • Helpful Resources
    • New to Web3?
    • Dictionary
    • Testnet Faucets
    • How to Get Etherscan API Key?
  • Litepaper
    • Litepaper
      • Overview
      • Problem Statement
      • Current State Does Not Solve the Problem
      • Introducing Kernels
      • Ecosystem of Kernels
      • The KRNL Operating System (kOS)
      • Decentralization and Security Considerations
      • Use Cases for KRNL
  • Appendices
    • FAQ
    • Bounty Program
    • Social Medias
    • Thank You
  • Workshop
    • KRNL Workshop
    • Speed's Workshop
Powered by GitBook
On this page
  • Prerequisites
  • Solidity Smart Contract Example
  • Remix
  • Environment
  • Compile
  • Deploy
  • Verify
  • Hardhat
  • Environment
  • Hardhat Configuration
  • Compile
  • Deploy
  • Verify
  • Foundry
  • Environment
  • Deploy
  • Verify

Was this helpful?

  1. Miscellaneous
  2. Smart Contract Fundamentals

How to Deploy a Smart Contract?

PreviousWhy Do I Have to Register a Smart Contract?NextRecommended Kernels List

Last updated 29 days ago

Was this helpful?

Prerequisites

  • Wallet application - for deploying and connecting with blockchain networks. We recommend .

  • Etherscan API key - this can be obtained from Etherscan's website: .

  • Infura project ID - this can be obtained from the Infura website: .


This page is for those who have never written or deployed a smart contract on a blockchain before. It is specifically designed for beginners.

The following sections will focus on using the Sepolia testnet as the blockchain network to deploy a smart contract. We recommend having some test tokens from one of the faucets before attempting to deploy any contract.

There are several methods for deploying smart contracts. On this page, we provide methods using the below tools and frameworks:

The choice of tool depends on each individual's requirements, and the coding language of each tool. Feel free to pick the one you feel most comfortable with.


Solidity Smart Contract Example

Hello World contract

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

contract HelloWorld {
    function sayHelloWorld() public pure returns (string memory) {
        return "Hello World";
    }
}

The Solidity example provided here can be used for testing the deployment of each tool you would like to try. You may write your own smart contract and use this page as a guideline for deployment.


Remix

Using Remix IDE as a tool for writing and deploying smart contracts is a simple procedure. Remix offers a web-based IDE service anyone can access to write and deploy smart contracts through their web browsers. You may also download Remix to your machine.

To access Remix, visit their website here.

Once on the Remix interface, one of the quickest ways to begin is by using their default template. You can initiate the process by choosing Start Coding.

In the project boilerplate, there are a few directories which might need to be acknowledged by the user/s before modifying the code. We recommend reading either the <README.txt> file or the relevant developer documents for more information.

In this guide, we are going to simplify the deployment process by using the default directories and files from this boilerplate.

Remix also supports connectivity with wallet browser extensions, such as MetaMask.

Environment

On the left sidebar, click on the "Deploy & run transactions" section. Select the very first dropdown box and then select the wallet that you want to connect. In this picture, we use MetaMask Flask as a hot wallet which already connects to the Sepolia testnet.

We can write our own smart contract by simply adding a new file and calling it <MyContract.sol>, as in the figure below.

Compile

At this point, the Solidity smart contract will have to be compiled before deploying. Check out "Solidity compiler" from the left sidebar and press "Compile MyContract.sol" as in the picture below.

Deploy

Deploy Using Remix Interface

After the smart contract has been compiled successfully, you may now deploy it. Head to the "Deploy & run transactions" section from the left sidebar to use the interface for deployment. If your smart contract requires constructor(s) as initial parameters for deployment, you must assign the value at this step on the Remix interface.

Deploy Using a Script

For this script method, we will be using <deploy_with_ethers.ts> as the main file for deploying smart contracts onto the network. In this deployment file, the only place you have to edit is the contract name and the arguments (if needed).

import { deploy } from './ethers-lib'

(async () => {
  try {
    const result = await deploy('', [<ARG>]) // match your contract name
    console.log(`address: ${result.address}`)
  } catch (e) {
    console.log(e.message)
  }
})()

Remix allows users to run an individual file by right-clicking and pressing "Run".

It could take a little while after pressing Run for the smart contract to be deployed. The terminal will show the deployed address once complete.

Verify

After deploying, the verification of this deployed contract can be done. Remix has a native plugin for verifying smart contracts on Etherscan. Before that, the plugin will have to be activated by selecting the "Plugin manager" from the sidebar in the bottom-left hand corner, as shown in the below figure.

You must provide the API key from Etherscan.

Now we can do the verification by filing the contract name and contract address of the deployed smart contract.

We can now access our smart contract details through Etherscan.


Hardhat

Environment

Once you are in the Hardhat project and finish writing your smart contract, you can use the command below to compile your contract.

PRIVATE_KEY=YOUR_WALLET_PRIVATE_KEY

ETHERSCAN_API_KEY=YOUR_ETHERSCAN_API_KEY

INFURA_PROJECT_ID=YOUR_INFURA_PROJECT_ID

Hardhat Configuration

You might have to configure hardhat.config.ts for connecting to the mainnet or testnet, depending on your requirements.

We will use the Sepolia testnet as an example of the network that we would like to deploy the smart contract on.

// ...
networks: {
    sepolia: {
      url: `https://sepolia.infura.io/v3/${process.env.}`,
      accounts: [`0x${process.env.}`],
    },
  }
// ...

Compile

Once you have finished writing your smart contract, you can use the command below to compile your contract:

npx hardhat compile

Deploy

The script provided here is an example of a deploy script for a Hardhat project. The parameters for "constructor" are optional, depending on the constructor of each smart contract.

import { ethers } from "hardhat";

async function main() {
  const [deployer] = await ethers.getSigners();
  
  console.log("Deploying contracts with the account:", deployer.address);

  const Contract = await ethers.getContractFactory("ContractName");
  const contract = await Contract.deploy("<PARAM1>", "<PARAM2>"); // constructor

  console.log("Contract deployed to:", contract.target);
  console.log("Contract: ", contract)
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Next, you can deploy your smart contract to the blockchain network, using this Hardhat command:

npx hardhat run <DIRECTORY_OF_YOUR_SCRIPTS>/deploy.ts --network sepolia

After deployment, you can verify your smart contract by configuring the Hardhat config in the below code block and using the Etherscan API key.

// ...
  networks: {
    sepolia: {
      url: `https://sepolia.infura.io/v3/${process.env.<INFURA_PROJECT_ID>}`,
      accounts: [`0x${process.env.<PRIVATE_KEY>}`],
    },
  },
  etherscan: {
    apiKey: process.env.
  }
// ...

Verify

Next, use this CLI to verify your smart contract through Hardhat:

npx hardhat verify --network sepolia \
  <DEPLOYED_CONTRACT_ADDRESS> \
  <ARGUMENTS_IF_NEEDED>

We can now access our smart contract details through Etherscan.


Foundry

There are several ways to deploy smart contracts on a specific blockchain network using Foundry Forge. In this tutorial, we will focus on a simple method that you can follow.

Environment

To begin, after you have created the Foundry project, you can now configure your <.env> file, as in the example below. This is for assigning the crucial variables, such as the wallet’s private key. Keep in mind that the variable names depend entirely on the developer.

PRIVATE_KEY=0x1234567890

ETHERSCAN_API_KEY=<YOUR_ETHERSCAN_API_KEY>

SEPOLIA_RPC=https://sepolia.infura.io/v3/<INFURA_PROJECT_ID>

Foundry projects consist of many directories, including test, script, etc.

Deploy

forge create

This method of forge create is probably the simplest way to deploy your smart contracts using Foundry Forge.

forge create --rpc-url <RPC_URL> \
  --private-key <PRIVATE_KEY> \
  src/<FILE_NAME>.sol:<CONTRACT_NAME> \
  --constructor-args <PARAM1> <PARAM2>

We will be using the smart contract file named Sample.sol, with the contract name SampleContract as an example for this scenario. Since we have other details in the .env file set up already, we can use the variables from that file - assuming that this smart contract does not require parameters when deploying.

forge create --rpc-url $SEPOLIA_RPC \
  --private-key $PRIVATE_KEY \
  src/Sample.sol:SampleContract

The contract name is defined in the Solidity smart contract file as in the code block below:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

contract ContractName {
    // ...
}

Verify

forge verify-contract

Once you have successfully deployed your smart contract, you can verify it by using either Etherscan or Foundry CLI.

Use the command below to verify your smart contract on Etherscan using CLI.

forge verify-contract <DEPLOYED_CONTRACT_ADDRESS> \
  <CONTRACT_DIRECTORY> \
  --chain-id <CHAIN_ID> \
  --etherscan-api-key <ETHERSCAN_API_KEY>

This means that we can fill the required values from the example as in the line below. Given that the chain ID of Sepolia testnet is 11155111.

forge verify-contract <DEPLOYED_CONTRACT_ADDRESS> \
  src/Sample.sol:SampleContract \
  --chain-id 11155111 \
  --etherscan-api-key $ETHERSCAN_API_KEY

You have now successfully verified your smart contract on Etherscan.

For smart contract deployment using Foundry, we recommend exploring their documents .

here
😄
MetaMask
here
here
Remix
Hardhat
Foundry
LogoRemix - Ethereum IDE