How to Deploy and Verify Using Sourcify On BuildBear Sandbox with Hardhat and Foundry

How to Deploy and Verify Using Sourcify On BuildBear Sandbox with Hardhat and Foundry

·

4 min read

In this tutorial, we’ll guide you through the process of deploying DeFi Protocol and verifying its source code on the BuildBear sandbox integrating with the Sourcify plugin.

Why is Source Code Verification Important?

Ensuring the security and reliability of smart contracts is crucial, especially in decentralized finance (DeFi) Protocols. Source code verification allows developers and users to verify that smart contracts behave as intended, avoiding risks of vulnerabilities and ensuring trust in the protocol’s functionality.

Introducing the Sourcify Verification Tool

Sourcify is a smart contract verification tool designed specifically for open-sourced and decentralized contracts on various EVM-based networks. Unlike some other verification services, Sourcify focuses on full matches with metadata hashes, ensuring a more thorough verification process. It enhances contract interactions by leveraging the ABI and NatSpec comments from the metadata file, making interactions more user-friendly.

Deploying DeFi Protocol and Verifying using Sourcify Plugin on Foundry

We will deploy Uniswap v3 contracts containing pool contracts that receive liquidity from users and make swaps within a price range. Foundry natively supports verification through Sourcify. We can use the --verifier and --verifier-url flags to enable Sourcify verification on Foundry.

Setting Development Environment

  • Foundry installation. Refer to this guide here.

  • Create a private sandbox. If you are new to BuildBear, refer to our comprehensive documentation to sign up and create your sandbox here.

  • Installing the Sourcify Plugin

Install the Sourcify plugin for your sandbox to enable Sourcify Verification. Click on the Plugins button, select Sourcify from the All Plugins tab, and install it. Once installed, it will appear in the Installed Plugins tab.

To get started with the project, fork and clone the uniswapv3 repo using the following command:

git clone https://github.com/BuildBearLabs/Sourcify-verify.git
 cd Foundry-Sourcify

Install all necessary dependencies. forge install will add dependencies as a git submodule in your project.

yarn install

Before deploying your contract, ensure that your account has enough tokens to cover transaction costs. You can obtain tokens from the faucet in your sandbox dashboard.

Deploy and Verify Smart Contract

To deploy and verify your contract using the Forge command, you’ll need to pass your RPC URL, private key, and Verifier URL. Your Verifier URL will follow this format: https://rpc.buildbear/verify/sourcify/server/{sandboxId or customRPC}.

forge create --rpc-url https://rpc.dev.buildbear.io/sanam --private-key 670e8f5f547a3bc4bd1869d473591ff10f6cf0d69a254257b61eca3f242ef188 script/DeployDevelopment.s.sol:DeployDevelopment --verify --verifier sourcify --verifier-url https://rpc.buildbear.io/verify/sourcify/server/sanam

After successful execution, Uniswapv3 contracts will be deployed and verified using the Sourcify tool. Navigate to BuildBear Explorer to see the details.

Deploying UniswapV3 and Verifying using Sourcify On Hardhat

To get started, use the same repo and change the directory to the Hardhat project containing uniswapv3 contracts using the following command and install all required dependencies.

cd Hardhat-Sourcify
 npm install

To verify code using Sourcify in a Hardhat project, use the Hardhat Verify plugin. Please note that Sourcify is not supported in Hardhat Ignition at the moment, as it defaults to Etherscan.

To Install the plugin:

npm install --save-dev @nomicfoundation/hardhat-verify

Then, add the following statement to your hardhat.config.js file:

require("@nomicfoundation/hardhat-verify");

This plugin enables you to verify the source code of your Solidity contracts, providing integration with Sourcify and other compatible explorers.

Next, configure the network details in your hardhat.config.js file for deploying the contract. Obtain the RPC URL from your BuildBear sandbox dashboard and make sure that the Sourcify plugin is installed.

Finally, enable Sourcify with an API URL following the same format but include your {sandboxId or customRPC}. Optionally, disable Etherscan to ensure that only Sourcify is utilized for contract verification, preventing any potential confusion or conflicts. Your configuration file should look like the following:

Now, deploy and verify the protocol by running;

npx hardhat run scripts/deploy-Uniswap.js --network buildbear

OR Use the below command to verify on Sourcify for deployed contracts by passing the contract address,

npx hardhat verify --network buildbear <contract address>

Navigate to BuildBear Explorer to see the contract details,

Congratulations on completing this tutorial on deploying and verifying Uniswap v3 protocol with Sourcify on BuildBear sandboxes!

About BuilBear Labs:

BuildBear is a platform tailored for DApp development and testing. Developers gain the freedom to construct a personalized Private Testnet sandbox across a variety of blockchain networks. The liberty to mint unlimited Native and ERC20 tokens, coupled with rapid transaction times on BuildBear (under 3 seconds!), enhances the DApp development lifecycle manifold. The platform comes equipped with tools and plugins designed for real-time testing and debugging, ensuring developers can keep tabs on intricate blockchain transactions with unparalleled ease.

Connect with us on Twitter | LinkedIn | Telegram | GitHub