Skip to main content

Hardhat thirdweb Deploy Greeter

To create a new project using this template, use the thirdweb CLI:

npx thirdweb create --template hardhat-thirdweb-deploy-greeter

Using this template

This template showcases a basic Solidity smart contract with a full development and deployment environment set up.


Tools used in this template:

Solidity for the development language of our smart contract

Hardhat for the development environment (testing, debugging, etc.)

thirdweb deploy to deploy the contract to the blockchain without using a private key


Key Commands:

npx thirdweb deploy: Deploy the smart contract

npx hardhat test: Run the test suite (unit tests)


How to use this template

Exploring the Smart Contract

Take a look at the Greeter.sol file, you'll find a smart contract!

It's very basic, but it's a great starting point to explain how to build, test, and deploy smart contracts using Solidity.

Firstly, we declare our contract's License and Solidity Version.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

Then, we define our first contract, called Greeter!

A contract is a smart contract, which is a collection containing:

  1. Functions
  2. Data / State

That live at a specific address on the blockchain.

contract Greeter {

}

We define a variable (data) called greeting, which is a private string.

This just means it is not publicly accessible by other contracts or users.

string private greeting;

The constructor is what gets called when the contract is first created.

When we deploy the contract, we'll let the contract know what the initial value of the greeting variable is, by passing in a string as an argument and setting the value of greeting to that string.

constructor(string memory _greeting) {
greeting = _greeting;
}

Since we made our greeting variable private, we can write a view that reads and returns the value of the greeting variable.

Since this is public, it can be accessed by other contracts or users. You'll also notice the view keyword, which means this function will not modify any state or data in our contract; it simply just returns some data to the caller.

function greet() public view returns (string memory) {
return greeting;
}

Finally, we have a function called setGreeting, which takes in a string as an argument and sets the value of greeting to that string.

This allows a user to change the value of greeting to something else.

function setGreeting(string memory _greeting) public {
greeting = _greeting;
}

Deploying the smart contract

To deploy the contract to the blockchain, run the below script:

npx thirdweb deploy

This command uses thirdweb deploy to:

  1. Compile your smart contract and detect any errors
  2. Upload the contract ABI to IPFS
  3. Generate a URL to deploy the contract on the thirdweb dashboard.

Testing the Contract

To run the test suite and see if your contract works as you expect, run the below script:

npx hardhat test

What's Next

To build a web-app application using this smart contract, check out our next template, "Build a web3 application using thirdweb"!