Deploy smart contracts
Guide to deploy smart contracts and sub-graphs
Summary
To begin, you'll need to write your Solidity smart contract that defines your application's business logic. This includes designing the data structure using struct, storing the data with mapping, and emitting events to support off-chain indexing. Once written, the contract should be placed in the contracts/ folder inside your code studio workspace.
Next, you need to prepare a deployment script using Hardhat Ignition. This script should go into the ignition/modules/ folder and will declare how your smart contract should be deployed. You'll use the buildModule function to specify which contract to deploy and how it should be initialized.
After setting up the script, you should compile the contract. This step generates the necessary build artifacts, including the ABI and bytecode, which are essential for testing, deploying, and integrating the contract with other components. Depending on the tool used (Hardhat or Foundry), the output will be stored in the artifacts/ or out/ directory respectively.
Once compiled, it's important to thoroughly test your contract using either Foundry or Hardhat. These tests will simulate real-world conditions. Writing these tests helps you catch logic errors early before deployment.
When the contract passes all tests, you're ready to deploy. Start your local network using the Hardhat - start network script and run the deployment script through the IDE task manager. You'll be prompted to select your custom deployment script file before the deployment begins.
Finally, to deploy to a SettleMint-hosted blockchain network, authenticate using the SettleMint login script, select the appropriate node and private key, and confirm deployment. The deployed address will be saved in a JSON file under ignition/deployments/, which can then be used in middleware or frontend applications to interact with the contract.
Learning with a user data manager smart contract example
The goal of this tutorial is to design and build a simple user data manager using Solidity. While the visible use case is centered around managing user data (such as name, email, age, etc.), the hidden objective is to demonstrate the core thought process behind building a smart contract that can store, update, read, and soft delete data on the blockchain.
This example is intentionally kept simple and non-technical in terms of blockchain identity (no wallets or signatures involved) to help beginners focus on the fundamentals of: - Designing smart contract data structures (structs and mappings) - Writing public and restricted functions to interact with data - Emitting and responding to events - Handling update and soft delete logic to mimic realistic scenarios (Understand that transaction data is never deleted, just a more recent entry is added about that record in a newer block on blockchain)
By the end of this tutorial, you'll not only learn the foundational patterns that apply to many real-world blockchain applications but also understand how to develop and deploy smart contracts on SettleMint platform.
1. Let's start with the solidity smart contract code
A smart contract is a self-executing program deployed on the blockchain that defines rules and logic for how data or assets are managed without relying on intermediaries. In this tutorial, we are writing our smart contract using Solidity, the most widely adopted programming language for Ethereum and EVM-compatible blockchains. Solidity is a statically typed, contract-oriented language designed specifically for writing smart contracts that run on the Ethereum Virtual Machine (EVM).
If you're new to Solidity or want to deepen your understanding, here are some helpful resources: - Official Solidity Documentation: https://soliditylang.org/ - Solidity by Example (interactive guide): https://solidity-by-example.org - CryptoZombies (gamified Solidity learning): https://cryptozombies.io/en/solidity
These resources provide both foundational knowledge and hands-on coding exercises to help you become comfortable with writing and deploying smart contracts.
In your learning phase, you can also use ChatGPT: https://chatgpt.com/ or any of your go to AI tools for generation of basic solidity smart contracts.
Example userdata smart contract solidity code
Please ensure that smart contract emits all required paramters in every event, otherwise, while indexing we will not get the parameters which are not emited.
Smart contract , events & functions overview
In a smart contract, we define a clear set of events and functions to manage the lifecycle of user profiles. These building blocks enable seamless interaction with the contract, supporting profile creation, updates, retrieval, and soft deletion, while ensuring all changes are traceable through emitted events.
Events play a crucial role in allowing off-chain services like The Graph to listen for and respond to changes in contract state, whereas functions provide the core interface for interacting with profile data on-chain.
Below is a structured overview of the key events and functions included in the contract:
# | Events | Parameters | Description |
---|---|---|---|
3.1 | ProfileCreated | uint256 userId , string name , string email , uint8 age , string country , bool isKYCApproved | Emitted when a new profile is created |
3.2 | ProfileUpdated | uint256 userId , string name , string email , uint8 age , string country , bool isKYCApproved | Emitted when a profile is updated |
3.3 | ProfileDeleted | uint256 userId | Emitted when a profile is soft-deleted |
# | Functions | Parameters | Returns | Description |
---|---|---|---|---|
4.1 | createProfile | uint256 userId , string name , string email , uint8 age , string country , bool isKYCApproved | – | Creates a new user profile |
4.2 | updateProfile | uint256 userId , string name , string email , uint8 age , string country , bool isKYCApproved | – | Updates an existing profile |
4.3 | getProfile | uint256 userId | UserProfile memory | Retrieves the profile if not soft-deleted |
4.4 | deleteProfile | uint256 userId | – | Soft-deletes the profile |
Crud mapping for the smart contract
This table maps traditional Web2-style CRUD operations to the equivalent Solidity functions in the smart contract:
CRUD | Solidity Function | Explanation |
---|---|---|
Create | createProfile() | Adds a new user profile to the blockchain using a unique userId . This simulates an INSERT operation in databases. It checks that the profile does not already exist (unless soft-deleted) and stores the user's details. Emits ProfileCreated with full data for off-chain indexing. |
Read | getProfile() | Retrieves an existing profile by its userId , similar to a SELECT query in SQL. It returns the user's profile only if it hasn't been soft-deleted. This function is marked view , meaning it does not modify blockchain state and can be called without gas. |
Update | updateProfile() | Modifies all fields of an existing user profile. Acts like an UPDATE in Web2 databases. It ensures the profile exists and is not deleted, then updates it with the provided values. Emits ProfileUpdated with full details for off-chain use. |
Delete | deleteProfile() | Performs a soft delete by setting the isDeleted flag to true , without removing the actual data from storage. This is similar to a logical delete used in many enterprise databases. The data remains on-chain (for auditability), but getProfile() will no longer return it. Emits ProfileDeleted . |
2. Let's add this smart contract to code studio
When you deploy an empty smart contract set on SettleMint platform, you get a very simple Counter.sol contract as an example, you may delete it.
In the contracts folder create a file called UserData.sol and copy paste the content of the above smart contract code.
3. Prepare deployment script
In ignition folder, you will find a folder called modules, there you will find a main.ts file which is basically a contract deployment script. You may delete it if you already know or once you understand the structure. In this folder create a file called deployUser.ts
Understanding the deployment script code structure.
Let's understand key parts of this code-
This deployment script uses Hardhat Ignition to define and execute the deployment of a smart contract. It begins by importing the buildModule function from the Ignition library, which is used to define a deployment module. The module is named "UserDataModule" and is constructed using a callback function that receives a context object m.
Within this function, m.contract("UserData") declares that a contract named UserData (which must match the name inside the Solidity source file) should be deployed. This is how it knows which contract is being refered.
The deployed contract instance is stored in a variable called userdata. This instance is then returned from the module so it can be accessed later if needed. Finally, the module is exported as the default export so it can be run by Hardhat's Ignition system using the CLI.
4. Compile the smart contract code
To run various scripts to compile, test, deploy smart contracts and sub-graphs you need to go to left top area of the IDE and go to task manager section. When a Solidity smart contract is compiled, the source code is transformed into low-level bytecode that can be executed on the Ethereum Virtual Machine (EVM). This process also generates important metadata such as the ABI (Application Binary Interface), which defines how external applications or scripts can interact with the contract's functions and events. Additionally, the compiler produces debugging information, source maps, and compiler settings. These outputs are essential for deploying, testing, and integrating the contract with dApps or frontend applications
Foundry build
If you compile using Foundry Build then in out folder a folder will be
created with the name of your smart contract file name, and within that folder,
contractname.json and contractname.metadata.json will be generated. This
contractname will be what you have as the name of the contract within solidity
file.
Hardhat build
If you compile using Hardhat Build, then in artifacts folder a folder will
be created with the name of your smart contract file name, and within that
folder - artifacts.d.ts, ContractName.d.ts, ContractName.dbg.ts,
ContractName.json are generated. ContractName.json is the ABI.
When you compile a Solidity smart contract in SettleMint, it processes .sol files and generates various output artifacts needed for deployment and interaction. For example, after compiling UserData.sol, you get the following inside the artifacts/ directory:
📂 artifacts/contracts/UserData.sol/
- UserData.json – This is the main artifact file. It contains the ABI (Application Binary Interface) - The compiler metadata
- UserData.dbg.json – Debugging info including source maps and AST
- UserData.d.ts – TypeScript definition file for better type safety when using the contract in frontend or scripting environments
- artifacts.d.ts – Global TypeScript declarations for all compiled contracts
📂 artifacts/build-info/
- hash.json – Contains detailed compiler input/output and full metadata for the build process, useful for verifying or analyzing compilation details
5. Test the smart contract
Smart contract testing is a critical part of the development lifecycle in blockchain and decentralized application (dApp) projects. Since smart contracts are immutable once deployed to the blockchain, bugs or vulnerabilities can result in permanent loss of funds, data corruption, or security breaches. Thorough testing ensures that smart contracts behave as expected under various scenarios and edge cases before they go live on the mainnet.
Testing frameworks like Hardhat and Foundry provide robust tooling to write and execute tests in Solidity or JavaScript/TypeScript. These frameworks offer helpful utilities such as assertions, mock accounts, blockchain state manipulation (e.g., time travel or snapshot/rollback), and expected reverts. Additionally, testing libraries like forge-std/Test.sol (in Foundry) or chai (in Hardhat) enable expressive and readable test assertions.
Foundry test
In the test folder in IDE, create a UserData.t.sol file for Foundry test script.
It uses forge-std/Test.sol is a powerful utility library provided by Foundry's standard library (forge-std) that simplifies writing and executing tests for smart contracts. It extends the base Solidity Test contract and includes a rich set of assertions, cheatcodes, and debugging tools that make testing more expressive and efficient.
When a test contract inherits from Test, it gains access to functions like assertEq, assertTrue, fail, and testing cheatcodes such as vm.prank, vm.expectRevert, vm.roll, and many more. These tools simulate complex behaviors and edge cases in a local testing environment without the need to manually manipulate the EVM state. For example, vm.expectRevert allows developers to anticipate and verify error conditions, while assertEq simplifies comparisons between expected and actual results.
Hardhat test
In the test folder in IDE, create a UserData.ts file for HardHat test script.
This test script leverages Hardhat's modern support for viem, a lightweight and fast alternative to Ethers.js, designed for more efficient interaction with Ethereum contracts. The test uses loadFixture from hardhat-toolbox-viem/network-helpers to ensure test isolation and efficient deployments, each test gets a clean contract instance to work with.
Inside the script, we define a fixture function (deployUserFixture) to deploy the User contract and provide access to the publicClient. The tests cover all core functionalities of the contract: creating, updating, reading, and soft-deleting user profiles. Assertions are written using Chai's expect syntax, while contract interactions (like write.createProfile and read.getProfile) follow the Viem pattern, making the test code both concise and expressive.
Please run hardhat test script to test the smart contract
Once the test is pass, you can deploy to local hardhat network by using script - hardhat - deploy to local network
Start test network using hardhat - start network script in task manager.
Deploy to test network
If you click on hardhat - deploy to local network and nothing happens, then you are missing the step to select the correct deployment script and hitting enter key. You will see a message - extra commandline arguments, e.g. --verify (press 'enter' to confirm or 'escape' to cancel) in the top middle of the IDE, hit enter, you will see ignition/modules/main.ts, edit the last part at put the correct filename (e.g. deployUserData.ts), basically the name of the deployment script you created in ignition folder, and hit enter agian to run the deployment script. This remains true for all the deploy cases, whether on local network or platform network.
6. Deploy the smart contract to platform network
Use SettleMint Login script in task manager to login, you will need your personal access token. To generate personal access token, refer - Personal access token
Hardhat deploy to platform network enter the path of the deployment script
ignition/modules/deployUserData.ts
If you click on hardhat - deploy to local network and nothing happens, then you are missing the step to select the correct deployment script and hitting enter key. You will see a message - extra commandline arguments, e.g. --verify (press 'enter' to confirm or 'escape' to cancel) in the top middle of the IDE, hit enter, you will see ignition/modules/main.ts, edit the last part at put the correct filename (e.g. deployUser.ts), basically the name of the deployment script you created in ignition folder, and hit enter agian to run the deployment script. This remains true for all the deploy cases, whether on local network or platform network.
Before deploying to network, please do not forget to login to SettleMint network via script settlemint login
Select the node to which you wish to deploy this smart contract. If you get an error, please ensure that a private key was created and attached to the node on which you wish to deploy the smart contract.
Select the private key you wish to use to deploy smart contract. If you are using a public network or a network with gas fee, then make sure that this private key's wallet is funded.
Select yes when prompted - confirm deploy to network (network name)? › (y/N).
Wait for a few minutes for the contract to be deployed.
Deployed contract address
Deployed contract address is stored in deployed_addresses.json file located in igntition>deployments folder.
Congratulations!
You have successfully compiled, tested and deployed your smart contract on blockchain network. Now you can proceed to middlewares for getting APIs to do smart contract transactions, write data to chain and read data in a structured format.