How to deploy a contract in Foundry using Catapulta?
Introduction
Deploying smart contracts is a crucial step in bringing your decentralized applications (dApps) to life on the blockchain. However, the deployment process can sometimes feel cumbersome, with multiple steps involved from setting up the environment to ensuring successful execution on the blockchain. In this blog, we’ll walk you through how to deploy a contract in Foundry using Catapulta.sh, a tool designed to simplify the deployment process.
Foundry, a powerful Ethereum development framework, allows developers to write, test, and deploy smart contracts with ease. On the other hand, Catapulta.sh is a deployment platform that streamlines the process by offering a one-click deployment solution with automatic verification and multi-chain support. By combining the strengths of both, this guide aims to help you deploy your smart contracts efficiently and securely.
The focus of this blog is to simplify the deployment of Foundry-based contracts using Catapulta.sh, guiding you step-by-step through the entire process.
Prerequisites
Before diving into the deployment process, ensure you have the following tools and dependencies installed:
- Git: A version control system used to manage and clone repositories.
- Node.js (v18 LTS or later): A JavaScript runtime that is required for running various tools, including Catapulta CLI.
- Foundry: A smart contract development framework that will help you build and test your contract locally before deployment.
Make sure these are installed and properly configured before moving on to the next steps.
Set up Catapulta
To get started with deploying your contract using Catapulta, you need to set up an account and project within the platform. Follow these steps:
- Sign up for a free account at Catapulta.sh: Visit Catapulta.sh and create an account to access the platform’s features. You’ll be required to provide basic information and verify your email address.
- Create a new project in the Catapulta dashboard: Once logged in, navigate to the dashboard and create a new project. This will be the environment where you manage your deployments.
- Retrieve the
CATAPULTA_API_KEY
from the project settings: In your project settings, you’ll find an API key, which is essential for interacting with Catapulta through the CLI. Copy this key and keep it handy, as you’ll need it in later steps to authenticate your deployment requests.
Once your Catapulta account and project are set up, you're ready to move on to the next steps in the deployment process.
Install Catapulta CLI
Now that you've set up your Catapulta project, it's time to install the Catapulta CLI, which will allow you to interact with the platform directly from your terminal.
-
Install the Catapulta CLI globally: To get started, run the following command in your terminal to install the Catapulta CLI globally using npm:
npm i -g catapulta
This will download and install the Catapulta CLI on your system.
-
Verify the installation: After the installation is complete, verify that Catapulta was installed correctly by checking its version:
catapulta -v
If the installation was successful, the version number of Catapulta will be displayed, confirming that you can now use the CLI to deploy contracts.
Prepare Your Foundry Project
With the Catapulta CLI installed, the next step is to set up your Foundry project. Foundry will provide the environment to write and test your smart contract before deploying it to the blockchain.
-
Clone the repository: Start by cloning the official Catapulta Foundry template repository. This template will provide a basic structure to work with, including the necessary configuration for deployment via Catapulta.
Run the following commands:
git clone https://github.com/catapulta-sh/catapulta-foundry-template cd catapulta-foundry-template
-
Install Foundry dependencies: After cloning the repository, navigate into the project folder and install the Foundry dependencies by running:
forge install
This command installs the required libraries and dependencies for Foundry, allowing you to compile and deploy your contract using the framework.
Configure Your Project
Now that your project is set up, it's time to configure it to work with Catapulta for deployment.
-
Create and configure the
.env
file: In the root directory of your project, create a new.env
file. This file will store sensitive information such as your Catapulta API key and private key for deployment.touch .env
-
Add the
CATAPULTA_API_KEY
the.env
file: Open the.env
file and add the following lines:CATAPULTA_API_KEY=your_api_key_here
-
Create a demo wallet, with a private key, to use for testnet deployments:
npx catapulta wallet
This command will generate a new private key and automatically add it to your
.env
file. Do not use in production, it's only for testing purposes. For production, use a Ledger or keystore wallet.
Now that your project is configured with the necessary keys, you're ready to move on to writing the deployment script.
Write Your Deployment Script
To deploy your contract using Catapulta, you'll need to write a Solidity deployment script. This script defines the steps to compile, deploy, and broadcast your contract to the blockchain. Here’s an example of a basic deployment script:
-
Create a new deployment script: In your project, navigate to the
scripts
directory and create a new file calledDeploy.sol
:// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import {Script} from "forge-std/Script.sol"; import {YourContract} from "../src/YourContract.sol"; contract DeployScript is Script { function run() public { uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); // Fetch the private key from the .env file vm.startBroadcast(deployerPrivateKey); // Start the broadcast to the blockchain using the deployer's private key new YourContract(); // Deploy the contract vm.stopBroadcast(); // Stop broadcasting the deployment transaction } }
-
Script explanation:
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
: This line retrieves the private key from the.env
file to authorize the deployment.vm.startBroadcast(deployerPrivateKey);
: This command starts broadcasting the deployment transaction to the blockchain using the deployer's private key.new YourContract();
: This line actually deploys the contract. ReplaceYourContract
with the name of your Solidity contract.vm.stopBroadcast();
: Stops the broadcast, signaling the end of the transaction.
This script will handle the deployment of your smart contract when executed.
Deploy Your Contract
Now that your deployment script is written, it’s time to deploy your contract using the Catapulta CLI. Follow these steps to initiate the deployment process:
-
Deploy the contract using the Catapulta CLI: Run the following command to deploy your contract:
catapulta script scripts/Deploy.sol --network sepolia --sponsor
catapulta script scripts/Deploy.sol
: This command tells Catapulta to execute theDeploy.sol
script located in thescripts
directory.-network sepolia
: This flag specifies the network on which you want to deploy the contract. In this case, it's the Sepolia testnet. You can change this to any other supported network, likemainnet
,rinkeby
, orgoerli
, depending on your needs.
-
Automatic deployment steps: Once the command is executed, Catapulta will automatically handle several important tasks:
- Compiling your contracts: Catapulta will compile your smart contracts before deployment, ensuring they are optimized and ready for deployment.
- Uploading artifacts: It will upload the compiled contract artifacts (bytecode and ABI) to the Catapulta platform for tracking and interaction.
- Broadcasting the transaction: The transaction for contract deployment will be broadcast to the selected blockchain network (e.g., Sepolia), using the private key stored in your
.env
file. - Contract verification: After broadcasting the deployment transaction, Catapulta will automatically verify your contract on Etherscan or the relevant block explorer, making your contract’s source code publicly accessible and verifiable.
Once the deployment is successful, you’ll receive a deployment report in your Catapulta dashboard, where you can view contract addresses, transaction hashes, and verification status.
With these steps, your contract will be live on the blockchain and verified, ready for interaction!
View Deployment Results
Once the deployment process is complete, you can view the results directly in your Catapulta dashboard. Catapulta provides detailed reports and insights into the deployment process, making it easy to track the success of your deployment and manage your contracts.
- Deployment Report: After deploying your contract, Catapulta generates a comprehensive deployment report. This report includes valuable information about the deployment process and outcomes, such as:
- Contract addresses: The unique address of your deployed contract on the selected blockchain network.
- Transaction hashes: The transaction ID associated with your contract deployment, allowing you to track the deployment transaction on the blockchain.
By accessing these results, you can monitor your contract’s deployment status, interact with your deployed contract, and share the verification details with others.
Conclusion
In this guide, we’ve walked through the simple yet powerful process of deploying a smart contract using Foundry and Catapulta.sh. By combining the development power of Foundry with the deployment capabilities of Catapulta, you can streamline the deployment process and save time.
Key benefits of using these tools include:
- Multi-chain support: Catapulta allows you to deploy contracts on various blockchain networks, providing flexibility for your projects.
- Automated verifications: With automatic contract verification on block explorers, Catapulta ensures that your contract’s source code is publicly available and trustworthy.
- Detailed reports: The comprehensive deployment reports in the Catapulta dashboard offer transparency, making it easy to track contract addresses, transaction hashes, and verification status.
By following these steps, deploying a smart contract becomes a hassle-free and efficient process, letting you focus more on building and less on the technicalities of deployment.