How to deploy Smart Contracts to Somnia using Thirdweb.

Thirdweb is a complete web3 development framework that offers everything you need to connect your apps or games to the Somnia network. Its service allows developers to build, manage, and analyze their Web3 applications.

This tutorial will guide you through deploying a Smart contract to the Somnia Devnet using Thirdweb’s command-line tool (`thirdweb deploy`). Thirdweb simplifies deployment and interaction with smart contracts on Somnia.

Prerequisites

  • This guide is not an introduction to Solidity Programming; you are expected to understand Basic Solidity Programming.

  • To complete this guide, you will need MetaMask installed and the Somnia DevNet added to the list of Networks. If you have yet to install MetaMask, please follow this guide to Connect Your Wallet.

  • Thirdweb CLI: Install globally with:

npx thirdweb install

Set Up the Project

First, create a new folder for your project and initialize it.

mkdir somnia-thirdweb-example
cd somnia-thirdweb-example

Write the Smart Contract

You can write your Smart Contract using the Remix IDE to ensure it works. Create a file OpenGreeter.sol and add the following code:

OpenGreeter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

contract OpenGreeter {
    string public name;
    address public owner;

    event NameChanged(string oldName, string newName);

    constructor(string memory _initialName) {
        name = _initialName;
        owner = msg.sender;
           }

    function changeName(string memory _newName) public {
        string memory oldName = name;
        name = _newName;
        emit NameChanged(oldName, _newName);
    }

    function greet() external view returns (string memory) {
        return string(abi.encodePacked("Hello, ", name, "!"));
    }
}

This is a simple Greeter Smart Contract that any address can call the changeName function. The Smart Contract has two functions: changeName - This function allows anyone to change the name variable. It stores the old name, updates the name variable, and emits the NameChanged event with the old and new names.

greet - This function returns a greeting message that includes the current name. It uses abi.encodePacked to concatenate strings efficiently.

Deploy the Smart Contract using Thirdweb.

First, go to Thirdweb and create a profile. After you have completed the onboarding process, create a Project.

Go to the project settings.

Copy your secret key, and keep it safe. The secret key will be used to deploy the Smart Contract.

Go to the terminal and paste the following command to deploy the Smart Contract:

npx thirdweb deploy -k your_secret_key

Select the solc option to be true in the prompts on Terminal.

Click the link to open the User Interface in your Browser to deploy the Smart Contract.

Enter an initialName. Select the Network as Somnia Devnet. Check the option to import it to the list of Contracts in your Thirdweb Dashboard. Click on Deploy Now and approve the Metamask prompts.

Your Smart Contract is deployed, and you can view it on your Thirdweb Dashboard.

Visit the Explorer section to simulate interactions with your deployed Smart Contract and carry out actual transactions.

Congratulations. πŸŽ‰ You have deployed your Smart Contract to the Somnia Network using Thirdweb. πŸŽ‰

Last updated