How To Write Smart Contract in Solidity
5-Step Tutorial: How to Write a Smart Contract in Solidity
In the following sections, we’ll show you how to write a smart contract in Solidity in five straightforward steps:
-
Step 1: Set Up Remix
-
Step 2: Write the Smart Contract Code in Solidity
-
Step 3: Set Up MetaMask
-
Step 4: Get Testnet Tokens
-
Step 5: Compile and Deploy the Solidity Smart Contract
The Solidity smart contract you’ll write is really straightforward, allowing you to store and retrieve an unsigned integer. Consequently, this tutorial is intended for developers with little to no experience in Ethereum development.
Step 1: Set Up Remix
You have many options for writing a smart contract in Solidity. However, to make this tutorial as beginner-friendly as possible, we’ll use Remix. Remix is a web-based integrated development environment (IDE) for developing EVM-compatible smart contracts.
It’s straightforward and supports several features you need for this tutorial, including the ability to compile and deploy Solidity smart contracts at the click of a few buttons. As such, let’s kick things off by visiting the Remix website.
Clicking on the link above takes you to the following page, where you’ll find a workspace called ”default_workspace”:
From there, open the ”contracts” folder, delete all the default files, and create a new one called ”Storage.sol”:
Step 2: Write the Smart Contract Code in Solidity
For the second step, we’ll show you how to write the smart contract code in Solidity. As such, solidity ahead and open the ”Storage.sol” file and input the following code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Storage {
uint data;
function set(uint x) public {
data = x;
}
function get() public view returns (uint) {
return data;
}
}
The code above is the complete contract, and we’ll now break down each row, starting from the top!
- On the initial line, we specify the SPDX license type. Whenever the source code of a contract is made publicly available, these licenses can help avoid and resolve copyright issues:
The third line of the code declares which Solidity compiler you want to use. In this case, we’re using any version upper than ^0.8.20;
- On the fifth line, we declare the contract, and in this case, we name it
_Storage_
. It’s common practice to use the same name for the file and contract:
- The sixth line declares an unsigned integer (uint) called
_data_
. This is the variable that will store the data you assign when executing the functions of the contract:
- On lines seven to nine, we declare the
_set()_
function. This function takes a uint variable as a parameter and assigns it to_data_
. Furthermore, the keyword_public_
ensures that anyone can call the_set()_
function:
- In the last couple of lines, we add the
_get()_
function that returns the value of the_data_
variable. The_view_
keyword tells the Solidity compiler it’s a read-only function, and the_returns (uint)_
part specifies that the function is to return a uint variable:
That’s it; you now know how to write a smart contract in Solidity!
In the next step, we’ll show you how to set up MetaMask, which you’ll need to compile and deploy the contract in the tutorial’s final part!
Step 3: Set Up MetaMask
Now that you’re done writing the code for your Solidity smart contract, the next thing you’ll need is a Web3 wallet. And for this tutorial, we’ll be using MetaMask. As such, if you haven’t already, head on over to “metamask.io” and hit the ”Download” button at the bottom left:
Next, add MetaMask as a browser extension:
Once you add MetaMask to your browser, you’ll be taken to the following page, where you can either set up a new wallet or import an existing one:
After you create a new or import a wallet, you should find yourself on a page looking something like this:
In addition to setting up a MetaMask account, you must add Ethereum’s Sepolia testnet to your wallet. The reason for doing this is that we don’t want to immediately commit to the Ethereum mainnet. Instead, you should deploy your smart contract to Sepolia so you can test it in a safe and secure environment.
To add and switch to the Sepolia testnet, solidity ahead and hit the networks drop-down menu at the top right of your MetaMask interface, click on the ”Show test networks” button, and select Sepolia:
Afterwards, it should now look something like this:
Step 4: Get Testnet Tokens
Deploying a smart contract to a blockchain requires a network transaction, meaning you must pay a gas fee. Consequently, in order to deploy your Solidity smart contract, you need to load up on some Sepolia ETH. Fortunately, since Sepolia is a testnet, you can leverage a faucet to get free tokens. And the easiest way to find a legit faucet is to visit the solidityogle Faucet
Clicking this takes you to the following page where you need to input your MetaMask wallet address and click on "Receive 0.05 sepolia ETH"(It has max Testnet Tokens limit of 0.05 sepolia For 24 hr )
As soon as you claim them, the balance in your MetaMask wallet should update:
From here, you can now use these testnet tokens to pay for transactions on the Sepolia testnet. This means you’re now ready for the final step, where we’ll show you how to compile and deploy your Solidity smart contract!
Step 5: Compile and Deploy the Solidity Smart Contract
Requirements
-
Solidity Version: Your smart contract must use Solidity version 0.8.0 or greater.
-
Hardcoded Deploy Parameters: All deploy parameters should be hardcoded in the contract. Kalp Studio does not support passing parameters during deployment.
Smart Contract Structure
Your smart contract should follow this general structure:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract YourContractName {
// State variables
uint256 public someValue;
address public owner;
// Events (if needed)
event ValueChanged(uint256 newValue);
// Constructor with hardcoded values
constructor() {
someValue = 100; // Hardcoded initial value
owner = msg.sender;
}
// Functions
function setValue(uint256 _newValue) public {
require(msg.sender == owner, "Only owner can set value");
someValue = _newValue;
emit ValueChanged(_newValue);
}
// Other functions...
}
Key Points
-
Start with the SPDX license identifier.
-
Specify the Solidity version (^0.8.0 or higher).
-
Use a meaningful name for your contract.
-
Declare state variables that will store your contract's data.
-
Include a constructor that sets initial values. These values are hardcoded and cannot be changed during deployment in Kalp Studio.
-
Implement functions to interact with your contract.
Example: Token Contract
Here's an example of a simple token contract that would be compatible with Kalp Studio:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleToken {
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
event Transfer(address indexed from, address indexed to, uint256 value);
constructor() {
name = "SimpleToken";
symbol = "SMPL";
decimals = 18;
totalSupply = 1000000 * 10**uint256(decimals);
balanceOf[msg.sender] = totalSupply;
}
function transfer(address _to, uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value, "Insufficient balance");
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
}
Deployment Process
-
Write your smart contract following the structure and requirements above.
-
make sure that your contract is using a Solidity compiler that supports version 0.8.0 or higher.
-
Review the contract details, ensuring all parameters are correct (remember, you can't change these during deployment).
-
In Kalp Studio, navigate to the smart contract deployment section.
- Add Smart Contract name and Description to deploy the smart contract
- Drag & Drop your compiled contract code into the designated area.
-
Click the "Continue" button to deploy your contract to the blockchain.
-
Wait for the transaction to be confirmed on the blockchain.
-
Once confirmed, Kalp Studio will provide you with the deployed contract address and transaction details.
Remember, all initial values and parameters must be set within the contract code itself, as Kalp Studio does not support passing parameters during deployment.
Note
The smart contract will deploy to the network only which the wallet is connected to.
Note
If you encounter any issues during development related to Kalp Studio, please raise a support ticket at care.kalp.studio. Our dedicated development team will assist you in finding the best possible solution.