How to Deploy Any Smart Contract Using the thirdweb CLI

How to Deploy Any Smart Contract Using the thirdweb CLI - thirdweb Guides

]

In this guide, we'll show you how to use the CLI to deploy any smart contract with one command.

By the end, we'll create a smart contract and deploy it to the blockchain without ever writing a script or exposing our private key.

Let's get started!

Background

Deploying smart contracts is generally a tedious task. In a typical workflow, you run the risk of exposing private keys in deployment scripts.

This process has the potential to be very dangerous; as a number of people accidentally push their private keys to GitHub; subsequently losing all their funds when their wallets are compromised to bots.

Using the CLI, you can deploy your smart contracts on the blockchain with just one click and the most important thing is there's no private keys involved!

The thirdweb CLI supports hardhat, foundry and bare solidity file (using solc) for compiling and deploying your smart contracts on the blockchain.

Creating A Smart Contract

If you don't already have a smart contract, navigate to a safe directory and run the following command to initialize a new smart contract project.

npx thirdweb@latest create --contract

You can choose to start from a blank canvas or from one of our base contracts.

Once your contract is set up, you're ready to deploy it to the blockchain!

Deploying A Smart Contract

Instead of using Hardhat and providing your private key to deploy the contract using a script, you can use thirdweb deploy to do so without exposing your private keys.

To deploy the smart contract we just made, run the following command in the terminal:

npx thirdweb@latest deploy

This will trigger a compilation process, and then the ABI for the contract will be uploaded to IPFS. When the upload is complete, you should see your terminal providing a link to deploy the contract through your browser:

Uploading contract data using thirdweb CLI

If your browser doesn't pop open automatically, paste the link provided on the terminal into your browser. Now, a page should be opened on your browser prompting you to deploy your contract:

Deploying the contract using the browser

As you can see, thirdweb automatically detects common features from your smart contract. In this example, it detects that my smart contract is an ERC-721 contract and detects additional features along with it.

Now, you can choose if you want the contract to be listed on the dashboard or not, choose the correct network and click on Deploy Now.

Once the deployment is complete, you should be redirected to your contract dashboard and you can see the contract explorer that lets you interact with various functions of your smart contract:

Contract explorer on thirdweb dashboard

Also on the top you can find tabs for each feature you implemented in your Solidity code; such as the NFTs tab.

NFT feature on smart contract detected by thirdweb

Conclusion

The CLI provides a safe and easy-to-use method of deploying smart contracts to the blockchain, without worrying about deploy scripts and dealing with private keys.

Learn more about thirdweb deploy in the documentation.