A Deep Dive into Smart Contract deployment on your custom Chain built on Saga

Leonardo Simone Digiorgio
7 min readNov 10, 2023

Introduction

“Deploying Smart Contracts on Your Custom Chain: A Comprehensive Approach” is your all-in-one guide to understanding and executing smart contract deployment on your unique chain built using Saga.

The core of this guide is its thorough exploration of smart contract deployment strategies. It provides detailed explanations on how to deploy, and interact with smart contracts on custom chains, utilizing well-known development tools.
Whether you’re a beginner seeking foundational knowledge or a seasoned developer looking to enhance your skills, this guide offers practical insights and real-world examples tailored to different expertise levels.

This is the second article in the Saga series:

For this article, it is necessary that you have already built your own chain, and you should have the following available:

  • The chain id of your chain
  • The JsonRPC

To get this, you can run these sagacli command:

sagacli chainlet list
sagacli chainlet apis <your_chain_id>

The Smart Contract

In this article, I’ll be using a project we discussed in the previous article (Github). However, feel free to choose any Solidity Smart Contract for deployment on your custom chain.

AddressBook.sol

This smart contract, allows users to manage their contacts on the Ethereum blockchain.
Users can add, view, and remove contacts.
Each contact is associated with an Ethereum address and an optional alias name chosen by the user.
The contract ensures that addresses and alias names are valid before processing any operations. The contract also supports batched function calls for gas efficiency (Multicall).

Deploy the Smart Contract in your custom chain using Remix (For beginners)

The easy way to deploy a smart contract is using the Remix Ethereum IDE.

First of all open Remix and create a new file called AddressBook.sol (<your_smart_contract>.sol).

Then copy the smart contract above (1), navigate to Solidity compile (2) and compile BuyMeACoffee.sol (3).
Now you have access to the ABI, which it can be useful if you want to interact your the front-end dApp with the smart contract.

Then:

  • Navigate to “Deploy and run transaction” (1) > Environment (2).
  • Click on Injected Provider, which should prompt the Metamask extension to appear (3).
  • Once connected, switch from the mainnet or the chain that you are now use it to your own chain network.
    To do this, click on the second icon located at the top left (4), and then select your chain (5) (in my case the chain tutorialsworld).

In the end, if you see the screen displaying the network as <chainId> (my case … ) and you can identify your account, you can be assured that you are on the right track (6).

You can proceed with the deployment of your smart contract by clicking the “Deploy” button (7).

After that, the MetaMask popup should appear, asking you to confirm the contract creation using your chain token (8).

Once you’ve done that, you should observe the following information displayed at the bottom (9).

Also you can see your smart contract creation on your chain explorer (check sagacli chainlet apis <your_chain_id>)

Your chain explorer

Deploy the Smart Contract in your custom chain using Hardhat

To deploy your smart contract using hardhat, you need first of all to add a network entry to your hardhat.config.js file and add your own chain like this:

Where PRIVATE_KEY is the private key of the your Metamask wallet.
To get your private key follow this guide from Metamask.

⚠️ Note: Please ensure that you keep your private key hidden from anyone by adding it to the .env file and including the .env file in your .gitignore.

If you are a beginner check this guide: How to Avoid Uploading Your Private Key to GitHub and use your dev/test wallet for your projects NOT your main one.

Before deployment, we need the smart contract ABI and its bytecode. To obtain these, we have to compile the contract and extract them from the artifacts.

To compile your smart contract using the command:

npx hardhat compile

After compile, you should see the folder artifacts appear. Then you can see in artifacts/contracts/<your_smart_contract>.sol/<your_smart_contract>.json your contract ABI and bytecode.

After config your hardhat, you can deploy your smart contracts using scripts with a deployment script where:

In the first part we define your own Chain:

First part of deploy.ts

In the second part, we deploy the smart contract using the ABI and bytecode we obtained earlier:

Second part of deploy.ts

In the end you should have your deploy.ts like this:

Lastly open a new terminal and deploy the smart contract in your network chain using this command.

npx hardhat run scripts/deploy.ts --network <your_chain_name>

In my case:

npx hardhat run scripts/deploy.ts --network tutorialsworld

In case of success you should have this message:

Deploy the Smart Contract in your custom chain using Foundry

To deploy your smart contract using Foundry, you need first of all that you had your smart contract in the folder src/your_contract.sol then try compiling your contract to make sure everything is in order.

forge build

If your output looks like this, the contracts successfully compiled.

And you should be able to get your ABI in the out/Your_Contract.sol/your_contract.json

Next, in the folderscript and create a file in it called Your_Contract.s.sol. This is where we will create the deployment script itself.

Where you have to import your smart contract from the src folder, and then add new YourContract() (my case new AddressBook()) between of the vm.startBroadcast() and vm.stopBroadcast() inside the run function.

If you want details on what happens under the hood of the Foundry script, you can refer to this guide.

Lastly, before deploy you need to export your private key from Metamask and the use this command in your terminal:

export PRIVATE_KEY=<your_private_key>

Then to deploy run the command:

forge script script/YourContract.s.sol:YourContractScript --rpc-url <your_rpc_url> --private-key $PRIVATE_KEY --broadcast

Where you can get <your_rpc_url> using Sagacli:

sagacli chainlet list
sagacli chainlet apis <your_chain_id>

So if i want to deploy in my case will be:

And if you added the flag --broadcast you can see also this output:

Otherwise you can check your contract address in the run-latest.json inside the broadcast folder

Conclusion

Congratulations! You now know how to deploy smart contracts on your custom chain built with Saga, thanks to the comprehensive insights provided in this article.

Whether you’re a beginner seeking foundational knowledge or an experienced developer aiming to enhance your skills, the step-by-step instructions offered practical guidance for deploying smart contracts using various methods, including Remix, Hardhat, and Foundry.
By empowering you with this knowledge, the article ensures that you can confidently navigate the complexities of smart contract deployment, making the most out of Saga’s innovative blockchain technology.

You are now well-prepared to actively participate in the thriving community of proficient developers within the Saga network. By mastering the smart contract deployment techniques outlined in this article, you can confidently engage in building a secure, efficient, and innovative blockchain environment for your projects. This newfound expertise not only enhances your skills but also enables you to collaborate effectively with fellow developers, fostering a dynamic ecosystem of innovation and growth within the Saga community.

If you found the article valuable and wish to offer a gesture of encouragement:

  1. Clap 50 times for this article
  2. Leave a comment telling me what you think
  3. Highlight the parts in this article that you resonate with

Further Reading

--

--

Leonardo Simone Digiorgio

Web3 Software Engineer | Wagmi | Ethers | React.js | Next.js | Smart Contracts | Solidity | Ethereum | I help build the future of the internet.