Deploy a smart contract

Overview for deploying a smart contract with Hardhat, Remix, Foundry, and Thirdweb.

Chain Information

Deploy with Hardhat

1. Install Hardhat

Install the hardhat framework in your project, by typing the following command:

$ npm install --save-dev hardhat

2. Create a hardhat project

After installing Hardhat, create a project. You can create it using typescript or javascript in the following screen:

$ npx hardhat init

888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

Welcome to Hardhat v2.22.8 

? What do you want to do?
❯ Create a JavaScript project
  Create a TypeScript project
  Create a TypeScript project (with Viem)
  Create an empty hardhat.config.js
  Quit

3. Create a Smart contract and Compile protect

After creating the smart contract in the “Contracts” folder, run the following command to check your code.

$ npx hardhat compile 

5. Set the hardhat configuration file and deploy the smart contract

After creating the ignition deploy file, let's set the blockchain information into the hardhat.config file, with the following information:

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

const config: HardhatUserConfig = {
  solidity: "0.8.20", // Solidity Version
  networks: {
    kiichain: {
      url: "<https://a.sentry.testnet.kiivalidator.com:8645>",
      chainId: 123454321,
      accounts: [`0x${yourApiKey}`],
    },
  },
};

export default config;
$ npx hardhat ignition deploy ignition/modules/<contractName>.ts --network kiichain

Deploy with Remix

Remix is a web compiler page where you can write smart contracts, scripts and deploy using your wallet from Metamask. You can enter Remix here.

In the Workspace create the file which contains the Smart contract with the .sol extension.

After writing the smart contract, go to the Solidity Compiler section and select your compiler version, is recommended to use the most recent one.

Then press “Compile” button under the Compiler selector.

3. Select your Metamask wallet as Deploy’s environment

In the “Deploy and Run Transactions” section, move to the Environment and select your Metamask Provider, remember you must be connected to KiiChain Testnet

4. Deploy Smart contract

Select the smart contract to be deployed and write the Constructor parameters is Remix detects them

Finally, you can interact with the different functions in the same section above the Deploy button.

Deploy with Foundry

1. Create a project and installation

Run the following commands in your terminal in order to start the foundry’s installation.

curl -L [<https://foundry.paradigm.xyz>](<https://foundry.paradigm.xyz/>) | bash

then run the following command in another terminal or restart the current one for finishing the installation process.

foundryup

After finishing the foundry’s installation let’s create a new project, run the following commands for starting the new project and then move to it’s folder:

forge init projectName
cd projectName

2. Write and build a contract

Write your smart contract in ‘/src/smartContractName.sol’ and the script in ‘/script/smartContractName.s.sol’, here is an example of that:

This contract will be placed in the path: /src/Counter.sol

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

contract Counter {
    uint256 public number;

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

    function increment() public {
        number++;
    }
    
    
    function getCounter() public view returns (uint256) {
        return number;
    }
}

This is the script and will be placed in /script/Counter.s.sol remember to add the .s before the .sol extension.

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

import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";

contract CounterScript is Script {
    Counter public counter;

    function setUp() public {}

    function run() public {
        vm.startBroadcast();

        counter = new Counter();

        vm.stopBroadcast();
    }
}

then run the following command for build and check that everything is correct.

forge build

3. Deploy

First, you need to run a simulation.

forge script script/Counter.s.sol:CounterScript --fork-url <https://a.sentry.testnet.kiivalidator.com:8645/> --private-key yourPrivateKey

The simulation will be complete after that, and you can run with broadcast.

forge script script/Counter.s.sol:CounterScript --fork-url <https://a.sentry.testnet.kiivalidator.com:8645/> --private-key yourPrivateKey --broadcast

A success message should appear.

Deploy with Thirdweb

In this example, we will create a ERC721 (Non fungible token).

1. Create an account

Create an account in thrirdweb, create an API key here, and link your wallet.

Storage the API key in a secure place.

2. Create a project

npx thirdweb create

A message appears

Need to install the following packages:
thirdweb@5.46.1
Ok to proceed? (y) y

After the installation this screen should appear, select “Contract”

    $$\\     $$\\       $$\\                 $$\\                         $$\\
    $$ |    $$ |      \\__|                $$ |                        $$ |
  $$$$$$\\   $$$$$$$\\  $$\\  $$$$$$\\   $$$$$$$ |$$\\  $$\\  $$\\  $$$$$$\\  $$$$$$$\\
  \\_$$  _|  $$  __$$\\ $$ |$$  __$$\\ $$  __$$ |$$ | $$ | $$ |$$  __$$\\ $$  __$$\\
    $$ |    $$ |  $$ |$$ |$$ |  \\__|$$ /  $$ |$$ | $$ | $$ |$$$$$$$$ |$$ |  $$ |
    $$ |$$\\ $$ |  $$ |$$ |$$ |      $$ |  $$ |$$ | $$ | $$ |$$   ____|$$ |  $$ |
    \\$$$$  |$$ |  $$ |$$ |$$ |      \\$$$$$$$ |\\$$$$$\\$$$$  |\\$$$$$$$\\ $$$$$$$  |
     \\____/ \\__|  \\__|\\__|\\__|       \\_______| \\_____\\____/  \\_______|\\_______/

 💎 thirdweb v0.14.12 💎

? What type of project do you want to create? » - Use arrow-keys. Return to submit.
    App
>   Contract
    Dynamic Contract Extension

Select the following options:

 What type of project do you want to create? » Contract
 What is your project named? ... ERC721-Example
 What framework do you want to use? » Forge
 What will be the name of your new smart contract? ... MyContract
 What type of contract do you want to start from? » ERC721
 What extensions do you want to add to your contract? » None

3. Write a smart contract and deploy

Enter the directory and run the following command:

cd erc721-example
npx thirdweb deploy -k yourApiKey

Open the link and fill in the fields.

In chain choose

add Custom Network

Finally, select Deploy Now and accept the transaction in your wallet.

Confirming Smart Contract on Explorer App

Remember that you can check the smart contracts Deployed here.

Last updated

Logo